Net profit or loss of a closed trade, after commission, in the account currency.
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
strategy.closedtrades.profit(trade_num: series int) -> series floatArguments
| Name | Type | Default | Description |
|---|---|---|---|
| trade_num | series int | required | Zero-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
- Returns na when trade_num is negative, is not below
strategy.closedtrades, or points at a trade that has dropped out of the history (seestrategy.closedtrades.first_index). - Divide by the trade's entry value, or use
strategy.closedtrades.profit_percent, when you want results comparable across different sizes.
Examples
//@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)//@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
strategy.closedtrades.profit_percentstrategy.closedtrades.commissionstrategy.opentrades.profitstrategy.netprofitstrategy.closedtrades
Runs on Sidechair