bukosabino / ta

Technical Analysis Library using Pandas and Numpy
https://technical-analysis-library-in-python.readthedocs.io/en/latest/
MIT License
4.33k stars 912 forks source link

Incorrect Bollinger low and high calculation #74

Closed juliangall closed 5 years ago

juliangall commented 5 years ago

Bollinger low and high calculate the standard deviation as follows:

mstd = close.rolling(n, min_periods=0).std()

According to the Pandas documentation, std() uses (N - ddof) as the denominator, where ddof defaults to 1, unlike numpy std().

StockCharts has an example of Bollinger in Excel which, unsurprisingly, uses N as the denominator. Wikipedia explains that N - 1 is used if the sd is being calculated from a sample of the population, rather than the whole. As this is rolling, and a very small set of numbers, I assume Pandas is calculating the actual sd from all the data fed to it, therefore we should use N.

The high and low band Bollinger calcs should be:

mstd = close.rolling(n, min_periods=0).std(ddof=0)

The same applies to the Bollinger indicators.

bukosabino commented 5 years ago

Hi @juliangall ,

You are right.

I have included your proposal code in the last version (0.4.7). You can use it updating the library installation:

pip install --upgrade ta

Thank you.