Back

Wrong logic in my code when computing Bollinger Band indicator

Hello everyone,

I'm writing a system which uses a Bollinger Band as an entry signal (in particular, the entry signal is when the price crosses the upper band of the BB with average=100 days and 2 standard deviations. I have read (enough!) carefully the documentation and I have embedded the logic of the entry signal in a SymbolData class, where I have also created two small Rolling Windows to store previous values of price and BB so to check if the cross has occurred and then to update the crossed variable. The update method of SymbolData is called from inside of CoarseSelectionFunction function passing cf.EndTime and cf.AdjustedPrice to it, as explained in the documentation.

class SymbolData():
def __init__(self, symbol):
self.symbol = symbol
self.bb_band = BollingerBands(100, 2, MovingAverageType.Simple)
self.priceWin = RollingWindow[float](2)
self.bbWin = RollingWindow[float](2)
self.crossed = False


def update(self, time, price):
self.priceWin.Add(price)
self.bb_band.Update(time, price)
if self.bb_band.IsReady:
upper_band_value = self.bb_band.UpperBand.Current.Value
self.bbWin.Add(upper_band_value)
if self.bb_band.IsReady and self.bbWin.IsReady and self.priceWin.IsReady:
self.crossed = self.priceWin[1] < self.bbWin[1] and self.priceWin[0] > self.bbWin[1]

Unfortunately, I have checked the values produced by my Bollinger Band indicator and I've observed that my code fragment does not produce correct numbers, and the Bollinger Band value is almost always very similar to the price value but never enough far from it (as sometimes it should be), a symptom that my code is wrong in computing it. Also drawing the indicator on a chart shows that it is not correct. Is it necessary to use somehow an event handler for updating it (i.e.   self.bb_band Updated += self.OnBBUpdated )? Could you kindly help me to understand where my code is wrong?

Thank you in advance,

Lew.

 

 

Update Backtest







0

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


Hi Lew,

I could not reproduce the error. Please attach a backtest or a full code snippet which produces the issue. I attached a backtest with your code which does not produce the issue. The values of the price and upper band are distinct in this case.

Best
Rahul

1

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


Update Backtest





0

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


Loading...

This discussion is closed