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]
Bodhi
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!