Hi, I am super new to Quantconnect Algorithms and was trying to update EMA bootcamp tutorial code such that it will automatically calculate slow(200) and fast(50) EMA values instead of manual updates. Code I am trying is:
def CoarseSelectionFunction(self, universe):
#initial code above
# Create loop to use all the coarse data
for coarse in universe:
symbol = coarse.Symbol
#2. Check if we've created an instance of SelectionData for this symbol
if symbol not in self.averages and symbol is not None:
#3. Create a new instance of SelectionData and save to averages[symbol]
self.averages[symbol] = SelectionData(symbol,self.EMA(symbol,200,Resolution.Daily),self.EMA(symbol,50,Resolution.Daily))
# above line throws error
# remaining code below
When I try to directly call self.EMA(symbol…) I am getting an error :
Runtime Error: Please register to receive data for symbol 'SPY R735QTJ8XC9X' using the AddSecurity() function. in Indicators.cs:line 2448
Do I need to add all securities explicitly even after having them in Universe section functions?
Vladimir
Log Up
Here is my attempt at what you are trying to do in your algorithm by initializing the indicators in OnSecuritiesChanged().
If you are satisfied with my answer, please accept it and don't forget to like it.
Log Up
Hi Vladimir ,Thank you for the reply. I saw the code you mentioned, but it seems it initializes the indicators when we get the changed securities right?(Please correct me if I am wrong here). What I want to achieve is to filter out top 10 stocks which has highest momentum of 50 EMA above 200 EMA. For that I need to have indicators in CoarseSelectionFunction to sort the securities.
Vladimir
Log Up
In my code, the algorithm selects stocks with the largest dollar volume in CoarseSelectionFunction(),Â
then initializes self.ema_f and self.ema_s in OnSecuritiesChanged(), and selects stocks with positive EMA momentum (self.ema_f > self.ema_s) in OnData().Â
What is not in line with your original request to directly call self.EMA(symbol)?
Log Up
Hi Vladimir, I am attempting to initialize EMA while in CoarseSelectionFunction() itself to prevent unnecessary additions of securities which will be called on OnData(), but it seems the security is not added into algorithm(hence no history for indicator also) while in CoarseSelectionFunction(), needing to call addSecurity() explicitly. Is there any way we can only have securities in algorithm which pass ema_f>ema_s at the time of universe selection itself?Â
Log Up
Vladimir because I don't only want ema_f>ema_s , I want top n which has highest ema_f>ema_s
Yuri Lopukhov
You can request history for all securities in CoarseSelectionFunction, use this data to calculate EMAs, sort and choose top symbols.Â
Direct answer to your question would be that yes, in order for automatic EMA calculation, you need to have security added to the universe, otherwise there will be no data sent to the indicator function. But it wouldn't be optimal processing all stocks minute data only to calculate daily EMAs, that is why I recommend using History API.
Log Up
Hi Yuri Lopukhov , Thanks for the reply. Now when you confirm it, I got it. I am processing the universe on daily resolution hence was thinking of calculating indicator in CoarseSelectionFunction. By History API can you please tell me if you mean something like below? If yes, then it seems here also “SPY” has been first added into algorithm to populate indicator right?
Vladimir
Log Up
Here is “top dollar volume top moving average ratio universe” based on self.History() in CoarseSelectionFunction().
If you are satisfied with my answer, please accept it and don't forget to like it.
Fred Painchaud
Hi Log Up,
To sum up:
Â
Fred
Vladimir
Log Up
-→I want top n which has highest ema_f>ema_s
Here is “Top Dollar Volume Top EMAR universe” which selects symbols exactly as you requested.
If you are satisfied with my answer, please accept it and don't forget to like it.
Log Up
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!