Skip to content
Open RaamTrade

BollingerBands

SMA midline with a ±N-standard-deviation envelope. Multi-value: .upper, .mid, .lower. Draws on the price pane.

Signature

BollingerBands(period, deviations, on=<source>)
Argument Type Required Notes
period int Param or literal Yes Lookback for the SMA and the deviation. Default 20, bounds 2–200.
deviations int Param or literal Yes Band width in standard deviations. Default 2, bounds 1–5.
on= symbol, OHLCV field, or indicator No Source. Defaults to the trade symbol’s close.

Formula

$$ \text{mid} = \text{SMA}N(\text{close}), \qquad \sigma = \sqrt{\frac{1}{N} \sum{i=0}^{N-1} (\text{close}_{t-i} - \text{mid})^2} $$

$$ \text{upper} = \text{mid} + k\sigma, \qquad \text{lower} = \text{mid} - k\sigma $$

where k is deviations. The deviation is population standard deviation (divide-by-N), matching StdDev.

Warmup

period bars. Until the window is full, all three fields read the running average (the bands have zero width).

Reading values

BollingerBands is multi-value — you access fields with attributes, not .value.

bb = BollingerBands(20, 2, on=self.trade)
bb.upper # mid + k·σ
bb.mid # SMA midline
bb.lower # mid − k·σ
if self.trade.close < bb.lower and self.trade.is_flat:
...

No crossed_above on multi-value

bb.crossed_above(...) is RC2_CROSS_MULTIVALUE. Compare price to a specific band field instead.

Example

class BollingerReversion(Strategy):
"""Mean reversion on band touches."""
period = Param(20, min=10, max=50)
deviations = Param(2, min=1, max=3)
trade = Symbol()
sizing = FixedCapital()
def execute(self):
bb = BollingerBands(self.period, self.deviations, on=self.trade)
self.stop_loss(3.0)
if self.trade.close < bb.lower and self.trade.is_flat:
self.buy(self.trade, reason="Close below lower band")
if self.trade.close > bb.mid and self.trade.is_long:
self.sell(self.trade, self.trade.position, reason="Reverted to midline")

Notes

  • Band touches are not signals by themselves — in a strong trend, price walks the band. Pair with a trend filter (Aroon) or an oscillator (RSI).
  • Band width (bb.upper - bb.lower) is a volatility measure — a squeeze (narrow bands) often precedes expansion.
  • Prefer composing SMA + StdDev when you need non-standard band logic — see the Bollinger Bounce example.