Hi Everyone,
Attached is the code that I use. I guess the title of this discussion says it all but im really having a hard time adding securities on the fly and then retreiving data. All I could find in the documentation and looking at other discussions are codes that only add a few securities in the INITIALIZE function but nothing in custom function.
THe code returns this error:
Backtest Handled Error: The order quantity for JNJ cannot be calculated: the price of the security is zero.
Also, all current prices returned by the Debug function return a price of $0.
Side question: Why is this code so slow ?! When I run it for a longer period, it takes hours to run. Does it have to do with "
self.UniverseSettings.Resolution = Resolution.Minute"if so, I would gladly put a daily resolution but will I be able to run the code 1 and 2 minutes after open ?
Thanks in advance.
Jack Simonson
Hi Stephane,
When you use Universe Selection, the algorithm automatically subscribes to all of the stocks selected and so there is no need to do so manually, such as in the following snippet in the setup_trade() method in the algorithm you posted.
for stock in self.my_universe: Â Â stockID = self.AddEquity(stock.Value, Resolution.Minute) Â Â stockID.SetDataNormalizationMode(DataNormalizationMode.Raw)
So any new securities will be added via the Universe Selection and automatically subscribed to, but these new securities can be accessed in the OnSecuritiesChanged(self, changes) method as seen here.
Additionally, to set the Data Normalization Mode to Raw when using Universe Selection the algorithm must employ the Security Initializer method, an example of which you can find here. This method allows you to set the Brokerage Model, Data Normalization Mode, and other Security attributes that are required by the algorithm.
As for why the prices are being reported as 0, this can be fixed by deleting the lines of code I've mentioned above and then using this for the my_stocks() method instead
Â
def my_stocks(self):   self.Debug("%%%%%%%%%%%%% my_stocks {}".format(self.Time))      ## With no more self.addedStocks list, the stock symbols can be accessed   ## via the self.Securities dictionary keys instead   for stockID in self.Securities.Keys:     if self.Securities[str(stockID)].Price != 0:       self.stock_list.append(str(stockID))     self.Debug(str(stockID))     self.Debug(self.Securities[str(stockID)].Price)
As for the speed of the algorithm, you can look into Scheduled Universe Selection to limit the number of times Universe Selection needs to occur if having it happen every minute is not necessary.
Stephane b
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!