The original code, which works, is this:


class OpeningRangeBreakout(QCAlgorithm):

openingBar = None

def Initialize(self):
self.SetStartDate(2018, 7, 10)
self.SetEndDate(2019, 6, 30)
self.SetCash(100000)
self.AddEquity("TSLA", Resolution.Minute)
self.Consolidate("TSLA", timedelta(minutes=30), self.OnDataConsolidated)

#3. Create a scheduled event triggered at 13:30 calling the ClosePositions function
self.Schedule.On(self.DateRules.EveryDay("TSLA"), self.TimeRules.At(13,30), self.ClosePositions)
def OnData(self, data):

if self.Portfolio.Invested or self.openingBar is None:
return

if data["TSLA"].Close > self.openingBar.High:
self.SetHoldings("TSLA", 1)

elif data["TSLA"].Close < self.openingBar.Low:
self.SetHoldings("TSLA", -1)

def OnDataConsolidated(self, bar):
if bar.Time.hour == 9 and bar.Time.minute == 30:
self.openingBar = bar

#1. Create a function named ClosePositions(self)
def ClosePositions(self):
#2. Set self.openingBar to None, and liquidate TSLA
self.openingBar = None
self.Liquidate("TSLA")

 

The new code, which doesnt work, is this:

class OpeningRangeBreakout(QCAlgorithm):

openingBar = None

def Initialize(self):
self.SetStartDate(2019, 6, 1)
self.SetEndDate(2019, 6, 10)
self.SetCash(25000)
self.AddForex("EURUSD", Resolution.Hour, Market.Oanda)
self.SetBrokerageModel(BrokerageName.OandaBrokerage)
self.Schedule.On(self.DateRules.EveryDay("EURUSD"), self.TimeRules.At(13,30), self.ClosePositions)
def OnData(self, data):
self.Debug("the close is" + str(data["EURUSD"].Close) + str(self.Portfolio.Invested) + str(self.openingBar))
if self.Portfolio.Invested or self.openingBar is None:
return

if data["EURUSD"].Close > self.openingBar.High:
self.SetHoldings("EURUSD", 1)

elif data["EURUSD"].Close < self.openingBar.Low:
self.SetHoldings("EURUSD", -1)

def OnDataConsolidated(self, bar):
if bar.Time.hour == 9 and bar.Time.minute == 30:
self.openingBar = bar

def ClosePositions(self):
self.openingBar = None
self.Liquidate("EURUSD")

The problem is that the openingBar is never set. I wonder if this has got to do with the unique 24 hour nature of the forex market. May I ask what time is data["EURUSD"].Close obtained from? Also, when does the market open (on monday) and close (on Friday) in the algorithmic time zone?