Sidechair

strategy.closedtrades.max_runup()

Browse the reference

The best unrealised profit a closed trade reached while it was open, in the account currency.

Try it in the editor

strategy.closedtrades.max_runup(trade_num) is the peak profit the trade showed at any time between entry and exit, measured against the entry price and using each bar's high and low rather than only closes.

Comparing it with the realised profit tells you how much a trade gave back before it closed, which is a direct way to judge your exit rules.

Syntax

Pine Script
strategy.closedtrades.max_runup(trade_num: series int) -> series float

Arguments

NameTypeDefaultDescription
trade_numseries intrequiredZero-based index of the closed trade. 0 is the first trade the strategy closed and strategy.closedtrades - 1 is the most recent.

Returns

series float: The largest unrealised gain the trade showed while open, in the account currency. na when the trade does not exist.

Remarks

  • Returns na when trade_num is negative, is not below strategy.closedtrades, or points at a trade that has dropped out of the history (see strategy.closedtrades.first_index).
  • The run-up is never below 0 for a trade that closed at a profit.

Example

Pine ScriptProfit given backOpen in editor
//@version=6
strategy("Given back", overlay = false)
fast = ta.sma(close, 10)
slow = ta.sma(close, 30)
if ta.crossover(fast, slow)
    strategy.entry("Long", strategy.long)
if ta.crossunder(fast, slow)
    strategy.close("Long")
n = strategy.closedtrades - 1
gave = n >= 0 ? strategy.closedtrades.max_runup(n) - strategy.closedtrades.profit(n) : na
plot(gave, "Peak minus final profit", color.orange, style = plot.style_columns)

See also

Runs on Sidechair