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

Understanding the strategy.close_all() Function in Pine Script

Photo of author
Published on

In this article, we will delve into the syntax, usage, and practical applications of strategy.close_all() to provide a comprehensive understanding of how it can be utilized in trading scripts.

Syntax and Overloads

The strategy.close_all() function comes with two syntax variations, allowing traders to add more context to their strategy executions:

  1. Basic Use: strategy.close_all(comment, alert_message) → void
  2. Extended Use with Immediate Exit and Alert Control:
    strategy.close_all(comment, alert_message, immediately, disable_alert) → void


  • comment (series string): This is an optional parameter where you can include additional notes or comments about the order. It helps in providing context or rationale behind the strategy’s decision to exit all positions.
  • alert_message (series string): Another optional parameter that allows for custom messages in alerts. This message replaces the {{strategy.order.alert_message}} placeholder in the “Create Alert” dialog box’s “Message” field on TradingView, offering a customized alert message based on the strategy’s actions.
  • immediately (bool): An optional parameter available in the extended use version, specifying whether the exit should be executed immediately at the moment the condition is met.
  • disable_alert (bool): Also an optional parameter in the extended use version, it allows traders to disable alert messages for this specific order.


Let’s look at a simple yet illustrative example of how strategy.close_all() can be implemented in a Pine Script strategy:

strategy("CloseAll Example", overlay=false)

if open > close
    strategy.entry("EnterLong", strategy.long)
if open < close
    strategy.close_all(comment = "Exiting all positions")


In this script, we’ve set up a basic strategy that enters a long position (strategy.long) when the opening price of a bar is greater than the closing price. Conversely, if the opening price is less than the closing price, the strategy exits all positions using strategy.close_all(), with a comment “Exiting all positions” for clarity.

Detailed Walkthrough

  • Entry Condition: if open > close checks if the current bar’s opening price is higher than its closing price. If true, the strategy initiates a long entry named “EnterLong”.
  • Exit Condition: if open < close evaluates whether the opening price is lower than the closing price for the current bar. When this condition is met, the strategy executes strategy.close_all(comment = "Exiting all positions"), closing all open positions to flatten the strategy’s market exposure.
  • Comment: The comment “Exiting all positions” serves as a note for this particular order, helping to identify the reason behind the strategy’s decision in historical data or logs.
  • Plotting Position Size: plot(strategy.position_size) visually represents the strategy’s position size over time on the chart, allowing you to observe the changes in market position directly.

Key Features, Usability, and Application

  • Functionality: Allows for a quick and efficient way to exit all positions, streamlining strategy management.
  • Custom Alerts: The ability to customize alert messages adds a layer of personalization and clarity to strategy alerts, improving the decision-making process.
  • Immediate Exit Option: With the extended syntax, traders have the flexibility to exit positions immediately, offering greater control over the timing of exits.


  • The strategy.close_all() function is a powerful feature in Pine Script for managing market exposure by exiting all positions.
  • Custom comments and alert messages enhance strategy tracking and analysis.
  • The function’s flexibility and control over exit timing and alerts make it a valuable tool for strategy optimization.

By incorporating strategy.close_all() into your trading scripts, you can improve the management of your strategies, ensuring that you have precise control over when to exit the market, thereby potentially increasing the effectiveness of your trading approach.

Leave a Comment