Hi,
I am writing my code based on the Sector Weighted Portfolio Construction Bootcamp - I changed to select the Technology sector only and instead of MarketCap I am using RevenueGrowth to sort and select the fine universe. I am getting the error when the algorithm processed around 2K data points (not sure if this is relevant to the issue, but this is what I observed. It doesn't happen right away after running a backtest).
I only use AssetClassification in two places and I can't see why they can be None. I am new and any insight is much appreciated!
def SelectFine(self, algorithm, fine):
filtered = [f for f in fine if f and f.AssetClassification.MorningstarSectorCode == MorningstarSectorCode.Technology]
self.technology = sorted(filtered, key=lambda f: f.OperationRatios.RevenueGrowth.ThreeMonths, reverse=True)[:10]
and
def OnSecuritiesChanged(self, algorithm, changes):
algorithm.Log("ADDED SECURITIES")
for security in changes.AddedSecurities:
if security.Fundamentals:
print_stock(algorithm, security)
sectorCode = security.Fundamentals.AssetClassification.MorningstarSectorCode
if sectorCode not in self.symbolBySectorCode:
self.symbolBySectorCode[sectorCode] = list()
self.symbolBySectorCode[sectorCode].append(security.Symbol)
algorithm.Log("REMOVED SECURITIES")
for security in changes.RemovedSecurities:
print_stock(algorithm, security)
algorithm.Log(security.Fundamentals.AssetClassification.MorningstarSectorCode)
if security.Fundamentals:
sectorCode = security.Fundamentals.AssetClassification.MorningstarSectorCode
if sectorCode in self.symbolBySectorCode:
symbol = security.Symbol
if symbol in self.symbolBySectorCode[sectorCode]:
self.symbolBySectorCode[sectorCode].remove(symbol)
Shile Wen
Hi Dung,
I tried the code posted, but I wasn't able to reproduce the error. Please post the entire code, it will help us to identify the problem.
Best,
Shile Wen
Dung Le
Thank you, Shile. Please find the code below:
from datetime import timedelta from QuantConnect.Data.UniverseSelection import * from Selection.FundamentalUniverseSelectionModel import FundamentalUniverseSelectionModel class SectorBalancedPortfolioConstruction(QCAlgorithm): def Initialize(self): self.SetStartDate(2003, 1, 1) self.SetEndDate(2020, 6, 6) self.SetCash(100000) self.UniverseSettings.Resolution = Resolution.Hour self.SetUniverseSelection(MyUniverseSelectionModel()) self.SetAlpha(ConstantAlphaModel(InsightType.Price, InsightDirection.Up, timedelta(1), 0.025, None)) self.SetPortfolioConstruction(MySectorWeightingPortfolioConstructionModel()) self.SetExecution(ImmediateExecutionModel()) class MyUniverseSelectionModel(FundamentalUniverseSelectionModel): def __init__(self): super().__init__(True, None, None) def SelectCoarse(self, algorithm, coarse): filtered = [x for x in coarse if x.HasFundamentalData and x.Price > 0] sortedByDollarVolume = sorted(filtered, key=lambda x: x.DollarVolume, reverse=True) return [x.Symbol for x in sortedByDollarVolume] def SelectFine(self, algorithm, fine): filtered_fine = [x for x in fine if x.AssetClassification and x.OperationRatios.ROIC.ThreeMonths and x.ValuationRatios.CashReturn] algorithm.Debug('remained to select %d'%(len(filtered_fine))) # rank stocks by three factor. sortedByfactor1 = sorted(filtered_fine, key=lambda x: x.OperationRatios.ROIC.ThreeMonths, reverse=True) sortedByfactor2 = sorted(filtered_fine, key=lambda x: x.ValuationRatios.CashReturn, reverse=True) sortedByfactor3 = sorted(filtered_fine, key=lambda x: x.OperationRatios.LongTermDebtEquityRatio.ThreeMonths) stock_dict = {} # assign a score to each stock, you can also change the rule of scoring here. for i,ele in enumerate(sortedByfactor1): rank1 = i rank2 = sortedByfactor2.index(ele) rank3 = sortedByfactor3.index(ele) score = sum([rank1 * 0.5, rank2 * 0.25 , rank3 * 0.25]) stock_dict[ele] = score # sort the stocks by their scores self.sorted_stock = sorted(stock_dict.items(), key=lambda d:d[1], reverse=False) sorted_symbol = [x[0] for x in self.sorted_stock] self.technology = [x for x in sorted_symbol if x.AssetClassification.MorningstarSectorCode == MorningstarSectorCode.Technology] if len(self.technology) > 5: self.technology = self.technology[:5] self.consumerCyclical = [x for x in sorted_symbol if x.AssetClassification.MorningstarSectorCode == MorningstarSectorCode.ConsumerCyclical] if len(self.consumerCyclical) > 5: self.consumerCyclical = self.consumerCyclical[:5] self.healthcare = [x for x in sorted_symbol if x.AssetClassification.MorningstarSectorCode == MorningstarSectorCode.Healthcare] if len(self.healthcare) > 5: self.healthcare = self.healthcare[:5] self.industrials = [x for x in sorted_symbol if x.AssetClassification.MorningstarSectorCode == MorningstarSectorCode.Industrials] if len(self.industrials) > 5: self.industrials = self.industrials[:5] self.basicMaterials = [x for x in sorted_symbol if x.AssetClassification.MorningstarSectorCode == MorningstarSectorCode.BasicMaterials] if len(self.basicMaterials) > 5: self.basicMaterials = self.basicMaterials[:5] algorithm.Debug(f"SELECT {len(self.technology)} Technology, {len(self.consumerCyclical)} Consumer cyclical, {len(self.healthcare)} Healthcare, {len(self.industrials)} Industrials, {len(self.basicMaterials)} Basic materials") for technology in self.technology: algorithm.Log(f"{str(technology.Symbol)} ROIC {str(technology.OperationRatios.ROIC.ThreeMonths)} CashReturn {str(technology.ValuationRatios.CashReturn)} LTD-E Ratio {str(technology.OperationRatios.LongTermDebtEquityRatio.ThreeMonths)}") for consumerCyclical in self.consumerCyclical: algorithm.Log(f"{str(consumerCyclical.Symbol)} ROIC {str(consumerCyclical.OperationRatios.ROIC.ThreeMonths)} CashReturn {str(consumerCyclical.ValuationRatios.CashReturn)} LTD-E Ratio {str(consumerCyclical.OperationRatios.LongTermDebtEquityRatio.ThreeMonths)}") for healthcare in self.healthcare: algorithm.Log(f"{str(healthcare.Symbol)} ROIC {str(healthcare.OperationRatios.ROIC.ThreeMonths)} CashReturn {str(healthcare.ValuationRatios.CashReturn)} LTD-E Ratio {str(healthcare.OperationRatios.LongTermDebtEquityRatio.ThreeMonths)}") for industrials in self.industrials: algorithm.Log(f"{str(industrials.Symbol)} ROIC {str(industrials.OperationRatios.ROIC.ThreeMonths)} CashReturn {str(industrials.ValuationRatios.CashReturn)} LTD-E Ratio {str(industrials.OperationRatios.LongTermDebtEquityRatio.ThreeMonths)}") for basicMaterials in self.basicMaterials: algorithm.Log(f"{str(basicMaterials.Symbol)} ROIC {str(basicMaterials.OperationRatios.ROIC.ThreeMonths)} CashReturn {str(basicMaterials.ValuationRatios.CashReturn)} LTD-E Ratio {str(basicMaterials.OperationRatios.LongTermDebtEquityRatio.ThreeMonths)}") return [x.Symbol for x in self.technology + self.consumerCyclical + self.healthcare + self.industrials + self.basicMaterials] class MySectorWeightingPortfolioConstructionModel(EqualWeightingPortfolioConstructionModel): def __init__(self, rebalance = Resolution.Daily): super().__init__(rebalance) self.symbolBySectorCode = dict() self.result = dict() def DetermineTargetPercent(self, activeInsights): #1. Set the self.sectorBuyingPower before by dividing one by the length of self.symbolBySectorCode self.sectorBuyingPower = 1 / len(self.symbolBySectorCode) for sector, symbols in self.symbolBySectorCode.items(): #2. Search for the active insights in this sector. Save the variable self.insightsInSector self.insightsInSector = [insight for insight in activeInsights if insight.Symbol in symbols] #3. Divide the self.sectorBuyingPower by the length of self.insightsInSector to calculate the variable percent # The percent is the weight we'll assign the direction of the insight self.percent = self.sectorBuyingPower / len(self.insightsInSector) #4. For each insight in self.insightsInSector, assign each insight an allocation. # The allocation is calculated by multiplying the insight direction by the self.percent for insight in self.insightsInSector: self.result[insight] = insight.Direction * self.percent return self.result def OnSecuritiesChanged(self, algorithm, changes): for security in changes.AddedSecurities: algorithm.Log(f"Remove {security.Symbol}") if security.Fundamentals: algorithm.Log(f"{security.Symbol} has fundamentals") sectorCode = security.Fundamentals.AssetClassification.MorningstarSectorCode if sectorCode not in self.symbolBySectorCode: self.symbolBySectorCode[sectorCode] = list() self.symbolBySectorCode[sectorCode].append(security.Symbol) for security in changes.RemovedSecurities: algorithm.Log(f"Remove {security.Symbol}") if security.Fundamentals: algorithm.Log(f"{security.Symbol} has fundamentals") sectorCode = security.Fundamentals.AssetClassification.MorningstarSectorCode if sectorCode in self.symbolBySectorCode: symbol = security.Symbol if symbol in self.symbolBySectorCode[sectorCode]: self.symbolBySectorCode[sectorCode].remove(symbol) super().OnSecuritiesChanged(algorithm, changes)
Alexandre Catarino
Hi Dung Le ,
Sorry about the wait.
When the algorithm adds the Security, it uses the data from the universe selection to update the Security.Fundamentals attribute while it may update the Security.Fundamentals with None/null when the Security is removed.
The AttributeError is raised when the algorithm removes the Security.
I would recommend using the following logic in the MySectorWeightingPortfolioConstructionModel.OnSecuritiesChanged:
def OnSecuritiesChanged(self, algorithm, changes): for security in changes.AddedSecurities: algorithm.Log(f"Remove {security.Symbol}") if security.Fundamentals is None: raise AttributeError(f"{security.Symbol} has fundamentals") sectorCode = security.Fundamentals.AssetClassification.MorningstarSectorCode if sectorCode not in self.symbolBySectorCode: self.symbolBySectorCode[sectorCode] = list() self.symbolBySectorCode[sectorCode].append(security.Symbol) sectorsToRemove = [] for security in changes.RemovedSecurities: algorithm.Log(f"Remove {security.Symbol}") if security.Fundamentals: algorithm.Log(f"{security.Symbol} has fundamentals") for sectorCode, symbols in self.symbolBySectorCode.items(): if security.Symbol in symbols: symbols.remove(security.Symbol) if not symbols: sectorsToRemove.append(sectorCode) for sectorCode in sectorsToRemove: self.symbolBySectorCode.pop(sectorCode) super().OnSecuritiesChanged(algorithm, changes)
It will raise handled AttributeError if there are no Fundamentals when the algorithm adds a Security because it shouldn't happen, it is a bug. While it won't use Fundamentals to remove the Symbol from the symbolBySectorCode dictionary.
Dung Le
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!