The indicators post used one number from the Bollinger Bands, the
distance of the price from its average in units of standard deviation, and left the bands
themselves for later. This is later. The bands are the most common way a chart shows “the price
is unusually far from normal”, and the reason they work on a $40 stock and a $400 one alike is
the part worth understanding. The post builds them on SPY’s closes from 2019 to 2023, works one
day by hand, and checks the books’ claims about them against what the data does.
Three lines from twenty closes
Take the last twenty closing prices. Their mean is the middle band, an ordinary 20-day simple
moving average. Their standard deviation (the usual measure of how spread out they are) sets the
other two: the upper band is the mean plus two standard deviations, the lower band is the
mean minus two. Achelis
reports John Bollinger’s own settings: twenty periods, a simple average, two standard deviations,
and a warning that averages under ten periods “do not work very well”. The
StockCharts
page writes the same three lines and adds why the middle band is a simple average: the standard
deviation is computed over the same window, so the two agree about which days count.
U, L, the upper and lower bandsσ, the standard deviation of the last 20 closes
MtUt=201∑i=019Pt−i=Mt+2σt,Lt=Mt−2σt
On 2020-03-16, the Monday SPY fell eleven percent in a day, the twenty closes ending that day
had a mean of 272.88 and a standard deviation of 25.62. Two standard deviations is
51.25, so the upper band sat at 324.13 and the lower band at 221.63. The close
was 218.09: below the lower band by $3.54. That is the same day the indicators post gave a
position of −1.07 bands below the average, and the two statements are one fact in two units.
The bands are the average plus and minus a margin, and the margin is the price’s own recent
spread. Everything the bands do that a fixed percentage envelope cannot comes from that one
choice.
The width is the volatility
Murphy
and Achelis both introduce the bands by contrast with envelopes, two lines a fixed percentage
above and below an average. An envelope five percent wide is five percent wide in a crash and in
a calm. Bollinger Bands “expand and contract based on the last 20 days’ volatility”, in Murphy’s
words, so the same rule, “the price is outside the band”, fires at a sensible distance in both.
Kirkpatrick and Dahlquist add why the fixed version fails in practice: volatility usually falls in a sideways
market and expands when a trend begins, so an envelope wide enough for the quiet stretch gives
false signals exactly when the move starts, and the percentage itself “must be determined
through experiment”.
The width is easy to read off: the upper band minus the lower, divided by the middle, is four
standard deviations as a fraction of the price.
Day
What was happening
Band width (fraction of the price)
2019-07-31
the calmest month of the five years
2.1%
2020-02-19
the high before the crash
6.2%
2020-03-16
the eleven-percent day
37.6%
2020-03-23
the low, and the widest bands of the five years
44.4%
2020-08-31
the recovery, back to its old range
7.0%
The bands were six times wider at the low than at the high five weeks earlier, and back to
normal by the end of summer. Both books read something into the narrow end. Achelis quotes
Bollinger: “sharp price changes tend to occur after the bands tighten, as volatility lessens”.
Murphy says that when the bands have “narrowed too far, that is often a sign that a market may
be about to initiate a new trend”. Kirkpatrick and Dahlquist agree that the tightening “is often
followed by a sharp price move”. None of the three gives a number for “too far”, and the
February 2020 width of 6.2% was not unusual; the squeeze is a pattern the books describe, not a
threshold they test.
Where the close sits in the band
Charts show the bands on the price; a model wants a single number. The usual one is the close’s
position between the two bands, zero at the lower band and one at the upper, called %B. It
is the earlier post’s “position in two-standard-deviation units” shifted and halved: a position
of b gives a %B of (b+1)/2. On the eleven-percent day the position was −1.07 and %B was
−0.035, a little below the lower band. The second panel of the chart shows %B through
2020: it goes below zero twice, in late February and mid March, and spends the summer near one.
Both numbers say the same thing and both are comparable across stocks and across years,
because the standard deviation in the denominator puts every stock on its own scale. A %B of 1.1
on SPY in 2020 and a %B of 1.1 on a small stock in a quiet year are both “a little above the
upper band”, which a dollar gap from the average never is.
What the books say it means, and what SPY did
The books agree on the construction and disagree on the reading. Murphy: prices “are considered
to be overextended on the upside (overbought) when they touch the upper band”, and oversold at
the lower one. Achelis, quoting Bollinger: “when prices move outside the bands, a continuation of
the current trend is implied”. One says a touch is a stretch that should give back; the other
says it is strength that should go on. StockCharts settles it the only safe way: a touch is a
“tag”, not a signal, and “relatively high” is not the same as “sell”.
Kirkpatrick and Dahlquist come down on Achelis’s side, and give reasons. Trading between the
bands, selling at the upper one and buying at the lower one, fails for three reasons they list:
the bands contract in a dull market and leave no room to pay costs, a new trend keeps the price
pressed against one band and throws off false exits, and widening bands mean volatility has
arrived and a position that bet on calm is stopped out. So in their account bands “have become
methods of determining the beginnings of trends”: the empirical entry is in the direction of a
break outside a band that held about 90% of the past prices, and the middle band becomes the
trailing stop. That is a rule, and a tested one, but it is still a rule laid on top of the
indicator. The band says where the
price is. What to do about it is a rule, with a threshold and a position, and the
indicators post is about why those are separate decisions.
One claim can be checked. Murphy writes that two standard deviations “ensures that 95% of the
price data will fall between the two trading bands”. Kirkpatrick and Dahlquist explain why it
does not: 95% is what two standard deviations contain under the bell curve the formula comes
from, and “price action is nonstationary and nonrandom”, so it “does not follow the statistical
properties of the standard deviation calculation precisely”. The bands hold “the majority of
price action” and no particular share of it. StockCharts quotes Bollinger himself at “about
88-89%”. SPY from 2019 to 2023 lands between the two:
Where the close was
Days
Share of 1,239 days
above the upper band
51
4.1%
inside the bands
1,134
91.5%
below the lower band
54
4.4%
So the bands held the close about nine days in ten, and a close outside them happened about once
every two or three weeks, roughly as often on each side. That is a useful fact about the
indicator: “outside the band” is uncommon but not rare, and a rule that fires on every tag fires
twenty times a year.
Sample or population standard deviation
pandas divides by 19 when it computes the standard deviation of twenty numbers (the sample
version); most charting software divides by 20 (the population version). On 2020-03-16 that is
25.62 against 24.97, and the bands move by about $1.30 each. The ratio is always the square root
of 19 over 20, about 0.975, so neither choice changes what a day looks like. The series’ earlier
posts use the pandas default and so does this one.
Traps
People think
Actually
A close above the upper band is a sell
Murphy reads it as overbought; Achelis and Kirkpatrick and Dahlquist read it as a trend starting; the band says “unusually high”, the rule is yours
Sell at the upper band, buy at the lower one
Kirkpatrick and Dahlquist give three reasons that fails: narrow bands in calm markets, the price riding one band in a trend, widening bands stopping out the bet on calm
The bands contain 95% of prices
that is the bell-curve figure; Bollinger says about 88-89%, SPY gave 91.5%
Wide bands mean the price will fall
wide bands mean the last twenty days moved a lot; the direction is not in the width
A narrow band is a signal
the books describe the squeeze; they give no threshold and the post found none
The bands can be read in dollars
the gap to the band depends on the stock’s price and spread; use %B or the position
The code
The snippet reproduces the bands on 2020-03-16 and the share of days inside them. Variables:
Variable
Holds
px
SPY’s adjusted close, one row per trading day
w
the twenty closes ending on 2020-03-16
mid, sd
the 20-day mean and standard deviation, one value per day
pct_b
%B: the close’s position between the bands, 0 at the lower band and 1 at the upper
import pandas as pdurl = "https://raw.githubusercontent.com/ankurtrapasiya/examples/main/data/prices.csv"px = pd.read_csv(url, index_col="date", parse_dates=True)["SPY"]w = px.loc[:"2020-03-16"].iloc[-20:] # the twenty closes ending that daymid, sd = w.mean(), w.std()print(f"middle {mid:.2f} upper {mid + 2 * sd:.2f} lower {mid - 2 * sd:.2f} close {w.iloc[-1]:.2f}")mid, sd = px.rolling(20).mean(), px.rolling(20).std() # the same, for every dayupper, lower = mid + 2 * sd, mid - 2 * sdpct_b = ((px - lower) / (upper - lower)).dropna() # 0 at the lower band, 1 at the upperprint(f"%B on 2020-03-16: {pct_b['2020-03-16']:.3f}")print(f"inside the bands: {100 * pct_b.between(0, 1).mean():.1f}% of {len(pct_b)} days")
It prints a middle band of 272.88, an upper band of 324.13, a lower band of 221.63 and a close of
218.09, then a %B of −0.035 and 91.5% of 1,239 days inside the bands.
The script behind the real numbers
The tables and the chart come from
examples/bollinger-bands/bollinger.py,
which runs on the blog’s five tickers. To run it yourself, clone the examples repo: its README has a three-command Docker setup, or run uv run bollinger-bands/bollinger.py.
Summary
Idea
In one line
On SPY, 2019 to 2023
Three lines from twenty closes
the mean, and the mean plus and minus two standard deviations
272.88, 324.13 and 221.63 on 2020-03-16
The width is the volatility
the bands open when the price moves a lot and close when it calms
6.2% wide before the crash, 44.4% at the low
%B is the close’s place in the band
0 at the lower band, 1 at the upper; comparable across stocks
−0.035 on the eleven-percent day
A tag is not a signal
the books read a touch two ways, and the tested reading is the breakout; the rule is still a separate decision
outside the bands on 8.5% of days
References
Steven B. Achelis, Technical Analysis from A to Z, 2nd edition (McGraw-Hill, 2001). The “Bollinger Bands” entry for the construction, Bollinger’s recommended settings and his four observations, including the tightening and the continuation reading.
John J. Murphy, Technical Analysis of the Financial Markets (New York Institute of Finance, 1999). Chapter 9, “Moving Averages”, for the bands against envelopes, the overbought and oversold reading, the 95% claim and the band width as volatility.
Charles D. Kirkpatrick and Julie R. Dahlquist, Technical Analysis: The Complete Resource for Financial Market Technicians (FT Press, 2007). Chapter 14, “Moving Averages”, for percentage envelopes and why fixed ones fail, the construction of the bands, why 95% does not hold, the three reasons range trading between bands fails, the breakout reading and the middle band as a trailing stop.
StockCharts ChartSchool, Bollinger Bands. The three formulas, why the middle band is a simple average, Bollinger’s 88-89% figure, and tags as distinct from signals.
Romero and Balch, What Hedge Funds Really Do (Business Expert Press, 2014). Chapter 11 for the band as a range around the mean and a strategy built on it.