Bollinger Bands
A moving average with an upper and a lower band at plus and minus k standard deviations of the same window.
The middle line measures the centre and the band width measures dispersion around it, so the picture is trend and volatility in one. Conventional readings: band width expanding or contracting, and price reaching a band, which says how far the move is from its own recent mean and nothing about what happens next. The two conventions agree here to sub-resolution — the same population deviation, reached by different arithmetic.
01The study, live
Computed by Bentrix’s own code over the same synthetic bars on every page — an illustration of how the study behaves, not a market.
What this figure shows
The figure is computed over 300 sample bars. Bollinger Bands has no value on the first 19 — the bars it needs before it can answer — and its first value stands on bar 20. Counting the conditions the study declares on these bars: price crosses above the upper band 11 times; price crosses below the lower band 8 times; price comes back inside the bands 19 times; price crosses above the middle line 11 times; price crosses below the middle line 11 times. Press a condition under the figure to see each place marked.
02Its parts, named
03What you see
Three lines: a moving average in the middle, and a band above and below it at a distance of two standard deviations. The bands widen when prices spread out and narrow when they bunch together.
04Where it comes from
John Bollinger developed the bands in the early 1980s and described them fully in Bollinger on Bollinger Bands (2001). His idea was that a band's width should come from the market's own recent variability, not be fixed in points or percent.
05The formula
Midₜ = SMA(P, N)ₜ, σₜ = √(1/N Σ[i=0..N−1] (P_(t−i) − Midₜ)²)
Upperₜ = Midₜ + k σₜ, Lowerₜ = Midₜ − k σₜ
with and by default.
06A worked example
Take the last five closes of the sample, 22, 24, 25, 24, 26, a period of 5 and 2 deviations.
The middle band is their average: 24.20. Their distances from it are -2.2, -0.2, 0.8, -0.2, 1.8; the average of the squares is 1.76, and its square root — the standard deviation — is 1.33.
- upper band = 24.20 + 2 × 1.33 = 26.85
- lower band = 24.20 − 2 × 1.33 = 21.55
The newest close, 26, is inside the upper band by 0.85. Note what a new extreme close does: it raises the average and the deviation, so the band moves away from the very close that approaches it.
07Built in Assay
The same study written in Assay, Bentrix’s script language, and drawn by Bentrix itself as the script’s graph: each block is one step of the formula. On the ladder the steps stand in the script’s order, one a row; on the board each wire carries a value from the block that gives it to the block that reads it, left to right. The script computes exactly what the study computes — Bentrix checks the two against each other, candle by candle.
//@version=6 indicator("Bollinger Bands", overlay = true) length = input.int(20, "Length") mult = input.float(2.0, "Deviations") middle = ta.sma(close, length) width = mult * ta.stdev(close, length) plot(middle, "Middle") plot(middle + width, "Upper") plot(middle - width, "Lower")
Paste it into the Assay IDE to put it on a chart, or to change it. Looking at the graph is free in every package; editing it as blocks is Plus.
08Readings — what traders look for
The squeeze. When the bands are narrower than they have been for a long time, traders read it as an unusually quiet market — and watch for the expansion that ends every quiet spell, in either direction. Bandwidth measures it.
Walking the band. In a strong move, closes keep touching the upper (or lower) band. Traders read repeated touches as the move's strength, not as an extreme to fade.
Tags in a range. In a sideways market, a touch of one band followed by a return inside is read as price having reached the edge of its recent spread.
Relative position. %B states where price sits between the bands as a number.
How a touch of a band fails. Bollinger's own first rule is that a tag of a band is not a signal in itself. In a strong trend price presses against one band bar after bar; a touch of the band is then the normal state of the trend, not its limit.
How "about 95 % inside" fails. Two standard deviations hold about 95 % of a normal distribution. Prices are not normally distributed and the window is short; in practice noticeably fewer closes stay inside, and they leave in clusters.
09In different markets and timeframes
Bollinger Bands adapt to volatility, so the same settings serve quiet and wild markets. In a range price travels from band to band; in a trend it walks one band and the other falls away. The bands narrow before many large moves and say nothing of their direction. On intraday charts the bands' width follows the time of day.
10With other studies
Bollinger reads them with an indicator from another family — volume (the Money Flow Index, Intraday Intensity) or momentum (the RSI) — to judge a tag of a band; BandWidth and %b are the bands restated as lines; Keltner Channels inside or outside them is the Squeeze.
11What it does not tell you
The bands describe the spread of the last closes. A touch of the upper band says price is high relative to that spread — not that it must come back, and not that it must go on. Bollinger himself writes that a tag of a band is not a signal by itself.
12Inputs and defaults
- 1Deviations — 2How many standard deviations the bands stand from the middle. Larger: wider bands that price reaches less often.
- 2Period — 20The window of both the average and the deviation. Bollinger's default is 20; he suggests changing the deviations with it — about 1.9 for 10 bars, 2.1 for 50.
- 3Shift — 0Draws every line that many candles to the right (a negative number: to the left). The values are not changed, only where they stand.
- 4Deviations below — 2The deviations of the lower line, where they differ from those of the upper one. Left alone, both use the same number.
- 5Average — The study's ownThe kind of moving average inside the study. Left at the study's own, it is the one the study's calculation uses by itself.
- 6Applied price — CloseAny of the seven prices, and several at once, each its own line with its own look.
- 7Timeframe — ChartThe chart's own by default; any higher one, read on the last closed candle. A finer one is never offered.
- 8Wait for the candle to close — OnThe value of a higher timeframe is taken only from a candle that has finished.
What each input changes
- Applied priceWhich price of the bar the bands are computed from.
- ShiftDraws all the lines that many candles to the right, or to the left with a negative number. The values do not change, only the candle each one stands on.
- Deviations belowThe deviations of the lower band alone. Left as it is, the lower band uses the same number as the upper one; set apart, the two bands stand at different distances from the middle line.
- AverageThe kind of average the middle line is. Left at the study's own it is the simple average Bollinger used; an exponential or a weighted one turns sooner, and the bands move with it.
- In use20 and 2 are Bollinger's. He suggests 10 with 1.9 and 50 with 2.1; fewer than 10 bars estimates the deviation poorly.
13Limits and common misreadings
Not 95 %. Two standard deviations would contain about 95 % of a normal distribution's values; prices are not normally distributed, and closes leave the bands much more often in trends.
The middle is an SMA, with all of the SMA's lag.
14Conditions an alert or a strategy can watch
an event happens on one bar a state holds on every bar while it lasts
These are the conditions the study itself declares — the same rules the alert editor and the strategy engine read, and the ones the figure's chips show.
15Practise it in the laboratory
Find the narrowest bands of a month, mark that bar, and play forward: in which direction did the expansion go, and how far? Repeat on ten squeezes and count.
Open the laboratory17In Bentrix
- From the menu: a chart's Indicators button › Trend & channels › Bollinger Bands, or type Bollinger Bands in its search.
- Its settings: the settings card opens when it is added; its legend's name opens it again.
18From an agent or a program
Two tools reach it. study_values computes it over a workbench’s candles up to the clock — no chart needed — and study_attach puts it on a chart, where it is drawn and saved with the chart ({"chart": "<chart uri>", "kind": "bollinger"}).
The study, as a chart files it
This is a new Bollinger Bands’s entry — its kind, its calculation, the price it reads and its settings. Change a setting here to read it with other inputs.
{
"applied": "close",
"convention": "mt5",
"kind": "bollinger",
"settings": {
"dev": 2.0,
"period": 20,
"shape": "period_dev"
}
}An agent
An agent calls the MCP tool with it:
{
"name": "study_values",
"arguments": {
"bench": "bentrix://lab/\u2026/bench/\u2026",
"symbol": "EURUSD",
"timeframe": "H1",
"limit": 5,
"study": {
"applied": "close",
"convention": "mt5",
"kind": "bollinger",
"settings": {
"dev": 2.0,
"period": 20,
"shape": "period_dev"
}
}
}
}A program
A program does the same in its language — bench and market as in the built-in indicators guide:
Python
values = client.study.values(bench, market, "H1", {"applied": "close", "convention": "mt5", "kind": "bollinger", "settings": {"dev": 2.0, "period": 20, "shape": "period_dev"}}, limit=5)
print("times:", values["times"])
for line in values["lines"]:
print("line:", line["line"])
print(" newest values:", line["values"])JavaScript
const values = await client.study.values({ bench, symbol: market, timeframe: "H1", study: { "applied": "close", "convention": "mt5", "kind": "bollinger", "settings": { "dev": 2.0, "period": 20, "shape": "period_dev" } }, limit: 5 });
console.log("times:", values.times);
for (const line of values.lines) {
console.log("line:", line.line);
console.log(" newest values:", line.values);
}C#
var values = await client.Study.Values(bench, market, "H1", new Dictionary<string, object?> { ["applied"] = "close", ["convention"] = "mt5", ["kind"] = "bollinger", ["settings"] = new Dictionary<string, object?> { ["dev"] = 2.0, ["period"] = 20, ["shape"] = "period_dev" } }, limit: 5);
Console.WriteLine($"times: {values!["times"]!}");
foreach (var line in values!["lines"]!.AsArray())
{
Console.WriteLine($"line: {line!["line"]!}");
Console.WriteLine($" newest values: {line!["values"]!}");
}Go
values := must(client.Study.Values(ctx, mcp.StudyValuesArgs{Bench: bench, Symbol: market, Timeframe: "H1", Study: map[string]any{"applied": "close", "convention": "mt5", "kind": "bollinger", "settings": map[string]any{"dev": 2.0, "period": 20, "shape": "period_dev"}}, Limit: mcp.Ptr[int64](5)}))
fmt.Println("times:", at(values, "times"))
for _, line := range at(values, "lines").([]any) {
fmt.Println("line:", at(line, "line"))
fmt.Println(" newest values:", at(line, "values"))
}Java
Object values = client.study.values(new Bentrix.StudyApi.ValuesArgs(bench, market, "H1", obj("applied", "close", "convention", "mt5", "kind", "bollinger", "settings", obj("dev", 2.0, "period", 20, "shape", "period_dev"))).limit(5L));
System.out.println("times:" + " " + at(values, "times"));
for (Object line : (List<Object>) at(values, "lines")) {
System.out.println("line:" + " " + at(line, "line"));
System.out.println(" newest values:" + " " + at(line, "values"));
}C++
auto values = client.study.values_(StudyApi::ValuesArgs(bench, market, "H1", Json::Object{{"applied", "close"}, {"convention", "mt5"}, {"kind", "bollinger"}, {"settings", Json::Object{{"dev", 2.0}, {"period", 20}, {"shape", "period_dev"}}}}).limit(5));
std::cout << "times:" << " " << values["times"].dump() << std::endl;
for (const Json &line : values["lines"].arr()) {
std::cout << "line:" << " " << line["line"].dump() << std::endl;
std::cout << " newest values:" << " " << line["values"].dump() << std::endl;
}Julia
values = client.study.values(bench, market, "H1", Dict{String,Any}("applied" => "close", "convention" => "mt5", "kind" => "bollinger", "settings" => Dict{String,Any}("dev" => 2.0, "period" => 20, "shape" => "period_dev")); limit = 5)
println("times:", " ", values["times"])
for line in values["lines"]
println("line:", " ", line["line"])
println(" newest values:", " ", line["values"])
endR
values <- client$study$values(bench, market, "H1", list(applied = "close", convention = "mt5", kind = "bollinger", settings = list(dev = 2.0, period = 20, shape = "period_dev")), limit = 5)
cat("times:", format(values$times), "\n")
for (line in values$lines) {
cat("line:", format(line$line), "\n")
cat(" newest values:", format(line$values), "\n")
}Rust
let values = client.study().values(mcp::StudyValuesArgs::new(bench.clone(), market.clone(), "H1", serde_json::json!({"applied": "close", "convention": "mt5", "kind": "bollinger", "settings": {"dev": 2.0, "period": 20, "shape": "period_dev"}})).limit(5))?;
println!("times: {}", values["times"]);
for line in values["lines"].as_array().into_iter().flatten() {
println!("line: {}", line["line"]);
println!(" newest values: {}", line["values"]);
}Reading the answer
times— the candles’ open times, in milliseconds since 1970 (UTC), oldest first.lines— one entry per line, in this order, each with itslinename and itsvalues;values[i]belongs to the candle attimes[i].- The last value is the candle at the clock’s, which may still be forming: it changes until that candle closes, as on the chart. Nothing after the clock is ever computed.
nullis a candle with no value yet — the warm-up, about 1 × the period at the start of the history.
line | What it is |
|---|---|
BandMid | middle band |
BandEdge | band |
BandEdge | band |
In a strategy
Filed under a key of your choice in a strategy document’s studies — here bollinger: {"bollinger": {"applied": "close", "convention": "mt5", "kind": "bollinger", "settings": {"dev": 2.0, "period": 20, "shape": "period_dev"}}} — its rules can name:
| Write | What it is |
|---|---|
bollinger.band_mid | this line, as a value in a compare |
{"cond": "bollinger:band.cross_above_upper"} | a condition it declares itself |
{"cond": "bollinger:band.cross_below_lower"} | a condition it declares itself |
{"cond": "bollinger:band.back_inside"} | a condition it declares itself |
{"cond": "bollinger:band.mid_cross_up"} | a condition it declares itself |
{"cond": "bollinger:band.mid_cross_down"} | a condition it declares itself |
Its lines named band_edge are more than one line of that name, so a strategy reaches them through the conditions above rather than by name.
19Sources
- Bollinger, John. Bollinger on Bollinger Bands. McGraw-Hill, 2001.
Bentrix 0.1.382Every number, name and default on these pages is read from the application itself.What is comingWhat changed