Back

Universe (Coarse/Fine Selection) with past Indicators values indexed in RollingWindow?

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.

Update Backtest








0

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.


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.

0

Hi CAPOCAPITAL,

I attached a backtest which fixes the following errors in the original algorithm:

  • The setting of MinimumTimeInUniverse and the explicit call to SetWarmUp were removed as we can ensure each security has a 200 day history to calculate the slow EMA by warming up the indicator during construction.
  • The fast/slow EMA rolling windows were removed from the QCAlgorithm object as these should reside inside each SymbolData object.
  • OnSecuritiesChanged was extended to add a SymbolData object to the indicators dictionary for any securities new to our universe. This allows us to remove the first for-loop inside OnData.
  • The consolidators were removed as the indicators' implicit data feed made them redundant.

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

0

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.


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.

  • I have tweaked the code to add some basic EMA crossover/crossunder logic, using Minute resolution for Universe and History.
  • I've also added some logic in OnData to prevent errors with symbols that don't have any data.
  • In addition, I have added some Schedule Events + logic to ensure the Universe triggers once a day, but this code may be redundant since I now understand that the Universe should only trigger at 0:00 every day.

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.

0


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

1

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.


Derek - Thanks again. Working as expected. Really appreciate your help with fixing the code.

0

Update Backtest





0

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.


Loading...

This discussion is closed