I'm having issues retreiving the opening price for a changing list of stocks. Below is a snippet of my algo;

def CoarseSelectionFunction(self, universe): self.treemodels = {} self.selected = [] self.universe = sorted(universe, key=lambda c: c.DollarVolume, reverse=True) self.universe = [c for c in self.universe if c.Price > 10][:10] for security in self.universe: symbol = security.Symbol sec_history = self.History(symbol, 45000, Resolution.Minute) try: self.treemodels[symbol] = TreeModel(sec_history) if self.treemodels[symbol].Predict() == 1: self.selected.append(symbol) except: pass return self.selected def OpeningBar(self): self.portfolio_current = [] for symbol in self.selected: open_price = self.Securities[symbol].Open volatility = self.treemodels[symbol].PreviousVolatility() close_price = self.treemodels[symbol].PreviousClose() deviation = (open_price - close_price)/volatility if deviation < -2: self.portfolio_current.append(symbol) if len(self.portfolio_current) != 0: percent = 1 / (len(self.portfolio_current)) for security in self.portfolio_current: self.SetHoldings(security,percent) def ClosePositions(self): self.Liquidate()

The code follows on from the Fading the Gap example in Boot camp but uses a tree model to decide which out of a universe to invest in.

def Initialize(self): self.SetStartDate(2020, 10, 19) self.SetEndDate(2020, 11, 27) self.SetCash(100000) self.UniverseSettings.Resolution = Resolution.Daily self.AddUniverse(self.CoarseSelectionFunction) self.AddEquity("SPY", Resolution.Minute) self.Schedule.On(self.DateRules.EveryDay(), self.TimeRules.AfterMarketOpen("SPY", 1), self.OpeningBar) self.Schedule.On(self.DateRules.EveryDay(), self.TimeRules.AfterMarketOpen("SPY", 45), self.ClosePositions)

 

In the above OpeningBar(self) for all symbol self.Securities[symbol].Open = 0.0. I then noticed that to get a value for this I have to AddEquity(symbol) for each one of these symbols. For this I put AddEquity()s inside the coarse selection as this is when the universe is determined. Below is the amended code with the AddEquity()s;

def CoarseSelectionFunction(self, universe): self.treemodels = {} self.selected = [] universe = sorted(universe, key=lambda c: c.DollarVolume, reverse=True) universe = [c for c in universe if c.Price > 10][:10] for security in universe: symbol = security.Symbol #if security not in self.Securities: self.AddEquity(symbol, Resolution.Minute) #self.totalsecurities.append(symbol) sec_history = self.History(symbol, 45000, Resolution.Minute) try: self.treemodels[symbol] = TreeModel(sec_history) if self.treemodels[symbol].Predict() == 1: self.selected.append(symbol) except: pass return self.selected def OpeningBar(self): self.portfolio_current = [] for symbol in self.selected: open_price = self.Securities[symbol].Open volatility = self.treemodels[symbol].PreviousVolatility() close_price = self.treemodels[symbol].PreviousClose() deviation = (open_price - close_price)/volatility if deviation < -2: self.portfolio_current.append(symbol) self.RemoveSecurity(symbol) if len(self.portfolio_current) != 0: percent = 1 / (len(self.portfolio_current)) for security in self.portfolio_current: self.SetHoldings(security,percent)

This however resulted in errors for each stock being untradable and nothing was traded (errors shown at the bottom). It may be a timing issue but even SPY which i used in my Initialization was untradable. Is there an easier way to get the opening prices for a changing list at a specific time (OpeningBar needs to be ran as close to opening as possible)? If not how should i approach adding the securities and why are the below stocks untradable? 

2020-10-19 00:00:00 :Launching analysis for e84f03c261b72aa99430d5d4946f7e27 with LEAN Engine v2.4.0.0.98832020-10-21 09:31:00 :The security with symbol 'NFLX' is marked as non-tradable.2020-10-26 09:31:00 :The security with symbol 'TSLA' is marked as non-tradable.2020-10-26 09:31:00 :The security with symbol 'INTC' is marked as non-tradable.2020-10-26 09:31:00 :The security with symbol 'MSFT' is marked as non-tradable.2020-10-28 09:31:00 :The security with symbol 'SPY' is marked as non-tradable.2020-10-28 09:31:00 :The security with symbol 'AAPL' is marked as non-tradable.2020-10-28 09:31:00 :The security with symbol 'TSLA' is marked as non-tradable.2020-10-28 09:31:00 :The security with symbol 'FB' is marked as non-tradable.2020-10-30 09:31:00 :The security with symbol 'AAPL' is marked as non-tradable.