I'm looking to do something like this where I have multiple Coarse selectors assigning tickers to their own array/list, but I suspect its not properly functioning and backtests are extremely lengthy.  Is this the correct way this should be done?

 

The universe selector:

def Initialize(self):
...
self.XLFsector = []
self.XLEsector = []

self.AddUniverse(self.XLFcoarse, self.XLFfine)
self.AddUniverse(self.XLEcoarse, self.XLEfine)
self.numberOfSymbolsCoarse = 500
self.lastMonth = -1

 

An example:



  def XLFcoarse(self, coarse):
       if self.Time.day == self.lastMonth:
           return Universe.Unchanged
       self.lastMonth = self.Time.day
       
       allCoarse = [x for x in coarse if x.HasFundamentalData and x.Price > 50 and x.Volume > 200000]
       finalCoarse = sorted(allCoarse, key = lambda x: x.DollarVolume, reverse = True)
       
       return [x.Symbol for x in finalCoarse][:self.numberOfSymbolsCoarse]
       #return self.tickers
   
   def XLFfine(self, fine):
       filteredSymbols = []
       sortedBySector = [x for x in fine]
       for code, g in groupby(sortedBySector, lambda x: x.AssetClassification.MorningstarSectorCode):
           for x in sorted(g, key = lambda x: x.ValuationRatios.PERatio, reverse = False)[:2]:
               filteredSymbols.append(x.Symbol)
       self.XLFsector = filteredSymbols
   
       return filteredSymbols[:3]