I have an hourly dataframe with open, high low and closing prices.
What I want to do is this:
Based on daily OHLC values I want to drop down to 1 hour data to see if I need to open a position. So my assumption is I want to have both 1 hour OHLC data and daily OHLC data in one dataframe. A daily bar ends at 22.00.
I am wondering if this is the most efficient way to present the data.
Moreover this code shows the daily bar ending at 02.00.
df = pd.DataFrame(bars)[["date", "open", "high", "low", "close"]]
# set the date as the index
df.set_index('date', inplace=True)
# adjust the time of the index by subtracting 2 hours
df.index = df.index - pd.Timedelta(hours=2)
# resample the data to daily bars and add new column for resampled data
df_resampled = df.resample("1D").agg({
"open": "first",
"high": "max",
"low": "min",
"close": "last"
})
# adjust the time of the index back by adding 2 hours
df.index = df.index + pd.Timedelta(hours=2)
df_resampled.index = df_resampled.index + pd.Timedelta(hours=2)
# create new column for df to store the resampled data
df = df.join(df_resampled, rsuffix='_daily')
print(df)
open high low close open_daily high_daily low_daily close_daily
date
2024-04-19 04:00:00+00:00 1.063730 1.064330 1.063370 1.063475 NaN NaN NaN NaN
2024-04-19 05:00:00+00:00 1.063475 1.064100 1.062975 1.063090 NaN NaN NaN NaN
2024-04-19 06:00:00+00:00 1.063090 1.064945 1.062715 1.064495 NaN NaN NaN NaN
2024-04-19 07:00:00+00:00 1.064495 1.065935 1.064385 1.065545 NaN NaN NaN NaN
2024-04-19 08:00:00+00:00 1.065545 1.065545 1.064305 1.064570 NaN NaN NaN NaN
2024-04-19 09:00:00+00:00 1.064570 1.066230 1.064525 1.065905 NaN NaN NaN NaN
2024-04-19 10:00:00+00:00 1.065905 1.065910 1.064685 1.064740 NaN NaN NaN NaN
2024-04-19 11:00:00+00:00 1.064740 1.065530 1.064300 1.065180 NaN NaN NaN NaN
2024-04-19 12:00:00+00:00 1.065180 1.067380 1.064965 1.066550 NaN NaN NaN NaN
2024-04-19 13:00:00+00:00 1.066550 1.067555 1.065870 1.066585 NaN NaN NaN NaN
2024-04-19 14:00:00+00:00 1.066585 1.067775 1.066385 1.066960 NaN NaN NaN NaN
2024-04-19 15:00:00+00:00 1.066960 1.067195 1.065370 1.065495 NaN NaN NaN NaN
2024-04-19 16:00:00+00:00 1.065495 1.065975 1.064025 1.065350 NaN NaN NaN NaN
2024-04-19 17:00:00+00:00 1.065350 1.065855 1.065090 1.065570 NaN NaN NaN NaN
2024-04-19 18:00:00+00:00 1.065570 1.065570 1.064655 1.065075 NaN NaN NaN NaN
2024-04-19 19:00:00+00:00 1.065075 1.065530 1.064975 1.065480 NaN NaN NaN NaN
2024-04-19 20:00:00+00:00 1.065480 1.065790 1.065315 1.065650 NaN NaN NaN NaN
2024-04-21 21:15:00+00:00 1.066115 1.066525 1.065445 1.065765 NaN NaN NaN NaN
2024-04-21 22:00:00+00:00 1.065765 1.066160 1.065470 1.065890 NaN NaN NaN NaN
2024-04-21 23:00:00+00:00 1.065890 1.066115 1.065760 1.065760 NaN NaN NaN NaN
2024-04-22 00:00:00+00:00 1.065760 1.066510 1.065275 1.065345 NaN NaN NaN NaN
2024-04-22 01:00:00+00:00 1.065345 1.066980 1.065330 1.066820 NaN NaN NaN NaN
2024-04-22 02:00:00+00:00 1.066820 1.067070 1.066395 1.066405 1.06682 1.06707 1.06240 1.06542
Koen Graveland is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1