Back

Time consolidation, ending at 30', example 9:30, 10:30 and last bar at 3:30

Hi QC Team,

I am trying to port some of my algo's over from TradingView. The hour intervals end at 9:30 and so on, with the last one at 3:30. That is quite helpful since one can act before the market closes at 4pm. Could you please provide an example of say hourly EMA/SMA cross, but ending at 30' every hour.

I have tried consolidating minute to hour, but even that ends at 10am and so on.
 

Update Backtest







0

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.


Hey Seersquant,

You can use custom start times for consolidators by using a custom CalendarInfo object when creating a Calendar consolidator. We created a custom CalendarInfo object which starts at 9:30 am and has a period of 1 hour.

We can create a RollingWindow to hold our 1 hour bars so that we can check if a crossover has occurred with the SMA.

We also created a SymbolData class which holds the indicator, consolidator, rolling window data for each Symbol. This makes our code more organized.
 

3

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.


Thanks a lot for the example, it very useful. A few quick questions on this.

Its a nice way to use the CalendarInfo to supply 1hr intervals starting at 9'30. The last tradeBar would be 30'?

this part is perfect:

period = timedelta(hours=1)
# replace the time with, 30min.
# 9:40 -> 9:30
# 10:05 -> 10:30
start = dt.replace(minute=30)
if start > dt:
# start period between 10:00 to 10:29, will be 9:30
start -= period
# return, starts at 30', with a 1hr period
self.algorithm.Debug("starting at: " + str(start))

Could you please elaborate on the workings of this:

# will this run the function, or not yet?
hourlyConsolidator.DataConsolidated += self.OnDataConsolidated
 

0

Hey Seersquant,

The last trade bar will only have 30 minutes of data. This is because the last bar includes all the data from 3:30 pm until 4:30. However since there is no data between 4 pm and 9:30 am the next day, the last bar will only contain 30 minutes of data.

If I understand your last question correctly, we set the DataConsolidated event handler for hourlyConsolidator to OnDataConsolidated. This means every time a new hourly bar is available from our consolidator, OnDataConsolidated will be called. You can test this by adding a Debug statement within OnDataConsolidated which prints the time stamp of the bar.

def OnDataConsolidated(self, sender, bar):
self.algorithm.Debug(f"DataConsolidated @ {bar.Time}")
.....

 

1

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.


Update Backtest





0

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.


Loading...

This discussion is closed