Skip to content

Instantly share code, notes, and snippets.

@databento-bot
Created April 23, 2025 01:15
Show Gist options
  • Save databento-bot/8d952b7172c017d1a0c51625b7bbaca7 to your computer and use it in GitHub Desktop.
Save databento-bot/8d952b7172c017d1a0c51625b7bbaca7 to your computer and use it in GitHub Desktop.
Resample OHLCV-1s with uniform
"""
OHLCV-1s data follows standard convention of most vendors and doesn't print on intervals
when there's no trade. This is the typical approach as there's no way to infer the type
of price interpolation or forward fill preferred. If you need prices printed uniformly
with forward filling or price interpolation, you can follow this example to do it on
client side.
See also: https://databento.com/docs/examples/basics-historical/tick-resampling/example
"""
import databento as db
client = db.Historical()
data = client.timeseries.get_range(
dataset="GLBX.MDP3",
schema="ohlcv-1s",
stype_in="continuous",
symbols=["ES.n.1"],
start="2025-04-15T15:30",
end="2025-04-15T15:40",
)
df = data.to_df(tz="US/Eastern")
# Step 1: Resample to 1-second frequency
df_resampled = df.resample('1s').asfreq()
# Step 2: Identify which rows were created by resampling (i.e., rows that were NaN)
new_rows_mask = df_resampled['close'].isna()
# Step 3: Forward fill the resampled data
df_resampled = df_resampled.ffill()
# Step 4: For synthetic rows, set OHLC to the last close
df_resampled.loc[new_rows_mask, ['open', 'high', 'low']] = df_resampled['close'][new_rows_mask]
# Step 5: Set volume to 0 for synthetic rows
df_resampled.loc[new_rows_mask, 'volume'] = 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment