Skip to content

Instantly share code, notes, and snippets.

@databento-bot
Created October 10, 2024 21:36
Show Gist options
  • Save databento-bot/99850121cfdc2380ecd43b6ecc8f5357 to your computer and use it in GitHub Desktop.
Save databento-bot/99850121cfdc2380ecd43b6ecc8f5357 to your computer and use it in GitHub Desktop.
Basic example of using live OHLCV data from Databento
import databento as db
@singledispatch
def dbn_handler(_: db.DBNRecord):
pass
@dbn_handler.register
def _(sym: db.SymbolMappingMsg):
instrument_id = sym.instrument_id
symbol = sym.stype_in_symbol
print(f"{symbol} has an instrument ID of {instrument_id}")
@dbn_handler.register
def _(ohlcv : db.OHLCVMsg):
o = ohlcv.open
h = ohlcv.high
l = ohlcv.low
c = ohlcv.close
v = ohlcv.volume
# TODO: Do something with the data
print(o, h, l, c, v)
@dbn_handler.register
def _(error: db.ErrorMsg):
print(f"ERROR: {error.err}")
live_client = db.Live()
live_client.subscribe(
dataset="DBEQ.BASIC",
schema="ohlcv-1m",
symbols=["AAPL"],
)
live_client.add_callback(dbn_handler)
live_client.block_for_close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment