Hey everyone!
I'm trying to create a Bollinger Band based on 5 minute candles, but I've only been able to get the values to update every 5 minutes, but not be calculated based on 5 minute candles. For example, the standard deviation bands should be much farther apart if based on 5 minute candles than 1 minute candles, but I cannot get them to be. Please see attached backtest. I've tried several methods listed both in the documentation and here in the community, but nothing seems to be working. I'm sure it's due to my inexperience. Thank you in advance for any help!
Edit also to add that I am also having trouble getting the warmup period for the indicator to work. Every morning the band values are way off as it doesn't seem to be warming up correctly.
Derek Melchin
Hi Kevin,
The reason the indicator is updated so frequently is because the BB method automatically subscribes the indicator to use the same resolution as the data subscription. See the source code here.
If we want to update the indicator with 5-minute bars, we need to use the full class name when creating the indicator.
self.bb = BollingerBands(BBLENGTH, 2, MovingAverageType.Exponential)
Then, in our consolidation handler, we can manually update the indicator with the latest 5-minute bar.
def OnStockBarConsolidated(self, consolidated): self.bb.Update(consolidated.EndTime, consolidated.Close)
To ensure we warmup the indicator properly, we can use
self.SetWarmUp(BBLENGTH * 5, Resolution.Minute)
See the attached backtest and plot for reference.
Best,
Derek Melchin
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.
Kevin Chaney
Hey Derek,
Thanks so much for the help! The indicator seems to be calculating correctly now, except that the warmup is still not working correctly. It seems it is not warming up on pre-market data as is desired, but instead just on the previous day's data (this latter part is a guess). Any idea why this would be the case? Do I need to somehow enable pre-market data? I can't seem to find an answer to this in the documentation.
Thanks for any help in advance!
Kevin
Jared Broad
Hi Kevin, you didn't request premarket data. Please see the documentation on Initializing Algorithms.
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.
Kevin Chaney
Thanks for the pointer, Jared, and my apologies for not seeing it there before!
That said, it still doesn't seem to be working (see attached backtest). If I run the code as-is then it still does not warmup the BB using premarket data, and if I run using the AddEquity line that is commented out I get an error that Market has no attribute USA. I believe I have all needed imports, so I'm not sure why this would be. Additionally, SetTimeZone does not seem to be working.
I greatly appreciate any help!
Derek Melchin
Hi Kevin,
Our Python API does not currently support named arguments. That is why
self.stock = self.AddEquity(SYMBOL, Resolution.Second, extendedMarketHours = True)
does not subscribe us to pre-market data. Instead, we could use
self.stock = self.AddEquity(SYMBOL, Resolution.Second, Market.USA, True, 1, True)
We just need to ensure we import the Market class.
from QuantConnect import *
In regards to warming up, backtests start at midnight on the start date of the algorithm. SetWarmUp streams data from the past. Since pre-market data is in the future at the start of the backtest, the data from the previous trading day is streamed during warm up. To warmup the indicator with the upcoming pre-market data, we need to remove the call to SetWarmUp.
See the attached backtest and plot for reference.
Best,
Derek Melchin
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.
Kevin Chaney
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.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!