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]
Louis Szeto
Hi Axist
It is possible to have multiple universes in an algorithm. We recommend saving the entire universe cache, then retrieve the symbol objects by .Members.Keys. Please find the attached backtest as an example.
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
Reading conflicting things on whether its possible to use multiple Coarse/Fine filters with AddUniverse. Is it true that AddUniverse can only be used once? Something like this isnt possible?
If its not possible to filter stocks into variables or “buckets” based on their sector they're associated in by using x.AssetClassification.MorningstarSectorCode == MorningstarSectorCode.Healthcare or x.AssetClassification.MorningstarSectorCode == MorningstarSectorCode.Energy is there any other method of accomplishing something similar using a single coarse/fine filter combonation?
Louis Szeto
Hi Axist
That would be possible using the same trick. Please refer to the attached backtest.
Best
Louis
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!