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.
Louis Szeto
Hi Axist
Please refer to this bootcamp tutorial for a similar implementation.
Best
Louis Szeto
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.
Axist
Thanks for that, it actually was pretty much spot on what I was looking for. Issue is though I dont think the tickers the Coarse and Fine are populating to their individual self.variables are properly getting added?Despite using a variation of the code from the bootcamp, I keep getting the following error:
Louis Szeto
Hi Axist
Note that the ETFs that have been manually added in the .initialize method have no Fundamentals nor sector code. Thus, it is necessary to exclude the fetch of sector code from them in the .AddedSecurities attribute.
Best
Louis Szeto
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.
Axist
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.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!