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

Understanding the strategy.cancel_all() Function in Pine Script

Photo of author
Published on

The strategy.cancel_all() function in Pine Script is a powerful tool for managing and controlling the execution of trading strategies within the TradingView platform. It serves as a command to instantly cancel or deactivate all pending orders that have been placed by the strategy’s order functions. This includes orders generated by strategy.order, strategy.entry, strategy.exit, and strategy.close. This tutorial will walk you through the syntax and a practical example to illustrate its use.

Syntax

The syntax for strategy.cancel_all() is straightforward:

strategy.cancel_all() → void

This function does not take any parameters and returns nothing (void). Its sole purpose is to cancel all pending orders at the point where it is called within the script.

Example

Let’s dive into an example that demonstrates the use of strategy.cancel_all() in a trading strategy.

//@version=5
strategy(title = "Simple Order Cancellation Example")
triggerBuy1 = open > high[1]
if triggerBuy1
    strategy.entry("Long Position 1", strategy.long, 1, limit = low) // Enter long by limit if triggerBuy1 is true
triggerBuy2 = triggerBuy1 and open[1] > high[2]
if triggerBuy2
    strategy.entry("Long Position 2", strategy.long, 1, limit = ta.lowest(low, 2)) // Enter long by limit if triggerBuy2 is true
stopTradingCondition = open < ta.lowest(low, 2)
if stopTradingCondition
    strategy.cancel_all() // Cancel both limit orders if the condition stopTradingCondition is true

Code Walkthrough

  • Initialization: The script starts by declaring the strategy with a title.
  • Condition for First Long Entry (triggerBuy1): Checks if the current open price is greater than the high of the previous candle.
  • First Long Entry Order: If triggerBuy1 is true, a long entry order is placed with a limit price equal to the current low price.
  • Condition for Second Long Entry (triggerBuy2): This condition is a bit stricter. It requires triggerBuy1 to be true and also the open price of the previous candle to be greater than the high of the candle before that.
  • Second Long Entry Order: Similar to the first, but the limit price is set to the lowest low of the last two candles.
  • Stop Trading Condition (stopTradingCondition): Determines when to stop trading by checking if the current open price is below the lowest low of the last two candles.
  • Canceling All Orders: If the stop trading condition is met, strategy.cancel_all() is invoked to cancel any pending orders.

Key Features and Takeaways

  • Function Useability: The strategy.cancel_all() function is crucial for strategies that require a dynamic response to changing market conditions, allowing for a quick exit from pending positions.
  • Syntax Simplicity: With no parameters needed, its implementation is straightforward, enhancing script readability and maintainability.
  • Application Scenario: Best used in scenarios where the market moves against your open or pending positions, and you need to exit these positions promptly to avoid losses.

This detailed walkthrough of the strategy.cancel_all() function demonstrates its significance in developing adaptable and responsive trading strategies in Pine Script. By incorporating this function, traders can enhance their strategy’s flexibility, effectively managing trades in accordance with market changes.

Leave a Comment