Unrealised profit or loss of an open trade as a percentage of its entry value.
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
strategy.opentrades.profit_percent(trade_num: series int) -> series floatArguments
| Name | Type | Default | Description |
|---|---|---|---|
| trade_num | series int | required | Zero-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
//@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
strategy.opentrades.profitstrategy.closedtrades.profit_percentstrategy.opentrades.max_runup_percentstrategy.openprofit_percent
Runs on Sidechair