Bollinger Bands trading strategy (rules, code and the squeeze)

Bollinger Bands wrap price in a volatility envelope: a 20-period moving average with bands set two standard deviations above and below. The bands breathe — narrowing when the market is calm and flaring when it moves. Two completely different strategies live inside them: a reversion play that fades touches of the outer band, and a breakout play that buys the squeeze when the bands pinch shut. This guide gives you the rules, the code, and the honest catch: a band touch alone is not a signal.

On this page
  1. What the bands measure
  2. Two strategies, one indicator
  3. The code
  4. The squeeze breakout
  5. Why a band touch isn't a signal
  6. Sizing & risk
  7. FAQ

What the bands actually measure

The middle band is a 20-period simple moving average. The outer bands sit two standard deviations away, so they widen when volatility rises and contract when it falls. Statistically, price spends roughly 88–90% of its time inside the bands — but the bands move with price, so “outside the band” never means “guaranteed to come back.” That misunderstanding is where most Bollinger losses come from.

Two strategies live inside one indicator

20 SMA squeeze breakout
Bands pinch shut during low volatility (the squeeze), then flare as a directional move breaks out.

The code

python · bollinger.pyimport statistics as st

def bands(closes, n=20, k=2.0):
    window = closes[-n:]
    mid = sum(window)/n
    sd  = st.pstdev(window)
    return mid - k*sd, mid, mid + k*sd   # lower, mid, upper

lower, mid, upper = bands(closes)
width = (upper - lower) / mid               # normalised band width
if closes[-1] <= lower: signal = 'buy'    # reversion: lower-band tag
elif closes[-1] >= upper: signal = 'sell'  # reversion: upper-band tag

The squeeze breakout play

Track band width and flag a squeeze when it falls to, say, a 100-bar low. Don't predict the direction — wait for price to close outside the band that formed during the squeeze, then trade that way with a stop back inside the bands. It's a volatility-expansion bet, closer in spirit to breakout trading than to reversion.

Why a band touch is not, by itself, a signal

“Walking the band” in a trend

In a strong trend, price hugs and rides the upper (or lower) band for dozens of bars — every “sell the upper band” loses. Always gate band-touch reversion behind a trend filter that confirms the market is ranging, exactly as you would with mean reversion. Bands describe volatility, not direction.

Sizing and risk

Set the stop just beyond the band that triggered you, size the position from that distance with the position calculator, and never add to a losing reversion trade. Then prove the rules out before risking a cent — run a Bollinger reversion against a Bollinger squeeze breakout on the same market in the backtester and compare drawdowns.

Not financial advice. This content is educational. Automated and algorithmic trading carries a real risk of financial loss. Never trade money you cannot afford to lose. Review the SEC investor.gov and CFTC resources before trading.

Frequently asked questions

What is the Bollinger Bands trading strategy?

It plots a 20-period moving average with bands two standard deviations above and below, then trades volatility. The reversion variant fades touches of the outer bands toward the middle; the breakout variant buys a squeeze (narrow bands) when price expands out of it.

What is a Bollinger Band squeeze?

A squeeze is when the bands contract to a multi-week low, signalling unusually low volatility. Low volatility tends to be followed by high volatility, so traders watch for a directional breakout out of the squeeze rather than guessing which way it will go.

Is touching the upper Bollinger Band a sell signal?

Not on its own. In a trend, price can 'walk the band' and ride the upper band for many bars, so blindly selling every touch loses. Band-touch reversion only works in ranging markets and needs a trend filter to avoid fighting strong moves.

How do I backtest a Bollinger Bands strategy?

Code the 20-period SMA and 2-standard-deviation bands, define exact entry/exit and a stop beyond the triggering band, then simulate with fees and no lookahead. Compare the reversion and squeeze-breakout variants on the same market and judge them by drawdown and profit factor, not win rate.

MB

Mustafa Bilgic

Algorithmic trading practitioner · Founder, AITradingBot.us

Mustafa builds and backtests automated trading systems and writes about them without the hype. Every tool on this site is free and runs entirely in your browser.