Hi, just as the title says, I am attempting to create an hourly MACD data consolidator, which would start at 9:30 and return values at the 30' mark (such as 10:30, 11:30, 12:30, etc.), with the last bar only having 30min of data (15:30->16:00). It seems as if the default setting for data consolidators is to have the last bar end at 16:00, so the first bar in an hourly consolidator is calculated at 10:00 after only 30min of data since market open.
Similarly, I would like to do the same thing for the four hour consolidator, which at the moment returns values at 12:00 (after 2.5 hours of data) and then at 16:00. I would like it to return values at 13:30 after the full 4 hours since market open, and at 16:00.
I wasn't able to figure out how to do it, and the only thing I found on the QC forum is the following discussion which gives a solution in Python, which I'm not familiar with at all:
https://www.quantconnect.com/forum/discussion/7455/time-consolidation-ending-at-30-039-example-9-30-10-30-and-last-bar-at-3-30However, it does seem to be a workaround, so perhaps there is a better way of solving this? I would appreciate any help either with writing the C# version of this solution or finding a different one.
Kuba K.
I just found another discussion regarding the same topic, but it's also in Pyhon. I'm linking it just in case it is useful for someone to help me.
https://www.quantconnect.com/forum/discussion/5122/any-way-for-a-bar-handler-to-trigger-every-60-minutes-but-at-10-30-11-30/p1Rahul Chowdhury
Hey Kuba,
Using calendar consolidators with a custom calendar info is the best way of accomplishing this.
This creates a calendar info object which starts the consolidator at the 30 minute mark and has a period of 1 hour.
private CalendarInfo CustomPeriod(DateTime datetime) { var period = TimeSpan.FromHours(1); var start = datetime.RoundDown(TimeSpan.FromMinutes(30)); return new CalendarInfo(start, period); }
We can then create a consolidator with this start and period.
var oneHour = new TradeBarConsolidator(CustomPeriod);
The last bar contains data from 3:30 to 4:30. But because there is no market data from 4:00 to 4:30 if extended hours has not been enabled, the last bar only contains data for 3:30 to 4:00.
Kuba K.
Thank you Rahul for this simple and working solution!
However, there is one thing that still needs to be modified - if you look at the logs, instead of returning a value at 16:00, the hourly consolidator returns it at 9:31 the next day. I checked and it is the correct value of consolidating data from 15:30->16:00 but is there any way for returning that value at 16:00 (and nothing being returned at 9:31)? Perhaps there is also a way for the last "hour" to be returned at 15:59 based on only 29 minutes of data (i.e. 15:30->15:59)?
Basically I am trying to get the MACD values from these periods 9:30->10:30, 10:30->11:30, 11:30->12:30, 12:30->13:30, 13:30->14:30, 14:30->15:30 and lastly 15:30->16:00 OR 15:30->15:59.
I would greatly appreciate if you would be able to show me both a solution to return that last "hour" at 16:00 and 15:59 if possible!
Rahul Chowdhury
Hey Kuba,
Although the hourly bar starting at 3:30 pm is emitted at 9:31 am the next day, you can access the partially consolidated bar at 4:00 pm. This can be done using Consolidator.WorkingData, which retrieves the consolidated value up to that point from the consolidator. We can use a scheduled event which fires at market close to access that value and act on it.
private void OnMarketClose() { bar = oneHour.WorkingData; close = bar.Value .... }
Kuba K.
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!