Bollinger Bands strategy: how it works and how to backtest it
Bollinger Bands are a 20-bar moving average with lines 2 standard deviations above and below it, the standard setting John Bollinger uses. They support two opposite strategies, mean reversion and breakout, and each holds up only in some markets and periods. Test band rules with next-bar fills, costs, out-of-sample data and a range of settings, not one lucky pair.
By Soren Huang · Published · updated · Editorial policy
Contents
- How do Bollinger Bands work as a trading strategy?
- How are Bollinger Bands calculated?
- Mean reversion or breakout: which way do you trade the bands?
- Which Bollinger Bands parameters should you use, and how sensitive are they?
- Which filters and exits turn a band signal into a strategy?
- How do you backtest a Bollinger Bands strategy step by step?
- Do the common rules hold up, and why does no setting work everywhere?
- Frequently asked questions
- References
How do Bollinger Bands work as a trading strategy?
Bollinger Bands draw a moving average with two lines a chosen number of standard deviations above and below it. Traders use them in two opposite ways: betting that price returns to the average (mean reversion) or following closes beyond a band (breakout). No single rule holds everywhere. A fair test uses next-bar fills, costs, unseen data and nearby settings.
Bollinger Bands are a price envelope made of a moving average and two outer lines whose distance from the average grows and shrinks with recent volatility. Think of a highway lane that gets wider when cars swerve more and narrower when traffic is calm. Traders in Taiwan call them 布林通道. Many tutorials teach a set of band "patterns". Each pattern becomes a testable strategy only when you write it down as an exact rule. Bollinger Bands belong to the volatility branch of technical indicators, and a close relative is the Keltner channel.
How are Bollinger Bands calculated?
Bollinger Bands take the average of recent closes, then add and subtract a multiple of how far those closes usually sit from that average. Standard deviation is a measure of how far a set of values typically sits from their own average. When closes are spread out, it is large. When closes cluster together, it is small. The Bollinger Bands indicator page explains how to read the three lines, so this article spends its words on testing them.
The calculation uses two settings: N, the number of bars (the length), and k, the number of standard deviations (the width).
Upper band = middle band + k × standard deviation; lower band = middle band − k × standard deviation. Here, the middle band is the simple average of the last N closes. The standard deviation is measured over the same N closes. The letter k is the width multiplier you choose.
Hypothetical example
The last 20 closes average 100.0, and their standard deviation is 2.0. With k = 2, the upper band is 104.0 and the lower band is 96.0. Now the market calms down and the standard deviation falls to 1.0. The bands move to 102.0 and 98.0, and the average does not change. A close at 103.0 is outside the bands in the calm market and inside them in the busy one.
John Bollinger, who created the bands, uses 20 bars and 2 standard deviations as the standard setting. Two related readings help when you write rules. %b is the position of the close inside the bands: 0 at the lower band, 0.5 at the middle and 1 at the upper band. Bandwidth is the distance between the bands divided by the middle band. It shows whether volatility is high or low compared with the past.
Two details matter in a test. First, standard deviation can be computed in a "population" or a "sample" way, and the bands differ slightly as a result. Use the same method in your research and in your trading platform. Second, under a normal distribution about 95% of values fall within two standard deviations, but price changes have more extreme moves than a normal distribution. The share of closes outside the bands is therefore not a fixed 5%. Count it on your own data.
Mean reversion or breakout: which way do you trade the bands?
The same event, a close outside a band, is a sell signal in one strategy and a buy signal in the other, so you first decide which market behaviour you are betting on. Mean reversion is a strategy that bets a stretched price will move back toward its average. A breakout strategy bets that a move strong enough to leave the bands will continue.
| Mean reversion | Breakout | |
|---|---|---|
| Basic long entry | Close below the lower band, or a close back above it | Close above the upper band |
| Basic exit | Middle band or the opposite band | Middle band, a trailing stop or the opposite signal |
| Market behaviour it needs | Sideways, range-bound periods | Trending periods, often after a quiet phase |
| Where it struggles | Strong trends, when price keeps closing at or outside one band | Ranges, where most breakouts fall back inside |
| Typical trade profile | Many small wins, some large losses | Many small losses, some large wins |
In a strong trend, price can close at or outside the upper band for many bars in a row. Traders call this "walking the band". It is exactly what a breakout trader wants and what a mean-reversion trader fears. A squeeze is a period when bandwidth falls to its lowest level in a long lookback. A squeeze tells you that volatility is unusually low. It does not tell you in which direction the next move will go.
Which Bollinger Bands parameters should you use, and how sensitive are they?
There is no best pair of settings. Length decides how much history the bands remember, width decides how rare a band touch is, and a good setting is one whose neighbours also give reasonable results. Length counts bars, not time. On 5-minute bars, 20 bars cover 100 minutes. On daily bars, 20 bars cover about a month of trading. A rule tuned on one bar size is a different strategy on another.
A wider band, for example k = 2.5 instead of 2, produces fewer signals, and each signal sits further from the average. Fewer trades also mean less evidence, so a high average trade from few trades can be luck. A narrower band trades more often, which makes trading costs weigh more heavily.
To see how sensitive a rule is, test a grid of settings instead of one pair. Then look at the shape of the results, not only at the top value. Sorting the results of an N and k grid in the strategy databank of AlphaPrime (Valynox) shows whether the neighbours of the top pair look similar or only that one pair stands out.
Hypothetical example
You test lengths 10, 15, 20, 25 and 30 against widths 1.5, 1.75, 2.0, 2.25 and 2.5. That is 25 combinations. In case A, the best cell is length 25 with width 2.25, and every neighbouring cell loses money. In case B, a block of nine cells around length 20 and width 2.0 shows similar, moderate results. Case B is more likely to describe a real market behaviour. Case A probably describes the noise in this particular data.
In AlphaPrime, strategy generation lets you set parameter ranges, entry and exit conditions and search constraints. This way the search can try many band settings inside your ranges, not just one pair.
Which filters and exits turn a band signal into a strategy?
A bare band touch is rarely a complete strategy. Most testable versions add one filter that matches the chosen market behaviour and one exit that limits the bad case.
| Addition | What it does | Fits |
|---|---|---|
| Trend filter (slope of a longer moving average) | Mean reversion only when the longer average is flat; breakouts only in its direction | Both |
| Squeeze filter | Breakout only after bandwidth has been at a low level | Breakout |
| Re-entry confirmation | Enter on a close back inside the band, not on the first close outside | Mean reversion |
| Session filter | Skip the first bars after the open, or skip the night session | Both |
| Middle-band exit | Takes profit when price returns to the average | Mean reversion |
| Volatility stop | Exits at a multiple of the average true range (the typical bar range) against the entry | Both |
| Time stop | Exits after a fixed number of bars if the target is not reached | Both |
A mean-reversion rule without a stop holds on during band walks, and those few trades often decide the whole result. Every filter adds at least one parameter, and every parameter is one more chance to fit noise. Add one filter at a time and keep it only if the improvement also shows up on data that was not used to choose it.
How do you backtest a Bollinger Bands strategy step by step?
Write the rule so exactly that two people would code it the same way, then test it with costs, next-bar fills and data it was not tuned on.
- Define the signal precisely. Use the bar close, not an intrabar touch. With bar data you cannot know whether the high or the low came first within a bar.
- Execute on the next bar. The bands of an unfinished bar keep moving. Decide on the completed bar and fill on the next bar's open.
- Set the session. If your bars include the night session, the quieter night bars narrow the bands. This can create extra signals at the day open. Choose day-only or full-session data on purpose.
- Put costs in from the first run. Mean-reversion targets at the middle band can be small on short bars. The guide to trading costs in backtests shows how to compare cost with the average trade.
- Test a grid and split the data. Keep a later period untouched, or use walk-forward analysis to re-tune and test forward in steps.
- Read long and short separately, and year by year. A result carried by one side or one year needs an explanation before you trust it.
Hypothetical example
On a 5-minute chart, the bands are 8 points apart, so a long entry at the lower band has a middle-band target about 4 points away. If commission plus slippage costs 1 point per round trip, costs take a quarter of the target before any losing trade is counted. On daily bars with bands 80 points apart, the same 1 point barely matters.
AlphaPrime's backtesting covers data resolution, fees and slippage, and trading sessions, and always uses next-bar execution. Step 2 is therefore built in, and steps 3 and 4 are settings you choose, not code you write.
Do the common rules hold up, and why does no setting work everywhere?
The common band rules hold up only in some markets, bar sizes and periods. Read every good result as a statement about the data you tested, not about Bollinger Bands in general. A market that ranged for three years can trend for the next two. An index future and a commodity future can behave differently on the same bar size. A rule that fits 5-minute bars may fail on daily bars.
The second reason is the number of variants you try. Research on technical trading rules shows that when many rules are tested on the same data, the best one can look strong by chance alone. Methods such as White's Reality Check adjust for this (Sullivan, Timmermann & White, 1999; White, 2000). The more variants you try, the more the best backtest is inflated (Bailey et al., 2014).
Hypothetical example
25 band settings × 2 directions (mean reversion and breakout) × 3 filter choices = 150 variants. Even if none of them had a real edge, a few of the 150 would show a good backtest just by luck.
| Check | Encouraging | Weak |
|---|---|---|
| Neighbouring settings | Similar results | Only one cell is positive |
| Average trade vs cost | Several times the round-trip cost | Close to the cost |
| Out-of-sample period | Keeps a reasonable share of the in-sample result | Turns flat or negative |
| Years | Results spread across years | One year carries most of the result |
| Long and short | Both sides contribute, or the difference has a reason | One side carries everything without a reason |
Strategy validation in AlphaPrime applies checks such as walk-forward analysis with thresholds you set. Each candidate is judged by the same standard, however many variants you tested.
What this means if you use AlphaPrime: define the band idea, its parameter ranges and one or two filters, and let the search include costs and next-bar fills from the start. Then judge the candidates by plateaus and out-of-sample results, not by the single best backtest.
Frequently asked questions
What are the best Bollinger Bands settings?
No setting is best for every market. The standard setting is 20 bars and 2 standard deviations, and it is a sensible starting point. Test a range of lengths and widths on your own market and bar size, then choose a setting from a region where nearby values give similar results.
Are Bollinger Bands a mean reversion or a breakout indicator?
They are neither by themselves. The bands only show where price sits compared with its recent average and volatility. A close outside a band can be traded as a return to the average or as a continuation, and these two strategies need opposite market behaviour.
What does a Bollinger Band squeeze mean?
A squeeze is a period when the distance between the bands is at its lowest level in a long lookback, so volatility is unusually low. Traders often watch it as a possible start of a larger move. The squeeze does not show the direction, so a breakout rule still needs its own entry and exit.
Do Bollinger Bands work for day trading futures?
They can be tested on intraday futures bars like any other rule. On short bars the bands are narrow, so the distance to a middle-band target is small, and commission and slippage take a large share of it. Include costs, next-bar fills and a clear session choice before you judge the result.
Why do my Bollinger Bands look different on two platforms?
Platforms can differ in how they compute the standard deviation (population or sample), in which price they use, and in which session bars they include. Small differences in the bands can move a signal by one bar. Use the same calculation and the same session in research and in trading.
References
- Bailey, D. H., Borwein, J., López de Prado, M., & Zhu, Q. J. (2014). Pseudo-Mathematics and Financial Charlatanism: The Effects of Backtest Overfitting on Out-of-Sample Performance. Notices of the American Mathematical Society, 61(5), 458–471.
- Sullivan, R., Timmermann, A., & White, H. (1999). Data-Snooping, Technical Trading Rule Performance, and the Bootstrap. The Journal of Finance, 54(5), 1647–1691.
- White, H. (2000). A Reality Check for Data Snooping. Econometrica, 68(5), 1097–1126.

