This issue has been partially addressed elsewhere but I'm having difficulty finding a way to make this work with OHLC financial market data.
I want to take 5-minute data and resample it into 4-hour data with the assumption that new data arrives every 5 minutes. Thus, using a fixed base in the Pandas resample does not work properly for this.
The last 4 hours containing Open(first), High(max), Low(min), Close(last), needs to be calculated from the last timestamp in reverse; from the bottom of the dataframe up. If the last time stamp is 14:05, then the final 4-hour resampled bar in the dataset should be stamped 10:05; when data at 14:10 is processed, it should resample back to 10:10, etc.
I'm seeking a method that can automatically determine how to set resample parameters based on the last timestamp or other dynamic approach (as this will update automatically with newer data) and then resample accordingly.
This is the traditional resample method I'm currently using:
ohlc_dict = {'Open': 'first', 'High': 'max', 'Low': 'min', 'Close': 'last'}
df = df.resample('240T', base=21).agg(ohlc_dict).dropna()
Is there any way to achieve this resampling objective?