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.
Rahul Chowdhury
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.
Seersquant
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
Rahul Chowdhury
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}") .....
Alexandre Catarino
Hi,
I am adding a C# version of Rahul's algorithm as requested by Magic Limited on another support channel.
Lucas Chan
Hi,
Appreciate your input here. I've added 1 line of Debug inside the handler and run Rahul's code here. I observe the handler are running weird with 2 inputs for every timestamp and 2 different SMA values.
Any idea what's going on here?
Louis Szeto
Hi Lucas
You have 2 securities in the universe, which they had both subscribed to the consolidator and updated SMA indicator accordingly. Thus, you have 2 SMAs for each security at the same time.
Best
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.
Seersquant
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!