See attached backtest. Trying to signal for MACD crossover on a 5 minute candle. I logged the data to confirm if the cross over is correct. The crossovers are not correct nor is the data on the signal line, MACD or histogram. The RSI values aren't exactly the same but the data is close enough to be useful and the differences are irrelevant. The MACD however is way off. This have something to do with the way the tradebar is consolidated?
Vladimir
David Eldringhoff,
I would try to help you, but what about my previous answer to your similar question?
David E
Vlad, thanks for your suggestion to the other post. I hadn't seen it yet because the correction the guy made prior to your post had fixed the problem for my RSI. Does your consolidated bars correctly run a MACD with accurate MACD measurements? I'll try to code up your RSI consolidator with MACD in place and see if the numbers are closer to correct or more like my incorrect numbers.
Fred Painchaud
Hi David,
From my day work, I have a tendency not to change people's code too much so they learn one thing at a time. I would suggest you look at Vladimir's code as you have many points to learn from it…
In the meantime, the reason why your MACD is way off is that you register it twice. When you call self.MACD("QQQ", 12, 26, 9, MovingAverageType.Exponential), that's the helper method so here MACD gets registered and everything. Then, you register again with the consolidator here self.RegisterIndicator("QQQ", self.qqqMACD, fiveMinuteConsolidator). Instead of using self.MACD, use MovingAverageConvergenceDivergence(…). That way, it won't get registered right away and you will be able to register it later with your consolidator in your second call.
Cheers,
Fred
David E
Fred and Vlad,
Thanks for both of you guys! Saved me a lot of trouble and now I have accurate data on cross overs for any time period I want for bar length.
Much appreciated!
David
David E
So at least I can get accurate data. However, now I can’t buy any stocks. If I try to just set holdings for QQQ under either the Bar handler or on OnData, it flags an error.
Fred Painchaud
Hi David,
Sorry to read that. Would you please elaborate on the error you get? And maybe post the latest version of your code?
Fred
Vovik
David Eldringhoff,
The best way to get help quickly is to attach a backtest.
Fred Painchaud
Hi Vovik,
There's no backtest if there's an error.
Fred
David E
Yeah I would have attached a backtest but it wouldn't run at all if I had an OnData section in my code. If I use OnData to to make a purchase it runs a loop and fails. Error:
Algorithm.Initialize() Error: Execution Security Error: Operation timed out - 1.5 minutes max. Check for recursive loops. Stack Trace: Execution Security Error: Operation timed out - 1.5 minutes max. Check for recursive loops.
Not sure why running this consolidator doesn't let me trade like I would in any of my other algorithms. I'm not a proficient coder but I've also been working on algos for over 5 years and have a successful live algo for both crypto and equities. One is over 1000 lines of code. Then I try to do something seemingly simple like this and its a mess and I cant figure it out. Keep loading this into other working algo and vise versa and things just don't jive. Frustrating… lol
If I try to purchase QQQ in the defined bar handler, if RSI is sloping upwards, it does the following error on the backtest:
Runtime Error: TypeError : Cannot get managed object
at thirtyMinuteBarHandler
if not self.Portfolio["QQQ"].Invested and qqqrsislope > 0:
===
at Python.Runtime.Dispatcher.TrueDispatch(Object[] args)
at Python.Runtime.Dispatcher.Dispatch(Object[] args)
at __System_EventHandler`1[[QuantConnect_Data_Market_TradeBar in main.py: line 58
Vladimir
David Eldringhoff,
I found two mistakes in your code:
line 32
replaceself.qqqMACD = MovingAverageConvergenceDivergence("QQQ", FAST, SLOW, 9, MovingAverageType.Exponential)
by
self.qqqMACD = MovingAverageConvergenceDivergence(FAST, SLOW, 9, MovingAverageType.Exponential)
line 54
replace
qqqrsislope = self.rsi_slope
by
qqqrsislope = self.rsi_slope.Current.Value
If you are satisfied with my answer, please accept it my and previous answer.
David E
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!