Sidechair

strategy.opentrades.max_runup_percent()

Browse the reference

The best unrealised profit an open trade has reached so far, as a percentage of its entry value.

Try it in the editor

strategy.opentrades.max_runup_percent(trade_num) is the peak profit since entry divided by the trade's entry value, times 100.

Use it for size-independent rules, such as moving a stop to breakeven once the peak gain passes a set percentage.

Syntax

Pine Script
strategy.opentrades.max_runup_percent(trade_num: series int) -> series float

Arguments

NameTypeDefaultDescription
trade_numseries intrequiredZero-based index of the open trade. 0 is the oldest open entry, the one a FIFO exit closes first, and strategy.opentrades - 1 is the newest.

Returns

series float: The best unrealised gain so far as a percentage of the trade's entry value. na when the trade does not exist.

Remarks

  • Returns na when trade_num is negative or is not below strategy.opentrades, so guard every call with the open-trade count.

Example

Pine ScriptBreakeven after a 3% peakOpen in editor
//@version=6
strategy("Breakeven rule", overlay = true)
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")
armed = strategy.opentrades > 0 and strategy.opentrades.max_runup_percent(0) >= 3
be = armed ? strategy.opentrades.entry_price(0) : na
plot(be, "Breakeven stop", color.blue, style = plot.style_linebr)

See also

Runs on Sidechair