Hi,

I can't finish the task "Tracking Security Changes", because of following error message:

Runtime Error: Error running OnEndOfAlgorithm(): Object reference not set to an instance of an object

 

I tried really hard to find the problem, but now I run out of ideas

Code (I'm not able to add it via backtest);

using System.Reflection; namespace QuantConnect { public partial class BootCampTask : QCAlgorithm { public override void Initialize() { SetStartDate(2016, 12, 28); SetEndDate(2017, 3, 1); SetCash(100000); UniverseSettings.Resolution = Resolution.Hour; SetUniverseSelection(new MyUniverseSelectionModel()); SetAlpha(new ConstantAlphaModel(InsightType.Price, InsightDirection.Up, TimeSpan.FromDays(1), 0.025, null)); SetPortfolioConstruction(new SectorWeightingPortfolioConstructionModel(Resolution.Daily)); SetExecution(new ImmediateExecutionModel()); } } public class MyUniverseSelectionModel : FundamentalUniverseSelectionModel { public MyUniverseSelectionModel() : base(true) { } public override IEnumerable<Symbol> SelectCoarse(QCAlgorithm algorithm, IEnumerable<CoarseFundamental> coarse) { return (from c in coarse where c.HasFundamentalData && c.Price > 0 orderby c.DollarVolume descending select c.Symbol).Take(100); } public override IEnumerable<Symbol> SelectFine(QCAlgorithm algorithm, IEnumerable<FineFundamental> fine) { var technology = new List<FineFundamental>(); technology.AddRange( (from f in fine where f.AssetClassification.MorningstarSectorCode == MorningstarSectorCode.Technology orderby f.MarketCap descending select f).Take(3) ); var financialServices = new List<FineFundamental>(); financialServices.AddRange( (from f in fine where f.AssetClassification.MorningstarSectorCode == MorningstarSectorCode.FinancialServices orderby f.MarketCap descending select f).Take(2) ); var consumerDefensive = new List<FineFundamental>(); consumerDefensive.AddRange( (from f in fine where f.AssetClassification.MorningstarSectorCode == MorningstarSectorCode.ConsumerDefensive orderby f.MarketCap descending select f).Take(1) ); var selection = new List<Symbol>(); selection.AddRange(technology.Select(f => f.Symbol)); selection.AddRange(financialServices.Select(f => f.Symbol)); selection.AddRange(consumerDefensive.Select(f => f.Symbol)); return selection; } } public class SectorWeightingPortfolioConstructionModel : EqualWeightingPortfolioConstructionModel { private readonly Dictionary<int, List<Symbol>> symbolBySectorCode = new Dictionary<int, List<Symbol>>(); private readonly Dictionary<Insight, double> result = new Dictionary<Insight, double>(); private decimal sectorBuyingPower = 0; public SectorWeightingPortfolioConstructionModel(Resolution resolution) : base(resolution) { } public override void OnSecuritiesChanged(QCAlgorithm algorithm, SecurityChanges changes) { foreach (var security in changes.AddedSecurities) { //1. When new assets are added to the universe, save the Morningstar Sector Code for each security to the variable sectorCode var sectorCode = security.Fundamentals?.AssetClassification.MorningstarIndustryGroupCode; //2. If there is a sectorCode and it is not in the symbolBySectorCode dictionary, save the values as a list // and append the security symbol as the value in the symbolBySectorCode dictionary if (sectorCode.HasValue) { List<Symbol> symbols; if (!symbolBySectorCode.TryGetValue(sectorCode.Value, out symbols)) { symbolBySectorCode[sectorCode.Value] = new List<Symbol>(); } symbolBySectorCode[sectorCode.Value].Add(security.Symbol); } } foreach (var security in changes.RemovedSecurities) { //3. For securities that are removed, save their MorningStar sector code to sectorCode var sectorCode = security.Fundamentals?.AssetClassification.MorningstarIndustryGroupCode; //4. If the saved sectorCode is in the symbolBySectorCode dictionary, remove the sectorCode. // If the saved symbol is a value in the symbolBySectorCode dictionary, remove the symbol if (sectorCode.HasValue) symbolBySectorCode.Remove(sectorCode.Value); } // We use the super() function to avoid using the base class name explicity base.OnSecuritiesChanged(algorithm, changes); } } }

.Thx,Sven
 

Author