Back

Tracking Security Changes - Task 198

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)

Update Backtest







0

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.


Hi William,

Sorry about this one. This is a new bootcamp lesson and there is a small typo in the solution file. Lines 58-59 should read

if security.Symbol in self.symbolBySectorCode[sectorCode]:
self.symbolBySectorCode[sectorCode].remove(security.Symbol)

I've reported this issue to the team and we'll have it fixed soon.

Best,
Derek Melchin

0

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.


Update Backtest





0

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.


Loading...

This discussion is closed