Skip to content
Open RaamTrade

Variance

Rolling population variance — the squared dispersion that standard deviation is the root of. Useful where the squared scale is what the maths wants (portfolio variance, risk budgeting) rather than the price-unit scale a chart wants.

Signature

Variance(period, on=<source>)
Argument Type Required Notes
period int Param or literal Yes Window. Default 20, bounds 2–400.
on= symbol, OHLCV field, or indicator No Source. Defaults to the trade symbol’s close.

Formula

$$ \text{Var} = \frac{1}{n}\sum_i (x_i - \bar{x})^2 $$

Population (÷n), matching the platform’s stddev.

Warmup

period bars; 0 until then.

Reading the value

var.value

Example

class VarianceGate(Strategy):
"""Stand aside when dispersion is extreme."""
trade = Symbol()
sizing = FixedCapital()
def execute(self):
var = Variance(20, on=self.trade)
sma = SMA(50, on=self.trade)
if var.value < 25 and self.trade.close > sma.value and self.trade.is_flat:
self.buy(self.trade, reason="Contained dispersion")
if var.value > 100 and self.trade.is_long:
self.sell(self.trade, self.trade.position, reason="Dispersion spike")

Notes

  • Proven by an assert-via-trade run on SPY daily (matched a strategy-side reference to 1e-3 on every bar; zero alarm trades).
  • In squared price units, so thresholds do not transfer between instruments. StdDev ( seeded) is the same information on the price scale.