Overall Statistics
from math import floor

class CalibratedTachyonAutosequencers(QCAlgorithm):

currentStrike = 0
numberOfContracts = 0

def Initialize(self):
self.SetStartDate(2010, 1, 1)  # Set Start Date
# self.SetEndDate(2020, 3, 1)
self.SetCash(100000)
equity = self.AddEquity("SPY", Resolution.Minute)
equity.SetDataNormalizationMode(DataNormalizationMode.Raw)
option = self.AddOption("SPY", Resolution.Minute)
self.symbol = option.Symbol
option.SetFilter(lambda universe: universe.IncludeWeeklys().Strikes(-400, 0).Expiration(timedelta(36), timedelta(45)))
self.SetBenchmark("SPY")

def OnData(self, data):
if not self.Portfolio.Invested:

for i in data.OptionChains:
if i.Key != self.symbol: continue
chain = i.Value
# filter the put option contracts
put = [x for x in chain if x.Right == OptionRight.Put]
# sort
contracts = sorted(sorted(put, key = lambda x: x.Strike / chain.Underlying.Price - 1, reverse=True), key = lambda x: x.Expiry, reverse=False)
# sorted(put, key = lambda x: x.Greeks.Delta)

n = 0
for i in contracts:
if (i.Strike / chain.Underlying.Price - 1) < -0.1:
break
else:
n = n + 1

if len(contracts) == 0: return

if n < (len(contracts) - 1):
self.put = contracts[n].Symbol

# short the put options
self.currentStrike = contracts[n].Strike
self.numberOfContracts = floor(self.Portfolio.Cash / self.currentStrike / 100)
self.MarketOrder(self.put, self.numberOfContracts)

elif (self.currentStrike + 3) >= self.Securities["SPY"].Close:
self.MarketOrder(self.put, -self.numberOfContracts)