The strategy.opentrades.max_runup()
function is a powerful tool in Pine Script, used within trading strategies to analyze the maximum potential profit (runup) that an open trade could achieve during its duration.
Syntax
strategy.opentrades.max_runup(trade_num) → series float
Arguments
trade_num
(series int): This is the identifier for the open trade whose max runup you wish to calculate. Trade numbering starts at zero, meaning the first trade is referenced astrade_num = 0
.
Example
To illustrate the use of strategy.opentrades.max_runup()
, let’s delve into two practical examples, modifying variable names and the strategy for uniqueness while ensuring the essence and educational value are retained.
Example 1: Tracking Max Runup for the Latest Open Trade
In this example, a simple strategy is devised where long trades are entered every 15 bars and exited every 20 bars. The goal is to plot the maximum runup of the most recent open trade.
//@version=5 strategy("Max Runup Example - Recent Trade") // Strategy to enter and exit long trades at different intervals. if bar_index % 15 == 0 strategy.entry("GoLong", strategy.long) if bar_index % 20 == 0 strategy.close("GoLong") // Plot the max runup for the most recent open trade. plot(strategy.opentrades.max_runup(strategy.opentrades - 1), "Max Runup - Latest Trade")
strategy.entry("GoLong", strategy.long)
: Enters a long trade with a custom identifier.plot(...)
: Visualizes the max runup for the latest trade on the chart.
Example 2: Finding the Largest Max Runup Among All Open Trades
In our second scenario, the strategy aims to enter a long position every 30 bars. The function maxTradeRunup()
calculates the highest max runup value from all open trades and plots this value on the chart.
//@version=5 strategy("Max Runup Example - All Trades", pyramiding = 100) // Enter long positions periodically. if bar_index % 30 == 0 strategy.entry("GoLong", strategy.long) // Function to calculate the highest max runup across all open trades. maxTradeRunup() => maxRunupValue = 0.0 for tradeIndex = 0 to strategy.opentrades - 1 maxRunupValue := math.max(maxRunupValue, strategy.opentrades.max_runup(tradeIndex)) maxRunupValue plot(maxTradeRunup(), "Highest Max Runup - All Trades")
- The
for
loop iterates over all open trades, calculating the maximum runup value usingmath.max()
. maxTradeRunup()
: A user-defined function to encapsulate the max runup calculation logic.
Key Features and Takeaways
- Functionality:
strategy.opentrades.max_runup()
enables traders to quantify the highest potential profit for an open trade, providing insights into trade performance. - Syntax and Application: This function takes the trade number as an argument, offering flexibility to analyze specific trades or all open trades within a strategy.
- Practical Usage: By applying this function, traders can fine-tune their strategies based on the potential profit opportunities and the efficiency of their trade execution.
Incorporating the strategy.opentrades.max_runup()
function into your trading strategy script in Pine Script allows for a deeper analysis of trade performance, offering valuable insights into the profitability and effectiveness of your trading decisions.