Hi, I'm trying to use the macd strategy with quantconnect, I'm trying to detect when the fast line crosses up the slow line of the MACD and print it with the debug function with the time in when it happened.
The problem that I'm facing is that in tradingview, that happens a lot more than in my program and the points aren't even the same
I've already tried comparing self.macd.Fast with self.macd.Slow and self.macd with self.macd.Signal.
I'e also rode this:
But that does not explain why my code outputs much les crosover points than tradingview
What am I doing wrong?
class CasualSkyBlueFly(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2021, 12, 1) # Set Start Date
self.SetEndDate(2021, 12, 31) # Set Start Date
self.SetCash(1000) # Set Strategy Cash
self.AddEquity("BTC", Resolution.Hour)
self.macd = self.MACD("BTC", 12, 26, 9, MovingAverageType.Exponential, Resolution.Hour)
self.SetWarmUp(200)
self.MACD_UP_Window = RollingWindow[bool](2)
self.Debug("-------------------------------------------------------------------------------------------------------------------")
def OnData(self, data):
#Update indicators
self.macd.Update(self.Time, data["BTC"].Close)
if not self.macd.IsReady:
return
if self.macd.Fast > self.macd.Slow:
self.MACD_UP_Window.Add(True)
else:
self.MACD_UP_Window.Add(False)
if not self.MACD_UP_Window.IsReady:
return
#Check MACD
if self.MACD_UP_Window[0] and not self.MACD_UP_Window[1]:
self.Debug("MACD - YEAR: " + str(self.Time.year) + "/MONTH: " + str(self.Time.month) + "/DAY: " + str(self.Time.day ) + "/HOUR: " + str(self.Time.hour))
self.Debug("MACD - short: " + str(self.macd.Fast) + "/long: " + str(self.macd.Slow))
Fred Painchaud
Hi David,
Have you tried with
instead of
?
You should also have a look at
I am not so sure about the equity “BTC” - as calculated by TV vs the source used by QC………
Cheers,
Fred
David Serra
Thanks for the reply,
Yes, I've tried it, it seems to be a problem with some kinds of crypto, because the backtest gave zero debug prints, I've run into the same issue with other coins and so far backtests only seem to work with BTC, but that is for another day.
I've also tested it with TSLA and SPY and even though it printed more points in which the lines crossed, they didn't match tradingview.
Fred Painchaud
Hi,
I've looked at your code a bit more carefully.
Have you tried to use
instead of
?
That's the right way to do it.
Fred
David Serra
I plotted the MACD of TSLA using:
self.Plot("MACD", self.macd, self.macd.Signal);
and also:
self.Plot("MACD_2", self.macd.Fast, self.macd.Slow);
and it showed:
Which should look like:
What is happening?
I really cannot find any resemblance and the parameters are the same.
Fred Painchaud
Hi David,
I did some more “looking carefully at your code” 😊. Sorry I guess I am lazy or something…
You're not using MACD correctly. MACD does not work like that. It is a difference of EMAs over a signal which is an ema of that difference. Check the attached backtest. And you were only logging when the macd is above the signal, not when it crosses down.
Hope this helps.
Fred
Fred Painchaud
Oh yeah, also, TV uses raw data by default while QC uses adjusted data, so I made QC switched to raw data for your comparison.
Fred
David Serra
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!