I'm trying to train a model before the market open every day, but it seems in backtest model, the self.History returns a dataframe that includes the bar of the same day.
As we can see from the below log, we get the same history data for 2013-01-01 data before and after the market open on 2013-01-01, and we get the same 2013-01-01 bar data on 2013-01-02 as well, which means self.History creates a look ahead bias if I trivially call self.Hist(sym, lookback, Resolution.Daily). the same also applies to later dates
2013-01-01 00:00:00 : Launching analysis for bb27e1cfdebee60555a4a6332c308f42 with LEAN Engine v2.5.0.0.13431
2013-01-01 01:00:00 : 2013-01-01 01:00:00 log: close high low open symbol time SPX 31 2012-12-29 1402.43 1418.1 1401.58 1418.1!
2013-01-01 01:00:00 : 2013-01-01 01:00:00 log: close high low open symbol time SPX 31 2013-01-01 1426.19 1426.74 1398.11 1402.43!
2013-01-02 01:00:00 : 2013-01-01 10:00:00 log: close high low open symbol time SPX 31 2012-12-29 1402.43 1418.1 1401.58 1418.1!
2013-01-02 01:00:00 : 2013-01-01 10:00:00 log: close high low open symbol time SPX 31 2013-01-01 1426.19 1426.74 1398.11 1402.43!
2013-01-02 01:00:00 : 2013-01-02 01:00:00 log: close high low open symbol time SPX 31 2013-01-01 1426.19 1426.74 1398.11 1402.43!
2013-01-02 01:00:00 : 2013-01-02 01:00:00 log: close high low open symbol time SPX 31 2013-01-02 1426.19 1426.74 1398.11 1402.43!
2013-01-02 10:00:00 : 2013-01-02 10:00:00 log: close high low open symbol time SPX 31 2013-01-01 1426.19 1426.74 1398.11 1402.43!
2013-01-02 10:00:00 : 2013-01-02 10:00:00 log: close high low open symbol time SPX 31 2013-01-02 1426.19 1426.74 1398.11 1402.43!
2013-01-03 01:00:00 : 2013-01-03 01:00:00 log: close high low open symbol time SPX 31 2013-01-02 1426.19 1426.74 1398.11 1402.43!
2013-01-03 01:00:00 : 2013-01-03 01:00:00 log: close high low open symbol time SPX 31 2013-01-03 1462.42 1462.43 1426.19 1426.19!
2013-01-03 10:00:00 : 2013-01-03 10:00:00 log: close high low open symbol time SPX 31 2013-01-02 1426.19 1426.74 1398.11 1402.43!
2013-01-03 10:00:00 : 2013-01-03 10:00:00 log: close high low open symbol time SPX 31 2013-01-03 1462.42 1462.43 1426.19 1426.19!
2013-01-04 01:00:00 : 2013-01-04 01:00:00 log: close high low open symbol time SPX 31 2013-01-03 1462.42 1462.43 1426.19 1426.19!
2013-01-04 01:00:00 : 2013-01-04 01:00:00 log: close high low open symbol time SPX 31 2013-01-04 1459.37 1465.47 1455.53 1462.42!
2013-01-04 10:00:00 : 2013-01-04 10:00:00 log: close high low open symbol time SPX 31 2013-01-03 1462.42 1462.43 1426.19 1426.19!
2013-01-04 10:00:00 : 2013-01-04 10:00:00 log: close high low open symbol time SPX 31 2013-01-04 1459.37 1465.47 1455.53 1462.42!
Arthur Asenheimer
Hi,
there is no lookahead bias on QuantConnect.
When calling self.History() with Resolution.Daily you see the end time of a bar which has a midnight timestamp.
For example, the bar with the timestamp 2022-01-04 00:00:00 refers to the previous trading day, i.e. it refers to 2022-01-03.
Xi.J.L
Hi Arthur, Thanks a lot for the answer.
Xi.J.L
Hi Arthur,
Actually I got another question, from line 4 of the above log, you can see that on 1st of Jan, the self.Trade function is called by the scheduler, which logs self.Time which is 30mins after open and the historical data, however, I think 1st of Jan is a SPX holiday, how come this self.Trade is triggered? also seems the debug time is 2013-01-02 01:00:00 but self.Time is 2013-01-01 10:00:00. below is an updated log which also logs which function is called.
Arthur Asenheimer
> I think 1st of Jan is a SPX holiday, how come this self.Trade is triggered?
I think the scheduled event handler checks the trading calendar of that security and since SPX is not tradable at all I guess it fires every day. If you replace SPX with SPY you'll see the scheduled event don't trigger at 1st of January.
>also seems the debug time is 2013-01-02 01:00:00 but self.Time is 2013-01-01 10:00:00.
I'm not 100% sure about this, but there are 3 timestamps: the console/debug time, algorithm time (self.Time) and the end time of the bars from the History() call. I think the first and second operate on different threads, which is why they can differ.
Xi.J.L
Thanks a lot, Arthur, will try SPY. It is SPX option that I am trying to trade in my strategy, not sure if there is be any mismatch on the calendar, any suggestions on the index option schedule?
Arthur Asenheimer
For a more reliable solution I recommend to check whether there is data for SPX in the current slice.
Refer to the attached backtest. There you can see it wouldn't fire on 1st January.
Note that you should use Resolution.Minute or Resolution.Hour if you need the latest data in your scheduled event handler.
Xi.J.L
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!