Sidechair

strategy.closedtrades

Browse the reference

The number of trades the strategy has closed so far, counting from the start of the chart.

Try it in the editor

strategy.closedtrades is a series int that grows by one each time a trade closes. It counts trades, not orders: an exit that closes three open entries at once adds three.

Use it as the upper bound when you walk the trade list. The valid trade_num values for the strategy.closedtrades.* functions run from 0 to strategy.closedtrades - 1.

Type

series int

Remarks

  • The value is 0 before the first trade closes, never na.
  • Standard Pine keeps the most recent 9000 closed trades. The count keeps rising past that, but trades that dropped out of the list return na from the accessor functions; start loops at strategy.closedtrades.first_index.
  • A trade still open on the last bar is not counted here. It appears in strategy.opentrades.

Examples

Pine ScriptClosed-trade counterOpen in editor
//@version=6
strategy("Closed trades", 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.closedtrades, "Closed trades", color.teal, 2, plot.style_stepline)
plot(strategy.opentrades, "Open trades", color.orange, 1, plot.style_stepline)
Pine ScriptProfit of the last closed tradeOpen in editor
//@version=6
strategy("Last closed trade", 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")
last = strategy.closedtrades - 1
plot(last >= 0 ? strategy.closedtrades.profit(last) : na, "Last trade profit", color.blue, style = plot.style_columns)

See also

Runs on Sidechair