How to calculate SMA every 2 hours

This algorithm is cloned from

How do I change the BarPeriod and OnData to be calulated/called every 2 hours for the last 20 Trading Hours (account for weekend, Holiday, etc.)

I want the SMA to be pased on last 10 periods.  Each period will have the price of the position at then end of 2 Hours, else the last price.

I am looking at similar posts to understand how to use this properly, but so far no luck



Update Backtest

In order to calculate an SMA every 2 hours, consolidators need to be used. Consolidators combine smaller data points into larger bars and then indicators can register to these consolidators. The code for creating this SMA looks like this:

def Initialize(self):
# ...other initialization...
self.AddEquity("SPY", Resolution.Minute)
consolidator = TradeBarConsolidator(120)
self._sma = SimpleMovingAverage(10)
self.RegisterIndicator("SPY", self._sma, consolidator)
self.SubscriptionManager.AddConsolidator("SPY", consolidator)

Note that the consolidator needs to be subscribed to the symbol data, and the indicator needs to be registered to the consolidated data.


Thanks for the comment.  I will look into Consolidators.


I am trying to work with Consolidators and Indicators.  My first confusion is the data returned by the Consolidator.  I started with code base I found at - MultipleSymbolConsolidationAlgorithm

I made changes to grab the data every 2 hours.  However the result is not clear to me.

My goal is to check for SMA every 2 hours.  Once the data is grabbed, I want to do something, however right now it seems like I have to wait another 2 hours before I could access the data I grabbed. 

The Log shows the following:

1998-01-05 12:00:00 HandleConsolidatedData - Time: Mon 1/5/1998 10:00:00 AM ; EndTime: Mon 1/5/1998 12:00:00 PM ; Value: 37.832367750000 ; Price: 37.832367750000
1998-01-05 12:00:00 HandleConsolidatedData - Time: Mon 1/5/1998 8:00:00 AM ; EndTime: Mon 1/5/1998 10:00:00 AM ; Value: 37.455820240000 ; Price: 37.455820240000


  1. What does Time and End Time mean here?  Is it showing the data for "Time" or "End Time"
  2. Is it that the data from 10 AM not available till 12 PM?
  3. The second line that shows the data for 8 AM, how would I go about capturing the data for Open 9:30 AM?
  4. I am also using "SetWarmup" in this code.  What is the right usage of "IsWarmingUp"?  Right now I put it under OnData.
  5. I also that indicators had to be Registerd, but the sample code does not do that.  Could someone explain why?
  6. Could the SMA be defined with the methid in QCAlgorithm?  Is there a reason why that approach was not used?
  7. Additionally the data does not seem to match the raw data found at - Adjusted IBM Historic Data.  Is there an explanation?

Update Backtest


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.


This discussion is closed