My backtest begins running and makes it through a few years, but then it halts after a trade is executed and position liquidated at a "stale price", and runtime error states "ArgumentNullException : Value cannot be null".

Just for some background, I'm trying to have the portfolio stay invested 99% in SPY and 1% in a put option hedge. The puts are rolled over once a month when portfolio allocations rebalance.

What's going on here, and how do I fix it? Having difficulty figuring it out. Thanks in advance.

class TailHedge(QCAlgorithm): def Initialize(self): self.SetStartDate(2008, 1, 1) self.SetEndDate(2020, 6, 30) self.SetCash(1000000) spy = self.AddEquity("SPY", Resolution.Minute) spy.SetDataNormalizationMode(DataNormalizationMode.Raw) self.SetSecurityInitializer(lambda x: x.SetMarketPrice(self.GetLastKnownPrice(x))) self.spy = spy.Symbol self.contract = None self.SetWarmUp(200) self.Schedule.On(self.DateRules.MonthStart("SPY"), self.TimeRules.AfterMarketOpen("SPY", 90), self.Rebalance) def OnData(self, data): if self.IsWarmingUp: return def Rebalance(self): self.Log("SpecificTime: Fired at : {0}".format(self.Time)) self.SetHoldings(self.spy, 0.99) self.Debug(str(self.Portfolio[self.spy].HoldingsValue)) if self.contract is None: self.contract = self.GetContract() self.SetHoldings(self.contract, 0.01) self.Debug(str(self.Portfolio[self.contract].HoldingsValue)) return if not self.contract is None: self.Liquidate(self.contract) self.RemoveSecurity(self.contract) self.contract = None self.contract = self.GetContract() self.SetHoldings(self.contract, 0.01) self.Debug(str(self.Portfolio[self.contract].HoldingsValue)) return def GetContract(self): targetStrike = self.Securities[self.spy].Price * 0.7 contracts = self.OptionChainProvider.GetOptionContractList(self.spy, self.Time) puts = [x for x in contracts if x.ID.OptionRight == OptionRight.Put] puts = sorted( sorted(puts, key = lambda x: x.ID.Date, reverse = True), key = lambda x: x.ID.StrikePrice) puts = [x for x in puts if (x.ID.StrikePrice - targetStrike) >= 0 and (x.ID.StrikePrice - targetStrike) < 6] puts = [x for x in puts if 50 < (x.ID.Date - self.Time).days <= 90] if len(puts) == 0: return None self.AddOptionContract(puts[0], Resolution.Minute) return puts[0]

 

Author