Sidechair

strategy.closedtrades.profit()

Browse the reference

Net profit or loss of a closed trade, after commission, in the account currency.

Try it in the editor

strategy.closedtrades.profit(trade_num) returns what the closed trade made or lost: (exit price - entry price) times the size for a long, the reverse for a short, minus the commission on both orders. A negative value is a loss.

It is the building block for your own statistics: sum it for gross profit, count the positives for the win rate, or plot the last value to see how each trade finished.

Syntax

Pine Script
strategy.closedtrades.profit(trade_num: series int) -> series float

Arguments

NameTypeDefaultDescription
trade_numseries intrequiredZero-based index of the closed trade. 0 is the first trade the strategy closed and strategy.closedtrades - 1 is the most recent.

Returns

series float: The trade's net profit or loss in the account currency, after commission. na when the trade does not exist.

Remarks

Examples

Pine ScriptWin rate from the trade listOpen in editor
//@version=6
strategy("Win rate", 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")
wins = 0
for i = strategy.closedtrades.first_index to strategy.closedtrades - 1
    if strategy.closedtrades.profit(i) > 0
        wins += 1
rate = strategy.closedtrades > 0 ? 100.0 * wins / strategy.closedtrades : na
plot(rate, "Win rate, %", color.green)
Pine ScriptNet profit curve and trade countOpen in editor
//@version=6
strategy("Profit and count", 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.netprofit, "Net profit", color.green, 2)
plot(strategy.closedtrades, "Closed trades", color.gray, style = plot.style_stepline)

See also

Runs on Sidechair