strategy
Properties
account_currency
Type: series string
Returns the currency code used for the strategy's account, as set in the strategy() declaration.
avg_losing_trade
Type: series float
Returns the average loss per losing trade in account currency.
Calculated as gross loss / number of losing trades. Expressed as a positive number.
avg_losing_trade_percent
Type: series float
Returns the average loss per losing trade as a percentage of initial capital.
Calculated as gross loss percent / number of losing trades. Expressed as a positive number.
avg_trade
Type: series float
Returns the average profit/loss per closed trade in account currency.
Calculated as net profit / number of closed trades.
avg_trade_percent
Type: series float
Returns the average profit/loss per closed trade as a percentage of initial capital.
Calculated as net profit percent / number of closed trades.
avg_winning_trade
Type: series float
Returns the average profit per winning trade in account currency.
Calculated as gross profit / number of winning trades.
avg_winning_trade_percent
Type: series float
Returns the average profit per winning trade as a percentage of initial capital.
Calculated as gross profit percent / number of winning trades.
cash
Type: const string
This is one of the arguments that can be supplied to the default_qty_type parameter in the strategy() declaration statement.
It is only relevant when no value is used for the qty parameter in strategy.entry() or strategy.order() function calls. It specifies that an amount of cash in the strategy.account_currency will be used to enter trades.
closedtrades
Type: series int
Returns the count of closed trades since strategy start.
A trade is counted as closed when an entry is fully exited. Use strategy.closedtrades.* functions to access details of individual closed trades.
default_entry_qty
Type: series float
Returns the default number of contracts/shares/lots/units used for market orders.
Calculated from the default_qty_type and default_qty_value parameters of the strategy() declaration. The value depends on the current price and equity.
equity
Type: series float
Returns the current account equity: initial capital + net profit + open profit.
This is the real-time value of the account including unrealized gains/losses from open positions.
eventrades
Type: series int
Returns the number of break-even closed trades.
A trade is considered even when its profit is exactly zero.
fixed
Type: const string
This is one of the arguments that can be supplied to the default_qty_type parameter in the strategy() declaration statement.
It is only relevant when no value is used for the qty parameter in strategy.entry() or strategy.order() function calls. It specifies that a fixed quantity will be used to enter trades.
grossloss
Type: series float
Returns the gross loss: the sum of losses from all losing closed trades.
Expressed as a positive number. In account currency.
grossloss_percent
Type: series float
Returns the gross loss as a percentage of initial capital.
Calculated as (gross loss / initial capital) * 100.
grossprofit
Type: series float
Returns the gross profit: the sum of profits from all winning closed trades.
Does not subtract losses. In account currency.
grossprofit_percent
Type: series float
Returns the gross profit as a percentage of initial capital.
Calculated as (gross profit / initial capital) * 100.
initial_capital
Type: series float
Returns the initial capital set in the strategy() declaration.
This is the starting cash for backtests, as specified by the initial_capital parameter.
long
Type: const strategy_direction
A named constant for use with the direction parameter of the strategy.entry() and strategy.order() commands.
It specifies that the command creates a buy order.
losstrades
Type: series int
Returns the number of losing closed trades.
A trade is considered losing when its profit is less than zero.
margin_liquidation_price
Type: series float
Returns the price at which a margin call will be triggered for the current position.
max_contracts_held_all
Type: series float
Returns the maximum number of contracts/shares/lots/units held at any one time during the strategy.
Considers both long and short directions (whichever was larger).
max_contracts_held_long
Type: series float
Returns the maximum number of contracts/shares/lots/units held in a long position at any one time during the strategy.
max_contracts_held_short
Type: series float
Returns the maximum number of contracts/shares/lots/units held in a short position at any one time during the strategy.
max_drawdown
Type: series float
Returns the maximum drawdown: the largest peak-to-trough decline in equity during the strategy's history.
In account currency. A key risk metric showing the worst loss from a peak.
max_drawdown_percent
Type: series float
Returns the maximum drawdown as a percentage of equity at the peak.
A drawdown of 20% means equity fell 20% from its highest point.
max_runup
Type: series float
Returns the maximum run-up: the largest peak-to-trough rise in equity during the strategy's history.
In account currency. Represents the best unrealized gain achieved.
max_runup_percent
Type: series float
Returns the maximum run-up as a percentage of equity at the trough.
netprofit
Type: series float
Returns the net profit in the account currency.
This is the sum of all realized profits and losses from closed trades. Does not include open profit.
netprofit_percent
Type: series float
Returns the net profit as a percentage of initial capital.
Calculated as (net profit / initial capital) * 100.
openprofit
Type: series float
Returns the current unrealized profit/loss from open positions in account currency.
Positive when positions are profitable, negative when at a loss. This value fluctuates with price movements.
openprofit_percent
Type: series float
Returns the current unrealized profit/loss as a percentage of initial capital.
Calculated as (open profit / initial capital) * 100.
opentrades
Type: series int
Returns the count of currently open trades.
Positions that have been entered but not yet fully closed. Use strategy.opentrades.* functions to access details of individual open trades.
percent_of_equity
Type: const string
This is one of the arguments that can be supplied to the default_qty_type parameter in the strategy() declaration statement.
It is only relevant when no value is used for the qty parameter in strategy.entry() or strategy.order() function calls. It specifies that a percentage (0-100) of equity will be used to enter trades.
position_avg_price
Type: series float
Returns the quantity-weighted average entry price of the current open position.
Calculated as the sum of (entry_price × quantity) for all open trades divided by the total quantity. Returns na when flat.
position_entry_name
Type: series string
Returns the entry ID of the first open trade in the current position.
position_size
Type: series float
Returns the current position size in units (contracts, shares, etc.).
Positive values indicate a long position, negative values indicate a short position, and zero means flat (no position).
short
Type: const strategy_direction
A named constant for use with the direction parameter of the strategy.entry() and strategy.order() commands.
It specifies that the command creates a sell order.
wintrades
Type: series int
Returns the number of winning closed trades.
A trade is considered winning when its profit is greater than zero.
Functions
cancel
strategy.cancel(series string id)Cancels a pending or unfilled order with the given ID.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
id | series string | The unique identifier of the order to cancel. |
cancel_all
strategy.cancel_all()Cancels all pending or unfilled orders.
close
strategy.close(
series string id,
series string comment = na,
series float qty = na,
series float qty_percent = na,
series string alert_message = na,
series bool immediately = false,
series bool disable_alert = false
)Closes an open position with the given id.
Uses either an absolute qty or a 0-100 qty_percent.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
id | series string | The unique identifier of the position to close. | |
comment | series string | na | A comment for the close order. |
qty | series float | na | The absolute quantity to close. |
qty_percent | series float | na | The percentage (0-100) of the position to close. |
alert_message | series string | na | A message to display in alerts. |
immediately | series bool | false | If true, closes the position immediately. |
disable_alert | series bool | false | If true, disables the strategy alert. |
close_all
strategy.close_all(
series string comment = na,
series string alert_message = na,
series bool immediately = false,
series bool disable_alert = false
)Closes all open positions.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
comment | series string | na | A comment for the close orders. |
alert_message | series string | na | A message to display in alerts. |
immediately | series bool | false | If true, closes all positions immediately. |
disable_alert | series bool | false | If true, disables the strategy alert. |
convert_to_account
strategy.convert_to_account(series float value) → series floatConverts a monetary value from the symbol's currency to the strategy's account currency.
Uses the configured currency converter. When no converter is provided, returns the value unchanged.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
value | series float | The value in the symbol's currency. |
Returns: series float
convert_to_symbol
strategy.convert_to_symbol(series float value) → series floatConverts a monetary value from the strategy's account currency to the symbol's currency.
Uses the configured currency converter. When no converter is provided, returns the value unchanged.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
value | series float | The value in the account currency. |
Returns: series float
entry
strategy.entry(
series string id,
series strategy_direction direction,
series float qty = na,
series float limit = na,
series float stop = na,
series string oca_name = na,
series string oca_type = oca.none,
series string comment = na,
series string alert_message = na,
series bool disable_alert = false
)Creates a new order to open or add to a position.
If an unfilled order with the same id exists, a call to this command modifies that order.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
id | series string | A unique identifier for the order. | |
direction | series strategy_direction | The order direction: strategy.long or strategy.short. | |
qty | series float | na | The quantity to open. When na, uses default quantity. |
limit | series float | na | The limit price for the order. When na, market order. |
stop | series float | na | The stop price for the order. |
oca_name | series string | na | The name for One-Cancels-All order group. |
oca_type | series string | oca.none | The OCA order type: oca.cancel_entries, oca.cancel_orders, or oca.none. |
comment | series string | na | A comment for the order. |
alert_message | series string | na | A message to display in alerts. |
disable_alert | series bool | false | If true, disables the strategy alert. |
exit
strategy.exit(
series string id,
series string from_entry = na,
series float qty = na,
series float qty_percent = na,
series float profit = na,
series float limit = na,
series float loss = na,
series float stop = na,
series float trail_price = na,
series float trail_points = na,
series float trail_offset = na,
series string oca_name = na,
series string comment = na,
series string comment_profit = na,
series string comment_loss = na,
series string comment_trailing = na,
series string alert_message = na,
series string alert_profit = na,
series string alert_loss = na,
series string alert_trailing = na,
series bool disable_alert = false
)Places an exit order for an open position, with optional take-profit, stop-loss, and trailing stop conditions.
If an order with the same id already exists, it is replaced. When both TP and SL are set, they form a bracket — one fill cancels the other.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
id | series string | A unique identifier for this exit order. | |
from_entry | series string | na | The entry ID this exit applies to. When na, applies to the entire position. |
qty | series float | na | Absolute quantity to close. When na, closes the full matched position. |
qty_percent | series float | na | Percentage (0-100) of the position to close. |
profit | series float | na | Take-profit distance in ticks from the entry price. |
limit | series float | na | Absolute take-profit price (limit order). Overrides profit. |
loss | series float | na | Stop-loss distance in ticks from the entry price. |
stop | series float | na | Absolute stop-loss price (stop order). Overrides loss. |
trail_price | series float | na | Absolute price at which the trailing stop activates. |
trail_points | series float | na | Tick distance from entry that activates the trailing stop. |
trail_offset | series float | na | Tick offset the trailing stop follows behind the best price. |
oca_name | series string | na | OCA group name (reserved for future use). |
comment | series string | na | Default comment for exit fills. |
comment_profit | series string | na | Comment for take-profit fills. |
comment_loss | series string | na | Comment for stop-loss fills. |
comment_trailing | series string | na | Comment for trailing-stop fills. |
alert_message | series string | na | Default alert message. |
alert_profit | series string | na | Alert message for take-profit. |
alert_loss | series string | na | Alert message for stop-loss. |
alert_trailing | series string | na | Alert message for trailing stop. |
disable_alert | series bool | false | If true, disables the strategy alert. |
order
strategy.order(
series string id,
series strategy_direction direction,
series float qty = na,
series float limit = na,
series float stop = na,
series string oca_name = na,
series string oca_type = oca.none,
series string comment = na,
series string alert_message = na,
series bool disable_alert = false
)Creates a new order to open, add to, or exit from a position.
If an unfilled order with the same id exists, a call to this command modifies that order.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
id | series string | A unique identifier for the order. | |
direction | series strategy_direction | The order direction: strategy.long or strategy.short. | |
qty | series float | na | The quantity for the order. When na, uses default quantity. |
limit | series float | na | The limit price for the order. When na, market order. |
stop | series float | na | The stop price for the order. |
oca_name | series string | na | The name for One-Cancels-All order group. |
oca_type | series string | oca.none | The OCA order type: oca.cancel_entries, oca.cancel_orders, or oca.none. |
comment | series string | na | A comment for the order. |
alert_message | series string | na | A message to display in alerts. |
disable_alert | series bool | false | If true, disables the strategy alert. |