I have seen several examples/discussions that broach this topic, but none that completely capture what I am trying to achieve.
This includes: EmaCrossUniverseSelectionAlgorithm.py, MultipleSymbolConsolidationAlgorithm.py, and the 200-50 EMA Momentum Universe bootcamp.
I would like to create a Coarse/Fine selection universe that picks a finite number of Equities, and using RollingWindows (or History although this may be slower) calculates the fast/slow EMA of each symbol from the current minute and previous several minutes, stores the EMA values into a RollingWindow or multiple RollingWindows. From there, I would apply trading logic to execute orders such as an EMA Cross/EMA Spread.
The resolution of the strategy needs to be in minutes but the Universe could update on a daily basis.
Is any of this possible to code in QuantConnect? I have attempted to store EMA 50 and EMA 200 into two separate RollingWindows using a dictionary however the RollingWindows seem to copy each other and the values being stored seems to be the Equity Price and not the EMA values. The complexity of this code is exceeding my humble python abilities.
If anyone is able to provide solutions/suggestions in Python I would appreciate it.
CAPOCAPITAL
The other detail I'm beginning to realize is the limitations behind defining and calculating indicators outside of Initialize and OnData functions. My EMA calculation in SymbolData class outputs zeros.
Derek Melchin
Hi CAPOCAPITAL,
I attached a backtest which fixes the following errors in the original algorithm:
I left the trading signal logic to be implemented, but now the algorithm is setup as envisioned. I recommend reviewing our Indicators documentation to acquire a solid understanding of the changes we made here. Furthermore, while the trading logic is being crafted, consider modifying the code to adhere to the Algorithm Framework. We have great documentation and a Bootcamp lesson which explain it and it's benefits.
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.
CAPOCAPITAL
Derek,
THANK YOU - This algorithm setup is exactly what I was trying to achieve. The changes make sense to me, I will study the history function being used to "warm up" the values in addition to the other tips mentioned.
The issue i'm encountering now is that the algorithm only appears to execute trades at 09:31 hrs (at market open). Usually, this type of strategy should generate dozens of buy/sell signals each day at Minute resolution. When I run live debugging I am able to see the code looping inside the OnData function throughout the Market hours. The sell logic doesn't seem to execute at all, and I believe something is preventing the orders from executing after 09:31 hrs. As a separate issue, I have been comparing the calculated EMA values to TradingView and am getting a small offset, which I need to investigate further.
Derek Melchin
Hi CAPOCAPITAL,
It is correct that the scheduled event related to universe selection were redundant. As was pointed out, universe selection occurs at midnight every day, regardless of the resolution of data we set. Our universe selection documentation and bootcamp highlight this concept well.
The reason the algorithm above only executes its buy orders at 9:31AM is because I accidentally forgot to update the EMA rolling windows with the algorithm I initially published to this thread. Without updating the rolling windows, the rolling window EMA values used in the buy/sell conditions never changed after the SymbolData object was constructed. This caused the buy/sell condition for each security to remain the same throughout the security's existence in the universe.
I've attached a new algorithm which fixes this issue. Specifically, inside OnData, we just need to add
# Ensure indicators are ready to update rolling windows if not self.indicators[symbol].slow_ema.IsReady: continue # Update EMA rolling windows self.indicators[symbol].fast_ema_window.Add(self.indicators[symbol].get_fast_EMA()) self.indicators[symbol].slow_ema_window.Add(self.indicators[symbol].get_slow_EMA())
With this small alteration to the code, we see the algo can place buy and sell orders for the same security throughout its time in the universe.
In regards to the EMA calculation, consult our source code for implementation-specific details.
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.
CAPOCAPITAL
Derek - Thanks again. Working as expected. Really appreciate your help with fixing the code.
CAPOCAPITAL
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!