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)
Derek Melchin
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
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.
Kevin Baker
I was able to complete this task step for Tracking Security Changes
by finding a post with this line of code:
for sectorCode, symbols in self.symbolBySectorCode.items():
https://www.quantconnect.com/forum/discussion/8649/attributeerror-039-nonetype-039-object-has-no-attribute-039-assetclassification-039/p1Karl H
Hi! I'm having a similar problem, though with a different error message, and can't finish the task. I'm getting the error “Looks like you haven't saved the changes as a variable.” I've checked and double-checked and compared my solution to Jared's in the video. Not sure what else to do. Thanks!
My code:
namespace QuantConnect
{
public partial class BootCampTask : QCAlgorithm
{
private IEnumerable<Symbol> filteredByPrice;
private SecurityChanges _changes = SecurityChanges.None;
public override void Initialize()
{
SetStartDate(2019, 1, 11);
SetEndDate(2019, 7, 1);
SetCash(100000);
AddUniverse(CoarseSelectionFilter);
}
public IEnumerable<Symbol> CoarseSelectionFilter(IEnumerable<CoarseFundamental> coarse)
{
var sortedByDollarVolume = coarse.OrderByDescending(x => x.DollarVolume);
filteredByPrice = sortedByDollarVolume.Where(x => x.Price > 10).Select(x => x.Symbol);
filteredByPrice = filteredByPrice.Take(8);
return filteredByPrice;
}
//1. Create a function OnSecuritiesChanged
public void OnSecuritiesChanged(SecurityChanges changes){
//2. Monitor for securities changed in the function
_changes = changes;
//3. Log the changes in the function
Log($"OnSecuritiesChanged({Time}):: {changes}");
}
}
}
Varad Kabade
Hi Karl Hoaglund,
We cannot reproduce this problem. This might be due to previous unsolved data issue. It is normally working now.
Best,
Varad Kabade
Karl H
Thanks, Varad. I appreciate your reply.
Strange. . . I'm still getting the error. (image attached).
It's not a big deal. I found that I can leave this lesson undone and proceed to the next ones.
William Godfrey
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!