During a backtest, is there a way to obtain the next trading date for a future contract? As an example: on friday return monday's date, unless monday is a holiday.
QUANTCONNECT COMMUNITY
During a backtest, is there a way to obtain the next trading date for a future contract? As an example: on friday return monday's date, unless monday is a holiday.
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.
Alexandre Catarino
The TradingCalendar object has that information.
We can get the next business day with this code snippet:
public override void OnEndOfDay() { var tomorrow = Time.Date.AddDays(1); var nextTradingDay = TradingCalendar .GetDaysByType(TradingDayType.BusinessDay, tomorrow, tomorrow.AddDays(5)) .FirstOrDefault() .Date; Log(Time + " -> " + nextTradingDay); }
Bolt.Investments
Are there those classes in the Python version?
Varad Kabade
Hi Bolt.Investments,
All the above classes are available in python. Note that we'll be depreciating OnEndOfDay(self) constructor, please use OnEndOfDay(self, symbol) constructor in the future for more accurate behavior as each security might have different EndOfDay time.
Best,
Varad Kabade
Fred Painchaud
Hi,
It should be this in Python (just in case it is useful):
Fred
T C
Hi Fred,
Do you know how the function differentiates the holidays between different regions/markets? A holiday in US might not be a holiday in London. Also the bond market and stock market do have different holidays. Thanks!
Best,
Yuan
Fred Painchaud
Hi TC,
The TradingCalendar takes a SecurityManger (to manage securities) and a MarketHoursDatabase (to know when a market opens and closes).
The one you get from self.TradingCalendar gets initialized with a time zone New York. So no London holidays unfortunately 😊.
The calendar is populated more or less like this:
In a nutshell, dates are computed from the securities in the security manager. Security objects know their market. The hours database knows the hours and dates for markets.
And this is the explanation to the magic trick 😊. The lady is long gone from the box when the blades enter into the box. 😊 Hope it does not ruin the magic but rather increases it.
Fred
P.S. Re my Python code above in previous post, I am not sure if the IEnumerable returned by GetDaysByType() will get properly converted to Python from C# via Python.NET. In other words, it may not work. There is another magic trick to happen there to bridge between the two runtimes. Not sure if Python.NET does it for all or if it needs to be added in within LEAN. Did not dive that deep yet. One may be able to iterate over the returned object by GetDaysByType() but not call len() on it. Calling len() on an object in Python requires the object to implement a special method called __len__(). I'd bet Python.NET does not add those methods automatically. If it does, then, hats off to Python.NET 😊.
T C
Thanks Fred!Â
Ivan Popivanov
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!