Sidechair

strategy.closedtrades.size()

Browse the reference

The quantity of a closed trade; positive for a long trade, negative for a short trade.

Try it in the editor

strategy.closedtrades.size(trade_num) returns the number of shares, contracts or units the trade held. The sign gives the direction: a long of 10 returns 10 and a short of 10 returns -10.

When one exit closes only part of an entry, the closed part becomes its own trade with the quantity that was closed, and the rest stays open.

Syntax

Pine Script
strategy.closedtrades.size(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 quantity: positive for a long, negative for a short. 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 (see strategy.closedtrades.first_index).
  • Use math.abs(strategy.closedtrades.size(i)) when only the magnitude matters, and math.sign to read the direction.

Examples

Pine ScriptLong and short trade countsOpen in editor
//@version=6
strategy("Trade directions", 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")
longs = 0
shorts = 0
for i = strategy.closedtrades.first_index to strategy.closedtrades - 1
    if strategy.closedtrades.size(i) > 0
        longs += 1
    else
        shorts += 1
plot(longs, "Long trades", color.green)
plot(shorts, "Short trades", color.red)
Pine ScriptNet position next to the trade countOpen in editor
//@version=6
strategy("Position size", 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.position_size, "Position", color.blue, style = plot.style_stepline)
plot(strategy.closedtrades, "Closed", color.gray, style = plot.style_stepline)

See also

Runs on Sidechair