Home » Strategy Functions » strategy() Function in Pine Script

strategy() Function in Pine Script

Photo of author
Published on

The strategy() function in Pine Script is a cornerstone for developing trading strategies. Each argument of this function plays a crucial role. Let’s dive into the details of each argument.

Arguments Explained

title (const string)

  • Purpose: Assigns the primary title of the strategy.
  • Display: Appears on the chart and as the default title when publishing.
  • Example: title = "My Trading Strategy"

shorttitle (const string)

  • Purpose: Provides an alternative, shorter title.
  • Display: Replaces the main title in chart-related interfaces.
  • Example: shorttitle = "MTS"

overlay (const bool)

  • Purpose: Determines where the strategy is displayed.
  • Options: true for overlaying on the chart; false for a separate pane.
  • Example: overlay = true

format (const string)

  • Purpose: Specifies value formatting (price, volume, percent).
  • Options: format.inherit, format.price, format.volume, format.percent.
  • Example: format = format.price

precision (const int)

  • Purpose: Sets the number of decimal places for values.
  • Range: 0 to 16.
  • Example: precision = 2

scale (const scale_type)

  • Purpose: Defines the price scale used.
  • Options: scale.right, scale.left, scale.none.
  • Example: scale = scale.left

pyramiding (const int)

  • Purpose: Limits the number of concurrent entries in the same direction.
  • Example: pyramiding = 2

calc_on_order_fills (const bool)

  • Purpose: Triggers recalculation post order fill.
  • Example: calc_on_order_fills = true

calc_on_every_tick (const bool)

  • Purpose: Enables recalculation on each real-time tick.
  • Example: calc_on_every_tick = false

max_bars_back (const int)

  • Purpose: Defines the historical buffer length for referencing past values.
  • Default: Automatically detected, but can be set manually.
  • Example: max_bars_back = 50

backtest_fill_limits_assumption (const int)

  • Purpose: Sets the threshold for limit order execution in ticks.
  • Example: backtest_fill_limits_assumption = 2

default_qty_type (const string)

  • Purpose: Determines the unit for default_qty_value.
  • Options: strategy.fixed, strategy.cash, strategy.percent_of_equity.
  • Example: default_qty_type = strategy.fixed

default_qty_value (const int/float)

  • Purpose: Specifies the default trading quantity.
  • Example: default_qty_value = 10

initial_capital (const int/float)

  • Purpose: Sets the initial trading capital.
  • Example: initial_capital = 100000

currency (const string)

  • Purpose: Defines the strategy’s operational currency.
  • Options: currency.NONE, currency.USD, etc.
  • Example: currency = currency.USD

slippage (const int)

  • Purpose: Accounts for slippage in ticks.
  • Example: slippage = 2

commission_type (const string)

  • Purpose: Sets the basis for commission calculations.
  • Options: strategy.commission.percent, strategy.commission.cash_per_contract, strategy.commission.cash_per_order.
  • Example: commission_type = strategy.commission.percent

commission_value (const int/float)

  • Purpose: Specifies the commission amount.
  • Example: commission_value = 0.1

process_orders_on_close (const bool)

  • Purpose: Determines if orders are processed at bar close.
  • Example: process_orders_on_close = true

close_entries_rule (const string)

  • Purpose: Sets the rule for closing trades.
  • Options: "FIFO" or "ANY".
  • Example: close_entries_rule = "FIFO"

margin_long (const int/float)

  • Purpose: Specifies the margin percentage for long positions.
  • Example: margin_long = 50

margin_short (const int/float)

  • Purpose: Specifies the margin percentage for short positions.
  • Example: margin_short = 50

explicit_plot_zorder (const bool)

  • Purpose: Controls the rendering order of plots and lines.
  • Example: explicit_plot_zorder = true

max_lines_count, max_labels_count, max_boxes_count (const int)

  • Purpose: Limits the number of display elements.
  • Example: max_lines_count = 100, max_labels_count = 50, max_boxes_count = 25

risk_free_rate (const int/float)

  • Purpose: Sets the annual risk-free rate of return.
  • Example: risk_free_rate = 2

use_bar_magnifier (const bool)

  • Purpose: Enables lower timeframe data for backtesting.
  • Example: use_bar_magnifier = true

max_polylines_count (const int)

  • Purpose: Limits the number of polyline drawings.
  • Example: max_polylines_count = 50

Practical Example of strategy() Function in Pine Script

Example Strategy Script

strategy("Enhanced Trading Strategy", shorttitle="ETS", overlay=true, 
         precision=2, pyramiding=3, calc_on_order_fills=false, 
         calc_on_every_tick=true, max_bars_back=100, 
         backtest_fill_limits_assumption=1, default_qty_type=strategy.percent_of_equity, 
         default_qty_value=5, initial_capital=50000, currency=currency.USD, 
         slippage=1, commission_type=strategy.commission.percent, 
         commission_value=0.05, process_orders_on_close=true, close_entries_rule="FIFO", 
         margin_long=30, margin_short=30, explicit_plot_zorder=false, 
         max_lines_count=50, max_labels_count=50, max_boxes_count=20, 
         risk_free_rate=2, use_bar_magnifier=false, max_polylines_count=30)

// Entry condition: Long when the current close is greater than the previous high.
if close > high[1]
    strategy.entry("LongEntry", strategy.long)

// Exit condition: Taking profit at 10 points and stop loss at 5 points.
strategy.exit("ExitLong", "LongEntry", profit=10, loss=5)

Key Takeaways

  • Customization and Flexibility: This example demonstrates how the strategy() function can be customized extensively with a variety of parameters to fit specific trading needs.
  • Strategy Identification: The title and shorttitle provide a clear identification of the strategy on charts.
  • Trade Execution and Management:
    • pyramiding is set to 3, allowing multiple entries in the same direction.
    • calc_on_every_tick ensures the strategy recalculates with each real-time price update.
    • process_orders_on_close allows for processing orders at the close of a bar, adding realism to backtesting.
  • Risk and Money Management:
    • default_qty_type and default_qty_value control the trade size based on a percentage of equity.
    • margin_long and margin_short set margin requirements, helping in risk management.
  • Commission and Slippage: Accounting for real-world trading costs with commission_type, commission_value, and slippage.
  • Visualization and Limits:
    • explicit_plot_zorder and limits on lines, labels, and boxes (max_lines_count, etc.) manage the visual aspects of the strategy on the chart.
  • Advanced Features:
    • use_bar_magnifier for detailed backtesting (disabled in this example).
    • risk_free_rate to set a baseline for performance comparison.
  • Practical Application: The entry and exit logic illustrate a simple trend-following strategy where positions are entered when the current close is above the previous high and exited with a predefined profit or loss.


Each argument of the strategy() function contributes to shaping the behavior and characteristics of a trading strategy in Pine Script. Understanding and effectively using these parameters allows for the creation of tailored and sophisticated trading strategies.

Leave a Comment