Skip to content

Technical indicators

Bollinger Bands

Bands around a moving average that stretch when the market is busy and squeeze when it is quiet.

The simple moving average of the closes, with bands deviations standard deviations above and below. The bands are wide when closes are spread out and narrow when they bunch together.

Traders read a narrow “squeeze” as a quiet market that may break out, and closes outside the bands as stretched. In a strong trend the price can ride one band for a long time, so a touch is not a reversal signal on its own.

middle = SMA(periods)
σ = standard deviation of the last periods closes (population)
lower = middle − deviations × σ
upper = middle + deviations × σ

Call: bollinger(periods, deviations)

Setting Chart default
periods: Periods 20
deviations: Deviations 2

What you get: a list of 3, in this order: 1. a number; 2. a number; 3. a number (“bollinger mid”).

Warm-up: with bollinger(20, 2), it asks for 20 bars of history and a run-up of 1, and its first value came on bar 21 of the example data.

Bollinger Bands drawn by its own code on example prices

The prices are made up, hourly, and the same on every page, so the chart shows how the indicator behaves, not how a market behaved.

function onIntervalClose() {
const bands = bollinger(20, 2);
const lower = bands[0];
const upper = bands[1];
if (OPEN_POSITIONS_COUNT === 0 && CLOSE < lower) {
openPosition("BUY", { quantity: 10000, stopLossPips: 25, takeProfit: bands[2] });
}
log("Band width in pips", (upper - lower) / PIP_SIZE);
}