All indicators
Volatility / Price pane

Bollinger Bands

A moving average with bands a chosen number of standard deviations above and below it, so the band width follows volatility.

  • BB
  • Built into Spectral
  • Updates every tick
  • bollinger bands
  • bands
  • standard deviation
  • moving average
  • squeeze
  • volatility

By Spectral · v1.0.0

MIT-0: free to use, change and share, no attribution needed. What you build from it is yours.

Already in Spectral: add it from the Indicators toolbox. The file here is the same indicator written as a custom indicator, to change or learn from. Installed, it appears next to the built-in one, marked Custom.

Bollinger Bands on a Spectral chart
Spectral chart with sample market data, default settings.

Overview

Bollinger Bands draw a moving average of price (the basis) with an upper and a lower band a set number of standard deviations away from it. Standard deviation measures how far price has been spreading around its average, so the bands widen when the market becomes volatile and narrow when it quiets down.

With the defaults the basis is the 20-bar simple moving average of the close, and the bands sit two standard deviations of the last 20 closes above and below it.

How to read it

The bands describe what is normal for the recent past. A close near the upper band is high relative to recent volatility, a close near the lower band is low; neither is a buy or sell signal by itself. In strong trends price can ride along one band for a long time, while in ranges it tends to move between them.

A squeeze, where the bands become unusually narrow, marks a quiet market that often comes before a larger move; it does not tell you the direction. A close outside a band followed by a close back inside is a common reversal setup, best confirmed with price structure or another indicator.

Settings and limitations

Length sets the window for both the average and the standard deviation; 20 is the standard. Standard deviations sets the band distance; 2 is the standard, and John Bollinger suggests about 1.9 with a 10-bar length and 2.1 with a 50-bar length, which the presets use. Basis average switches the middle line to an exponential, smoothed (Wilder) or weighted average; the band distance is still measured as the standard deviation of the source.

The bands appear after Length bars. Offset only moves the drawing in time. Works on every bar size.

Notes

Matches the Bollinger Bands of TradingView and most platforms: the standard deviation is the population standard deviation (divided by the number of bars, not by one less). With the EMA basis, the average starts at the first bar instead of after Length bars, so the first few dozen basis values can differ from TradingView's. Bollinger %B and Bollinger BandWidth, also built in, turn the same bands into oscillators.

Settings

Each setting appears in the indicator's settings panel in Spectral, next to a color setting for every output.

SettingTypeDefaultRange or optionsWhat it does
LengthWhole number202 – 500Bars in the moving average and in the standard deviation.
Basis averageChoiceSMASMA, EMA, SMMA (RMA), WMAMoving average used for the middle line. Bollinger's definition uses the simple moving average (SMA).
SourcePrice sourcecloseopen, high, low, close, hlc3Price the average and the standard deviation are computed from.
Standard deviationsNumber20.1 – 10, step 0.1Distance of each band from the basis, in standard deviations of the source over Length bars.
OffsetWhole number0-500 – 500Draw the bands this many bars to the right (positive) or left (negative) of the bars they were computed on. 0 is the usual setting.

Presets

Each preset is its own entry in the Indicators toolbox, with these settings changed.

Toolbox entryShort nameSettings
Bollinger Bands (10, 1.9)BB 10Length: 10Standard deviations: 1.9
Bollinger Bands (50, 2.1)BB 50Length: 50Standard deviations: 2.1

What it draws

Lines

  • Basissolid · 1.5 px · primary#C4A46A
  • Upper bandsolid · 1 px#8AA9C3
  • Lower bandsolid · 1 px#8AA9C3

Zones

  • Band fillfilled at 8% opacity#8AA9C3

Source

Save it as bollinger-bands.js and add it with Indicators → Custom indicators → Add file…. The format is explained in the custom indicators guide.

// Copyright (c) 2026 Spectral
// SPDX-License-Identifier: MIT-0
// Free to use, copy, modify and share: https://spectral.trade/docs/custom-indicators/LICENSE.txt
/** @type {import('./spectral-indicator').IndicatorV2} */
export default {
  schemaVersion: 2,
  id: 'bollinger-bands',
  name: 'Bollinger Bands',
  shortName: 'BB',
  version: '1.0.0',
  author: 'Spectral',
  description: 'A moving average with bands a chosen number of standard deviations above and below it, so the band width follows volatility.',
  pane: 'price',
  live: { lookback: 'length' },
  inputs: {
    length: { type: 'integer', label: 'Length', default: 20, min: 2, max: 500, description: 'Bars in the moving average and in the standard deviation.' },
    maType: { type: 'select', label: 'Basis average', default: 'SMA', options: ['SMA', 'EMA', 'SMMA (RMA)', 'WMA'], description: 'Moving average used for the middle line. Bollinger\'s definition uses the simple moving average (SMA).' },
    source: { type: 'source', label: 'Source', default: 'close', description: 'Price the average and the standard deviation are computed from.' },
    mult: { type: 'number', label: 'Standard deviations', default: 2, min: 0.1, max: 10, step: 0.1, description: 'Distance of each band from the basis, in standard deviations of the source over Length bars.' },
    offset: { type: 'integer', label: 'Offset', default: 0, min: -500, max: 500, description: 'Draw the bands this many bars to the right (positive) or left (negative) of the bars they were computed on. 0 is the usual setting.' },
  },
  plots: {
    basis: { label: 'Basis', color: '#C4A46A', lineWidth: 1.5, primary: true, offset: 'offset' },
    upper: { label: 'Upper band', color: '#8AA9C3', lineWidth: 1, offset: 'offset' },
    lower: { label: 'Lower band', color: '#8AA9C3', lineWidth: 1, offset: 'offset' },
  },
  zones: {
    band: { label: 'Band fill', color: '#8AA9C3', opacity: 0.08, offset: 'offset' },
  },
  presets: [
    { id: 'short', name: 'Bollinger Bands (10, 1.9)', shortName: 'BB 10', inputs: { length: 10, mult: 1.9 } },
    { id: 'long', name: 'Bollinger Bands (50, 2.1)', shortName: 'BB 50', inputs: { length: 50, mult: 2.1 } },
  ],
  docs: {
    overview: 'Bollinger Bands draw a moving average of price (the basis) with an upper and a lower band a set number of standard deviations away from it. Standard deviation measures how far price has been spreading around its average, so the bands widen when the market becomes volatile and narrow when it quiets down.\n\nWith the defaults the basis is the 20-bar simple moving average of the close, and the bands sit two standard deviations of the last 20 closes above and below it.',
    howToRead: 'The bands describe what is normal for the recent past. A close near the upper band is high relative to recent volatility, a close near the lower band is low; neither is a buy or sell signal by itself. In strong trends price can ride along one band for a long time, while in ranges it tends to move between them.\n\nA squeeze, where the bands become unusually narrow, marks a quiet market that often comes before a larger move; it does not tell you the direction. A close outside a band followed by a close back inside is a common reversal setup, best confirmed with price structure or another indicator.',
    settings: 'Length sets the window for both the average and the standard deviation; 20 is the standard. Standard deviations sets the band distance; 2 is the standard, and John Bollinger suggests about 1.9 with a 10-bar length and 2.1 with a 50-bar length, which the presets use. Basis average switches the middle line to an exponential, smoothed (Wilder) or weighted average; the band distance is still measured as the standard deviation of the source.\n\nThe bands appear after Length bars. Offset only moves the drawing in time. Works on every bar size.',
    notes: 'Matches the Bollinger Bands of TradingView and most platforms: the standard deviation is the population standard deviation (divided by the number of bars, not by one less). With the EMA basis, the average starts at the first bar instead of after Length bars, so the first few dozen basis values can differ from TradingView\'s. Bollinger %B and Bollinger BandWidth, also built in, turn the same bands into oscillators.',
  },
  calculate(ctx) {
    const { bars, inputs, ta } = ctx;
    const source = ta.source(bars, inputs.source);
    const basis = movingAverage(ta, source, inputs.maType, inputs.length);
    const deviation = ta.stdev(source, inputs.length);
    const upper = basis.map((value, index) => (value === null || deviation[index] === null ? null : value + inputs.mult * deviation[index]));
    const lower = basis.map((value, index) => (value === null || deviation[index] === null ? null : value - inputs.mult * deviation[index]));
    return { plots: { basis, upper, lower }, zones: { band: { upper, lower } } };
  },
};

function movingAverage(ta, values, type, length) {
  if (type === 'EMA') return ta.ema(values, length);
  if (type === 'SMMA (RMA)') return ta.rma(values, length);
  if (type === 'WMA') return ta.wma(values, length);
  return ta.sma(values, length);
}

Adapt it with an LLM

Paste this into your assistant and replace the second-to-last line with the change you want.

Start from this Spectral custom indicator: https://spectral.trade/indicators/bollinger-bands.js
Format reference: https://spectral.trade/docs/custom-indicators.llm.md
Keep the file format and every rule of the reference; give the result a new id, name, shortName and version, and update docs and input descriptions to match.
Keep its live updates exact and fast (section "Fast live updates" of the reference): carry values from bar to bar only through ctx.ta, and raise live.lookback if the change reads further back; Spectral rejects a file whose live updates differ from a full recalculation or are too slow.
Change I want: <describe the change>
Answer with the complete file in one code block, ready to save as <id>.js.