class OpenRangeBreakout(QCAlgorithm):
openingBar = None
currentBar = None
def Initialize(self):
self.SetStartDate(2018, 7, 10) # Set Start Date
self.SetEndDate(2019, 6, 30) # Set End Date
self.SetCash(100000) # Set Strategy Cash
# Subscribe to TSLA with Minute Resolution
future = self.AddFuture(Futures.Indices.SP500EMini)
#1. Create our consolidator with a timedelta of 30 min
self.Consolidate("SP500EMini", timedelta(minutes=30),self.OnDataConsolidated)
def OnData(self, data):
if self.Portfolio.Invested or self.openingBar is None:
return
if data['SP500EMini'].Close > self.openingBar.High:
self.SetHoldings("SP500EMini",1)
elif data["SP500EMini"].Close < self.openingBar.Low:
self.SetHoldings("SP500EMini",-1)
#2. Create a function OnDataConsolidator which saves the currentBar as bar
def OnDataConsolidated(self, bar):
#1. Check the time, we only want to work with the first 30min after Market Open
if bar.Time.hour == 9 and bar.Time.minute == 30:
#2. Save one bar as openingBar
self.openingBar = bar
I am trying to take the "Boot camp" tutorial and apply them to futures. And I keep getting stuck. What am I doing wrong? Any help is greatly appreciated.
Sherry Yang
Hi Joshua,
Thanks for sharing your algorithm! Good work building off the Boot Camp lesson.
In this case we do not have enough cash ($100,000). If we increase our cash to $1,000,000 we will see trades. We are using a manual consolidator in this example.
Here is a revision of your algorithm:
class BasicTemplateFuturesConsolidationAlgorithm(QCAlgorithm): def Initialize(self): self.SetStartDate(2018, 7, 10) # Set Start Date self.SetEndDate(2019, 6, 30) # Set End Date self.SetCash(1000000) # Set Strategy Cash # Subscribe and set our expiry filter for the futures chain future = self.AddFuture(Futures.Indices.SP500EMini) future.SetFilter(timedelta(0), timedelta(182)) self.consolidators = dict() def OnDataConsolidated(self, bar): symbol = bar.Symbol price = self.Securities[symbol].Close if price >= bar.High: self.SetHoldings(symbol, 1) if price <= bar.Low: self.SetHoldings(symbol,-1) def OnSecuritiesChanged(self, changes): for security in changes.AddedSecurities: symbol = security.Symbol consolidator = self.Consolidate(symbol, timedelta(minutes=30), self.OnDataConsolidated) self.consolidators[symbol] = consolidator for security in changes.RemovedSecurities: symbol = security.Symbol consolidator = self.consolidators.pop(symbol) self.SubscriptionManager.RemoveConsolidator(symbol, consolidator) consolidator.DataConsolidated -= self.OnDataConsolidated if security.Invested: self.Liquidate(symbol, 'Remove from Futures Chains')
Also note that we need to remove securities that leave our universe.
Hope this is helpful!
Sherry
Joshua Dawson
Shirley,
Thank you for a 1000 times over. With the manual consolidator, is there a way to define time, so that it starts at 09:30 the opening of the RTH session and exists at or before the RTH close 16:15 eastern? As of right now, it is generating orders at all times of the day? What I am attempting to do is this.
First 60 minute high or low of the RTH session which starts 09:30 eastern. Printing High and Low of the session at 10:30 Eastern
exit at 1.3 ibh or ibl which is:
bar.High + 0.3 * (bar.High - bar.Low)
if 1.3 isn't hit then exit at or before 16:15
Sherry Yang
Hi Joshua,
My understanding of what you’d like to do is:
1. Save the high and low from the first 60 minutes of regular trading hours each day. Use those values to calculate: bar.High + 0.3 * (bar.High - bar.Low) for the day.
2. From 10:30am to 4:15pm ET, if the SP500EMini hits the calculated value at any time, exit.
3. If the SP500EMini does not hit the calculated value by 4:15pm ET, exit anyway.
4. Discard the saved high and low for the day and start again the next day.
Right now, it’s not possible to start a consolidator at a custom time. There’s an open issue for it to get addressed: https://github.com/QuantConnect/Lean/issues/3027.
However, if you want the high and low for a 60 minute period you can use a 2 period rolling window. You can check out the Fading The Gap lesson to see how you can create a rolling window and implement it.
https://www.quantconnect.com/terminal/#task-151/Creating-a-Rolling-Window
https://www.quantconnect.com/terminal/#task-149/Accessing-a-Rolling-Window
You can using the rolling window this way:
access the highest bar
max(window[0].High, window[1].High)
access the lowest bar
min(window[0].Low, window[1].Low)
You'll need OnData() to check for whether SP500EMini hits the calculated value and to exit. To do this you'll want to save your indicator as a class variable so it can be referenced in OnData().
Hope that helps!
Sherry
Alun Thomas
Hi guys, I'm wondering whether the issue has been solved for setting a consolidator up custom timeframes please? I'm trying to do something similar where I take the first hour starting at 09:30 -10:30 RTH and returning max/min without use of a rolling window. Code below:
For some reason, whyen I revert back to 30 min timedelta, I get trades but not when switching to an hourly format [as seen in the code]:
Thanks in advance!!
Alun Thomas
Moved to question separate post
Louis Szeto
Hi Alun
Yes, we can create a custom time consolidator. Refer to this doc for technical details.
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.
Joshua Dawson
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!