fintech-algorithms
Using a coding agent? Give it the skill: npx skills add IslamBaraka90/Fintech-Algorithms-Library What it does →

Bollinger Bands

Install and import#

bash
npm install fintech-algorithms
ts
import { bollingerBands } from "fintech-algorithms/technical-indicators/volatility-and-channels/bollinger-bands";

Signature#

bollingerBands(close, p, multiplier)

A moving average with bands a number of standard deviations away, so the channel widens and narrows with realised volatility.

Parameters#

NameTypeNotes
closenumber[]Per-bar closing prices, chronological.
pnumberLookback for both the average and the standard deviation.
min: 1 · integer: true
multipliernumberNumber of standard deviations from the middle band to each outer band.
min: 0

Returns#

Record<string, (number | null)[]> · length same-as-input

Parallel series: middle, stddev, upper, lower, and percent_b — where price sits within the channel, 0 at the lower band and 1 at the upper.

Warm-up#

The first p − 1 positions are null.

Errors#

  • When p < 1, is not an integer, or multiplier is negative — throws RangeError

Complexity: time O(n × p), space O(n).

Worked example#

executed Captured by running this function on the input its own test provides. Real output of real code — but not asserted against a published figure.

Input#

close
[100, 100.54129063, 101.06589044, 101.55765885, 102.00153762, 102.3840473]

Showing 6 of 240 elements.

p
20
multiplier
2

Call#

bollingerBands(close, p, multiplier)

Returns#

object with 5 fields: middle, stddev, upper, lower, percent_b

{
  "middle": [null, null, null, null, null, null],
  "stddev": [null, null, null, null, null, null],
  "upper": [null, null, null, null, null, null],
  "lower": [null, null, null, null, null, null],
  "percent_b": [null, null, null, null, null, null]
}

Diagrams#

Bollinger Bands — canonical trace
Bollinger Bands — failure boundary
Bollinger Bands — mechanism map
Bollinger Bands — memory comparison

Calculation flow#

Bollinger Bands calculation flow
flowchart LR
    A["Trailing n closes"] --> B["SMA middle"]
    A --> C["Population σ using denominator n"]
    B --> D["Upper = middle + kσ"]
    C --> D
    B --> E["Lower = middle - kσ"]
    C --> E
    D --> F["%b location"]
    E --> F
    G["σ = 0"] --> H["Bands collapse; %b = null"]

How it works#

This page states the contract — how to call it correctly. The article explains the concept: why it works, and where it breaks.

Read the article →

References#

  • Bollinger Bands Rules — John Bollinger
  • Bollinger Bands (BB) — TradingView
  • Bollinger Bands — TA-Lib
  • ta_BBANDS.c — TA-Lib
  • e-Handbook: Measures of Scale — NIST/SEMATECH
  • Public evidence boundary

The rest of the Volatility and Channels family#