Sidechair

strategy.opentrades.max_drawdown()

Browse the reference

The worst unrealised loss an open trade has suffered so far, in the account currency.

Try it in the editor

strategy.opentrades.max_drawdown(trade_num) is the lowest profit the trade has shown since entry, measured against the entry price and using each bar's high and low. It can only deepen while the trade stays open.

It tells you how far the trade has already gone against you, even if it has since recovered.

Syntax

Pine Script
strategy.opentrades.max_drawdown(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 the open trade has shown so far, in the account currency. 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.
  • Use math.abs when comparing values from different trades, so the result does not depend on the sign.

Example

Pine ScriptHeat on the open tradeOpen in editor
//@version=6
strategy("Open heat", 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")
heat = strategy.opentrades > 0 ? math.abs(strategy.opentrades.max_drawdown(0)) : na
plot(heat, "Worst dip so far", color.red, style = plot.style_linebr)

See also

Runs on Sidechair