Sidechair

strategy.opentrades

Browse the reference

The number of trades currently open, counting each entry order that has not been closed as its own trade.

Try it in the editor

strategy.opentrades is a series int. With the default pyramiding of 0 it is 0 or 1. With pyramiding above 0, every strategy.entry that fills adds one trade, so the count can be higher than one in the same direction.

Trades close first in, first out unless close_entries_rule says otherwise, so a partial exit shrinks the oldest trade before touching the next. The valid trade_num values for strategy.opentrades.* are 0 to strategy.opentrades - 1.

Type

series int

Remarks

  • The value is 0 while the strategy is flat, never na.
  • It counts entries, not position size. strategy.position_size gives the net quantity across all open trades, positive long and negative short.

Examples

Pine ScriptOpen positions against the net sizeOpen in editor
//@version=6
strategy("Open trades", 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, "Open trades", color.orange, 2, plot.style_stepline)
plot(strategy.position_size, "Net position", color.gray)
Pine ScriptAverage entry of every open tradeOpen in editor
//@version=6
strategy("Open entries", overlay = true, 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()
sum = 0.0
for i = 0 to strategy.opentrades - 1
    sum += strategy.opentrades.entry_price(i)
avg = strategy.opentrades > 0 ? sum / strategy.opentrades : na
plot(avg, "Mean entry", color.orange, 2, plot.style_linebr)

See also

Runs on Sidechair