Hi,
I am attempting to use the MACD indicator with a consolidator but am having trouble. In my Initialize() function i have:
self.macd = self.MACD(self.my_ticker, 12, 26, 9, MovingAverageType.Exponential, Resolution.Minute)
self.macd_cons = TradeBarConsolidator(timedelta(minutes=10))
self.macd_cons.DataConsolidated += self.macdUpdated
self.SubscriptionManager.AddConsolidator(self.my_ticker, self.macd_cons)
self.RegisterIndicator(self.my_ticker, self.macd, self.macd_cons)
and in the same class, the helper function:
def macdUpdated(self,sender,bar):
if self.macd.Current.Value >= self.macd.Signal.Current.Value:
self.Debug("[+] above {} {} {} ".format(self.Time, self.macd.Current.Value, self.macd.Signal.Current.Value))
else:
self.Debug("[-] below {} {} {} ".format(self.Time, self.macd.Current.Value, self.macd.Signal.Current.Value))
The prices I see (when compared against Charles Schwab's) are somewhat similar. I am aware of your FAQ regarding prices. However, the ten minute MACD that CS provides versus this one has completely different data, which leads me to believe I'm doing something wrong. The output below is from 11 to 1 on a random day. This link
https://imgur.com/a/urcPQP0
is from CS for the same time. For instance, the MACD at 1230, according to CS, is around .043, while at 11:30 it is .026. But more importantly, the MACD line is always above the signal line from 11 to 1 according to CS, which is not the case in the data below. I am not 100% sure CS uses EMA for their MACD lines but https://help.streetsmart.schwab.com/edge/1.6/Content/Chart%20Studies.htm seems to say they do. Trying on QuantConnect with different MovingAverageType didn't change the differences between the two.
[+] above 2021-02-03 11:00:00 0.01086282153091968 0.01043351510466672
[+] above 2021-02-03 11:10:00 0.017115039083645686 0.013846390720055628
[-] below 2021-02-03 11:20:00 0.004363630117023701 0.008898289604792024
[+] above 2021-02-03 11:30:00 0.004280274735000386 0.002952826515702166
[+] above 2021-02-03 11:40:00 0.009948153148334535 0.007532968454340619
[+] above 2021-02-03 11:50:00 0.014130897930721201 0.010762260173893637
[-] below 2021-02-03 12:00:00 0.01618329286993292 0.016434888114781054
[-] below 2021-02-03 12:10:00 0.008510039349490243 0.011037537106545658
[-] below 2021-02-03 12:20:00 0.002854144002440724 0.004979843427408466
[-] below 2021-02-03 12:30:00 0.0003614507231492533 0.001764756038148813
[-] below 2021-02-03 12:40:00 -0.0002237201709032017 7.342287373448871e-05
[+] above 2021-02-03 12:50:00 0.009090660517783726 0.004878490081079443
[-] below 2021-02-03 13:00:00 0.003567513368485681 0.004957401141400548
Thanks for your help
Derek Melchin
Hi Lodi,
When using the short-cut methods to creating indicators (self.MACD instead of MovingAverageConvergenceDivergence), the indicator is automatically updated on the given resolution. Therefore, in the code above, the MACD indicator is updated with minute bars and 10-minute consolidated bars. To resolve the issue, we should only update the indicator with the consolidated bars. We can accomplish this with:
self.macd = MovingAverageConvergenceDivergence(12, 26, 9, MovingAverageType.Exponential) self.RegisterIndicator(self.symbol, self.macd, timedelta(minutes=10))
See the attached backtest 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.
Lodi
Thank you. That works and mostly jives with CS. Reposting your code for ease for others
class GeekyYellowGreenButterfly(QCAlgorithm): def Initialize(self): self.SetStartDate(2020, 9, 2) # Set Start Date self.SetCash(100000) # Set Strategy Cash self.symbol = self.AddEquity("SPY", Resolution.Minute).Symbol self.macd = MovingAverageConvergenceDivergence(12, 26, 9, MovingAverageType.Exponential) self.RegisterIndicator(self.symbol, self.macd, timedelta(minutes=10)) # Optional: Used for plotting consolidator = TradeBarConsolidator(timedelta(minutes=10)) consolidator.DataConsolidated += self.consolidation_handler self.SubscriptionManager.AddConsolidator(self.symbol, consolidator) def consolidation_handler(self, sender, bar): self.Plot("MACD", "Value", self.macd.Current.Value)
Lodi
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!