Last active
March 6, 2025 15:20
-
-
Save databento-bot/882f65e0c97f0e18c933598c6a7f8414 to your computer and use it in GitHub Desktop.
Use intraday replay to synthetically generate a snapshot
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# | |
# replay_snapshot.py | |
# | |
# As of Databento's Python client v0.33.0, intraday snapshots are not available via | |
# the historical (HTTP) API, so you can use intraday replay to generate those snapshots | |
# on client side. This is the recommended method until these two features are released: | |
# | |
# If no bar for a symbol is received in the first second, there will be no entry in the | |
# DataFrame for that symbol. | |
# | |
# 1. https://roadmap.databento.com/roadmap/provide-snapshots-for-historical-and-live-data | |
# 2. https://roadmap.databento.com/roadmap/expose-intraday-and-current-trading-session-historical-data-over-historical-http-api-and-clients | |
import databento as db | |
import pandas as pd | |
client = db.Live() | |
START = pd.Timestamp("2024-04-29T09:30:00", tz="US/Eastern") | |
STOP = pd.Timestamp("2024-04-29T09:30:01", tz="US/Eastern") | |
SYMBOL_MAP = {} | |
BARS: dict[str, dict | None] = { | |
"AMZN": None, | |
"MSFT": None, | |
"NFLX": None, | |
"TSLA": None, | |
} | |
client.subscribe( | |
dataset=db.Dataset.XNAS_ITCH, | |
schema=db.Schema.OHLCV_1S, | |
symbols=BARS.keys(), | |
start=START, | |
) | |
for record in client: | |
if isinstance(record, db.SymbolMappingMsg): | |
SYMBOL_MAP[record.hd.instrument_id] = record.stype_in_symbol | |
elif isinstance(record, db.OHLCVMsg): | |
symbol = SYMBOL_MAP.get(record.instrument_id, None) | |
if symbol is not None and BARS[symbol] is None: | |
BARS[symbol] = { | |
"ts_event": record.pretty_ts_event.tz_convert("US/Eastern"), | |
"symbol": symbol, | |
"open": record.pretty_open, | |
"high": record.pretty_high, | |
"low": record.pretty_low, | |
"close": record.pretty_close, | |
"volume": record.volume, | |
} | |
if record.pretty_ts_event >= STOP or all(BARS.values()): | |
client.stop() | |
df = pd.DataFrame(row for row in BARS.values() if row is not None).set_index("ts_event") | |
print(df) | |
""" | |
symbol open high low close volume | |
ts_event | |
2024-04-29 09:30:00-04:00 AMZN 182.72 182.77 182.54 182.73 671567 | |
2024-04-29 09:30:00-04:00 MSFT 405.65 405.88 405.40 405.87 268154 | |
2024-04-29 09:30:00-04:00 NFLX 559.18 559.32 558.84 558.84 27780 | |
2024-04-29 09:30:00-04:00 TSLA 188.39 188.48 188.21 188.29 1324424 | |
""" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment