Overall Statistics
class SupportAndResistance(QCAlgorithm):
    
    def Initialize(self):
            self.SetStartDate(2020, 1, 1)
            self.SetEndDate(2020,1,31)
            self.SetCash(100000)
            self.SetWarmUp(timedelta(7)) # Warm up 7 days of data.
            
            self.UnderlyingTicker = 'SPY'
            self.openingBar = None
            
            equity = self.AddEquity(self.UnderlyingTicker, Resolution.Minute)
            equity.SetDataNormalizationMode(DataNormalizationMode.Raw)

            option = self.AddOption(self.UnderlyingTicker)
            self.Consolidate(self.UnderlyingTicker, timedelta(1), self.OnDataConsolidated)

            self.option_symbol = option.Symbol
    
            # set our strike/expiry filter for this option chain
            option.SetFilter(lambda u: u.IncludeWeeklys().Strikes(0, 10).Expiration(timedelta(0), timedelta(7)))
            
            # use the underlying equity as the benchmark
            self.SetBenchmark(equity.Symbol)
            
    def OnData(self, slice):
        
        if self.openingBar == None: return
        if not self.Portfolio.Invested:
            
            if not slice.ContainsKey(self.UnderlyingTicker):
                self.Debug(f"NO {self.UnderlyingTicker} data on {self.Time}")
                return
            
            chain = slice.OptionChains.GetValue(self.option_symbol)
            if chain is None:
                return
            
            contracts_put = sorted(sorted(sorted(chain, \
                key = lambda x: abs(chain.Underlying.Price - x.Strike)), \
                key = lambda x: x.Expiry, reverse=False), \
                key = lambda x: x.Right, reverse=True)
                
            contracts_call = sorted(sorted(sorted(chain, \
                key = lambda x: abs(chain.Underlying.Price - x.Strike)), \
                key = lambda x: x.Expiry, reverse=False), \
                key = lambda x: x.Right, reverse=False)
                
            if len(contracts_put) == 0: return
            self.symbol_put = contracts_put[0].Symbol
            
            if len(contracts_call) == 0: return
            self.symbol_call = contracts_call[0].Symbol
            
            if slice[self.UnderlyingTicker].Close >= (self.openingBar.High - .10):
                self.MarketOrder(self.symbol_put, 10)
            
        if self.Portfolio.Invested:
            option_invested = [x.Key.Value for x in self.Portfolio if x.Value.Invested and x.Value.Type==SecurityType.Option]
            # Print to log
            for contract in option_invested:
                self.c = contract
                quantity = self.Portfolio[contract].Quantity
                lastPrice = self.Securities[contract].Price
                if self.Securities[contract].BidPrice > (self.Securities[contract].Holdings.AveragePrice + .10):
                    self.Debug('Here')
                    self.Liquidate(contract)
                    self.openingBar = None
                if self.Securities[contract].Holdings.AveragePrice < self.Securities[contract].BidPrice - .20:
                    self.Debug('Here')
                    self.Liquidate(contract)
                    self.openingBar = None
                
                
        
        
    def OnOrderEvent(self, orderEvent):
        self.Log(str(orderEvent))
        
    def OnDataConsolidated(self, bar):
        self.openingBar = bar