The current day's daily bar??

Suppose I want to get info from the previous days close about a stock. Today is currently the 19th of August. Stock data is available for the 18th of August and prior. When I try to grab the most recent daily bar, it seems to only give me the day before the 18th. Since I am running the test on the 18th, the day is completed and the market and after-hours have closed already, I should be able to grab that day's (the 18th) information, like close, high and so forth.
How Can I do this?


  • ptunneyptunney Posts: 246

    Daily bars are not created until the whole day is completed, the files are physically created overnight.

    If your backtest is still running in the day you are interested in then those values will be in :

    md.L1.opening_trade # Opening Trade Official End of Auction on Primary exchange
    md.L1.daily_high # Daily High
    md.L1.daily_low # Daily Low
    md.L1.closing_trade # Closing Trade Official End of Auction on Primary Exchange.
    md.L1.core_acc_volume # Accumulated Volume During Market Hours
    md.L1.daily_count # Daily Count

    Depending on the symbols you are using, I would wait until well after the close before capturing as not all symbols close precisely at 4pm. I would suggest 4:20pm.

  • jg892461jg892461 Posts: 11

    Oh I see. So even though I run the backtest the following day, the 19th (when the files have been completed for the 18th and prior), I cant get the day I run the test for ?
    Remember, It was the 19th, the 18th dataset had been completed the night before, I run a test for the 18th (remember, a completed date), I cant get the 18th Daily bar?

  • ptunneyptunney Posts: 246

    The backtester is simulating a day. It is like a playback of a recording of the day, millisecond by millisecond. So you are in that day.
    At 9:30 what you can see is what you could have seen at 9:30 on that day.
    So for our at 10:30 you cannot see the future. You can get previous days bars and data for "today", ie the day that is running.
    You cannot look forward. Looking forward is the most common mistake people make, even when they aware of the possibility.

    Another example, if you run a backtest on say 1st January 2020 for a $10 stock that did a two for one split on 20th January then looking at a chart after 20th January the 1st January price would display as around $5 but running a backtest on the 1st of January the price is and will actually present to you as around $10.

    So no, for a backtest playing back the 18th you cannot get the daily bar for the 18th.

  • jg892461jg892461 Posts: 11

    Thank you Pt. That does make sense. I was approaching it from the wrong point of view.
    What I am trying to do is simply grab the day I am testing for the High price and the Close price on the last candle at the market close. When I tried to use the L1 (md.L1.closing_trade or md.L1.daily_high ) data, it shows a close price of nan because I assume that L1 had not completed?
    How can I go about getting this info from the day I running the test on.

  • ptunneyptunney Posts: 246

    Just run until 16:30 and capture the information at that time. Either by setting a timer or using on_minute or on trade and checking that md.L1.timestamp > service.time(16,29) or in on_finish.

Sign In or Register to comment.