Sidechair

strategy.opentrades.max_drawdown_percent()

Browse the reference

The worst unrealised loss an open trade has suffered so far, as a percentage of its entry value.

Try it in the editor

strategy.opentrades.max_drawdown_percent(trade_num) is the trade's worst intrabar loss since entry divided by its entry value, times 100.

A rule such as "exit once the trade has dipped more than 4%" reads this value, because it keeps the dip even after price recovers.

Syntax

Pine Script
strategy.opentrades.max_drawdown_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 deepest unrealised loss 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 ScriptExit after a 4% dipOpen in editor
//@version=6
strategy("Dip exit", 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")
if strategy.opentrades > 0 and math.abs(strategy.opentrades.max_drawdown_percent(0)) > 4
    strategy.close("Long", comment = "dip exit")

See also

Runs on Sidechair