AverageDeviation — Average Deviation
Mean absolute deviation from the window’s mean. Unlike standard deviation it does not square, so a single outlier moves it much less — the robust dispersion measure, and the one inside CCI.
Signature
AverageDeviation(period, on=<source>)| Argument | Type | Required | Notes |
|---|---|---|---|
period |
int Param or literal | Yes | Window. Default 20, bounds 1–400. |
on= |
symbol, OHLCV field, or indicator | No | Source. Defaults to the trade symbol’s close. |
Formula
$$ \text{AD} = \frac{1}{n}\sum_i |x_i - \bar{x}| $$
Warmup
period bars; partial windows use the bars available.
Reading the value
ad.valuead.value < sd.valueExample
class RobustBands(Strategy): """Bands built from absolute deviation rather than standard deviation.""" trade = Symbol() sizing = FixedCapital()
def execute(self): ad = AverageDeviation(20, on=self.trade) sma = SMA(20, on=self.trade) if self.trade.close < sma.value - 2 * ad.value and self.trade.is_flat: self.buy(self.trade, reason="Two mean-deviations low") if self.trade.close > sma.value and self.trade.is_long: self.sell(self.trade, self.trade.position, reason="Back to the mean")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).
- For a normal distribution AD ≈ 0.8 σ; on fat-tailed data the gap widens, and that gap is itself informative.