Quant League is evolving into Strategies, our new home for sharing, discovering, and exploring trading strategies, with improved organization and a better overall experience. Q4-2025 will be the final Quant League.
LEAN is the open-source algorithmic trading engine powering QuantConnect.
Founded in 2012 LEAN has been built by a global community of 180+ engineers and powers more than 300+ hedge funds today.
Join Our Discord Channel
Join QuantConnect's Discord server for real-time support, where a vibrant community of traders and developers awaits to help you with any of your QuantConnect needs.
This research is under review. To publish this research attract three community upvotes.
This research is under review. To publish this research attract three community upvotes.
This discussion is a draft. Click here to publish this discusison.
Algorithmic Trading Course #7 | Rolling Windows & Consolidators
Hi,
Below you can find the backtest and code from the 7th part of my algorithmic trading video series. Simply click the "Clone Algorithm" button to copy the code and play around with it yourself.
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.
Mitch Christow
878 Pro
,
Hi Louis,
Thanks a lot for the amazing video series. I have a question on this chapter. Could you explain why you set up the SPY with a minute resolution in
Are you doing this so that you have minute data for SPY, but you want to add the minute data only once a day to the consolidator? If you used Resolution.Minute for the consolidator, would that mean that the customeBarHandler is called every minute? I assume if that is the case, you would still have the same results (2 one-minute bars in the rollingWindow) but you just call the custombarhandler every minute which is wasteful. Is that the right way to think about this? Thanks for your help.
Cheers, Mitch
Louis
141.8k Pro
,
Hi Mitch,
Thanks for the question. I just used this as an example for how you can consolidate data to another resolution. Since we want the bot to open and close trades intraday, we need a resolution lower than daily (e.g. minutely), but to identify the gap up or down, we don't need to save all the minutely bars. Instead, we just need the closing price in daily resolution which is why I went with daily resolution here. I hope this makes sense. Otherwise, let me know.
Louis Szeto
45.8k Pro
,
Hi Mitch
The method CustomBarHandler will only be called once a bar is consolidated (EndTime), in this case, 09:31:00 since it is the next Slice data received not for the previous consolidated bar. So it would only be adding the consolidated tradebar (daily resolution one) into the self.rollingWindow once a day. By using Resolution.Minute, we can
be using bid/ask data and minute data for precise signal and filling with minimal slippage and prevent stale price filling at the end of the day by daily bar.
Run signal check every minute by OnData method since it's called whenever receiving a Slice data
Allow scheduling events run at precise time intraday
but an equal or lower frequency (e.g. daily) is applicable to indicators.
Best Louis Szeto
Mitch Christow
878 Pro
,
Thank you both for your replies and the explanations you have provided.Â
Chad Castanheiro
26 Pro
,
Hi, I'm going through this video and QuantConnect keeps giving me this error. I can't find any documentation that shows me a different way to do this. Can someone point me in the right direction. Even the current documentation shows this implementation of RollingWindow.Â
During the algorithm initialization, the following exception has occurred: cannot instantiate an open generic type   at Initialize     self.rw = RollingWindow(TradeBars)[2]               ^^^^^^^^^^^^^^^^^^^^^^^^
# region imports
from AlgorithmImports import *
from QuantConnect.Indicators import RollingWindow
# endregion
class QCConsolidationRollingWindows(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2018, 1, 1)
self.SetStartDate(2021, 1, 1)
self.SetCash(100000)
self.spy = self.AddEquity("SPY", Resolution.MINUTE).Symbol
self.rw = RollingWindow(TradeBar)[2]
self.consolidate(self.spy, Resolution.DAILY, self.CustomBarHandler)
self.schedule.on(self.DateRules.EveryDate(self.spy),
self.time_rules.before_market_close(self.spy, 15),
self.ExitPositions)
def OnData(self, data: Slice):
if not self.rw.IsReady:
return
if not (self.time.hour == 9 and self.time.minute == 31):
return
if data[self.spy].Open >= 1.01 * self.rw[0].Close:
self.set_holdings(self.spy, -1)
elif data[self.spy].Open <= 0.99 * self.rw[0].Close:
self.set_holdings(self.spy, 1)
def CustomBarHandler(self, bar):
self.rw.Add(bar)
def ExitPositions(self):
self.liquidate(self.spy)
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.
Dmytro Geras
17 Pro
,
If we sell 100% of shares every day at 15:45 how can we sell them again next day at 9:31?
You may short-sell it by borrowing the stocks from the broker and bet on its drop. Of course, you'll be subject to a more expensive margin and a borrowing fee. It depends on your broker.
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!
Allocate to this Strategy
Institutional clients can contact the author and allocate capital to this strategy.
Mitch Christow
Hi Louis,
Thanks a lot for the amazing video series. I have a question on this chapter. Could you explain why you set up the SPY with a minute resolution in
while the consolidator that you created has a daily resolution?
Are you doing this so that you have minute data for SPY, but you want to add the minute data only once a day to the consolidator? If you used Resolution.Minute for the consolidator, would that mean that the customeBarHandler is called every minute? I assume if that is the case, you would still have the same results (2 one-minute bars in the rollingWindow) but you just call the custombarhandler every minute which is wasteful. Is that the right way to think about this? Thanks for your help.
Cheers,
Mitch
Louis
Hi Mitch,
Thanks for the question. I just used this as an example for how you can consolidate data to another resolution. Since we want the bot to open and close trades intraday, we need a resolution lower than daily (e.g. minutely), but to identify the gap up or down, we don't need to save all the minutely bars. Instead, we just need the closing price in daily resolution which is why I went with daily resolution here. I hope this makes sense. Otherwise, let me know.
Louis Szeto
Hi Mitch
The method CustomBarHandler will only be called once a bar is consolidated (EndTime), in this case, 09:31:00 since it is the next Slice data received not for the previous consolidated bar. So it would only be adding the consolidated tradebar (daily resolution one) into the self.rollingWindow once a day. By using Resolution.Minute, we can
but an equal or lower frequency (e.g. daily) is applicable to indicators.
Best
Louis Szeto
Mitch Christow
Thank you both for your replies and the explanations you have provided.Â
Chad Castanheiro
Hi, I'm going through this video and QuantConnect keeps giving me this error. I can't find any documentation that shows me a different way to do this. Can someone point me in the right direction. Even the current documentation shows this implementation of RollingWindow.Â
During the algorithm initialization, the following exception has occurred: cannot instantiate an open generic type
  at Initialize
    self.rw = RollingWindow(TradeBars)[2]
              ^^^^^^^^^^^^^^^^^^^^^^^^
Ashutosh
Hey Chad CastanheiroÂ
You need to use:
Attached backtest for reference.
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.
Dmytro Geras
If we sell 100% of shares every day at 15:45 how can we sell them again next day at 9:31?
Louis Szeto
Hi Dmytro Geras
You may short-sell it by borrowing the stocks from the broker and bet on its drop. Of course, you'll be subject to a more expensive margin and a borrowing fee. It depends on your broker.
Best
Louis
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.
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!