As I know, the history request does not include the latest/current price bar until it closes in the end of market and I want to add it to the previous data whenever i called.

My strategy is to buy 7-days low SPY at 15mins before market close. Also , at that time, the price should be above sma200.

Below is my first code but i got the error. Could someone help, please. Thanks!


BacktestingRealTimeHandler.Run(): There was an error in a scheduled event SPY: EveryDay: SPY: 30 min before MarketClose. The error was AttributeError : 'NoneType' object has no attribute 'Close'

from System import *
from QuantConnect import *
from QuantConnect.Algorithm import *
from QuantConnect.Indicators import *
from QuantConnect.Data.Market import TradeBar
from datetime import datetime, timedelta
import decimal

class DaysAlgorithm(QCAlgorithm):

def Initialize(self):

self.spy = self.AddEquity("SPY", Resolution.Daily)

#consolidator = TradeBarConsolidator(timedelta(1))
#consolidator.DataConsolidated += self.OnDailyData
#self.SubscriptionManager.AddConsolidator("SPY", consolidator)
self.daily = RollingWindow[TradeBar](2)
self.window = RollingWindow[TradeBar](2)
self.leverage = 1
self.Schedule.On(self.DateRules.EveryDay("SPY"), self.TimeRules.BeforeMarketClose("SPY", 30), Action(self.Rebalance))

self.sma = self.SMA(self.spy.Symbol, 200, Resolution.Daily)
self.sma.Updated += self.SmaUpdated
self.smaWin = RollingWindow[IndicatorDataPoint](5)

def SmaUpdated(self, sender, updated):

def OnData(self, data):


if not (self.window.IsReady and self.smaWin.IsReady): return

def Rebalance(self):

close = self.History(self.spy.Symbol, 6, Resolution.Daily)['close']
holdings = self.Portfolio[self.spy.Symbol].Quantity
currBar = self.window[0].Close
currSma = self.smaWin[0].Value

if not self.Portfolio.Invested and currBar > currSma and currBar < min(close):
self.SetHoldings(self.spy.Symbol, 1)
elif currBar < currSma or currBar > max(close):
self.SetHoldings(self.spy.Symbol, 0)