Sidechair

strategy.opentrades.profit_percent()

Browse the reference

Unrealised profit or loss of an open trade as a percentage of its entry value.

Try it in the editor

strategy.opentrades.profit_percent(trade_num) divides the trade's unrealised profit at the current close by its entry value and multiplies by 100. A long entered at 100 with the close at 103 returns about 3 before commission.

It is the natural input for a trailing rule or a take-profit threshold that should work at any position size.

Syntax

Pine Script
strategy.opentrades.profit_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 open trade's unrealised profit as a percentage of its entry value; negative for a loss. 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 ScriptWarn when a trade is up 5%Open in editor
//@version=6
strategy("Open gain flag", 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")
up5 = strategy.opentrades > 0 and strategy.opentrades.profit_percent(0) >= 5
bgcolor(up5 ? color.new(color.green, 85) : na)

See also

Runs on Sidechair