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

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

Photo of author
Published on

In this tutorial, we delve into the Pine Script strategy.closedtrades.max_runup() function. This function returns the maximum run-up of a closed trade, essentially quantifying the highest potential profit that was available during the trade’s duration. It’s measured in the currency of the strategy’s account, providing a clear insight into the trade’s profitability.

Syntax

strategy.closedtrades.max_runup(trade_num) → series float

Arguments

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

This function thus offers a way to retrospectively evaluate the performance of individual trades within a broader trading strategy, allowing traders to fine-tune their approaches based on the maximum profit potential that was available.

Example: Monitoring Maximum Trade Run-Up

Consider a simple strategy that enters long positions every 15 bars and exits them every 20 bars. Our goal is to evaluate the maximum profit potential (max run-up) of each closed trade throughout this strategy’s execution.

//@version=5
strategy("Max Run-Up Analysis")

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

// Function to get the highest max trade run-up value from all closed trades.
maxTradeRunUpCalc() =>
    maxRunupCalc = 0.0
    for tradeIdx = 0 to strategy.closedtrades - 1
        maxRunupCalc := math.max(maxRunupCalc, strategy.closedtrades.max_runup(tradeIdx))
    maxRunupCalc

plot(maxTradeRunUpCalc(), "Max Trade Run-Up")
Example

Detailed Walkthrough

  1. Strategy Definition: We initiate our strategy using strategy(), naming it “Max Run-Up Analysis”.
  2. Trading Conditions: The script enters a long position (strategy.entry) every 15 bars and closes the position (strategy.close) every 20 bars.
  3. Maximum Run-Up Calculation: The function maxTradeRunUpCalc() iterates through all closed trades, determining the maximum run-up value using strategy.closedtrades.max_runup(tradeIdx). This is accomplished by comparing the current maximum (maxRunupCalc) with the run-up of each trade, ensuring the highest value is always stored.
  4. Plotting: The maximum run-up value is visualized on the chart with plot(), allowing for easy analysis of profit potential over time.

Key Features and Takeaways

  • Functionality: The strategy.closedtrades.max_runup() function is essential for analyzing the maximum profit potential of trades within a strategy, expressed in the account’s currency.
  • Syntax and Application: Requires a single argument, the trade number, and returns a float series representing the maximum run-up values.
  • Analytical Insight: By assessing the max run-up, traders can understand the efficiency of their exit strategy and potentially uncover opportunities to lock in higher profits.

This tutorial not only explains the syntax and usage of the strategy.closedtrades.max_runup() function but also provides a practical example to illustrate its application in strategy analysis and optimization. By incorporating this function into your Pine Script strategies, you can gain deeper insights into trade performance and enhance your trading decision-making process.

Leave a Comment