I am attempting to predict the upcoming period's trends and timing to use in my Insights. When a bar is produced and my alpha/insight begins generating, I need to determine the next bar's period (start and end times) in order to know when the current trend will end (Insight period). As it stands I'm taking the current algorithm time (occurs when bar is generated...so it's the TradeBar.EndTime essentially) and adding the bar's Period.
This causes an issue when we hit the last bar of the trading day/week. So for example if I'm on a 4H consolidator and the 16:00 bar completes and triggers the insight, the insight's period will occur from 16:00 - 20:00 which doesn't make sense considering those aren't tradeable hours. The same issue occurs with 1D timeframes over weekends. So I can't rely on just "adding time" to the last bar generated - I need the start/end times of the next bar that will be generated in the series.
Douglas Stridsberg
Hi Jake,
I'd say this is a rather non-trivial problem. I don't think any part of the engine "knows" when the next bar will start (or end for that matter). Everything is handled dynamically:
- New bar is created with a default period
- Time passes or new data arrives causing either Scan() or Update() to trigger
- Period is set accordingly, and then EndTime is simply given as Time + Period
I'm not sure at which point in this process you'd be able to know the period or end time - it depends on how time passes and when data comes in (weekends, holidays, market closes, etc.).I imagine you can come quite close by checking IsOpen() on your security's SecurityExchangeHours object, but I don't think fundamentally there's a guaranteed way to know when the next bar will open or close.Jake Mitchell
Thank you for leading me in the right direction. After some more searching I came across the solution which is already a part of the framework (but not documented outside of LEAN repo):
# Where self.Timeframe/self.Exchange are custom object attributes. # self.Exchange holds an Exchange object set by using self.Securities["EXAMPLE"].Exchange time = self.Timeframe.Consolidator.WorkingBar.EndTime if not self.Exchange.DateTimeIsOpen(time): Â Â time = self.Exchange.Hours.GetNextMarketOpen(time, False)
Â
Douglas Stridsberg
Thanks for sharing!
I'd still advise you to build in robustness into your strategy such that if the market doesn't open on the given time (or the data doesn't arrive), you can still manage.
Jake Mitchell
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!