1

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?

  • I'm afrait there's no such resampling method in pandas. The closest I've found to that is negatively offsetting `resample`'s `base` parameter: Say your last OHLC row is at 14:05, and your first was at 00:00. Doing `df.resample('240T', base=-114)` will get you a resampled index ending in ***10:06***. If you need ***10:05*** to be there instead of ***10:06***, you could do `base=-115` - but your last index value will then be ***14:05***; i.e. a 4h interval that only accounts for the last OHLC row. Not useful for your usecase. – Julio Cezar Silva May 29 '20 at 03:18

0 Answers0