Home » Strategy Functions » Understanding the strategy.risk.max_position_size() Function in Pine Script

Understanding the strategy.risk.max_position_size() Function in Pine Script

Photo of author
Published on

This article delves into the usage, syntax, and practical application of strategy.risk.max_position_size() function to ensure your trading strategies adhere to predefined risk parameters.


The syntax for the strategy.risk.max_position_size() the function is straightforward:

strategy.risk.max_position_size(contracts) → void


  • contracts: This argument is a simple integer or float that specifies the maximum number of contracts, shares, lots, or units allowed in a position.


Here’s a modified example to illustrate how the function works:

strategy("Risk Control Demo", default_qty_value = 100)
max_position_cap = 10
if open > close
    strategy.entry("sell_order", strategy.short)
plot(strategy.position_size)  // maximum plot value will be 10

In this example, we define a trading strategy named “Risk Control Demo”. We set the default quantity value for entering trades at 100. However, we impose a cap on the position size using strategy.risk.max_position_size(max_position_cap), where max_position_cap is set to 10. This means that despite the default entry size, the strategy will not exceed a position size of 10 contracts/shares/lots/units.


  1. Defining the Strategy: strategy("Risk Control Demo", default_qty_value = 100) initializes our strategy with a name and a default quantity for trades.
  2. Setting Maximum Position Size: max_position_cap = 10 followed by strategy.risk.max_position_size(max_position_cap) uses the strategy.risk.max_position_size() function to limit the maximum size of any position to 10 units. This ensures that the strategy adheres to our risk management rules.
  3. Condition for Entry: if open > close checks if the opening price is greater than the closing price. If true, it attempts to enter a short position with strategy.entry("sell_order", strategy.short).
  4. Plotting Position Size: plot(strategy.position_size) visually represents the position size on the chart, confirming that it does not exceed the set limit of 10 units.

Key Features and Takeaways

  • Risk Management: The strategy.risk.max_position_size() function is essential for implementing risk management in trading strategies. It ensures that the strategy does not take on a position larger than what is considered safe or within the risk appetite.
  • Syntax and Application: The function requires a single argument that specifies the maximum position size. It is versatile and can be applied to various asset types, including stocks, futures, and forex.
  • Strategy Entry Impact: This function directly affects the strategy.entry() function by potentially reducing the entry quantity to ensure the total position size remains within the specified limit.
  • Protection Against Oversized Positions: In scenarios where the minimum possible quantity still exceeds the maximum position size, the order will not be placed, providing an additional layer of risk control.
  • Visualization and Debugging: Plotting the position size on the chart can be a helpful tool for visualizing how the function controls position sizes and for debugging strategy behavior.

In conclusion, the strategy.risk.max_position_size() function is a powerful tool for Pine Script developers looking to incorporate strict risk management rules into their trading strategies. By limiting the maximum position size, traders can avoid excessive exposure and align their strategies with their risk tolerance levels.

Leave a Comment