strategy() - Pine Script Function
strategy()
Section titled “strategy()”Overview
Section titled “Overview”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.
Syntax
Section titled “Syntax”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) → voidKey parameters
Section titled “Key parameters”| Name | Type | Description |
|---|---|---|
title / shorttitle | const string | Name in the indicator list and chart legend. |
overlay | const bool | true draws on the price pane; false creates a separate pane. |
pyramiding | const int | Maximum simultaneous entries per direction. Defaults to 0 (no pyramiding). |
calc_on_order_fills | const bool | Recalculate equity after each simulated fill. Enables intrabar order granularity but slows the script. |
calc_on_every_tick | const bool | Evaluate orders on every tick instead of bar close. Useful for stop/limit precision; can repaint. |
max_bars_back | const int | Manual history buffer size for all series. Prefer the targeted max_bars_back() function when possible. |
default_qty_type / default_qty_value | const string / const float | Default order sizing (strategy.percent_of_equity, strategy.fixed, etc.) and quantity. |
initial_capital | const float | Starting equity used in performance calculations. |
currency | const string | Currency for performance metrics. |
slippage, commission_type, commission_value | const | Model transaction costs. |
process_orders_on_close | const bool | Submit all orders on bar close (backtest style). |
close_entries_rule | const string | Controls how exits match entries (strategy.close_entries_rule_fifo, etc.). |
calc_bars_count | const int | Limit bars Pine loads before the last historical bar for faster initialisation. |
use_bar_magnifier, fill_orders_on_standard_ohlc | const bool | Control intrabar order handling with the Bar Magnifier feature. |
max_lines_count, max_labels_count, max_boxes_count, max_polylines_count | const int | Resource limits for drawings created by the script. |
Refer to the official manual for the full parameter list and defaults.
Remarks
Section titled “Remarks”- 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.
Example
Section titled “Example”//@version=5strategy("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)