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?
Lu Hao
I realise the new code is missing the line
self.Consolidate("TSLA", timedelta(minutes=30), self.OnDataConsolidated)
in the initiation function. However, it still does not work even when this new line is added.
Lu Hao
My bad i set the resolution to hour then tried to consolidate every 30 min...
Derek Melchin
Hi Lu,
Thank you for providing the solution. For completeness, I've attached a working backtest. Note that we pass "EURUSD" to the Consolidate method instead of "TSLA".
Best,
Derek Melchin
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.
Lu Hao
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!