Bollinger Bands

Technical Analysis Indicator: bbands

Fork me on GitHub

Function Prototype

/* Bollinger Bands */
/* Type: overlay */
/* Input arrays: 1    Options: 2    Output arrays: 3 */
/* Inputs: real */
/* Options: period, stddev */
/* Outputs: bbands_lower, bbands_middle, bbands_upper */
int ti_bbands_start(TI_REAL const *options);
int ti_bbands(int size,
      TI_REAL const *const *inputs,
      TI_REAL const *options,
      TI_REAL *const *outputs);

Description

This documentation is still a work in progress. It has omissions, and it probably has errors too. If you see any issues, or have any general feedback, please get in touch.

The Bollinger Bands indicator calculates three results. A middle band, which is a Simple Moving Average, as well as upper and lower bands which are spaced off the middle band.

It takes two parameters: the period n, as well as a scaling value a. The upper and lower bands are spaced off of the middle band by a standard deviations of the input.

$$bbands^{middle}_{t} = \frac{1}{n} \sum_{i=0}^{n-1} in_{t-i}$$
$$bbands^{lower}_{t} = bbands^{middle}_{t} - a \sqrt {\frac{1}{n} \sum_{i=0}^{n-1} (in_{t-i}-bbands^{middle}_{t})^{2}}$$
$$bbands^{upper}_{t} = bbands^{middle}_{t} + a \sqrt {\frac{1}{n} \sum_{i=0}^{n-1} (in_{t-i}-bbands^{middle}_{t})^{2}}$$

See Also

References

Example Usage

Calling From C

/* Example usage of Bollinger Bands */
/* Assuming that 'input' is a pre-loaded array of size 'in_size'. */
TI_REAL *inputs[] = {input};
TI_REAL options[] = {5, 2}; /* period, stddev */
TI_REAL *outputs[3]; /* bbands_lower, bbands_middle, bbands_upper */

/* Determine how large the output size is for our options. */
const int out_size = in_size - ti_bbands_start(options);

/* Allocate memory for each output. */
outputs[0] = malloc(sizeof(TI_REAL) * out_size); assert(outputs[0] != 0); /* bbands_lower */
outputs[1] = malloc(sizeof(TI_REAL) * out_size); assert(outputs[1] != 0); /* bbands_middle */
outputs[2] = malloc(sizeof(TI_REAL) * out_size); assert(outputs[2] != 0); /* bbands_upper */

/* Run the actual calculation. */
const int ret = ti_bbands(in_size, inputs, options, outputs);
assert(ret == TI_OKAY);

Calling From Lua (with Tulip Chart bindings)

-- Example usage of Bollinger Bands
bbands_lower, bbands_middle, bbands_upper = ti.bbands(input, 5, 2)

Example Calculation

period = 5, stddev = 2

dateinputbbands_lowerbbands_middlebbands_upper
2005-11-0181.59
2005-11-0281.06
2005-11-0382.87
2005-11-0483.00
2005-11-0783.6180.5382.4384.32
2005-11-0883.1580.9982.7484.49
2005-11-0982.8482.5383.0983.66
2005-11-1083.9982.4783.3284.16
2005-11-1184.5582.4283.6384.84
2005-11-1484.3682.4483.7885.12
2005-11-1585.5382.5184.2586.00
2005-11-1686.5483.1484.9986.85
2005-11-1786.8983.5485.5787.61
2005-11-1887.7783.8786.2288.57
2005-11-2187.2985.2986.8088.32

Chart

 

Other Indicators

Previous indicator: Average Price

Next indicator: Balance of Power

Random indicator: Williams %R