Bollinger Bands (BBANDS)
Summary
Bollinger Bands: a moving-average middle band with upper and lower bands offset by a multiple of the standard deviation. Used to gauge relative price volatility.
Chart
Chart metadata i
Outputs
outRealUpperBand:upper_limitoutRealMiddleBand:lineoutRealLowerBand:lower_limit
Formula
where is the input series, the period, the moving-average type, and , the upper and lower deviation multipliers.
Notes
- The defaults reproduce Bollinger's original definition: a 20-period SMA middle band with . Any other is a TA-Lib generalisation.
- sets where the envelope is centred; and set how wide it is. The two are independent — depends only on the price window, so changing the middle band re-centres the bands without resizing them.
Inputs
inReal— Input data series
Outputs
outRealUpperBand— Middle band plus nbDevUp standard deviationsoutRealMiddleBand— The moving averageoutRealLowerBand— Middle band minus nbDevDn standard deviations
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
Properties
Numerical Stability: Depends on MA Type — This function's default, SMA, is start-independent.
| ✅ Overlap Input i |
| ☐ Independent Y-Axis |
| ☐ Candlestick |
| ☐ Display Shift |
| ☐ Can Output NaN or ±Inf |
| ☐ Identity at Period 1 |
| ☐ Uses Transcendental |
| ☐ Input Must Be Positive |
Example
C
#include <stdio.h>
#include <ta_libc.h>
int n = 252;
double series[252]; /* your data, oldest first */
double upperBand[252], middleBand[252], lowerBand[252];
int outBegIdx, outNBElement;
TA_Initialize();
TA_BBANDS( 0, n - 1, series, 20, 2.0, 2.0, TA_MAType_SMA,
&outBegIdx, &outNBElement, upperBand, middleBand, lowerBand );
printf( "%d values from bar %d; last upperBand = %f\n", outNBElement, outBegIdx, upperBand[outNBElement - 1] );Rust
use ta_lib::{Core, MAType};
let series = vec![0.0; 252]; // your data, oldest first
let mut upper_band = vec![0.0; 252];
let mut middle_band = vec![0.0; 252];
let mut lower_band = vec![0.0; 252];
let core = Core::new();
let range = core.bbands(0, series.len() - 1, &series, 20, 2.0, 2.0, MAType::SMA,
&mut upper_band, &mut middle_band, &mut lower_band)?;
println!("{} values from bar {}; last upper_band = {}", range.count, range.beg_idx, upper_band[range.count - 1]);Java
import io.github.talib.Core;
import io.github.talib.MAType;
import io.github.talib.OutRange;
double[] series = new double[252]; // your data, oldest first
double[] upperBand = new double[252];
double[] middleBand = new double[252];
double[] lowerBand = new double[252];
Core core = Core.DEFAULT;
OutRange r = core.bbands(0, series.length - 1, series, 20, 2.0, 2.0, MAType.SMA,
upperBand, middleBand, lowerBand);
System.out.println(r.count() + " values from bar " + r.begIdx() + "; last upperBand = " + upperBand[r.count() - 1]);C#
using System;
using TALib;
var series = new double[252]; // your data, oldest first
var upperBand = new double[252];
var middleBand = new double[252];
var lowerBand = new double[252];
var core = Core.Default;
OutRange r = core.Bbands(0, series.Length - 1, series, 20, 2.0, 2.0, MAType.SMA,
upperBand, middleBand, lowerBand);
Console.WriteLine($"{r.Count} values from bar {r.BegIdx}; last upperBand = {upperBand[r.Count - 1]}");TypeScript
import { MAType, bbands } from 'ta-lib';
const series = new Float64Array(252); // your data, oldest first
const {
begIdx,
outUpperBand,
outMiddleBand,
outLowerBand,
} = bbands(series, {
timePeriod: 20,
nbDevUp: 2,
nbDevDn: 2,
maType: MAType.SMA,
});
console.log(outUpperBand.length + ' values from bar ' + begIdx + '; last outUpperBand = ' + outUpperBand[outUpperBand.length - 1]);Implementation
Added in 0.4.0.
TA-Lib Definition: bbands.c · bbands.yaml
| Native | File |
|---|---|
| C | ta_BBANDS.c |
| Rust | bbands.rs |
| Java | Core_BBANDS.java |
| C# | Core_BBANDS.cs |
| TypeScript | bbands.ts |
Python: talib.BBANDS, see ta-lib-python.
TA-Lib is also available for R and more using a wrapper.
Aliases
Bollinger Bands
See Also
References
- John A. Bollinger, Bollinger on Bollinger Bands, McGraw-Hill Trade (ISBN 0071373683)