Using universe selection and indicators together

I'm looking at the documentation for use of indicators, and it's well put together and straight forward, thanks. The indicator should be setup in initialize (using AddEquity, then self. EMA for example), and then as a second step, consumed in OnData. This I understand.

What about if I have a varying universe, for example the QC500 which I will select using universe selection, and hence not use AddEquity. Can I do the universe selection as normal, then in OnData (or any scheduled function), consume the indicator? 

I'll try to illustrate with a code snippet:

def Initialize(self):
self.UniverseSettings.Resolution = Resolution.Daily
self.AddUniverse( self.CoarseSelectionFunction, self.FineSelectionFunction)

def OnData(self, data):
self.atr = self.ATR(stocks, 200, Resolution.Daily)
# where stocks is a list of tickers that has passed through universe selection

So will this work if I never use AddEquity in Initialize, using only self.AddUniverse, then setup the indicator in OnData? I guess it's important to clarify that I'm not looking to use the indicator in Universe Selection, merely apply it afterwards, so I didn't find the transferable.

Apologies if this post isn't clear, and especially if there is already a forum post or example on this. I've searched already, but if I've missed the previous forum post, please send me the link for it.

Thanks heaps!

Update Backtest

Take a look at this sample code. While this is the alpha model of the EMA cross algorithm you've seen, note how the object is initialized in OnSecuritiesChanged. This is done after the universe selection and creates the indicators for every newly added security. The class SymbolData is can hold all variables associated with the specific symbol and it is stored in a dictionary keyed by its symbol. This implementation makes sure an indicator is not created every day like above.


Thank you Gurumeher, that is very helpful. If I'm using a monthly rebalancing strategy, with a user definined function for the rebalance, scheduled to run every month, I assume using a code similar to the link you have provided would do the job. However, would it be computationally wasteful to use OnSecuritiesChanged, which for a universe of daily resolution, I would assume would run once a day, when I only need a monthly (not daily) rebalance? Or would it not make much difference?

I guess what I'm trying to ask is whether it would make more sense to try and fit the initializing of the indicator object in the monthly rebalance function (which frankly seems much harder to do since, I don't have the luxury of OnSecuritiesChanged, so would have to code something myself which checked which securities in the universe are new additins since last month), or just use OnSecuritiesChanged as per the example you provided?

Thanks again for the help.


We can make the universe selection occur once a month using a simple flag. In the algorithm below the universe selection is triggered once a year with the flag self.yearly_rebalance. This way the code for SymbolData can remain in OnSecuritiesChanged.


That makes perfect sense to me thank you. Since universe selection is triggered only once every month or every year, the changes in OnSecuritiesChanged only occur once a month or once a year, hence the ability to leave the code for SymbolData in OnSecuritiesChanged. Thanks for your patience in explaining and taking the time to attach the examples.


Update Backtest


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.


This discussion is closed