Trying to keep things simple here and coding the trading logic to run base on system time. I am having difficulty saving the open and closing futures as objects to use in trading logic. Code and backtests are below. Any suggestions?

Unable to connect backtest because it is erroring out. Here is the code

import datetime

class ScikitLearnLinearRegressionAlgorithm(QCAlgorithm):
    
    openPeriod = None
    closePeriod = None
    
    def Initialize(self):
        self.SetTimeZone("America/New_York")
        self.SetStartDate(2015, 1, 1)
        self.SetEndDate(2021, 12, 31)
        self.NQ = self.AddFuture(Futures.Indices.NASDAQ100EMini) 
        self.NQ.SetFilter(lambda x: x.FrontMonth())

        self.Schedule.On(self.DateRules.EveryDay(), self.TimeRules.BeforeMarketClose(self.NQ, 0), self.ClosePositions)

    def OnData(self, data):
        
        now = datetime.datetime.now()

        if now.hour == 9 and now.minute == 30:
            self.closePeriod = self.Securities[self.NQ].Close
        
        if now.hour == 16 and now.minute == 00:
            self.openPeriod = self.Securities[self.NQ].Open
            
        if self.closePeriod is None or self.openPeriod is None:
            return
    
        change = (self.closePeriod - self.openPeriod) / self.openPeriod

        if now.hour == 9 and now.minute == 31 and self.change >= 0:
            self.SetHoldings(self.NQ, 1)
        elif now.hour == 9 and now.minute == 31 and self.change < 0:
            self.SetHoldings(self.NQ, -1)
        
    def ClosePositions(self):
        self.Liquidate()
        
#        for chain in data.FutureChains.Values:
#            contracts = chain.Contracts
#            for contract in contracts.Values:
#                history = self.History(contract.Symbol, 30, Resolution.Minute)
#                self.Log(history.to_string())
#                self.Quit()
#                return