Bollinger Bands
Install and import#
npm install fintech-algorithmsimport { 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#
| Name | Type | Notes |
|---|---|---|
close | number[] | Per-bar closing prices, chronological. |
p | number | Lookback for both the average and the standard deviation. min: 1 · integer: true |
multiplier | number | Number 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#
[100, 100.54129063, 101.06589044, 101.55765885, 102.00153762, 102.3840473]Showing 6 of 240 elements.
202Call#
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#
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.
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