
Volatility indicator consisting of a middle band (SMA) and two outer bands (standard deviations above/below). Bands expand during high volatility and contract during low volatility.
Volatility indicator consisting of a middle band (SMA) and two outer bands (standard deviations above/below). Bands expand during high volatility and contract during low volatility.
Bollinger Bands excel at breakout detection. When bands contract (low volatility "squeeze"), a significant price move is imminent. Breakout strategies enter when price closes above the upper band (bullish breakout) or below the lower band (bearish breakout), riding the volatility expansion.
Learn more about Breakout Trading Strategies →import backtrader as bt
class BollingerBreakout(bt.Strategy):
params = (('period', 20), ('devfactor', 2.0))
def __init__(self):
self.bb = bt.indicators.BollingerBands(self.data.close,
period=self.p.period,
devfactor=self.p.devfactor)
def next(self):
# Price breaks above upper band = bullish breakout
if self.data.close[0] > self.bb.lines.top[0] and self.data.close[-1] <= self.bb.lines.top[-1]:
self.buy()
# Price breaks below lower band = bearish breakout
elif self.data.close[0] < self.bb.lines.bot[0] and self.data.close[-1] >= self.bb.lines.bot[-1]:
self.sell()| Parameter | Default | Description |
|---|---|---|
| period | 20 | SMA period for middle band |
| devfactor | 2 | Standard deviation multiplier for outer bands |