class TrialHighalgorithm(QCAlgorithm): def Initialize(self): self.SetStartDate(2020, 11, 1) #self.SetEndDate(2020, 11, 11) self.SetCash(10000) self.numberOfSymbols = 200 self.numberOfSymbolsFine = 10 self.res = Resolution.Minute self.averages = {} self.symbols = [] self.sma = {} self.selectedSymbols = [] self.SPY = self.AddEquity('SPY', self.res).Symbol self.Securities["SPY"].SetDataNormalizationMode(DataNormalizationMode.Raw) self.SetTimeZone(TimeZones.HongKong) self.SetBrokerageModel(BrokerageName.InteractiveBrokersBrokerage, AccountType.Margin) self.AddUniverse(self.CoarseSelectionFunction, self.FineSelectionFunction) def CoarseSelectionFunction(self, coarse): filtered = [ x for x in coarse if (x.HasFundamentalData) & (x.Price > 1) & ( x.Volume > 500000) ] filtered.sort(key=lambda x: x.DollarVolume, reverse=True) self.symbols = [ x.Symbol for x in filtered[:self.numberOfSymbols] ] return self.symbols def FineSelectionFunction(self, fine): symbols = [x.Symbol for x in fine] history = self.History(symbols, 252, Resolution.Daily) for symbol in symbols: if str(symbol) in history.index.get_level_values(0): symbolHistory = history.loc[str(symbol)] if not symbolHistory.empty: symbolSMA = SimpleMovingAverage(200) for tuple in symbolHistory.itertuples(): bar = TradeBar(tuple.Index, symbol, tuple.open, tuple.high, tuple.low, tuple.close, tuple.volume) symbolSMA.Update(tuple.Index, tuple.close) self.sma[symbol] = symbolSMA else: symbols.pop(symbols.index(symbol)) self.selectedSymbols = [ x for x in symbols if (max(history.loc[str(x)]['high'][:-1]) < history.loc[str(x)]['high'][-1]) & (history.loc[str(x)]['close'][-1] > history.loc[str(x)]['open'][-1])] return self.selectedSymbols def OnData(self, data): if self.selectedSymbols is None: return for x in self.selectedSymbols: self.SetHoldings(x, 0.01)

 

I want to filter 52 weeks high stocks and make sure they close above open yesterday. I know my coding is bad. Is there any easy way to do it in the same way? It is a bit slow.

sometimes it got : max() arg is an empty sequence

Could someone help me, please? Thanks