Sidechair

strategy.opentrades.size()

Browse the reference

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

Try it in the editor

strategy.opentrades.size(trade_num) returns the quantity still held by that open trade. A partial exit reduces the oldest trade first, so its size can be smaller than the quantity it was entered with.

Adding the size of every open trade gives strategy.position_size.

Syntax

Pine Script
strategy.opentrades.size(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 open 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 or is not below strategy.opentrades, so guard every call with the open-trade count.

Examples

Pine ScriptSize of the oldest open tradeOpen in editor
//@version=6
strategy("Oldest trade size", 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()
plot(strategy.opentrades > 0 ? strategy.opentrades.size(0) : na, "Oldest open size", color.blue, style = plot.style_linebr)
plot(strategy.position_size, "Net position", color.gray)
Pine ScriptOpen trades and net positionOpen in editor
//@version=6
strategy("Open size", 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()
plot(strategy.position_size, "Net position", color.blue, style = plot.style_stepline)
plot(strategy.opentrades, "Open trades", color.orange, style = plot.style_stepline)

See also

Runs on Sidechair