Hi,
There seems to be a problem with the solution to this task. I wrote code but then I got an error saying 'Runtime Error: NameError : name 'symbol' is not defined NameError : name 'symbol' is not defined (Open Stacktrace)'
When comparing my code to the solution, it was identical. I then ran the code in the solution and still got the same error. Here is the code from the solution to this task:
class MySectorWeightingPortfolioConstructionModel(EqualWeightingPortfolioConstructionModel):
def __init__(self, rebalancingParam = Resolution.Daily):
super().__init__(rebalancingParam)
self.symbolBySectorCode = dict()
def OnSecuritiesChanged(self, algorithm, changes):
#1. When new assets are added to the universe, save the MorningStar sector code for each security to the variable sectorCode
for security in changes.AddedSecurities:
sectorCode = security.Fundamentals.AssetClassification.MorningstarSectorCode
# 2. If the sectorCode is not in the self.symbolBySectorCode dictionary, save the values as a list
# and append the security symbol as the value in the self.symbolBySectorCode dictionary
if sectorCode not in self.symbolBySectorCode:
self.symbolBySectorCode[sectorCode] = list()
self.symbolBySectorCode[sectorCode].append(security.Symbol)
#3. For securities that are removed, save their MorningStar sector code to sectorCode
for security in changes.RemovedSecurities:
sectorCode = security.Fundamentals.AssetClassification.MorningstarSectorCode
#4. If the saved sectorCode is in the self.symbolBySectorCode dictionary, save the security's symbol object to symbol
if sectorCode in self.symbolBySectorCode:
if symbol in self.symbolBySectorCode[sectorCode]:
self.symbolBySectorCode[sectorCode].remove(symbol)
# We use the super() function to avoid using the base class name explicity
super().OnSecuritiesChanged(algorithm, changes)