Hello there. I am very new to quantconnect and I'm trying to backtest my first strategy. It's a strategy from Ernest Chan's book : “Algorithmic Trading: Winning Strategies and their Rationale”. The strategy aims to take positions in the direction of an overnight gap in GBPUSD. Overnight here means 5pm to 5am ET.

I have been searching this community for past issues that are related to this but to no avail. This error code comes up when I tried to backtest it:

"'GBPUSD' wasn't found in the Slice object, likely because there was no-data at this moment in time and it wasn't possible to fillforward historical data. Please check the data exists before accessing it with data.ContainsKey("GBPUSD") in Slice.cs:line 328"

 

Here is the code:

#region imports
from AlgorithmImports import *
#endregion
''' Following Overnight Gaps GBPUSD '''

# 

class FollowTheGap(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(2007, 7, 24) 
        self.SetEndDate(2012, 2, 20) 
        self.SetCash(100000)
        self.symbol = self.AddForex("GBPUSD", Resolution.Hour, Market.Oanda).Symbol
        self.dev = self.STD(self.symbol, 90, Resolution.Daily)

        self.Schedule.On(self.DateRules.EveryDay(self.symbol), self.TimeRules.At(17, 0), self.ClosingBar)
        self.Schedule.On(self.DateRules.EveryDay(self.symbol), self.TimeRules.At(5, 0), self.OpeningBar)

        # Save a RollingWindow with type QuoteBar and length of 2 as self.window
        self.window = RollingWindow[QuoteBar](2)
        # Warm up the indicator
        self.SetWarmUp(90, Resolution.Daily)


    def OnData(self, data):
        if data.Bars.ContainsKey(self.symbol):
            pass
        else:
            self.Debug(f"On time >> {self.Time} :: Quotebar does not contain GBPUSD!")


    def ClosingBar(self):
        if self.Portfolio.Invested:
            self.Liquidate()

        # Add the final bar of GBPUSD to our rolling window
        self.window.Add(self.CurrentSlice[self.symbol])
        
 
    def OpeningBar(self):

        # "GBPUSD" is in the current slice, add the current slice to the window
        if self.symbol in self.CurrentSlice.Bars:
            self.window.Add(self.CurrentSlice[self.symbol])

        # If our window is not full use return to wait for tomorrow
        if not self.window.IsReady:
            return

        if self.dev is None or not self.dev.IsReady:
            return
        
        # Calculate the change in overnight price
        # The difference between the Low of previous night and today's Open
        gap = self.window[0].Open - self.window[1].Low
        

        if not self.Portfolio.Invested:
            # If gap is more than the positive value of 1 std of last 90 days close to close daily returns, go long
            if gap > self.dev.Current.Value:
                self.SetHoldings("GBPUSD", 1)
            # If gap is less than the negative value of 1 std of last 90 days close to close daily returns, go short
            elif gap < -self.dev.Current.Value:
                self.SetHoldings("GBPUSD", -1)

I appreciate any help. Thank you!

Author