Appearance
strategy
How do you place orders and read what your strategy holds? Declare the script with strategy(), place orders with strategy.entry, strategy.exit and strategy.close, and read the position and the results through the strategy.* values. Each fill of an entry is a lot: a position is one or more lots on the same side, and exits close them. Every value reads the account as it stands at this bar's close, after every fill so far. When each order fills, and at what price, is on How a strategy fills.
pine
//@version=6
strategy("Moving-average cross", overlay = true,
default_qty_type = strategy.percent_of_equity, default_qty_value = 50)
float fast = ta.sma(close, 10)
float slow = ta.sma(close, 30)
if ta.crossover(fast, slow)
strategy.entry("Long", strategy.long)
if ta.crossunder(fast, slow)
strategy.close("Long")
// A fill shows here from the bar after the order:
// the close that placed it was already over.
plot(strategy.position_size, "Units held", display = display.data_window)
plot(strategy.equity, "Equity", display = display.data_window)strategy.entry
pine
strategy.entry(id, direction, qty, limit, stop, oca_name, oca_type, comment, alert_message, disable_alert) → voidPlaces an order that opens a position, or adds to it up to pyramiding, and reverses a position held the other way.
Placed at a close, a market entry fills at the next open, and one with limit or stop rests until the price reaches it. Calling strategy.entry again with the same id before it fills replaces the waiting order. An entry against the position held closes it and opens its own size the other way. A size under 0 or over 1e12 stops the script with an error. A refused entry, past pyramiding, a risk rule or strategy.risk.allow_entry_in, adds a warning and places nothing. See sizes and reversals.
| Parameter | Default | Description |
|---|---|---|
idseries string | The order's name. Exits and closes name the entry by it, and the trade list shows it. | |
directionseries strategy_direction | strategy.long or strategy.short. | |
qtyseries int/float | Units to trade. Left out or na, strategy()'s default_qty_type and default_qty_value size the order. 0 places nothing. | |
limitseries int/float | A limit price. The order fills at this price or better. | |
stopseries int/float | A stop price. With limit too, the order is a stop-limit. | |
oca_nameseries string | A group name. Orders sharing it cancel or reduce each other as they fill. | |
oca_typeinput string | strategy.oca.none | strategy.oca.cancel, strategy.oca.reduce or strategy.oca.none. |
commentseries string | Text for the trade list. | |
alert_messageNo effect hereseries string | Accepted. Order fills raise no alerts in this engine. | |
disable_alertNo effect hereseries bool | false | Accepted. |
strategy.order
pine
strategy.order(id, direction, qty, limit, stop, oca_name, oca_type, comment, alert_message, disable_alert) → voidPlaces an order that nets against what you hold, with no pyramiding check.
It takes strategy.entry's arguments. At its fill it trades against the open position first: a sell of 5 against a long of 3 closes the 3 and opens a short of 2. Use it to build a position in steps that pyramiding would refuse.
| Parameter | Default | Description |
|---|---|---|
idseries string | The order's name. | |
directionseries strategy_direction | strategy.long to buy or strategy.short to sell. | |
qtyseries int/float | Units to trade. Left out or na, the strategy's default size applies. | |
limitseries int/float | A limit price. | |
stopseries int/float | A stop price. With limit too, the order is a stop-limit. | |
oca_nameseries string | A group name. | |
oca_typeinput string | strategy.oca.none | How the group's orders treat each other. |
commentseries string | Text for the trade list. | |
alert_messageNo effect hereseries string | Accepted. Order fills raise no alerts in this engine. | |
disable_alertNo effect hereseries bool | false | Accepted. |
strategy.exit
pine
strategy.exit(id, from_entry, qty, qty_percent, profit, limit, loss, stop, trail_price, trail_points, trail_offset, oca_name, comment, comment_profit, comment_loss, comment_trailing, alert_message, alert_profit, alert_loss, alert_trailing, disable_alert) → voidPlaces a take-profit, a stop-loss or a trailing stop for an entry, or for every open lot.
The exit rests from the next bar and fills on whichever of its levels the bar's path reaches first. Levels come as prices (limit, stop) or as ticks from the entry price (profit, loss). Given both for one side, the one the price reaches first counts. Call it again with the same id to move its levels. A call whose every level is na places nothing and leaves an exit already resting as it is.
The exit's size belongs to the order, so a second exit with no size on the same entry never fills. See exits and closes and trailing stops.
| Parameter | Default | Description |
|---|---|---|
idseries string | The exit's name. | |
from_entryseries string | The entry to exit. Left out, the exit applies to every open lot. | |
qtyseries int/float | Units to close. Left out with qty_percent also left out, the exit takes what the entry has left. | |
qty_percentseries int/float | A percent of what the lot filled. | |
profitseries int/float | The take-profit distance in ticks from the entry price. | |
limitseries int/float | The take-profit price. | |
lossseries int/float | The stop-loss distance in ticks from the entry price. | |
stopseries int/float | The stop-loss price. | |
trail_priceseries int/float | The price that arms a trailing stop. | |
trail_pointsseries int/float | The distance in ticks, from the entry price, that arms a trailing stop. | |
trail_offsetseries int/float | How far in ticks the trailing stop follows behind the best price. | |
oca_nameNo effect hereseries string | Accepted. An exit's own levels already cancel each other. | |
commentseries string | Text for the trade list, whichever level fills. | |
comment_profitseries string | Text when the take-profit fills. | |
comment_lossseries string | Text when the stop-loss fills. | |
comment_trailingseries string | Text when the trailing stop fills. | |
alert_messageNo effect hereseries string | Accepted. Order fills raise no alerts in this engine. | |
alert_profitNo effect hereseries string | Accepted. | |
alert_lossNo effect hereseries string | Accepted. | |
alert_trailingNo effect hereseries string | Accepted. | |
disable_alertNo effect hereseries bool | false | Accepted. |
strategy.close
pine
strategy.close(id, comment, qty, qty_percent, alert_message, immediately, disable_alert) → voidCloses as many units as the entry named id holds, at the next open, or at this close with immediately = true. By default they come off the oldest lots first, whichever entry opened them, as TradingView does; with close_entries_rule = "ANY" in strategy() they come off that entry's own lots. See which lot closes.
With nothing of that id open, the call does nothing, so an entry placed just before it on the same bar stands. Its order id in the trade list is Close entry(s) order <id>.
| Parameter | Default | Description |
|---|---|---|
idseries string | The entry to close. | |
commentseries string | Text for the trade list. | |
qtyseries int/float | Units to close. Left out, all of them. | |
qty_percentseries int/float | A percent of the position to close. | |
alert_messageNo effect hereseries string | Accepted. Order fills raise no alerts in this engine. | |
immediatelyseries bool | false | true fills at this bar's close instead of the next open. |
disable_alertNo effect hereseries bool | false | Accepted. |
strategy.close_all
pine
strategy.close_all(comment, alert_message, immediately, disable_alert) → voidCloses the whole position, at the next open, or at this close with immediately = true. When flat, it does nothing.
| Parameter | Default | Description |
|---|---|---|
commentseries string | Text for the trade list. | |
alert_messageNo effect hereseries string | Accepted. Order fills raise no alerts in this engine. | |
immediatelyseries bool | false | true fills at this bar's close instead of the next open. |
disable_alertNo effect hereseries bool | false | Accepted. |
strategy.cancel
pine
strategy.cancel(id) → voidCancels the waiting orders with this id, and takes away a trailing stop an exit of that id holds.
| Parameter | Description |
|---|---|
idseries string | The order to cancel. |
strategy.cancel_all
pine
strategy.cancel_all() → voidCancels every waiting order.
strategy.default_entry_qty
pine
strategy.default_entry_qty(fill_price) → series floatThe size a default-sized entry would take if it filled at fill_price now, on this bar's equity.
| Parameter | Description |
|---|---|
fill_priceseries int/float | The price to size at. |
strategy.convert_to_account
Pine v6No effect here
pine
strategy.convert_to_account(value) → series floatThe value it's given, unchanged. Money stays in the symbol's currency in this engine, so there is nothing to convert.
| Parameter | Description |
|---|---|
valueseries int/float | An amount in the symbol's currency. |
strategy.convert_to_symbol
Pine v6No effect here
pine
strategy.convert_to_symbol(value) → series floatThe value it's given, unchanged. Money stays in the symbol's currency in this engine, so there is nothing to convert.
| Parameter | Description |
|---|---|
valueseries int/float | An amount in the account's currency. |
strategy.commission
AmenoScript onlyNo effect here
ameno
strategy.commission(value, type) → voidDoes nothing. TradingView has no such function, only the strategy.commission.* constants, and this engine accepts the call so that a script naming it still runs. Set the commission with strategy()'s commission_type and commission_value.
| Parameter | Description |
|---|---|
valuesimple float | Ignored. |
typesimple string | Ignored. |
strategy.risk.allow_entry_in
pine
strategy.risk.allow_entry_in(value) → voidAllows entries on one side only. An entry on the other side closes what it faces and opens nothing.
| Parameter | Description |
|---|---|
valuesimple string | strategy.direction.long, strategy.direction.short or strategy.direction.all. |
strategy.risk.max_cons_loss_days
pine
strategy.risk.max_cons_loss_days(count, alert_message) → voidStops trading for good after this many losing days in a row: every order is cancelled and what's open closes. A day is a UTC calendar day here, where TradingView counts the symbol's trading day: the two differ on a symbol whose session doesn't start at midnight UTC, such as a US share.
| Parameter | Description |
|---|---|
countsimple int | The losing days allowed. |
alert_messageNo effect heresimple string | Accepted. |
strategy.risk.max_drawdown
pine
strategy.risk.max_drawdown(value, type, alert_message) → voidStops trading for good once equity falls this far from its peak: every order is cancelled and what's open closes.
| Parameter | Description |
|---|---|
valuesimple int/float | The drawdown allowed. |
typesimple string | strategy.cash for money, or strategy.percent_of_equity for a percent of the peak. Left out, cash. |
alert_messageNo effect heresimple string | Accepted. |
strategy.risk.max_intraday_filled_orders
pine
strategy.risk.max_intraday_filled_orders(count, alert_message) → voidStops the day once this many orders have filled, exits included: every order is cancelled, what's open closes, and entries wait for the next UTC day, where TradingView waits for the next trading day.
| Parameter | Description |
|---|---|
countsimple int | The fills allowed in a day. |
alert_messageNo effect heresimple string | Accepted. |
strategy.risk.max_intraday_loss
pine
strategy.risk.max_intraday_loss(value, type, alert_message) → voidCloses everything and stops the day once equity falls this far from the day's high-water mark, checked at every point of each bar's path.
The mark starts at the day's first open and rises at each close. When the loss reaches the limit, the position closes there as Close Position (Max intraday Loss), every order is cancelled, and entries wait for the next UTC day, where TradingView waits for the next trading day. See risk rules.
| Parameter | Description |
|---|---|
valuesimple int/float | The loss allowed. |
typesimple string | strategy.cash for money, or strategy.percent_of_equity for a percent of the mark. Left out, cash. |
alert_messageNo effect heresimple string | Accepted. |
strategy.risk.max_position_size
pine
strategy.risk.max_position_size(contracts) → voidCaps the units held in one direction. An entry past the cap is cut to the room left, and refused when there is none.
| Parameter | Description |
|---|---|
contractssimple int/float | The most units to hold. |
strategy.position_size
pine
series float strategy.position_sizeThe position in units at this bar's close: positive when long, negative when short, 0 when flat.
It includes every fill up to this close. A market order placed at a close fills at the next open, so the position shows it at the next bar's close.
strategy.position_avg_price
pine
series float strategy.position_avg_priceThe average entry price of the open lots, or na when flat.
strategy.position_entry_name
pine
series string strategy.position_entry_nameThe id of the oldest open lot's entry, or "" when flat.
strategy.equity
pine
series float strategy.equityInitial capital plus net profit plus the open position's profit, marked at this bar's close, rounded to the tick, syminfo.mintick.
strategy.netprofit
pine
series float strategy.netprofitThe profit of all closed trades, less every commission paid so far, the open lots' entry commission included.
strategy.openprofit
pine
series float strategy.openprofitThe open position's profit at this bar's close, before its commission.
strategy.initial_capital
pine
series float strategy.initial_capitalThe capital the strategy started with: strategy()'s initial_capital, 1,000,000 by default.
strategy.account_currency
pine
simple string strategy.account_currencyReads "USD", whatever strategy()'s currency or the symbol's own currency is. No amount is converted, so every money figure is in the symbol's currency, and on a symbol quoted in euros the figures are euros under a "USD" label.
strategy.closedtrades
pine
series int strategy.closedtradesHow many trades have closed. The trade accessors number them from 0, the oldest.
strategy.closedtrades.first_index
pine
int strategy.closedtrades.first_indexAlways 0: every closed trade is kept, so the oldest is trade 0.
strategy.opentrades
pine
series int strategy.opentradesHow many lots are open. The open-trade accessors number them from 0, the oldest.
strategy.opentrades.capital_held
pine
series float strategy.opentrades.capital_heldThe open lots' value at their entry prices, or na when margin_long and margin_short are both 0.
strategy.margin_liquidation_price
pine
series float strategy.margin_liquidation_priceThe price at which the open position would take a margin call, rounded to the tick. It reads na when flat, when margin is off, and for a long held at 100% margin, which a fall can't call.
strategy.max_drawdown
pine
series float strategy.max_drawdownThe largest drawdown so far, as TradingView reads it: how far capital plus net profit, with the open lots at their worst, has fallen under the highest that capital plus net profit has been. See the report.
strategy.max_runup
pine
series float strategy.max_runupThe largest run-up so far, as TradingView reads it: how far capital plus net profit, with the open lots at their best, has risen over the lowest that capital plus net profit has been.
strategy.closedtrades.*
What one closed trade was, read by its number. trade_num counts from 0, the oldest, up to strategy.closedtrades - 1. Past the last trade, a number reads na and a text reads "". A trade's profit, run-up and drawdown are in money, in the symbol's currency.
| Name | Gives | What it means |
|---|---|---|
strategy. | series float | The commission it paid, entry and exit together. |
strategy. | series int | The bar index where it entered. |
strategy. | series string | The comment on its entry, or "". |
strategy. | series string | Its entry's id. |
strategy. | series float | The price its entry filled at. |
strategy. | series int | Its entry bar's time, in milliseconds since 1970 UTC. |
strategy. | series int | The bar index where it exited. |
strategy. | series string | The comment on its exit, or "". |
strategy. | series string | The id of the order that closed it, or "". |
strategy. | series float | The price its exit filled at. |
strategy. | series int | Its exit bar's time, in milliseconds since 1970 UTC. |
strategy. | series float | The most it was down while open, plus its entry commission, as TradingView's trade list reads it. |
strategy. | series float | Its drawdown as a percent of its entry cost, commission included. |
strategy. | series float | The most it was up while open, less its entry commission, as TradingView's trade list reads it. |
strategy. | series float | Its run-up as a percent of its entry cost, commission included. |
strategy. | series float | Its profit, after commission. |
strategy. | series float | Its profit as a percent of what it cost to enter. |
strategy. | series float | Its size in units, negative for a short. |
| Each takes | Description |
|---|---|
trade_numseries int | The trade's number, from 0, the oldest. |
strategy.opentrades.*
What one open lot is, read by its number. trade_num counts from 0, the oldest, up to strategy.opentrades - 1. Past the last lot, a number reads na and a text reads "". Profit, run-up and drawdown are in money, in the symbol's currency, marked at this bar's close.
| Name | Gives | What it means |
|---|---|---|
strategy. | series float | The entry commission it paid. |
strategy. | series int | The bar index where it entered. |
strategy. | series string | The comment on its entry, or "". |
strategy. | series string | Its entry's id. |
strategy. | series float | The price its entry filled at. |
strategy. | series int | Its entry bar's time, in milliseconds since 1970 UTC. |
strategy. | series float | The most it has been down so far. |
strategy. | series float | Its drawdown as a percent of its entry value. |
strategy. | series float | The most it has been up so far. |
strategy. | series float | Its run-up as a percent of its entry value. |
strategy. | series float | Its profit at this bar's close, less its entry commission. |
strategy. | series float | Its profit as a percent of what it cost to enter. |
strategy. | series float | Its size in units, negative for a short. |
| Each takes | Description |
|---|---|
trade_numseries int | The lot's number, from 0, the oldest. |
strategy.oca.*
Values for an order's oca_type.
Each is a const string.
| Name | What it means |
|---|---|
strategy. | The first fill in the group cancels the group's other orders. |
strategy. | The order belongs to no group. |
strategy. | Each fill reduces the group's other orders by its size. |
strategy.commission.*
Values for strategy()'s commission_type.
Each is a const string.
| Name | What it means |
|---|---|
strategy. | An amount per unit traded. |
strategy. | An amount per order, shared by size among the lots it closes and opens. |
strategy. | A percent of each fill's value. |
strategy.direction.*
Values for strategy.risk.allow_entry_in.
Each is a const string.
| Name | What it means |
|---|---|
strategy. | Both sides. |
strategy. | Long entries only. |
strategy. | Short entries only. |
strategy.*
The strategy's other figures and constants, each read at this bar's close.
| Name | Type | What it means |
|---|---|---|
strategy. | series float | The mean loss of the losing trades. |
strategy. | series float | The mean loss percent of the losing trades, negative. |
strategy. | series float | The mean profit per closed trade. |
strategy. | series float | The mean profit percent per closed trade. |
strategy. | series float | The mean profit of the winning trades. |
strategy. | series float | The mean profit percent of the winning trades. |
strategy. | const string | default_qty_type for an amount of money, and the cash type for a risk limit. |
strategy. | series int | How many closed trades broke even. |
strategy. | const string | default_qty_type for a fixed number of units. |
strategy. | series float | The sum of the losing trades' losses, as a positive number. |
strategy. | series float | Gross loss as a percent of the initial capital. |
strategy. | series float | The sum of the winning trades' profits. |
strategy. | series float | Gross profit as a percent of the initial capital. |
strategy. | const strategy_direction | The buy direction for an order. |
strategy. | series int | How many closed trades lost money. |
strategy. | series float | The most units held at once, either side. |
strategy. | series float | The most units held long at once. |
strategy. | series float | The most units held short at once. |
strategy. | series float | The largest drawdown as a percent. |
strategy. | series float | The largest run-up as a percent. |
strategy. | series float | Net profit as a percent of the initial capital. |
strategy. | series float | The open profit as a percent of the initial capital plus net profit. |
strategy. | const string | default_qty_type for a percent of equity, and the percent type for a risk limit. |
strategy. | const strategy_direction | The sell direction for an order. |
strategy. | series int | How many closed trades made money. |