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

Understanding the strategy.close() Function in Pine Script

Photo of author
Published on

In this article, we delve into the strategy.close() function, a crucial component for managing positions in trading scripts created with Pine Script. This function allows script authors to exit positions based on specific criteria, offering a high degree of control over trading strategies. 

Syntax and Arguments

The strategy.close() function follows this syntax:

strategy.close(id, comment, qty, qty_percent, alert_message, immediately, disable_alert) → void

Let’s dissect the arguments to understand their roles and how they can be utilized in a script:

  • id (series string): This mandatory parameter identifies the order to be closed. By specifying the order’s ID, you can target specific entries for closure.
  • comment (series string): An optional field for notes or comments about the order. It’s a useful feature for keeping track of order details within the script.
  • qty (series int/float): Specifies the number of contracts, shares, lots, or units to close in the trade. This optional parameter defaults to ‘NaN’, meaning it’s not set unless explicitly defined by the script writer.
  • qty_percent (series int/float): Determines the percentage (0-100) of the position to close. This optional parameter provides an alternative to qty for defining the exit size and defaults to 100, intending to close the entire position unless stated otherwise.
  • alert_message (series string): Replaces the {{strategy.order.alert_message}} placeholder in the “Create Alert” dialog box. This optional parameter allows for customized alert messages when the order is executed.
  • immediately (series bool): If set to true, the order will be executed immediately on the current tick, bypassing any restrictions on order execution timing. By default, it’s false, meaning orders are typically executed at the opening of the next bar.
  • disable_alert (series bool): Prevents the strategy alert from triggering when the order is executed if set to true. This optional parameter provides control over which orders generate alerts upon completion.

Example

Consider the following script that demonstrates the use of strategy.close():

//@version=5
strategy("exitDemo", overlay=false)
if open > close
    strategy.entry("purchase", strategy.long)
if open < close
    strategy.close("purchase", qty_percent = 50, comment = "close purchase for 50%")
plot(strategy.position_size)
Example

In this example, the strategy named exitDemo decides on entry and exit points based on the relationship between the open and close prices of a bar:

  1. Entry Condition: If the opening price is greater than the closing price, an entry order named “purchase” is placed to go long.
  2. Exit Condition: Conversely, if the opening price is lower than the closing price, half of the “purchase” position is closed. This is specified by setting qty_percent to 50 and providing a comment for clarity.
  3. Position Size Plotting: The plot() function visualizes the size of the current position, offering insight into how the position size changes over time as entries and exits are executed.

Key Takeaways

  • The strategy.close() function is essential for dynamically managing exits from positions in Pine Script trading strategies.
  • By carefully setting parameters like id, qty, and qty_percent, script authors can tailor exit strategies to their specific trading criteria.
  • The immediately parameter offers flexibility in order execution, allowing for immediate action based on the script’s logic.
  • Utilizing comments and alert messages can enhance the clarity and monitoring capabilities of a trading script.

Incorporating the strategy.close() function into your Pine Script strategies enables precise control over position management, vital for implementing effective trading strategies.

Leave a Comment