The size of the current position: positive for long, negative for short, 0 when flat.
strategy.position_size is a series float in contracts, shares or units. A long of 10 shares reads 10, a short of 10 reads -10 and no position reads 0. It is the quickest test of whether the strategy is in a trade.
Type
series float
Remarks
- The value updates when an order fills, which is on the bar after the signal for market orders, so a condition such as
strategy.position_size== 0 on the signal bar still sees the old position. - Compare its sign to the direction of a new entry to decide whether it would reverse.
Example
//@version=6
strategy("Enter only when flat", overlay = true, default_qty_value = 10)
fast = ta.ema(close, 12)
slow = ta.ema(close, 26)
if ta.crossover(fast, slow) and strategy.position_size == 0
strategy.entry("Long", strategy.long)
if ta.crossunder(fast, slow)
strategy.close("Long")
bgcolor(strategy.position_size > 0 ? color.new(color.green, 88) : na)
plot(strategy.position_size, "Position size", display = display.data_window)See also
Runs on Sidechair