Skip to content

strategy() - Pine Script Function

Calling strategy() defines a Pine Script strategy. Strategies can send simulated orders (strategy.entry, strategy.exit, strategy.order) and produce backtest statistics, equity curves, and performance reports. Every strategy script must invoke strategy() exactly once, typically at the top of the file.

If you omit the `strategy()` call—or place it inside runtime conditionals—the script compiles as an indicator and cannot execute orders.

strategy(title, shorttitle, overlay, format, precision, scale, pyramiding,
calc_on_order_fills, calc_on_every_tick, max_bars_back,
backtest_fill_limits_assumption, default_qty_type, default_qty_value,
initial_capital, currency, slippage, commission_type, commission_value,
process_orders_on_close, close_entries_rule, margin_long, margin_short,
explicit_plot_zorder, max_lines_count, max_labels_count, max_boxes_count,
calc_bars_count, risk_free_rate, use_bar_magnifier,
fill_orders_on_standard_ohlc, max_polylines_count,
dynamic_requests, behind_chart) → void
NameTypeDescription
title / shorttitleconst stringName in the indicator list and chart legend.
overlayconst booltrue draws on the price pane; false creates a separate pane.
pyramidingconst intMaximum simultaneous entries per direction. Defaults to 0 (no pyramiding).
calc_on_order_fillsconst boolRecalculate equity after each simulated fill. Enables intrabar order granularity but slows the script.
calc_on_every_tickconst boolEvaluate orders on every tick instead of bar close. Useful for stop/limit precision; can repaint.
max_bars_backconst intManual history buffer size for all series. Prefer the targeted max_bars_back() function when possible.
default_qty_type / default_qty_valueconst string / const floatDefault order sizing (strategy.percent_of_equity, strategy.fixed, etc.) and quantity.
initial_capitalconst floatStarting equity used in performance calculations.
currencyconst stringCurrency for performance metrics.
slippage, commission_type, commission_valueconstModel transaction costs.
process_orders_on_closeconst boolSubmit all orders on bar close (backtest style).
close_entries_ruleconst stringControls how exits match entries (strategy.close_entries_rule_fifo, etc.).
calc_bars_countconst intLimit bars Pine loads before the last historical bar for faster initialisation.
use_bar_magnifier, fill_orders_on_standard_ohlcconst boolControl intrabar order handling with the Bar Magnifier feature.
max_lines_count, max_labels_count, max_boxes_count, max_polylines_countconst intResource limits for drawings created by the script.

Refer to the official manual for the full parameter list and defaults.

  • Strategies run in the historical context unless calc_on_every_tick=true, so be mindful of repaint risks when enabling tick-level calculations.
  • When you need separate logic for the chart pane (e.g., display-only signals) and the strategy engine, consider building a library or using inputs that toggle visualisation vs order execution.
  • Commission and slippage settings impact performance metrics; configure them to mirror the target venue.
//@version=5
strategy("EMA Cross Strategy",
overlay=true,
initial_capital=25_000,
currency=currency.USD,
pyramiding=1,
commission_type=strategy.commission.percent,
commission_value=0.1)
fastLen = input.int(21, "Fast EMA", minval=1)
slowLen = input.int(55, "Slow EMA", minval=1)
fast = ta.ema(close, fastLen)
slow = ta.ema(close, slowLen)
longSignal = ta.crossover(fast, slow)
shortSignal = ta.crossunder(fast, slow)
if longSignal
strategy.entry("Long", strategy.long)
else if shortSignal
strategy.entry("Short", strategy.short)
strategy.exit("Exit", "Long", stop=slow - ta.atr(14))
strategy.exit("Cover", "Short", stop=slow + ta.atr(14))
plot(fast, "Fast EMA", color=color.teal)
plot(slow, "Slow EMA", color=color.orange)