What are the Bollinger Bands? When prices move, we can calculate a moving average (mean) around them so that we better understand their position regarding their mean. By doing this we can also calculate where do they stand statistically. But first we need to understand the concept of the normal distribution curve which is the essence of statistics and the foundation of statistical theory.

The above curve shows the number of values within a number of standard deviations. For example, the area shaded in red represents around 1.33x of standard deviations away from the mean of zero. We know that if data is normally distributed then:

**About 68% of the data falls within 1 standard deviation of the mean.****About 95% of the data falls within 2 standard deviations of the mean.****About 99% of the data falls within 3 standard deviations of the mean.**

Presumably, this can be used to approximate the way to use financial returns data, but studies show that financial data is not normally distributed but at the moment we can assume it is so that we can use such indicators. The flawness of the method does not hinder much its usefulness.

Hence, the Bollinger bands are simple a combination of a moving average that follows prices and a moving standard deviation(s) band that moves alongside the price and the moving average.

To calculate the two Bands, we use the following relatively simple formulas:

With the constant being the number of standard deviations that we choose to envelop prices with. By default, the indicator calculates a 20-period simple moving average and two standard deviations away from the price, then plots them together to get a better understanding of any statistical extremes. This means that on any time, we can calculate the mean and standard deviations of the last 20 observations we have and then multiply the standard deviation by the constant. Finally, we can add and subtract it from the mean to find the upper and lower band.

Clearly, the below chart seems easy to understand. Every time the price reaches one of the bands, a contrarian position is most suited and this is evidenced by the reactions we tend to see when prices hit these extremes. So, whenever the EURUSD reaches the upper band, we can say that statistically, it should consolidate and when it reaches the lower band, we can say that statistically, it should bounce.

`def BollingerBands(Data, boll_lookback, standard_distance, what, where):`** # Calculating mean**

ma(Data, boll_lookback, what, where)

** # Calculating volatility**

volatility(Data, boll_lookback, what, where + 1)

** # Calculating Bands**

Data[:, where + 2] = Data[:, where] + (standard_distance * Data[:, where + 1])

Data[:, where + 3] = Data[:, where] - (standard_distance * Data[:, where + 1])

return Data