After the market close, when you run backtest including the current day, I don't see the OHLC values of the current day.
When is the current day OHLC ready to use?
For example, when I run it for today:
I only get this latest value of SPY which belongs to the day before.
2022-03-16 16:00:00 Time: 2022-03-16 16:00:00 -> Bar close 435.62 high 435.67
Since today is over, I expected to get current day's OHLC after market close. Am I missing something here?
Thanks,
mustafa
class DeterminedTanPigeon(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2022, 3, 15) # Set Start Date
self.SetEndDate(2022, 3, 18)
self.SetCash(100000) # Set Strategy Cash
self.symbol = self.AddEquity("SPY", Resolution.Minute).Symbol
self.consolidator = TradeBarConsolidator(timedelta(1))
self.SubscriptionManager.AddConsolidator(self.symbol, self.consolidator)
def OnData(self, data):
if not data.Bars.ContainsKey("SPY"):
return
if data.Time.hour == 16 and data.Time.minute == 0:
bar = self.consolidator.WorkingBar
self.Log("Time: {} -> Bar close {} high {}".format(self.Time, bar.Close,bar.High))
Fred Painchaud
Hi Mustafa,
Daily bars are fed into OnData at midnight. So the daily bar for say 2022-3-16 is going to be given to OnData at 00:00 on 2022-3-17.
A “day” depends on the market and asset. Doing it like that is thus portable, among other things.
If you want the OHLC for the day at 16:00, consolidate 6.5 hours * 60 minutes = 390 minutes. Note that some US equity trading days ARE SHORTER than 6.5 hours however. Sometimes the market closes at 13:00 for instance. Etc etc. So consolidating 390 minutes is less robust.
Have a look at
and
Fred
Mustafa Oz
Hi Fred,
Thank you for the suggestion!
I did your suggestion that I created a custom period ending at 4pm. However, I still see the same issue. It never picks up the current day.
The last day I see is 03/17. I never see OHLC from today, 03/18 even though the market closed 1 and half an hour ago:
2022-03-17 16:00:00 Time: 2022-03-17 16:00:00 -> Bar close 441.07 high 441.07
Fred Painchaud
Hi Mustafa,
If you could share the new code, it would help to spot a potential error. Or maybe we just do not quite understand each other's…
Fred
Mustafa Oz
oops. I thought I added the project.
Adding it now…
Fred Painchaud
Hi Mustafa,
Running your code revealed no problem to me. I only removed your OnData method since it is not needed: 1) your consolidator self.consolidator is not registered so it does not receive any data and 2) btw, you should not use the WorkingBar directly.
I got the following output from your custom consolidator, which looks like daily bars:
2022-03-15 00:00:00 : Launching analysis for 506b13e3018c4189b907d0ef78dcd503 with LEAN Engine v2.5.0.0.13727
2022-03-15 16:00:00 : 2022-03-15 09:00:00 -> 2022-03-15 16:00:00
2022-03-15 16:00:00 : Time: 2022-03-15 16:00:00 -> Bar close 424.85015151 high 425.51807652
2022-03-16 16:00:00 : 2022-03-16 09:00:00 -> 2022-03-16 16:00:00
2022-03-16 16:00:00 : Time: 2022-03-16 16:00:00 -> Bar close 434.27088486 high 434.32073001
2022-03-17 16:00:00 : 2022-03-17 09:00:00 -> 2022-03-17 16:00:00
2022-03-17 16:00:00 : Time: 2022-03-17 16:00:00 -> Bar close 439.70400621 high 439.70400621
2022-03-18 16:00:00 : 2022-03-18 09:00:00 -> 2022-03-18 16:00:00
2022-03-18 16:00:00 : Time: 2022-03-18 16:00:00 -> Bar close 444.52 high 444.86
2022-03-18 16:00:00 : Algorithm Id:(506b13e3018c4189b907d0ef78dcd503) completed in 0.56 seconds at 6k data points per second. Processing total of 3,157 data points.
You do not get a bar for the 19th, it is a Saturday. Market makers are burning their money 😊.
Your custom period function returns 9:00 to 16:00 (i.e., 9:30 → 9:00 + 7 hours).
Fred
Mustafa Oz
Hi Fred,
I don't know at what time you run it but when I run one and half an hour after the market closes, it still does not give me any data of the current day. How long do we need to wait to get the current day data?
I just checked it again today. The time is exactly 5:30 pm on 03/21 and I get this result:
2022-03-18 16:00:00 2022-03-18 09:00:00 -> 2022-03-18 16:00:00
2022-03-18 16:00:00 Time: 2022-03-18 16:00:00 -> Bar close 444.52 high 444.86
I don't see the result from 03/21.
Thanks,
mustafa
Fred Painchaud
Hi Mustafa,
It is 5:30 pm which time zone? If it is 5:30pm New York time, then yeah, you should have the bar for the 21.
Since it is a live algo, I would say you should email support@quantconnect.com to get help with it. If you are trading NYSE securities and at 5:30 pm NYC time you do not have your 4:00 pm, something looks wrong.
Fred
Mustafa Oz
Hi Fred,
This is a backtesting environment, not live algo. In live algo, all is good… :)
It is 5:30pm New York Time and I expected the current day to be available right after the market close?
Thanks,
Fred Painchaud
😊
When we are due not to understand each other's huh?
Now I am tracking. I don't know exactly how much time it takes after market close for the data to be available to the backtesting/historical database. But it can certainly have some lag. I certainly ran it late at night NY time. 5:30 PM can be maybe a bit early I'd guess since you do not see the data. I did look at algoSeek, the data provider and maybe you would need to wait after 8:00PM, since that is the extended hours end. I don't have the details of how the feed is handled, when it is processed, etc, in QC.
I did learn however the live node servers are at the same location then the algoSeek's data feeds. Pretty neat.
Fred
Mustafa Oz
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!