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.


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

pine
strategy.cancel(series string id)

Cancels a pending or unfilled order with the given ID.

Parameters

NameTypeDefaultDescription
idseries stringThe unique identifier of the order to cancel.

cancel_all

pine
strategy.cancel_all()

Cancels all pending or unfilled orders.


close

pine
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

NameTypeDefaultDescription
idseries stringThe unique identifier of the position to close.
commentseries stringnaA comment for the close order.
qtyseries floatnaThe absolute quantity to close.
qty_percentseries floatnaThe percentage (0-100) of the position to close.
alert_messageseries stringnaA message to display in alerts.
immediatelyseries boolfalseIf true, closes the position immediately.
disable_alertseries boolfalseIf true, disables the strategy alert.

close_all

pine
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

NameTypeDefaultDescription
commentseries stringnaA comment for the close orders.
alert_messageseries stringnaA message to display in alerts.
immediatelyseries boolfalseIf true, closes all positions immediately.
disable_alertseries boolfalseIf true, disables the strategy alert.

convert_to_account

pine
strategy.convert_to_account(series float value) → 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

pine
strategy.convert_to_symbol(series float value) → 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

pine
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

NameTypeDefaultDescription
idseries stringA unique identifier for the order.
directionseries strategy_directionThe order direction: strategy.long or strategy.short.
qtyseries floatnaThe quantity to open. When na, uses default quantity.
limitseries floatnaThe limit price for the order. When na, market order.
stopseries floatnaThe stop price for the order.
oca_nameseries stringnaThe name for One-Cancels-All order group.
oca_typeseries stringoca.noneThe OCA order type: oca.cancel_entries, oca.cancel_orders, or oca.none.
commentseries stringnaA comment for the order.
alert_messageseries stringnaA message to display in alerts.
disable_alertseries boolfalseIf true, disables the strategy alert.

exit

pine
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

NameTypeDefaultDescription
idseries stringA unique identifier for this exit order.
from_entryseries stringnaThe entry ID this exit applies to. When na, applies to the entire position.
qtyseries floatnaAbsolute quantity to close. When na, closes the full matched position.
qty_percentseries floatnaPercentage (0-100) of the position to close.
profitseries floatnaTake-profit distance in ticks from the entry price.
limitseries floatnaAbsolute take-profit price (limit order). Overrides profit.
lossseries floatnaStop-loss distance in ticks from the entry price.
stopseries floatnaAbsolute stop-loss price (stop order). Overrides loss.
trail_priceseries floatnaAbsolute price at which the trailing stop activates.
trail_pointsseries floatnaTick distance from entry that activates the trailing stop.
trail_offsetseries floatnaTick offset the trailing stop follows behind the best price.
oca_nameseries stringnaOCA group name (reserved for future use).
commentseries stringnaDefault comment for exit fills.
comment_profitseries stringnaComment for take-profit fills.
comment_lossseries stringnaComment for stop-loss fills.
comment_trailingseries stringnaComment for trailing-stop fills.
alert_messageseries stringnaDefault alert message.
alert_profitseries stringnaAlert message for take-profit.
alert_lossseries stringnaAlert message for stop-loss.
alert_trailingseries stringnaAlert message for trailing stop.
disable_alertseries boolfalseIf true, disables the strategy alert.

order

pine
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

NameTypeDefaultDescription
idseries stringA unique identifier for the order.
directionseries strategy_directionThe order direction: strategy.long or strategy.short.
qtyseries floatnaThe quantity for the order. When na, uses default quantity.
limitseries floatnaThe limit price for the order. When na, market order.
stopseries floatnaThe stop price for the order.
oca_nameseries stringnaThe name for One-Cancels-All order group.
oca_typeseries stringoca.noneThe OCA order type: oca.cancel_entries, oca.cancel_orders, or oca.none.
commentseries stringnaA comment for the order.
alert_messageseries stringnaA message to display in alerts.
disable_alertseries boolfalseIf true, disables the strategy alert.

Released under the MIT License.