Sidechair

strategy.opentrades.profit()

Browse the reference

Unrealised profit or loss of an open trade, valued at the current bar's close.

Try it in the editor

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

Pine Script
strategy.opentrades.profit(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 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

Pine ScriptProfit of the newest open tradeOpen in editor
//@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)
Pine ScriptOpen profit and net profitOpen in editor
//@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

Runs on Sidechair