Unrealised profit or loss of an open trade, valued at the current bar's close.
strategy.opentrades.profit(trade_num) values the open trade at the current close: (close - entry price) times the size for a long, the reverse for a short, minus the commission already paid on entry.
The sum over all open trades equals strategy.openprofit. On the live bar the value moves with every tick, so a rule built on it can change until the bar closes.
Syntax
strategy.opentrades.profit(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 unrealised profit or loss of the open trade at the current close, 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.
Examples
//@version=6
strategy("Newest trade P&L", overlay = false, pyramiding = 3)
fast = ta.sma(close, 10)
slow = ta.sma(close, 30)
if fast > slow and bar_index % 20 == 0
strategy.entry("Layer " + str.tostring(strategy.opentrades + 1), strategy.long)
if ta.crossunder(fast, slow)
strategy.close_all()
n = strategy.opentrades - 1
plot(n >= 0 ? strategy.opentrades.profit(n) : na, "Newest trade", color.blue, style = plot.style_linebr)//@version=6
strategy("Profit curves", 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")
plot(strategy.openprofit, "Open profit", color.orange)
plot(strategy.netprofit, "Net profit", color.green)See also
strategy.opentrades.profit_percentstrategy.closedtrades.profitstrategy.openprofitstrategy.opentrades
Runs on Sidechair