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!
Louis Szeto
Hi Olawale
Please note that Forex class does not provide TradeBar data, only QuoteBars are available. Change all the data/slice.Bars into data/slice.QuoteBars in the algorithm will work.
Best
Louis
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
Olawale
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!