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.
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.
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.
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.
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).
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(id: series string)Cancels a pending order by ID.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
id | series string | Order identifier to cancel. |
cancel_all
strategy.cancel_all()Cancels every pending order created by the strategy.
close
strategy.close(
id: series string,
comment: series string = na,
qty: series float = na,
qty_percent: series float = na,
alert_message: series string = na,
immediately: series bool = false,
disable_alert: series bool = false
)Closes an open position by entry ID.
Use either qty or qty_percent to request a partial close.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
id | series string | Entry ID to close. | |
comment | series string | na | Optional comment for the generated close order. |
qty | series float | na | Absolute quantity to close. |
qty_percent | series float | na | Percentage of the position to close. |
alert_message | series string | na | Optional alert message. |
immediately | series bool | false | If true, closes the position immediately. |
disable_alert | series bool | false | If true, suppresses the strategy alert. |
close_all
strategy.close_all(
comment: series string = na,
alert_message: series string = na,
immediately: series bool = false,
disable_alert: series bool = false
)Closes every open position.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
comment | series string | na | Optional comment for the generated close orders. |
alert_message | series string | na | Optional alert message. |
immediately | series bool | false | If true, closes all positions immediately. |
disable_alert | series bool | false | If true, suppresses the strategy alert. |
convert_to_account
strategy.convert_to_account(value: series float): 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(value: series float): 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(
id: series string,
direction: series Direction,
qty: series float = na,
limit: series float = na,
stop: series float = na,
oca_name: series string = na,
oca_type: series OcaType = OcaType.None,
comment: series string = na,
alert_message: series string = na,
disable_alert: series bool = false
)Places or updates an entry order.
Reusing an existing pending id updates that order instead of creating a new one.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
id | series string | Unique order identifier. | |
direction | series Direction | Order side: Direction.Long or Direction.Short. | |
qty | series float | na | Order size. When na, the strategy default is used. |
limit | series float | na | Limit price. When na, the order is not limited. |
stop | series float | na | Stop price. When na, no stop trigger is attached. |
oca_name | series string | na | Optional One-Cancels-All group name. |
oca_type | series OcaType | OcaType.None | OCA behavior, such as oca.cancel, oca.reduce, or oca.none. |
comment | series string | na | Optional order comment. |
alert_message | series string | na | Optional alert message for this order. |
disable_alert | series bool | false | If true, suppresses the strategy alert for this order. |
exit
strategy.exit(
id: series string,
from_entry: series string = na,
qty: series float = na,
qty_percent: series float = na,
profit: series float = na,
limit: series float = na,
loss: series float = na,
stop: series float = na,
trail_price: series float = na,
trail_points: series float = na,
trail_offset: series float = na,
oca_name: series string = na,
comment: series string = na,
comment_profit: series string = na,
comment_loss: series string = na,
comment_trailing: series string = na,
alert_message: series string = na,
alert_profit: series string = na,
alert_loss: series string = na,
alert_trailing: series string = na,
disable_alert: series bool = false
)Places or updates an exit order for an open position.
The order can combine take-profit, stop-loss, and trailing-stop settings. If both take-profit and stop-loss values are supplied, they behave as a bracket.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
id | series string | Unique exit-order identifier. | |
from_entry | series string | na | Entry ID to exit. When na, the exit applies to the whole matching position. |
qty | series float | na | Absolute quantity to close. When na, the full matched quantity is used. |
qty_percent | series float | na | Percentage 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. Overrides profit. |
loss | series float | na | Stop-loss distance in ticks from the entry price. |
stop | series float | na | Absolute stop-loss price. Overrides loss. |
trail_price | series float | na | Absolute price that activates the trailing stop. |
trail_points | series float | na | Tick distance from entry that activates the trailing stop. |
trail_offset | series float | na | Tick offset maintained behind the best price after activation. |
oca_name | series string | na | Optional OCA group name. |
comment | series string | na | Default comment for fills from this exit. |
comment_profit | series string | na | Comment used for take-profit fills. |
comment_loss | series string | na | Comment used for stop-loss fills. |
comment_trailing | series string | na | Comment used for trailing-stop fills. |
alert_message | series string | na | Default alert message. |
alert_profit | series string | na | Alert message used for take-profit fills. |
alert_loss | series string | na | Alert message used for stop-loss fills. |
alert_trailing | series string | na | Alert message used for trailing-stop fills. |
disable_alert | series bool | false | If true, suppresses the strategy alert for this exit. |
order
strategy.order(
id: series string,
direction: series Direction,
qty: series float = na,
limit: series float = na,
stop: series float = na,
oca_name: series string = na,
oca_type: series OcaType = OcaType.None,
comment: series string = na,
alert_message: series string = na,
disable_alert: series bool = false
)Places or updates a general strategy order.
This API can open, add to, reduce, or close a position depending on the current position and arguments.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
id | series string | Unique order identifier. | |
direction | series Direction | Order side: Direction.Long or Direction.Short. | |
qty | series float | na | Order size. When na, the strategy default is used. |
limit | series float | na | Limit price. When na, the order is not limited. |
stop | series float | na | Stop price. When na, no stop trigger is attached. |
oca_name | series string | na | Optional One-Cancels-All group name. |
oca_type | series OcaType | OcaType.None | OCA behavior, such as oca.cancel, oca.reduce, or oca.none. |
comment | series string | na | Optional order comment. |
alert_message | series string | na | Optional alert message for this order. |
disable_alert | series bool | false | If true, suppresses the strategy alert for this order. |