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

Understanding the strategy.closedtrades.max_drawdown() Function in Pine Script

Photo of author
Published on

The strategy.closedtrades.max_drawdown()function returns the maximum drawdown of a closed trade, essentially measuring the maximum loss from a peak to a trough during the trade, expressed in the account currency. Below, we will explore the syntax, usage, and an example of how to implement this function within your trading strategy.

Syntax

strategy.closedtrades.max_drawdown(trade_num) → series float

Arguments

  • trade_num (series int): The trade number of the closed trade, starting from zero for the first trade.

Example

The given example demonstrates how to utilize the strategy.closedtrades.max_drawdown() function within a simple trading strategy. This strategy enters long trades every 15 bars and exits them every 20 bars. The objective is to calculate and plot the largest maximum drawdown from all closed trades. Let’s break down the example:

//@version=5
strategy("strategy.closedtrades.max_drawdown Example")

// Strategy calls to enter long trades every 15 bars and exit long trades every 20 bars.
if bar_index % 15 == 0
    strategy.entry("LongPosition", strategy.long)
if bar_index % 20 == 0
    strategy.close("LongPosition")

// Get the biggest max trade drawdown value from all of the closed trades.
maxTradeDrawdown() =>
    largestDrawdown = 0.0
    for tradeIndex = 0 to strategy.closedtrades - 1
        largestDrawdown := math.max(largestDrawdown, strategy.closedtrades.max_drawdown(tradeIndex))
    largestDrawdown

plot(maxTradeDrawdown(), "Biggest Max Drawdown")
Example

Detailed Walkthrough

  • Strategy Definition: The strategy is named “strategy.closedtrades.max_drawdown Example” using the strategy() function.
  • Entry and Exit Logic: The script contains logic to enter long positions (strategy.entry()) every 15 bars and close them (strategy.close()) every 20 bars.
  • Calculating the Maximum Drawdown:
  • A user-defined function maxTradeDrawdown() is created to calculate the largest drawdown across all closed trades.
  • It initializes a variable largestDrawdown to zero. This variable will store the maximum drawdown encountered.
  • A for-loop iterates over all closed trades, using strategy.closedtrades.max_drawdown(tradeIndex) to obtain the drawdown of each trade and updating largestDrawdown with the maximum value found.
  • Plotting: The plot() function is used to display the largest drawdown on the chart with the label “Biggest Max Drawdown”.

Key Features and Takeaways

  • Function Usefulness: The strategy.closedtrades.max_drawdown() function is crucial for evaluating the risk of trading strategies by identifying the maximum loss that occurred in any closed trade.
  • Syntax and Application: It requires a single argument, the trade number, and returns the drawdown as a floating-point number, allowing for easy integration into strategy analysis and optimization.
  • Practical Implementation: The example provided illustrates a practical application of this function, enabling users to monitor and assess the risk level of their strategies dynamically.

By understanding and implementing the strategy.closedtrades.max_drawdown() function, Pine Script users can gain valuable insights into the risk profile of their trading strategies, aiding in the development of more robust and resilient trading algorithms.

Leave a Comment