Is there any relatively easy way to find out what the average trading range is for ES futures for each of the last five hours of trading each day?
I'm using IB I am afraid, writing iron condors 0dte, and wanting to looking at average movement hour by hour over a long period of time, at least a year.
import databento as db
import pandas as pd
client = db.Historical(key='YOUR_API_KEY')
data = client.timeseries.stream(dataset='GLBX.MDP3',
schema='ohlcv-1h',
stype_in='smart', # use smart lead month symbol
symbols=['ES.v.0'], # lead month ES by volume
start='2022-12-18',
end='2023-01-18')
# Convert to dataframe with UNIX timestamps and display prices
df = data.to_df(pretty_ts=False, pretty_px=True)
# Convert UNIX timestamps to US Eastern Time
df.index = pd.to_datetime(df.index).tz_localize('UTC').tz_convert('America/New_York')
# Only last 5 hours of each day
df = df.between_time('12:00', '17:00')
# Group by day, then compute ATR of last 5 hours of each group
df = df.groupby(df.index.date).apply(lambda row: row['high'].max() - row['low'].min())
print(df)
2022-12-19 35.50
2022-12-20 22.50
2022-12-21 22.25
2022-12-22 68.25
2022-12-23 20.75
2022-12-27 27.00
2022-12-28 38.50
2022-12-29 14.75
2022-12-30 47.00
2023-01-03 34.75
2023-01-04 47.00
2023-01-05 28.75
2023-01-06 42.50
2023-01-09 60.25
2023-01-10 37.25
2023-01-11 30.00
2023-01-12 29.75
2023-01-13 34.50
2023-01-16 6.75
2023-01-17 17.25
data = client.timeseries.stream(dataset='GLBX.MDP3',
schema='ohlcv-1m', # 1 min bars instead
stype_in='smart',
symbols=['ES.v.0'],
start='2023-01-09',
end='2023-01-14')
...
...
# Only last 5 hours of each day
df = df.between_time('12:00', '17:00')
plt.plot(df['close'].resample('1min').first().fillna(value=np.nan))
plt.show()
