strategy reference
Strategy declaration, orders, position and results.
strategy(initialCapital: 100000.0, defaultQtyValue: 1.0) after the header turns a script into a strategy, backtested by a broker emulator that follows TradingView. Place orders from handlers with strategy.entry, strategy.exit, strategy.close and friends; orders fill from the next bar’s open, or at the signal bar’s close with processOrdersOnClose: true. Results appear in the Strategy Tester and as marks on the chart.
Functions
strategy.entry
strategy.entry(id: string, direction: Direction, qty: float? = null, limit:, stop:, ocaName:, ocaType:, comment:, alertMessage:)Enters a position; an entry in the other direction reverses it.
A market order unless limit or stop is given. qty defaults to the declaration’s defaultQtyType and defaultQtyValue. Entries in the same direction are limited by pyramiding. Placing an entry with the id of a pending order replaces it.
| Parameter | Type | Default |
|---|---|---|
id | string | — |
direction | Direction | — |
qty | float? | null |
limit | float? | null |
stop | float? | null |
ocaName | string? | null |
ocaType | OcaType? | null |
comment | string? | null |
alertMessage | string? | null |
strategy.order
strategy.order(id: string, direction: Direction, qty: float? = null, limit:, stop:, ocaName:, ocaType:, comment:, alertMessage:)Places a raw order: buys or sells qty, ignoring pyramiding and without reversing.
Unlike strategy.entry, an order in the opposite direction reduces the position by its size.
| Parameter | Type | Default |
|---|---|---|
id | string | — |
direction | Direction | — |
qty | float? | null |
limit | float? | null |
stop | float? | null |
ocaName | string? | null |
ocaType | OcaType? | null |
comment | string? | null |
alertMessage | string? | null |
strategy.exit
strategy.exit(id: string, fromEntry: string? = null, qty:, qtyPercent:, profit:, limit:, loss:, stop:, trailPrice:, trailPoints:, trailOffset:, ocaName:, comment:, commentProfit:, commentLoss:, commentTrailing:, alertMessage:)Places take-profit, stop-loss and trailing-stop orders for an entry.
profit and loss are distances in ticks from the entry price, limit and stop absolute prices; the trailing stop activates at trailPrice (or trailPoints ticks in profit) and follows the price trailOffset ticks behind. Without fromEntry the exit applies to every open entry.
| Parameter | Type | Default |
|---|---|---|
id | string | — |
fromEntry | string? | null |
qty | float? | null |
qtyPercent | float? | null |
profit | float? | null |
limit | float? | null |
loss | float? | null |
stop | float? | null |
trailPrice | float? | null |
trailPoints | float? | null |
trailOffset | float? | null |
ocaName | string? | null |
comment | string? | null |
commentProfit | string? | null |
commentLoss | string? | null |
commentTrailing | string? | null |
alertMessage | string? | null |
strategy.close
strategy.close(id: string, qty:, qtyPercent:, comment:, alertMessage:, immediately: bool = false)Closes the trades of entry id with a market order.
Closes all of them unless qty or qtyPercent is given. With immediately: true the order fills at the current close instead of the next open.
| Parameter | Type | Default |
|---|---|---|
id | string | — |
qty | float? | null |
qtyPercent | float? | null |
comment | string? | null |
alertMessage | string? | null |
immediately | bool | false |
strategy.closeAll
strategy.closeAll(comment:, alertMessage:, immediately: bool = false)Closes the whole position with a market order.
| Parameter | Type | Default |
|---|---|---|
comment | string? | null |
alertMessage | string? | null |
immediately | bool | false |
strategy.cancel
strategy.cancel(id: string)Cancels the pending orders with this id.
| Parameter | Type |
|---|---|
id | string |
strategy.cancelAll
strategy.cancelAll()Cancels every pending order.
strategy.openTrade
strategy.openTrade(index: int) -> StrategyTrade?The open trade at index, oldest first, or null.
| Parameter | Type |
|---|---|
index | int |
Returns StrategyTrade?
strategy.closedTrade
strategy.closedTrade(index: int) -> StrategyTrade?The closed trade at index, oldest first, or null.
| Parameter | Type |
|---|---|
index | int |
Returns StrategyTrade?
Properties
strategy.positionSize
strategy.positionSize: floatPosition size: positive long, negative short, 0 flat.
Returns float
strategy.positionAvgPrice
strategy.positionAvgPrice: float?Average entry price of the open position, or null when flat.
Returns float?
strategy.positionEntryName
strategy.positionEntryName: string?Id of the entry that opened the position, or null when flat.
Returns string?
strategy.initialCapital
strategy.initialCapital: floatThe declared initial capital.
Returns float
strategy.equity
strategy.equity: floatInitial capital plus net profit plus open profit.
Returns float
strategy.netProfit
strategy.netProfit: floatSum of the closed trades’ profits after commission and funding.
Returns float
strategy.grossProfit
strategy.grossProfit: floatSum of the winning trades’ profits.
Returns float
strategy.grossLoss
strategy.grossLoss: floatSum of the losing trades’ losses, as a positive number.
Returns float
strategy.openProfit
strategy.openProfit: floatUnrealised profit of the open trades.
Returns float
strategy.openTrades
strategy.openTrades: intNumber of open trades.
Returns int
strategy.closedTrades
strategy.closedTrades: intNumber of closed trades.
Returns int
strategy.winTrades
strategy.winTrades: intNumber of closed trades with a profit.
Returns int
strategy.lossTrades
strategy.lossTrades: intNumber of closed trades with a loss.
Returns int
strategy.evenTrades
strategy.evenTrades: intNumber of closed trades at break-even.
Returns int
strategy.maxDrawdown
strategy.maxDrawdown: floatLargest equity drawdown so far, in account currency.
Returns float
strategy.maxRunup
strategy.maxRunup: floatLargest equity run-up so far, in account currency.
Returns float
strategy.maxContractsHeldAll
strategy.maxContractsHeldAll: floatLargest position held so far, either side.
Returns float
strategy.maxContractsHeldLong
strategy.maxContractsHeldLong: floatLargest long position held so far.
Returns float
strategy.maxContractsHeldShort
strategy.maxContractsHeldShort: floatLargest short position held so far.
Returns float
strategy.marginLiquidationPrice
strategy.marginLiquidationPrice: float?Price at which a margin call would liquidate the position, or null.
Returns float?
Records
StrategyTrade
type StrategyTradeA trade returned by strategy.openTrade and strategy.closedTrade.
| Field | Type | History | Description |
|---|---|---|---|
entryId | string | — | |
entryPrice | float | — | |
entryTime | time | — | |
entryBarIndex | int | — | |
entryComment | string? | — | |
size | float | — | |
exitId | string? | — | |
exitPrice | float? | — | |
exitTime | time? | — | |
exitBarIndex | int? | — | |
exitComment | string? | — | |
profit | float | — | |
profitPercent | float | — | |
commission | float | — | |
maxRunup | float | — | |
maxDrawdown | float | — |
Constants
| Constant | Type | Value | Description |
|---|---|---|---|
strategy.long | Direction | 1 | Long direction for strategy.entry and strategy.order. |
strategy.short | Direction | 2 | Short direction for strategy.entry and strategy.order. |
strategy.fixed | QtyType | 0 | defaultQtyType: defaultQtyValue contracts per order. |
strategy.cash | QtyType | 1 | defaultQtyType: as many contracts as defaultQtyValue of account currency buys. |
strategy.percentOfEquity | QtyType | 2 | defaultQtyType: defaultQtyValue percent of current equity per order. |
strategy.commission.percent | CommissionType | 0 | commissionType: percent of the order value. |
strategy.commission.cashPerContract | CommissionType | 1 | commissionType: money per contract traded. |
strategy.commission.cashPerOrder | CommissionType | 2 | commissionType: money per filled order. |
strategy.oca.none | OcaType | 0 | No one-cancels-all behaviour. |
strategy.oca.cancel | OcaType | 1 | A fill cancels the other orders of the OCA group. |
strategy.oca.reduce | OcaType | 2 | A fill reduces the other orders of the OCA group by the filled size. |