Bollinger BandWidth (BBW)
Summary
Bollinger BandWidth: the distance between the upper and lower Bollinger Bands as a percentage of the middle band. Low values mark contracting volatility, the setup John Bollinger calls the Squeeze; high values mark expanding volatility.
Chart
Chart metadata i
Outputs
outReal:line·zero·positive·negative
Formula
where , and are the BBANDS bands of with period , moving-average type and deviation multipliers , . Since both bands sit on the middle band, , with the population standard deviation of the last values of .
Notes
- The input is assumed positive. The middle band is the divisor. Where it is exactly 0 the output is 0. Near zero the divisor is small and the output very large; no error is returned. A series that reaches or crosses zero, such as an oscillator or a spread, has no meaningful result here.
- With Bollinger's settings (a simple moving average and two deviations on each side) BBW is 400 times the window's coefficient of variation: its standard deviation divided by its mean.
- The two deviation multipliers enter only through their sum.
- The result is in percent: 10 means the bands are 10% of the middle band apart.
- Any
optInMATypeother than SMA is a TA-Lib generalisation, as it is for BBANDS: the deviation stays the population standard deviation about the simple mean. - Wherever the middle band is not 0, BBW is bit for bit
((upper - lower) / middle) * 100computed from BBANDS' own outputs.
Inputs
inReal— Input data series
Outputs
outReal— Width of the bands as a percentage of the middle band
Parameters
| Parameter | Type | Default | Accepted values | Description |
|---|---|---|---|---|
optInTimePeriod | integer | 20 | 2–100000 | Periods for the MA and standard deviation |
optInNbDevUp | real | 2 | any real | Standard-deviation multiplier for the upper band |
optInNbDevDn | real | 2 | any real | Standard-deviation multiplier for the lower band |
optInMAType | MAType | SMA (0) | any MAType | Moving-average type for the middle band |
MAType values: 0 SMA · 1 EMA · 2 WMA · 3 DEMA · 4 TEMA · 5 TRIMA · 6 KAMA · 7 MAMA · 8 T3 · 9 HMA · 10 DISABLED · 11 DEFAULT · 12 ZLEMA · 13 RMA · 14 VIDYA · 15 ALMA · 16 SSF
Properties
Numerical Stability: Depends on MA Type — This function's default, SMA, is start-independent.
| ☐ Overlap Input |
| ✅ Independent Y-Axis i |
| ☐ Candlestick |
| ☐ Display Shift |
| ☐ Can Output NaN or ±Inf |
| ☐ Identity at Period 1 |
| ☐ Uses Transcendental |
| ✅ Input Must Be Positive i |
Example
#include <stdio.h>
#include <ta_libc.h>
int n = 252;
double series[252]; /* your data, oldest first */
double out[252];
int outBegIdx, outNBElement;
TA_Initialize();
TA_BBW( 0, n - 1, series, 20, 2.0, 2.0, TA_MAType_SMA, &outBegIdx, &outNBElement, out );
printf( "%d values from bar %d: [%.4f, ..., %.4f]\n", outNBElement, outBegIdx, out[0], out[outNBElement - 1] );use ta_lib::{Core, MAType};
let series = vec![0.0; 252]; // your data, oldest first
let mut out = vec![0.0; 252];
let core = Core::new();
let range = core.bbw(0, series.len() - 1, &series, 20, 2.0, 2.0, MAType::SMA, &mut out)?;
println!("{} values from bar {}: [{:.4}, ..., {:.4}]", range.count, range.beg_idx, out[0], out[range.count - 1]);import io.github.talib.Core;
import io.github.talib.MAType;
import io.github.talib.OutRange;
import java.util.Locale;
double[] series = new double[252]; // your data, oldest first
double[] out = new double[252];
Core core = Core.DEFAULT;
OutRange r = core.bbw(0, series.length - 1, series, 20, 2.0, 2.0, MAType.SMA, out);
System.out.printf(Locale.ROOT, "%d values from bar %d: [%.4f, ..., %.4f]%n", r.count(), r.begIdx(), out[0], out[r.count() - 1]);using System;
using TALib;
var series = new double[252]; // your data, oldest first
var outReal = new double[252];
var core = Core.Default;
OutRange r = core.Bbw(0, series.Length - 1, series, 20, 2.0, 2.0, MAType.SMA, outReal);
Console.WriteLine(FormattableString.Invariant($"{r.Count} values from bar {r.BegIdx}: [{outReal[0]:F4}, ..., {outReal[r.Count - 1]:F4}]"));import { MAType, bbw } from 'ta-lib';
const series = new Float64Array(252); // your data, oldest first
const { begIdx, values } = bbw(series, {
timePeriod: 20,
nbDevUp: 2,
nbDevDn: 2,
maType: MAType.SMA,
});
console.log(values.length + ' values from bar ' + begIdx + ': [' + values[0]?.toFixed(4) + ', ..., ' + values[values.length - 1]?.toFixed(4) + ']');import (
"fmt"
"log"
talib "ta-lib.github.io/go"
)
series := make([]float64, 252) // your data, oldest first
out := make([]float64, 252)
core := talib.NewCore()
r, err := core.BBW(0, len(series)-1, series, 20, 2, 2, talib.MATypeSMA, out)
if err != nil {
log.Fatal(err)
}
fmt.Printf("%d values from bar %d: [%.4f, ..., %.4f]\n", r.NBElement, r.BegIdx, out[0], out[r.NBElement-1])Implementation
Added in 0.8.3 (ta-lib-python 0.8.3).
TA-Lib Definition: bbw.c · bbw.yaml
| Native | File |
|---|---|
| C | ta_BBW.c |
| Rust | bbw.rs |
| Java | Core_BBW.java |
| C# | Core_BBW.cs |
| TypeScript | bbw.ts |
| Go | bbw.go |
Python: talib.BBW (ta-lib-python 0.8.3 or later), see ta-lib-python.
TA-Lib is also available for R and more using a wrapper.
Aliases
Bollinger BandWidth, Bollinger Band Width, Bollinger Bands Width, BandWidth
See Also
BBANDS · PERCENTB · STDDEV · NATR
References
- John A. Bollinger, Bollinger on Bollinger Bands, McGraw-Hill 2001 (ISBN 0071373683)
- John Bollinger, Bollinger Band Rules, rule 18