'Getting resampled data even if target time is not finished
I am trying to resample '5m' data into a '240m' data. However when I resample, with this code:
import yfinance as yf
from dateutil.relativedelta import relativedelta
import datetime
ticker ='tsla'
df = yf.download(tickers=ticker, start=datetime.datetime.now()-relativedelta(days=30), end= datetime.datetime.now(), interval="5m", progress = False)
t4h = df.resample('240min').mean().dropna()
This will produce more data like this :
Open High ... Adj Close Volume
Datetime ...
2022-02-28 08:00:00-05:00 852.525315 857.157102 ... 854.357377 564232.666667
2022-02-28 12:00:00-05:00 862.665577 864.999358 ... 862.596879 289393.666667
2022-03-01 08:00:00-05:00 869.184041 872.438151 ... 869.149200 466986.633333
2022-03-01 12:00:00-05:00 864.464479 866.511848 ... 864.564774 208426.895833
What i want to understand is if there is a way for it to be resampled as such so that even if 12:00 is not yet but given the data which is available, to get the next 1 resample time. Trading softwares let you see the candle price which is not yet completed. So one can see the 2h candle event though it has 1 hour left for it to be completed. I am trying to do the same with pandas.
At the time of writing this question it was 2:15 PM in new york. However if i resample this data, the last data I have is for 1 PM. :
2022-03-29 13:00:00-04:00 1097.674362 1099.311020 ... 1098.094414 146068.187500
Even though on a '5m' resolution there is data for :
2022-03-29 14:14:40-04:00 1100.609985 1100.609985 ... 1100.609985 0
What can I do so that i can get the next 1 resample even if it is not completed or reached target time of resample.
Any help would be appreciated!
===Edit===
As suggested by @Jezrael
I have changed the ticker to acc.ns since US market is closed and there is no lower resolution data available.
t4h = df.resample('240min').mean().dropna().ffill()
last line is :
2022-03-30 08:00:00+05:30 2157.858836 2160.852941 ... 2158.283828 8992.705882
so the last sample time is 08:00:00+05:30
t4h = df.resample('240min').mean().dropna().bfill()
produces:
2022-03-30 08:00:00+05:30 2157.841187 2160.835291 ... 2158.298534 9016.617647
again the last sample time is 08:00:00+05:30
But in df with 5m interval the last time index available is 11:58:49+05:30
2022-03-30 11:58:49+05:30 2153.949951 2153.949951 ... 2153.949951 0
Thus what I wanted was to have a resample data in df of the next resample which will happen on 2022-03-30 12:00:00+05:30 based on the average data available even though it its not 12:00 and thus is incomplete for the resample.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
