This algorithm is cloned from
https://github.com/QuantConnect/Lean/blob/master/Algorithm.CSharp/MultipleSymbolConsolidationAlgorithm.cs
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
Thanks.
Gurumeher Sawhney
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.
Pranav Shah
Thanks for the comment. I will look into Consolidators.
Pranav Shah
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
Questions:
Pranav Shah
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!