Metadata / Reference
strategy
TradingView-compatible spelling: strategy, runs without warnings
Defines the script as a strategy and sets backtesting properties. Use as the single script header (do not combine with indicator/study headers).
strategy(title, shorttitle, overlay, initial_capital, default_qty_value, default_qty_type, currency, pyramiding, commission_type, commission_value, slippage, process_orders_on_close, calc_on_order_fills, calc_on_every_tick, close_entries_rule, margin_long, margin_short, max_lines_count, max_labels_count, max_boxes_count, max_polylines_count, max_bars_back, risk_free_rate, use_bar_magnifier, fill_orders_on_standard_ohlc, scale) → voidParameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
title | string | Yes | — | Title of the strategy. |
shorttitle | string | Not specified | — | Short title. |
overlay | bool | Not specified | true | Overlay on main chart. |
initial_capital | number | Not specified | 10000 | Backtest starting capital. |
default_qty_value | number | Not specified | 1 | Default position size. |
default_qty_type | string | Not specified | strategy.fixed | Default quantity type. Pine v6: strategy.fixed (default_qty_value contracts) | strategy.contracts | strategy.cash (default_qty_value of cash per entry) | strategy.percent_of_equity (default_qty_value % of equity per entry, sized at the signal bar's close). |
currency | string | Not specified | currency.NONE | Account currency. currency.NONE (default) is the symbol's own currency (USDT on BTCUSDT); no FX conversion is applied. |
pyramiding | number | Not specified | 0 | Maximum entries in same direction. |
commission_type | string | Not specified | percent | Commission mode. |
commission_value | number | Not specified | 0 | Commission amount. |
slippage | number | Not specified | 0 | Per-order slippage in ticks. |
process_orders_on_close | bool | Not specified | false | Pine v6: fill market-on-close orders at the closing price of the same bar. |
calc_on_order_fills | bool | Not specified | false | Accepted for compatibility. The strategy is calculated once per bar; a fill does not trigger another calculation on the same bar. |
calc_on_every_tick | bool | Not specified | false | Accepted for compatibility. On a live chart a strategy is recalculated while the bar forms whether or not this is set; historical bars are calculated once. |
close_entries_rule | string | Not specified | FIFO | How stacked entries are unwound by exit-by-contract orders. FIFO (default) or ANY. |
margin_long | number | Not specified | 100 | Required margin (%) for long positions, 0..100. |
margin_short | number | Not specified | 100 | Required margin (%) for short positions, 0..100. |
max_lines_count | number | Not specified | 50 | Max live line.new objects (50..500). |
max_labels_count | number | Not specified | 50 | Max live label.new objects (50..500). |
max_boxes_count | number | Not specified | 50 | Max live box.new objects (50..500). |
max_polylines_count | number | Not specified | 50 | Max live polyline.new objects (1..100). |
max_bars_back | number | Not specified | — | Accepted for compatibility and has no effect: every bar of a series' history can be referenced. |
risk_free_rate | number | Not specified | 2.0 | Accepted for compatibility; the Strategy Report has no Sharpe or Sortino ratio, so it changes nothing. |
use_bar_magnifier | bool | Not specified | false | Accepted for compatibility. Fills are simulated on the chart's own bars (see the note on the order within a bar); lower-timeframe data is not used. |
fill_orders_on_standard_ohlc | bool | Not specified | false | Accepted for compatibility: strategies always run and fill on the standard OHLC bars, also when the chart draws Heikin Ashi candles. |
scale | string | Not specified | — | Price scale the strategy is drawn against: scale.right, scale.left or scale.none. Named only here: listed last so positional arguments keep their places. |
Returns
voidExample
strategy
strategy("Trend Strategy", overlay=true)if bar_index == 1 strategy.entry("Long", "long")Notes
- Only one script header is allowed: indicator OR strategy OR study.
- Orders fill in the order the price reaches them within a bar, as on TradingView: market orders at the open (a close first, then an entry), then every resting limit, stop and trailing order along the path open → nearer extreme → farther extreme → close. An entry filled on the way arms the exits placed with it for the rest of that bar, and an exit filled on the way lets a resting entry fill later in the bar. A bar whose low is nearer the open than its high reaches a stop below the price before a target above it.
- On a live chart the forming bar follows the path the price really took, so live fills match the order of the ticks. Each fill can fire the strategy's order-fill alert, also when it happened between two updates of the chart.
Names and aliases
Guides and navigation are localized. Built-in reference descriptions and code examples are maintained in English.