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

Bollinger BandWidth

Install and import#

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

Signature#

bollingerBandwidth(close, p, multiplier)

The width of a Bollinger channel relative to its middle band. Low readings mark contraction, which historically precedes expansion — the quantity behind every 'squeeze' screen.

Parameters#

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

Returns#

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

Parallel series: middle, stddev, upper, lower and bandwidth.

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.40234656, 100.79879348, 101.18354586, 101.55101638, 101.89592451]

Showing 6 of 240 elements.

p
20
multiplier
2

Call#

bollingerBandwidth(close, p, multiplier)

Returns#

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

{
  "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],
  "bandwidth": [null, null, null, null, null, null]
}

Other exports#

This module also exports bollingerBands. Every module additionally exports run as an alias of its primary function, and a meta object carrying its catalog id, domain, family, shape and article URL.

Diagrams#

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

Calculation flow#

Bollinger BandWidth calculation flow
flowchart LR
    A["Trailing closes"] --> B["Bollinger middle, upper, lower"]
    B --> C["Absolute width = upper - lower"]
    C --> D{"Middle > 0?"}
    D -- "no" --> E["BandWidth = null"]
    D -- "yes" --> F["100 × width / middle"]
    F --> G["Percent, directionless"]
    H["Multiply all prices by k>0"] --> I["BandWidth unchanged"]
    J["Add a constant"] --> K["BandWidth changes"]

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 BandWidth (BBW) — TradingView
  • Bollinger Bands (BB) — TradingView
  • Bollinger Bands — TA-Lib
  • e-Handbook: Measures of Scale — NIST/SEMATECH
  • Public evidence boundary

The rest of the Volatility and Channels family#