Skip to main content
Language version: 2 · Reference version: 2.7 · Date: 2026-10-25 · Compiler build: df7b3b14 · Namespace: strategy · Entries: 30

strategy (library)

Definition Functions, variables and constants for strategies: scripts declared with strategy() that place simulated orders. Orders are placed with strategy.entry, strategy.order, strategy.exit, strategy.close and strategy.close_all, and cancelled with strategy.cancel and strategy.cancel_all. Performance is read through variables such as strategy.netprofit, strategy.equity and strategy.position_size, and trade by trade through the strategy.closedtrades_* and strategy.opentrades_* functions. Constants such as strategy.long, strategy.fixed and strategy.commission_percent configure orders and the strategy itself. Example
GoCharting is a charting and analytics tool. This is not investment advice. Trading and investing carry a high risk of loss and are not suitable for everyone. You can lose some or all of your money.

strategy.cancel (function)

Definition Cancels the pending order named id: a limit or stop order that has not filled yet. Syntax
Parameters Returns void Example

strategy.cancel_all (function)

Definition Cancels every pending order that has not filled yet. Syntax
Returns void Example

strategy.close (function)

Definition Closes the position opened by the entry named id with a market order. qty closes only part of it; comment labels the closing order. Syntax
Parameters Returns void Example

strategy.close_all (function)

Definition Closes the whole open position, whatever entries it came from, with a market order. Syntax
Parameters Returns void Example

strategy.closedtrades_commission (function)

Definition For the closed trade number trade_num, returns the commission paid on the trade, in the account currency. Trades are numbered from 0 in the order they were closed, so the most recent is strategy.closedtrades - 1. Syntax
Parameters Returns series float Example

strategy.closedtrades_entry_bar_index (function)

Definition For the closed trade number trade_num, returns the bar index of the bar on which the entry order was placed (where strategy.entry or strategy.order ran), not the bar on which it filled; the two differ when the order fills on a later bar. Trades are numbered from 0 in the order they were closed, so the most recent is strategy.closedtrades - 1. The result is na for a number that is not a closed trade, and for a trade that has dropped out of the 1000 most recent closed trades kept. Syntax
Parameters Returns series int Example

strategy.closedtrades_entry_comment (function)

Definition For the closed trade number trade_num, returns the comment of the entry order that opened the trade. Trades are numbered from 0 in the order they were closed, so the most recent is strategy.closedtrades - 1. Syntax
Parameters Returns series string Example

strategy.closedtrades_entry_id (function)

Definition For the closed trade number trade_num, returns the id of the entry order that opened the trade. Trades are numbered from 0 in the order they were closed, so the most recent is strategy.closedtrades - 1. Syntax
Parameters Returns series string Example

strategy.closedtrades_entry_price (function)

Definition For the closed trade number trade_num, returns the price at which the trade was entered. Trades are numbered from 0 in the order they were closed, so the most recent is strategy.closedtrades - 1. Syntax
Parameters Returns series float Example

strategy.closedtrades_entry_time (function)

Definition For the closed trade number trade_num, returns the UNIX time, in milliseconds, at which the trade was entered. Trades are numbered from 0 in the order they were closed, so the most recent is strategy.closedtrades - 1. Syntax
Parameters Returns series int Example

strategy.closedtrades_exit_bar_index (function)

Definition For the closed trade number trade_num, returns the bar index at which the trade was closed. In this build it is always 0: the broker does not record the exit bar. strategy.closedtrades_exit_time holds the time of the exit. Trades are numbered from 0 in the order they were closed, so the most recent is strategy.closedtrades - 1. The result is na for a number that is not a closed trade, and for a trade that has dropped out of the 1000 most recent closed trades kept. Syntax
Parameters Returns series int Example

strategy.closedtrades_exit_price (function)

Definition For the closed trade number trade_num, returns the price at which the trade was closed. Trades are numbered from 0 in the order they were closed, so the most recent is strategy.closedtrades - 1. Syntax
Parameters Returns series float Example

strategy.closedtrades_exit_time (function)

Definition For the closed trade number trade_num, returns the UNIX time, in milliseconds, at which the trade was closed. Trades are numbered from 0 in the order they were closed, so the most recent is strategy.closedtrades - 1. Syntax
Parameters Returns series int Example

strategy.closedtrades_max_drawdown (function)

Definition For the closed trade number trade_num, returns the largest loss the trade showed while it was open, in the account currency. In this build it is always 0 for a closed trade: the figure measured while the trade was open is not carried into the closed-trade record. strategy.opentrades_max_drawdown and strategy.opentrades_max_runup are measured. Trades are numbered from 0 in the order they were closed, so the most recent is strategy.closedtrades - 1. The result is na for a number that is not a closed trade, and for a trade that has dropped out of the 1000 most recent closed trades kept. Syntax
Parameters Returns series float Example

strategy.closedtrades_max_runup (function)

Definition For the closed trade number trade_num, returns the largest profit the trade showed while it was open, in the account currency. In this build it is always 0 for a closed trade: the figure measured while the trade was open is not carried into the closed-trade record. strategy.opentrades_max_drawdown and strategy.opentrades_max_runup are measured. Trades are numbered from 0 in the order they were closed, so the most recent is strategy.closedtrades - 1. The result is na for a number that is not a closed trade, and for a trade that has dropped out of the 1000 most recent closed trades kept. Syntax
Parameters Returns series float Example

strategy.closedtrades_profit (function)

Definition For the closed trade number trade_num, returns the profit of the trade, in the account currency; a loss is negative. Trades are numbered from 0 in the order they were closed, so the most recent is strategy.closedtrades - 1. Syntax
Parameters Returns series float Example

strategy.closedtrades_size (function)

Definition For the closed trade number trade_num, returns the size of the trade. The size is never negative: a short trade has a positive size, because the direction is stored apart from the quantity. Syntax
Parameters Returns series float Example

strategy.entry (function)

Definition Opens a position, or adds to one, in direction (strategy.long or strategy.short). An entry in the opposite direction to the current position first closes that position, then reverses it. id names the entry so that strategy.close and strategy.exit can refer to it. Without limit or stop it is a market order; with limit or stop it waits for that price. qty overrides the default quantity set in strategy(). Syntax
Parameters Returns void Example

strategy.exit (function)

Definition Places exit orders for an open position: a take-profit, a stop-loss, or both. from_entry names the entry to exit; leave it out to exit all entries. Prices can be given as levels (limit for the take-profit, stop for the stop-loss) or as distances in ticks from the entry price (profit and loss). When both kinds are given for the same side, the one that would fill first is used. Syntax
Parameters Returns void Example

strategy.opentrades_entry_bar_index (function)

Definition For the open trade number trade_num, returns the bar index of the bar on which the entry order was placed (where strategy.entry or strategy.order ran), not the bar on which it filled; the two differ when the order fills on a later bar. Trades are numbered from 0 in the order they were opened, so the most recent is strategy.opentrades - 1. Syntax
Parameters Returns series int Example

strategy.opentrades_entry_comment (function)

Definition For the open trade number trade_num, returns the comment of the entry order that opened the trade. Trades are numbered from 0 in the order they were opened, so the most recent is strategy.opentrades - 1. Syntax
Parameters Returns series string Example

strategy.opentrades_entry_id (function)

Definition For the open trade number trade_num, returns the id of the entry order that opened the trade. Trades are numbered from 0 in the order they were opened, so the most recent is strategy.opentrades - 1. Syntax
Parameters Returns series string Example

strategy.opentrades_entry_price (function)

Definition For the open trade number trade_num, returns the price at which the trade was entered. Trades are numbered from 0 in the order they were opened, so the most recent is strategy.opentrades - 1. Syntax
Parameters Returns series float Example

strategy.opentrades_entry_time (function)

Definition For the open trade number trade_num, returns the UNIX time, in milliseconds, at which the trade was entered. Trades are numbered from 0 in the order they were opened, so the most recent is strategy.opentrades - 1. Syntax
Parameters Returns series int Example

strategy.opentrades_max_drawdown (function)

Definition For the open trade number trade_num, returns the largest fall of the trade’s profit from its highest profit so far, in the account currency. The highest profit starts at 0, so for a trade that has never been in profit this is its largest loss. Trades are numbered from 0 in the order they were opened, so the most recent is strategy.opentrades - 1. Syntax
Parameters Returns series float Example

strategy.opentrades_max_runup (function)

Definition For the open trade number trade_num, returns the highest profit the trade has shown so far, in the account currency (0 until the trade is in profit). Trades are numbered from 0 in the order they were opened, so the most recent is strategy.opentrades - 1. Syntax
Parameters Returns series float Example

strategy.opentrades_profit (function)

Definition For the open trade number trade_num, returns the current, unrealised profit of the trade, in the account currency; a loss is negative. Trades are numbered from 0 in the order they were opened, so the most recent is strategy.opentrades - 1. Syntax
Parameters Returns series float Example

strategy.opentrades_size (function)

Definition For the open trade number trade_num, returns the size of the trade. The size is never negative: a short trade has a positive size, because the direction is stored apart from the quantity. Syntax
Parameters Returns series float Example

strategy.order (function)

Definition Places an order without any position logic: a buy adds to the position and a sell subtracts from it, whatever the current position is. Unlike strategy.entry, it does not reverse a position automatically and it is not limited by pyramiding. Syntax
Parameters Returns void Example