ta-lib-python icon indicating copy to clipboard operation
ta-lib-python copied to clipboard

Lookahead bias within std dev of BBands when matype=MAMA

Open remingm opened this issue 5 years ago • 2 comments

I'm also getting zeroes for the last 26 values of returns in my backtest when using the BBands and matype=7 MAMA.

I'm experiencing the same behavior for the upper and lower bands. This does not occur with other MA types.

Additionally, when I attempt to build my own bands with talib.MAMA and talib.STDDEV, I can't get them to be identical with the output from talib.BBANDS(matype=7). However, I can build identical bands to talib.BBANDS(matype=0) with talib.SMA and talib.STDDEV.

This is very strange. I can assert that talib.MAMA = the middle band from talib.BBands(matype=7). So the error may be with the upper and lower band calculations.

Originally posted by @remingm in https://github.com/mrjbq7/ta-lib/issues/92#issuecomment-713219494

remingm avatar Oct 21 '20 03:10 remingm

I originally posted the above in #92. I did some digging and discovered that the apparent standard deviation in the bands has a lookahead bias. See the below charts.

The upper band spikes around 175 before the volatility actually increases: both Here we see why. The apparent std dev (measured by subtracting the bands) is shifted to the left. stddev This made my backtests incredible and I thought I was a genius for a minute, ha. Let me know if this checks out or if there could be another cause.

remingm avatar Oct 21 '20 03:10 remingm

yah you know what, this issue makes my backtests look very amazing but once I removed the bband indicator the result was very worse. I thought I was a genius previously till i found the same issue. I dig out into the C code in talib but didn't recognize the bug yet. I guess the look adhead bias may caused by array buffer operations or somewhere related to it.

HongzoengNg avatar Jan 19 '21 17:01 HongzoengNg