Back

Asset not in security list, despite adding it through CoarseUniverse function

I am selecting a Universe of 20 stocks using the CoarseUniverse function. When I later try to get the price of one of these assets, I get the error 

The error was Exception : This asset symbol (AAPL R735QTJ8XC9X) was not found in your security list. Please add this security or check it exists before using it with 'Securities.ContainsKey("AAPL R735QTJ8XC9X")

What did I do wrong? Here is the complete code:

class Algorithm(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2012,1,1)
self.SetEndDate(2012,2,5)
self.SetCash(100000)
self.UniverseSettings.Resolution = Resolution.Daily
self.AddUniverse(self.CoarseSelectionFunction)
self.AddEquity('SPY', Resolution.Daily)
self.Schedule.On(self.DateRules.MonthStart("SPY"),
self.TimeRules.AfterMarketOpen("SPY", 60),
Action(self.Rebalance))



def CoarseSelectionFunction(self, coarse):
CoarseWithFundamental = [x for x in coarse if x.HasFundamentalData]
sortedByDollarVolume = sorted(CoarseWithFundamental, key=lambda x: x.DollarVolume, reverse=True)
self.universe = [x.Symbol for x in sortedByDollarVolume[:20]]
return self.universe


def Rebalance(self):
self.Securities[self.universe[0]].Price
Update Backtest







When creating an algorithm using AddUniverse(), you want to track the removed securities and added securities and execute actions on them. In the attached backtest below the above code was taken and rearranged to follow this protocol. The Debug() statements print out what is trying to be attempted above. I recommend cloning the backtest and using this as the structure for future algorithms. The UniverseManager will also help find symbols from the universe selection.  The documentation on Universes and this sample code should be useful. 

1


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