Hi,
I used the method mentioned in Algorithmic Trading Lesson to warm up indicators with historical data. However, it does not work. Can anyone help me to fix it. Many thanks.
class HmaIchimokuAlgorithm(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2010, 1, 1)
self.SetCash(25000)
self.qqq = self.AddEquity("QQQ", Resolution.Hour).Symbol
self.hma = self.HMA(self.qqq, 12, Resolution.Hour)
self.delayed_hma = IndicatorExtensions.Of(Delay(2), self.hma)
# ==== System Inputs ====
TenkanPeriod = 9
KijunPeriod = 26
SenkouAPeriod = 26
SenkouBPeriod = 52
SenkouADelay = 26
SenkouBDelay = 26
self.ichimoku = self.ICHIMOKU(self.qqq, TenkanPeriod, KijunPeriod, SenkouAPeriod, SenkouBPeriod, SenkouADelay, SenkouBDelay, Resolution.Hour)
closing_prices = self.History(self.qqq, 52, Resolution.Hour)["close"]
for time, price in closing_prices.loc[self.qqq].items():
self.hma.Update(time, price)
self.delayed_hma.Update(time, price)
self.ichimoku.Update(time, price)
def OnData(self, data):
if not (self.qqq in data.Bars and self.hma.IsReady and self.delayed_hma.IsReady and self.ichimoku.IsReady):
return
history = self.History(self.qqq, 52, Resolution.Hour)
self.hma = SelectionData(self.qqq, history)
self.delayed_hma = SelectionData(self.qqq, history)
self.ichimoku = SelectionData(self.qqq, history)
price = self.Securities["QQQ"].Price
currentHma = self.hma.Current.Value
previousHma = self.delayed_hma.Current.Value
tenkan = self.ichimoku.Tenkan.Current.Value
kijun = self.ichimoku.Kijun.Current.Value
chikou = self.ichimoku.Chikou.Current.Value
senkou_span_a = self.ichimoku.SenkouA.Current.Value
senkou_span_b = self.ichimoku.SenkouB.Current.Value
cloud_top = max(senkou_span_a, senkou_span_b)
cloud_bottom = min(senkou_span_a, senkou_span_b)
if not self.Portfolio.Invested and currentHma > previousHma and price > previousHma and price > chikou and price > cloud_top and (tenkan >= kijun or price > kijun):
self.SetHoldings("QQQ",1)
elif not self.Portfolio.Invested and currentHma < previousHma and price < previousHma and price < chikou and price < cloud_bottom and (tenkan <= kijun or price < kijun):
self.SetHoldings("QQQ",-1)
# Liquidate
if self.Portfolio["QQQ"].IsLong and currentHma < previousHma and (price < previousHma or tenkan < kijun or price < tenkan or price < kijun or price < cloud_top or price < chikou):
self.Liquidate()
elif self.Portfolio["QQQ"].IsShort and currentHma > previousHma and (price > previousHma or tenkan > kijun or price > tenkan or price > kijun or price > cloud_bottom or price > chikou):
self.Liquidate()
Vladimir
Ken Lu
You can warm up self.HMA and self.ICHIMOKU with:
self.SetWarmUp(2*SenkouBPeriod, Resolution.Hour)
Ken Lu
Hi Vladimir,
Thank you for your response. I know I can warm up indicates with setwarmup, but it takes time. With the history data, the indicator can be ready immediately. I would appreciate it if you could point what's wrong with my code. Thanks
Vladimir
Ken Lu,
As far as I know, indicators with short names like self.HMA(), self.ICHIMOKU() only need warm up with self.SetWarmUp() or self.AutomaticIndicatorWarmUp = True.
When we set an algorithm warm-up period, the engine pumps data in and automatically update all the indicators from before the start date.
Alternatively, we can request for historical data to update the indicator manually but that is not required in your case.
Do you think that the 11.75 years backtest using 3 indicators based on hourly data and completed in 3.49 sec is not fast enough?
Try it.
Ken Lu
Hi Vladimir,
Thank you for your thorough explanation. If I use this strategy with SetWarmUp() in live trading, the history data will be pumped into the indicators automatically? Thanks.
Vladimir
Ken Lu,
When we set an algorithm warm-up period, the engine pumps data in and automatically update all the indicators from before the start date.
Ken Lu
Vladimir,
Thank you so much.
Ro García G
Nice
Louis Szeto
Hi Ken, Vladimir
Thank you for your reply, Vladimir. Indeed if we're importing a static universe that all the securities are initialized in .initialize method, like in this case, self.SetWarmUp() can handle the job.
As for a dynamic universe, the implementation of warm-up in the original post would be required. The update of the IchimokuKinkoHyo indicator has to be done by TradeBar or QuoteBar input. For example:
Best
Louis Szeto
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.
Vladimir
Louis Szeto,
Here is Ken Lu's original code with a history warm-up.
The universe is static
self.qqq = self.AddEquity("QQQ", Resolution.Hour).Symbol.
The indicator is self.ICHIMOKU() not IchimokuKinkoHyo.
Can you please show a full example for dynamic universe were history warm-up is required.
Louis Szeto
Hi Vladimir
Please note that self.ICHIMOKU is the auto-updating version of IchimokuKinkoHyo.
For dynamic universe, we can put the logic into a class object, while initializing the instance in the OnSecuritiesChanged method and save it in a dictionary. Please find the attached backtest for example.
Best
Louis
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.
Vladimir
Louis Szeto,
Here is "12268 dynamic universe warm up" with Ken Lu's original setup.
It completed in 46.31 sec versus 3.49 sec when using self.HMA(), self.ICHIMOKU() with self.SetWarmUp().
Is there any ways to speedup a backtest?
Also how to avoid such
Warning: No quote information available at 03/29/2011 11:00:00 America/New_York?
Varad Kabade
Hi Vladimir,
To speed up the above backtest we recommend using a consolidator to register the indicators in SymbolData class and removing the consolidator of the removed securities as deleting them from the dictionary does not stop them from receiving data. Also, note that in the SymbolData class, we do not need to register self.delayed_hma because it is updated when we update self.hma.
Best,
Varad Kabade
Vladimir
Varad kabade.
Thanks for the answer.
Can you post a sample, please?
Louis Szeto
Hi Vladimir
The extended time mainly comes from active security objects kept changing. If a static universe is used, it will not register and unregister security in the universe repeatedly, causing the backtest time to lengthen. self.SetWarmUp is an easy and recommended way for using static universe.
Yet, this is an example for referencing how to implement the logic for a dynamic universe. The below is the sample requested.
Best
Louis
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.
Ken Lu
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!