Quant from Scratch by QuantWright

Testing Popular Ideas · Lesson 5

Volatility bands (Bollinger Bands®)

An average, a width and two opposite readings: the 72 versions inside one idea.

Know the basics? Read the lesson notes

An average and a typical distance

The bands are built from two numbers. One is the average of the last few closes (a close is a day's last price): lesson E2's moving average. The other is their standard deviation, their typical distance from that average: square each distance, average the squares, take the square root (lesson A8).

Here are five made-up closes, few enough to work by hand. Press Next step.

Five closes (made up)

Dotted: their average. Lines: each close's distance from it.

Average–
Squared distances–
Standard deviation–

The average is 22 and the standard deviation 4.

We divided the 80 by 5, the number of closes, as this lesson does throughout. Some tools divide by one less (lesson A8 has both): √(80 ÷ 4) ≈ 4.47. Sources differ, so check which a tool uses.

Two bands around the average

Now draw one line 2 standard deviations above the average and one 2 below: 22 ± 2 × 4 = 30 and 14. The average is the middle band, and the two lines are the upper band and the lower band. The 2, the number of standard deviations, is the width. Try another width.

Five closes and their bands (made up)

Dotted: the middle band. Orange: the upper and lower band.

Width
Bands–

A larger width moves both bands out; the middle band stays.

The customary values are 20 closes and a width of 2, written 20/2: a custom, not the result of a calculation.

The bands on a market

Now the last 20 closes at a time, on one year of a simulated market: prices made up by a computer, not a real market. It rises slowly on average; beyond that, past prices say nothing about which way the next move goes (we built it so).

Each day the middle band is the average of the last 20 closes, today's included, and the bands sit 2 standard deviations of those closes above and below it. Volatility is how much prices move: the bands spread after big moves and narrow after small ones. John Bollinger devised them (sources below the lesson).

A close above the upper band or below the lower band is a close outside. Lesson A5's bell-curve rule says about 95 values in 100 land within 2 standard deviations of the average, leaving about 5 outside. Guess how many of this year's 260 closes were outside, then count them.

Closes and their bands, 2017 (simulated)

Dark: the close. Dotted: the middle band. Orange: the bands; orange dots: closes outside.

Closes averaged
Width
Closes outside?

33 of the 260: about 13 in 100, not 5. The dots come in runs: 27 of the 33 sit in runs of two or more days. When prices move fast in one direction, the average trails behind them, so several closes in a row sit beyond a band. Lesson A5's rule is for values drawn independently around a fixed, known average; here neither holds. Now try another number of closes and another width.

Other settings give other counts: how often a close is outside depends on the data and the settings. It is a count, not a law.

On 1,000 more simulated markets built the same way, the share at 20/2 averaged 12.6 in 100.

For the preciseAcross the 1,000 simulated markets the share ran between 10.1 and 14.9 in 100. Each is ten years long; the first nine months only start the averages, as in lessons E2 to E4, and every count uses the rest. In this simulated 2017 the 33 closes outside formed 14 runs, the longest 6 days. Over the five days ending on a close outside the price had moved 4.0% on average, sign dropped, against 1.4% on the other days. Across the 1,000 simulated markets, 50.8 in 100 closes outside came directly after another one. In 2017 the counts at 10/2, 50/2, 20/1.5 and 20/2.5 were 18, 53, 85 and 10; across all nine settings they ran from 2 (at 10/2.5) to 92 (at 50/1.5). With only 5 closes, dividing by 5, no close can ever be more than 2 standard deviations from its own average, so 5-close bands of width 2 never have a close outside. In general the limit is √(n − 1) standard deviations for n closes: 3 for 10 closes, 4.36 for 20. On the bell curve's exact figure, 4.6 values in 100 land more than 2 standard deviations out.

One name, opposite rules

"Trade the bands" is not a rule yet. Two common readings are opposite:

"At the band" is a choice too. A touch: the day's lowest price reached the lower band (for breakout, the highest reached the upper band). Or a close outside. On touches, one of John Bollinger's published rules says: "Tags of the bands are just that, tags not signals."

A rule decides at a day's close, since the bands need that close, and trades at the next day's first price (lesson E2). Between a buy and its sale it is in the market; the rest of the time it holds nothing. Show all four on the same year, at 20/2.

Two readings, two entries, 2017, at 20/2 (simulated)

Shaded: days the chosen rule is in the market. Dots: the days that made it buy.

Reading
Entry
Buys that year–
Days in the market–

With a touch, mean reversion bought 5 times and breakout 7; with a close outside, 2 and 6. One name, four different sets of trades.

One rule against just holding

Start with one exact rule: mean reversion, entered on a touch and sold above the middle band, at 20/2. Call it the baseline version. It is this lesson's starting point, not a rule the bands' originator gives (his quoted rule says a touch is not a signal); only the 20 and the 2 are customary.

It runs on six years of the simulated market (late 2015 to the end of 2021) against just holding: buying on the first day and never selling (lesson E1). Each buy and each sale costs 0.1% of the price (lesson A16, Costs); holding pays that once. Results are in % a year: the steady yearly growth that gives the same total.

Holding made 6.3% a year. After costs, where did the baseline version end?

The baseline version and holding, 2015–2021, after costs (simulated)

Accounts starting at 100. Orange: the baseline version. Dotted: holding.

Your guess: it ends
Baseline version, after costs?
Holding?

After costs it made 3.7% a year in this simulated market: a gain, and behind holding's 6.3%.

One market is one draw

Now run the baseline version on each of the 1,000 other simulated markets.

A result's lead is its % a year minus holding's, in points (a point is 1% a year). On the market above the lead was 3.7 − 6.3 = −2.6 points a year.

The baseline version's lead over holding on 1,000 simulated markets, after costs

Bars: markets by size of lead. Orange: ahead of holding.

Ahead of holding?
Average lead?

After costs it beat holding in 130 of the 1,000 simulated markets and trailed it in 870. The average lead was −7.4 points a year.

These markets rise on average, and across them the baseline version was in the market on only 29 days in 100, on average.

These markets were built with nothing to find in past prices, so the counts show what these rules produce when there is nothing to find. They cannot show whether the bands work in any real market.

Is the opposite reading the answer?

In this simulation, buying at the lower band trailed holding. So try the baseline version's opposite: breakout, entered on a touch and sold below the middle band, at 20/2, on the same market, years and costs. Where do you think it ended?

Holding had every day's gain, so gaining what the other missed would still leave it behind. And on many days both rules hold nothing.
On many days both rules hold nothing, and holding is always in the market.
Their results are not mirror images: each is in the market on its own days, and on many days both hold nothing.
After costs, in this simulated market, breakout made 1.1% a year: a lead of 1.1 − 6.3 = −5.2 points. Both readings ended with a gain, and both trailed holding.

On the 1,000 simulated markets, after costs, both of these rules ended with a gain in 387. Both beat holding in 39.

So "it made money" can be true of a rule and its opposite at once. In markets that rise on average, a rule that is in the market part of the time often ends with a gain, whichever band it buys at.

How many versions?

The tempting next move is to try other settings: three lengths (the last 10, 20 or 50 closes), three widths (1.5, 2 or 2.5), either reading, a touch or a close outside. And two sales: at the middle band, as before, or the opposite-band sale, which waits for a close beyond the other band (above the upper band for mean reversion, below the lower one for breakout).

Each different set of choices is a version: the same idea with different settings (lesson E1, How we test an idea). How many versions is that?

That adds the choices (3 + 3 + 2 + 2 + 2). Every choice goes with every other, so they multiply.
That is 3 × 3 × 2 × 2, which leaves one choice out. There are five: length, width, reading, entry and sale.
3 lengths × 3 widths = 9 pairs. 2 readings × 2 entries × 2 sales = 8 rules. 9 × 8 = 72 versions.

All 72, on one market and on 1,000

Here are all 72 versions on the same simulated market and years, 2015 to 2021. Call those the choosing years: a version will be chosen on them. Show them, then turn the costs on.

72 versions: each one's lead over holding, 2015–2021 (simulated)

One bar per version; in each reading, largest before-costs lead first, so with costs on the bars need not descend. Above the line: beat holding. Black: the baseline version. Solid orange: the best. Ticks: before-costs leads.

Costs
Beat holding?
The best?
The baseline version?

Before costs 12 of the 72 beat holding; after costs, 8. The best was a breakout version (touch, middle-band sale, 10/1.5): it led holding by 6.3 points a year before costs and by 3.0 after. The baseline version came 30th of the 72: its place, counting from the largest lead after costs.

A find, or the luck of the best of many (lesson B7, The best of many)? Run all 72 on the 1,000 simulated markets.

Across the 1,000 simulated markets the best version after costs was a mean-reversion one in 478 and a breakout one in the rest: close to a coin flip between two opposite readings.

For the preciseAll after costs, on the choosing years of the 1,000 simulated markets. A breakout version was the best in 522. "Best" is the largest lead, even one below zero: in 224 markets no version beat holding, and on average 11.9 of the 72 did. The baseline version's place averaged 43.2 of 72; a version drawn at random averages 36.5. On the one market shown, the best version made about 30 buys and sells a year, and its costs took 3.3 points a year.

Does the best stay best?

Before any version was run, 2022 to 2024 were set aside: years held out, to be used once (lesson B10, The held-out test).

On each of the 1,000 simulated markets we took the best of the 72 on the choosing years, the pick, and ran it once on the held-out years, after costs. A blind draw, one version taken at random, would be best there in about 14 of 1,000 (1,000 ÷ 72). Guess how often the pick stayed best, then run them.

The pick's held-out place on 1,000 simulated markets, after costs

One bar per place, 1st on the left. Dashed: a blind draw.

Stayed best?

The pick stayed best in 43 of the 1,000 simulated markets, against a blind draw's 14. In the other 957 it was not the best on the held-out years.

Why 43 and not 14? The pick usually had the opposite-band sale, so it was in the market on more days, which helps in markets that rise on average. That is what the averages suggest, not a controlled test.

For the preciseAll after costs, across the 1,000 simulated markets. The pick had the opposite-band sale in 915 markets. On the choosing years the picks were in the market on 61% of days and made 5.3 buys and sells a year, on average; all 72 versions averaged 42% and 9.3. On the held-out years the pick beat holding in 310 markets; the baseline version, with no picking, in 226. The pick's held-out place averaged 29.2 of 72; all versions of its rule averaged 31.9, and a blind draw averages 36.5. In 24 markets two or more versions shared first place on the held-out years; counting those ties, a blind draw is best in 14.3 markets, where 1,000 ÷ 72 gives 13.9. On the one market shown earlier, the best version of the choosing years made 5.5% a year on the held-out years against holding's 10.0%: a lead of −4.5 points, 19th of the 72.
Theory

The three bands

Volatility bands are three lines built from the last n closes: a simple moving average, and a line k standard deviations above and below it. The standard deviation is a moving standard deviation: worked out again each day, like the average. Until n closes exist there are no bands. A touch compares a day's highest or lowest price with bands built from closes, that day's included.

Mt = (Ct−n+1 + … + Ct) ÷ n
st = √( [(Ct−n+1 − Mt)² + … + (Ct − Mt)²] ÷ n )
uppert = Mt + k × st,   lowert = Mt − k × st
Ct
the close on day t; each sum runs over the last n closes, today's included
n
the length: how many closes (20 by custom)
Mt
the middle band: the simple moving average of those n closes
st
their standard deviation, dividing by n in this course (some tools divide by n − 1)
k
the width, in standard deviations (2 by custom)
Worked example: your five closes

n = 5, k = 2. M = (24 + 20 + 16 + 22 + 28) ÷ 5 = 22. Squared distances: 4 + 4 + 36 + 0 + 36 = 80, so s = √(80 ÷ 5) = 4. Upper = 22 + 2 × 4 = 30, lower = 22 − 2 × 4 = 14.

Holds when
  • the closes are in order, one per day, none missing or wrong (lesson B4, Data quality);
  • the length, the width and the divisor are stated.
Breaks when

The bands are read with the bell curve's "95 in 100 inside": in this lesson's simulated markets about 13 closes in 100 were outside at 20/2.

Four new closes: 8, 12, 12 and 8, with n = 4 and k = 1.5, dividing by n. What is the upper band?

M = 40 ÷ 4 = 10. Squared distances: 4 + 4 + 4 + 4 = 16, so s = √(16 ÷ 4) = 2. Upper = 10 + 1.5 × 2 = 13.
M = (8 + 12 + 12 + 8) ÷ 4 = 10. Each close is 2 from it, so the squared distances are 4 + 4 + 4 + 4 = 16 and s = √(16 ÷ 4) = 2. Upper = 10 + 1.5 × 2 = 13.

What this means for your test

  1. Write the exact rule before you run it.

    "The bands" names three lines, not a rule. Say the length, the width and the divisor; the reading; touch or close outside; where it sells; the price each trade gets and the costs.

Practice 1 of 2

A friend's spreadsheet

The situationA friend builds bands of width 2 on the five closes 57, 45, 49, 48 and 51. Their sheet shows a lower band of 41.06. You check by hand, dividing by the number of closes.

What lower band do you get?

Hint: the average; the squared distances, added up; ÷ 5; square root. Lower band = average − 2 × that.

Average: 250 ÷ 5 = 50. Squared distances: 49 + 25 + 1 + 4 + 1 = 80, so the standard deviation is √(80 ÷ 5) = 4. Lower = 50 − 2 × 4 = 42.
Average: 250 ÷ 5 = 50. Distances: +7, −5, −1, −2, +1; squared, 49 + 25 + 1 + 4 + 1 = 80. Standard deviation: √(80 ÷ 5) = 4. Lower = 50 − 2 × 4 = 42.
The sheet is not broken. It divided by 4: √(80 ÷ 4) ≈ 4.47, and 50 − 2 × 4.47 = 41.06. Two tools can draw different bands from the same closes.
Practice 2 of 2

Two videos, one chart

The situationAn invented video shows buying at the lower band ending with a gain on one market from 2015 to 2021. A second shows buying at the upper band ending with a gain on the same market and years.

What follows?

In this lesson's 1,000 simulated markets a rule and its opposite both ended with a gain, after costs, in 387. Both beat holding in 39.
Not necessarily: on a market that rose, a rule and its opposite can both end with a gain. In this lesson's 1,000 simulated markets they did in 387. Ask what just holding made.
On a market that rose, a rule that is in the market part of the time often ends with a gain. In this lesson's simulated markets, built with nothing for a rule to find, a rule and its opposite both did in 387 of 1,000. Ask what just holding made.
✓

Lesson done

The bands are an average of the last closes with a line a set number of standard deviations above and below it. A close lands outside them about 13 times in 100 at 20/2 in these simulated markets, not the bell curve's 5: a count that moves with the data and the settings. Reading, entry, sale, length and width made 72 versions. These markets were built with nothing to find in past prices, so the counts show what these rules produce when there is nothing to find: after costs the baseline version trailed just holding in 870 of 1,000 simulated markets, it and its opposite both ended with a gain in 387, and the best of 72 stayed best on held-out years in 43 (a blind draw: about 14; the extra is not skill: the pick usually had the opposite-band sale, so more days in a market that rises on average, as the averages suggest). They cannot show whether the bands work in any real market.

Key ideas

Middle band
the average of the last n closes (20 by custom): a simple moving average
Upper band, lower band
the middle band plus and minus k standard deviations of the same closes (the width k is 2 by custom; dividing by n in this course, by n − 1 in some tools)
Close outside, touch
a close beyond a band; a day whose highest or lowest price reached one. How often depends on the data and the settings
Mean reversion, breakout
opposite readings of the bands: buy at the lower band, or at the upper one. Each is a rule to test, not a finding
Version
the same idea with different settings; count every one

Formulas

Mt = (Ct−n+1 + … + Ct) ÷ n  ·  st = √( [(Ct−n+1 − Mt)² + … + (Ct − Mt)²] ÷ n )

upper = M + k × s  ·  lower = M − k × s

Lesson notes

Volatility bands are three lines: the middle band, which is the average of the last closes of a price (a close is a day's last price), and an upper and a lower band a set number of standard deviations above and below it. That number is the width. A touch is a day whose lowest price reached the lower band, or whose highest price reached the upper band. "Trade the bands" is not one rule: reading, entry, sale, length and width made 72 versions in this lesson. The lesson's simulated markets were built with nothing to find in past prices, so its counts show what these rules produce when there is nothing to find.

The baseline version, the lesson's choice of starting point, buys after a touch of the lower band and sells on a close above the middle band, with the last 20 closes and a width of 2. Its opposite buys after a touch of the upper band and sells on a close below the middle band. After costs the baseline version trailed just holding (buying on the first day and never selling) in 870 of 1,000 simulated markets. After costs, it and its opposite both ended with a gain in 387 of the 1,000 simulated markets. After costs, the best of the 72 on the choosing years (the years a version is chosen on) stayed best on the held-out years (later years, set aside) in 43 of the 1,000 simulated markets, against about 14 for a version drawn at random; in the other 957 it was not the best. The counts cannot show whether the bands work in any real market; that needs its own test.

Building the bands

A close is a day's last price. Take the last n closes; n is the length. Their average is the middle band (a moving average, as in lesson E2). Their standard deviation is their typical distance from that average: square each close's distance from the average, average the squares, take the square root. The upper band is the middle band plus k standard deviations and the lower band the middle band minus k; k is the width.

Five made-up closes, 24, 20, 16, 22 and 28, with a width of 2. Average: 110 ÷ 5 = 22. Distances: +2, −2, −6, 0, +6. Squared: 4 + 4 + 36 + 0 + 36 = 80. Standard deviation, dividing by 5, the number of closes: √(80 ÷ 5) = √16 = 4. Bands: 22 ± 2 × 4 = 30 and 14. A larger width moves both bands out; the middle band stays.

Some tools divide by one less than the number of closes: 80 ÷ 4 = 20, and √20 ≈ 4.47. Sources differ, so two tools can draw different bands from the same closes. This lesson divides by the number of closes. The customary values are 20 closes and a width of 2, written 20/2; they are a custom, not the result of a calculation.

How often a close is outside

A close outside is a close above the upper band or below the lower band. Volatility is how much prices move; the bands spread after big moves and narrow after small ones. On a bell curve about 95 values in 100 land within 2 standard deviations of the average, so about 5 in 100 land outside (lesson A5). That rule is for values drawn independently around a fixed, known average. Closes are neither: each starts from the one before, and the bands' average and standard deviation are worked out again each day from the last closes, today's included.

The lesson's simulated market (prices made up by a computer) rises slowly on average; beyond that, past prices say nothing about which way the next move goes. In its 2017, 33 of the 260 closes were outside the 20/2 bands: about 13 in 100, not 5. They came in runs: 27 of the 33 sat in runs of two or more days, during fast moves. After one close outside the next often is too, because it starts from there while the bands follow with a lag. Other lengths and widths gave other counts. The lesson built 1,000 more simulated markets the same way, each ten years long: nine months for the averages to start, then the rest of each ten years, which every count uses. Across them the share of closes outside at 20/2 averaged 12.6 in 100, between 10.1 and 14.9. How often a close is outside depends on the data and the settings; it is a count, not a law.

Two opposite readings, two entries

Mean reversion (mean is the textbook word for average) reads a price at the lower band as fallen too far: it buys there and sells when the price closes back above the middle band. Breakout reads a price at the upper band as strong: it buys there and sells when the price closes back below the middle band. "At the band" is a choice too: a touch means the day's lowest price reached the lower band (for breakout, the highest price reached the upper band); a close outside means the close itself ended beyond the band. One of John Bollinger's published rules for the bands says that a touch of a band (a "tag", in the rule's word) is only that, not a signal.

A rule decides at a day's close, since the bands need that close, and trades at the next day's first price. Between a buy and its sale it is in the market; the rest of the time it holds nothing. On the lesson's simulated 2017, at 20/2, mean reversion bought 5 times with a touch and 2 with a close outside; breakout bought 7 times and 6. One name, four sets of trades.

The baseline version, its opposite and just holding

Just holding buys on the first day and never sells. Results are in % a year, the steady yearly growth giving the same total, and a result's lead is its % a year minus holding's, in points (a point is 1% a year). Each buy and each sale costs 0.1% of the price; holding pays that once. The baseline version is mean reversion, entered on a touch and sold on a close above the middle band, at 20/2. It is the lesson's choice of starting point; only the 20 and the 2 are customary. On the lesson's simulated market, from late 2015 to the end of 2021, holding made 6.3% a year and the baseline version 3.7% after costs: a lead of 3.7 − 6.3 = −2.6 points a year. Its opposite (breakout, entered on a touch and sold on a close below the middle band, at 20/2) made 1.1% a year after costs: a lead of −5.2. On that one simulated market both ended with a gain and both trailed holding.

One market is one draw. On the 1,000 other simulated markets, after costs, the baseline version beat holding in 130 and trailed it in 870, with an average lead of −7.4 points a year. Those markets rise on average, and across them it was in the market on only 29 days in 100, on average. It and its opposite both ended with a gain, after costs, in 387 of the 1,000 and both beat holding in 39. In markets that rise on average, as these do, a rule that is in the market part of the time often ends with a gain, whichever band it buys at, which is why the comparison is with just holding.

72 versions

A version is the same idea with different settings. Three lengths (the last 10, 20 or 50 closes) and three widths (1.5, 2 or 2.5) make 3 × 3 = 9 pairs. Each can be traded with either reading, entered on a touch or a close outside, and sold at the middle band or with the opposite-band sale, which waits for a close beyond the other band (above the upper band for mean reversion, below the lower one for breakout): 2 × 2 × 2 = 8 rules. So there are 9 × 8 = 72 versions.

The years 2015 to 2021 are the choosing years: a version is chosen on them. On the lesson's simulated market, 12 of the 72 beat holding there before costs and 8 after. The best, a breakout version (touch, middle-band sale, 10/1.5), led by 6.3 points a year before costs and 3.0 after. The baseline version came 30th; a version's place counts from the largest lead after costs. Across the 1,000 simulated markets the best version after costs on the choosing years (the largest lead, even one below zero) was a mean-reversion one in 478 and a breakout one in the rest: close to a coin flip between two opposite readings.

Does the best stay best?

Before any version was run, 2022 to 2024 were set aside: years held out, to be used once. On each of the 1,000 simulated markets the pick (the best of the 72 on the choosing years, after costs) was run once on the held-out years, after costs. It stayed best there in 43 markets; in the other 957 it was not the best. A blind draw, one version taken at random, would be the best in 1 market of 72: 1,000 ÷ 72, about 14. The extra is not skill. The pick had the opposite-band sale in 915 of the 1,000 simulated markets, so it was in the market on more days, which helps in markets that rise on average. That is what the averages suggest, not a controlled test.

Formula: the three bands

Volatility bands are three lines built from the last n closes: a simple moving average, and a line k standard deviations above and below it. The standard deviation is a moving standard deviation: worked out again each day, like the average. Until n closes exist there are no bands. A touch compares a day's highest or lowest price with bands built from closes, that day's included.

Mt = (Ct−n+1 + … + Ct) ÷ n
st = √( [(Ct−n+1 − Mt)² + … + (Ct − Mt)²] ÷ n )
uppert = Mt + k × st,   lowert = Mt − k × st
Ct
the close on day t; each sum runs over the last n closes, today's included
n
the length: how many closes (20 by custom)
Mt
the middle band: the simple moving average of those n closes
st
their standard deviation, dividing by n in this course (some tools divide by n − 1)
k
the width, in standard deviations (2 by custom)
Worked example: the lesson's five closes

With the five closes above, n = 5 and k = 2: M = 22 and s = 4, so upper = 22 + 2 × 4 = 30 and lower = 22 − 2 × 4 = 14.

Holds whenBreaks when

The bands are read with the bell curve's "95 in 100 inside": in this lesson's simulated markets about 13 closes in 100 were outside at 20/2.

One more worked example

Made-up round numbers

Five closes, 31, 29, 25, 28 and 37, with n = 5 and k = 2. M = 150 ÷ 5 = 30. Distances: +1, −1, −5, −2, +7. Squared: 1 + 1 + 25 + 4 + 49 = 80, so s = √(80 ÷ 5) = 4. Upper = 30 + 2 × 4 = 38, lower = 30 − 8 = 22.

A sixth close of 41 is above 38. But it is not judged against that band: its own bands use the last five closes, 29, 25, 28, 37 and 41. M = 160 ÷ 5 = 32. Distances: −3, −7, −4, +5, +9. Squared: 9 + 49 + 16 + 25 + 81 = 180, so s = √(180 ÷ 5) = √36 = 6 and upper = 32 + 2 × 6 = 44. The close of 41 is inside: it pulled the average up and widened the bands it is measured against.

Common mistakes

Check yourself

  1. Four closes: 30, 36, 36 and 30. Build bands with a length of 4 and a width of 2, dividing by 4. What are the upper and lower band?
    Answer

    Average: 132 ÷ 4 = 33. Each close is 3 from it, so the squared distances add up to 9 + 9 + 9 + 9 = 36 and the standard deviation is √(36 ÷ 4) = 3. Upper = 33 + 2 × 3 = 39, lower = 33 − 6 = 27.

  2. The average of the last 20 closes is 80 and their standard deviation 1.5. With a width of 2, today's lowest price was 76.8 and the close 77.4. Was there a touch of the lower band (the day's lowest price at or below it)? A close outside?
    Answer

    Lower band = 80 − 2 × 1.5 = 77. The lowest price, 76.8, reached it: a touch. The close, 77.4, is above 77: not a close outside.

  3. A test uses lengths of 10 or 20 closes, widths of 1.5, 2 or 2.5, entries on a touch or a close outside, both readings, and one sale. How many versions?
    Answer

    2 × 3 × 2 × 2 × 1 = 24.

  4. In the lesson's simulation, after costs, the best of 72 versions stayed best on held-out years in 43 of 1,000 simulated markets, against about 14 for a blind draw. What does that suggest, and what does it not show?
    Answer

    The pick was best more often than a blind draw would be, but in 957 of the 1,000 it was not the best. It does not show that picking found anything in the prices: the pick had the opposite-band sale in 915 of the 1,000 simulated markets, so it was in the market on more days, which helps in markets that rise on average. That is what the averages suggest, not a controlled test. And it is a simulation; a real market needs its own test.

Try it yourself

The rules, written before the code. Daily bars. Middle band = the average of the last n closes, today's included. Standard deviation = the square root of the average squared distance of those n closes from the middle band (dividing by n). Upper and lower band = middle ± k × standard deviation. Mean reversion enters on a touch (the day's low at or below the lower band) or on a close outside (the close below the lower band), and sells on a close above the middle band, or above the upper band. Breakout enters on a touch (the day's high at or above the upper band) or on a close above the upper band, and sells on a close below the middle band, or below the lower band. At each close: if the condition for selling holds, the rule is out; otherwise, if the entry holds, it is in; otherwise it stays as it was. In the market or in cash, never short. Decide at each close, fill at the next open. 0.1% of the price per buy and per sell, or no costs. Compared with buying at the first test close and holding, which pays 0.1% once.

Run the script. Every number in this lesson comes from one Python script (below). It needs simmarket.py, the course's simulated market, in the same folder. Add a third sale or other lengths and watch the version count and the counts across markets move.

By hand. In a spreadsheet with closes in column B from row 2: the 20-close middle band starts in C21 with =AVERAGE(B2:B21). The standard deviation in D21 is =STDEV.P(B2:B21), which divides by 20; =STDEV.S(B2:B21) divides by 19. The upper band in E21 is =C21+2*D21 and the lower band in F21 is =C21-2*D21. Copy each down.

Written by QuantWright from the published definition of the indicator: the formulas only. No code from any indicator script or charting platform.

The code behind every number (Python, SimMarket seed 20261109)
Loading…

Going further: how far out can a close be?

A close is one of the closes its own bands are built from, and that sets a hard limit. Among n numbers, none can be more than √(n − 1) standard deviations from their average, when the standard deviation divides by n. For 5 closes the limit is √4 = 2, so 5-close bands of width 2 can never have a close outside; a close exactly on a band needs the other four to be equal. For 10 closes the limit is 3 and for 20 it is √19 ≈ 4.36. On the lesson's simulated market the furthest any close got from its middle band was 1.995, 2.854, 3.734 and 4.977 standard deviations at lengths of 5, 10, 20 and 50.

With the opposite-band sale and the close-outside entry, the two readings split the days: after the first signal, exactly one of them is in the market on every day, as with lesson E3's two readings of the RSI. With the middle-band sale both are in cash much of the time. Across the 1,000 simulated markets the eight rules averaged these shares of days in the market on the choosing years, with the middle-band sale then the opposite-band sale: mean reversion on a touch 27% and 53%, on a close outside 20% and 41%; breakout on a touch 38% and 67%, on a close outside 30% and 57%. Their average places among the 72 on the choosing years (after costs), in the same order: 44.3 and 29.5, 44.2 and 33.0, 42.5 and 26.1, 43.0 and 29.4. In these rising markets, where past prices say nothing about which way the next move goes, the rules that stay in the market longer mostly place higher. The 72 versions are also close relatives, not 72 separate tries: versions with neighbouring settings trade on many of the same days (lesson E1's second theory card).

For the preciseThe lesson's market: SimMarket Market 1, seed 20261109, effect=False (no planted pattern), daily bars, the same data and periods as lessons E2 to E4. The 1,000 simulated markets: the same generator with seeds 2026130501 to 2026131500, effect off. First decision at the close of 9 Oct 2015 (the 200th day). Choosing years: that close to 31 Dec 2021 (6.23 calendar years); held-out: to 31 Dec 2024 (3.00 years); each version carries its position across. % a year = exp(sum of daily log returns)1 ÷ years − 1. Fill at the next open: the night before the open earns the old position, the day after it the new one. Costs: a factor (1 − 0.001) per buy or sell; holding pays it once, at its buy. A touch is read at the close, against that day's band. Closes outside the 20/2 bands on the lesson's market: 309 of its 2,408 decision days (178 above, 131 below), 12.8 in 100; days with a touch, 628. In 2017: 33 closes outside (24 above, 9 below) and 76 days with a touch. The 1,000 simulated markets: closes outside 12.6 in 100 on average (none under 10.1), days with a touch 27.2 in 100. The lesson's market: holding 6.32% a year before its cost and 6.30% after (choosing), 10.00% (held-out). Baseline version: 5.08% a year before costs, 3.70% after (82 buys and sells, 13.17 a year), lead −1.24 then −2.60, in the market 25.9% of days, 30th of 72; held-out 1.96% a year, lead −8.0, 45th. Its opposite: 2.52% a year before costs, 1.08% after (88 buys and sells), lead −5.22, in the market 37.9% of days, 44th. Best after costs: breakout, touch, middle-band sale, 10/1.5: +6.29 before costs, +2.98 after, 30.02 buys and sells a year, in the market 44.7% of days; held-out 5.47% a year against holding's 10.00%, lead −4.53, 19th. Of the 36 pairs of a rule and its opposite on the lesson's market, both ended with a gain in 15 and both beat holding in none. On the held-out years 5 of the 72 beat holding. The 1,000 simulated markets, baseline version: beat holding in 191 before costs and 130 after (choosing years), 226 after costs (held-out); ended with a gain after costs in 629; average lead −6.05 before costs, −7.35 after, −7.65 held-out; 13.0 buys and sells a year and 28.7% of days in the market on average; average place 43.2, median 45; best of 72 in 4 markets. Its opposite: beat holding in 128 after costs, a gain in 611, average lead −6.88, average place 42.0. Versions beating holding on the choosing years: 14.7 of 72 on average before costs, 11.9 after; at least one after costs in 776 markets; 396 markets had 8 or more. The pick: beat holding in 776 markets on the choosing years and 310 on the held-out years; average lead +3.01 then −4.04; stayed best in 43 (a blind draw: about 14), top ten in 257; average held-out place 29.2, median 26; its entry was a touch in 655 markets, its length 10 in 492 and its width 2.5 in 580. Of all 72,000 held-out results, 24.5% beat holding.

Honest notes

Where this comes from