Is it possible to pick the top 2 stocks of each sector and store each of the two stocks into its own seperate variable?  So the idea is to use something like 

For each of the following:

self.XLF = self.AddEquity('XLF', Resolution.Hour).Symbol
self.XLE = self.AddEquity('XLE', Resolution.Hour).Symbol
self.XLB = self.AddEquity('XLB', Resolution.Hour).Symbol
self.XLI = self.AddEquity('XLI', Resolution.Hour).Symbol
self.XLY = self.AddEquity('XLY', Resolution.Hour).Symbol
self.XLP = self.AddEquity('XLP', Resolution.Hour).Symbol
self.XLU = self.AddEquity('XLU', Resolution.Hour).Symbol
self.XLK = self.AddEquity('XLK', Resolution.Hour).Symbol
self.XLV = self.AddEquity('XLV', Resolution.Hour).Symbol
self.XLC = self.AddEquity('XLC', Resolution.Hour).Symbol

 

I want to do something like the following to essentially assign the top 2 individual stocks to a variable.  Something like this to get the two stocks by sector:

def Fine(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 = True)[:self.exposureToSector]:
               filteredSymbols.append(x.Symbol)
           
       return filteredSymbols[:2]

 

But I'd want this running continually, each updating their own sector variable.  I understand the below example uses IndustryTemplateCode and theres only 6 of them, which doesnt match to what MorningStar provides

utility = filter(fine, key=lambda x: x.CompanyReference.IndustryTemplateCode = 'U')
techonology = filter(fine, key=lambda x: x.CompanyReference.IndustryTemplateCode = 'B')

 

The idea would then be that I could select stocks based from the sector I chose. Effectively combining it with another algorithm found from these forums, the KEI framework as seen in the attached backtest 

so for example, from the attached backtest, Instead of doing SetHoldings on self.XLK or self.XLV it would be on the variable containing the list of stocks for that given sector.

elif (keimom < 0 and kei < keihisthight and kei > keihistmidt) and not (self.Securities[self.XLK].Invested):
           # LATE
           self.Liquidate()
           self.SetHoldings(self.XLK, .5)
           self.SetHoldings(self.XLV, .5)
           self.Debug("INFO TECH {0} >> {1}".format(self.XLK, self.Time))

 

Please let me know if this question makes sense or if I should clarify anything.