Skip to content

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

navi
strategy.cancel(id: series string)

Cancels a pending order by ID.

Parameters

NameTypeDefaultDescription
idseries stringOrder identifier to cancel.

cancel_all

navi
strategy.cancel_all()

Cancels every pending order created by the strategy.


close

navi
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

NameTypeDefaultDescription
idseries stringEntry ID to close.
commentseries stringnaOptional comment for the generated close order.
qtyseries floatnaAbsolute quantity to close.
qty_percentseries floatnaPercentage of the position to close.
alert_messageseries stringnaOptional alert message.
immediatelyseries boolfalseIf true, closes the position immediately.
disable_alertseries boolfalseIf true, suppresses the strategy alert.

close_all

navi
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

NameTypeDefaultDescription
commentseries stringnaOptional comment for the generated close orders.
alert_messageseries stringnaOptional alert message.
immediatelyseries boolfalseIf true, closes all positions immediately.
disable_alertseries boolfalseIf true, suppresses the strategy alert.

convert_to_account

navi
strategy.convert_to_account(value: series float): series float

Converts 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

NameTypeDefaultDescription
valueseries floatThe value in the symbol's currency.

Returns: series float


convert_to_symbol

navi
strategy.convert_to_symbol(value: series float): series float

Converts 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

NameTypeDefaultDescription
valueseries floatThe value in the account currency.

Returns: series float


entry

navi
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

NameTypeDefaultDescription
idseries stringUnique order identifier.
directionseries DirectionOrder side: Direction.Long or Direction.Short.
qtyseries floatnaOrder size. When na, the strategy default is used.
limitseries floatnaLimit price. When na, the order is not limited.
stopseries floatnaStop price. When na, no stop trigger is attached.
oca_nameseries stringnaOptional One-Cancels-All group name.
oca_typeseries OcaTypeOcaType.NoneOCA behavior, such as oca.cancel, oca.reduce, or oca.none.
commentseries stringnaOptional order comment.
alert_messageseries stringnaOptional alert message for this order.
disable_alertseries boolfalseIf true, suppresses the strategy alert for this order.

exit

navi
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

NameTypeDefaultDescription
idseries stringUnique exit-order identifier.
from_entryseries stringnaEntry ID to exit. When na, the exit applies to the whole matching position.
qtyseries floatnaAbsolute quantity to close. When na, the full matched quantity is used.
qty_percentseries floatnaPercentage of the position to close.
profitseries floatnaTake-profit distance in ticks from the entry price.
limitseries floatnaAbsolute take-profit price. Overrides profit.
lossseries floatnaStop-loss distance in ticks from the entry price.
stopseries floatnaAbsolute stop-loss price. Overrides loss.
trail_priceseries floatnaAbsolute price that activates the trailing stop.
trail_pointsseries floatnaTick distance from entry that activates the trailing stop.
trail_offsetseries floatnaTick offset maintained behind the best price after activation.
oca_nameseries stringnaOptional OCA group name.
commentseries stringnaDefault comment for fills from this exit.
comment_profitseries stringnaComment used for take-profit fills.
comment_lossseries stringnaComment used for stop-loss fills.
comment_trailingseries stringnaComment used for trailing-stop fills.
alert_messageseries stringnaDefault alert message.
alert_profitseries stringnaAlert message used for take-profit fills.
alert_lossseries stringnaAlert message used for stop-loss fills.
alert_trailingseries stringnaAlert message used for trailing-stop fills.
disable_alertseries boolfalseIf true, suppresses the strategy alert for this exit.

order

navi
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

NameTypeDefaultDescription
idseries stringUnique order identifier.
directionseries DirectionOrder side: Direction.Long or Direction.Short.
qtyseries floatnaOrder size. When na, the strategy default is used.
limitseries floatnaLimit price. When na, the order is not limited.
stopseries floatnaStop price. When na, no stop trigger is attached.
oca_nameseries stringnaOptional One-Cancels-All group name.
oca_typeseries OcaTypeOcaType.NoneOCA behavior, such as oca.cancel, oca.reduce, or oca.none.
commentseries stringnaOptional order comment.
alert_messageseries stringnaOptional alert message for this order.
disable_alertseries boolfalseIf true, suppresses the strategy alert for this order.

Released under the MIT License.