Skip to content

Instantly share code, notes, and snippets.

@databento-bot
databento-bot / live_ohlcv.py
Created October 10, 2024 21:36
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
@databento-bot
databento-bot / pairs_trading.py
Last active August 11, 2025 11:37
A cross-venue pairs trading strategy between WTI (CME) and Brent (ICE) crude oil
import databento as db
import pandas as pd
import matplotlib.pyplot as plt
from statsmodels.tsa.stattools import coint
from sklearn.linear_model import LinearRegression
plt.style.use("ggplot")
# Instrument-specific configuration
@databento-bot
databento-bot / x2522.20240813.log
Last active October 1, 2024 14:08
X2522 benchmark, 2024-08-13
# One-way, overall
#
# 0.973/1.093/3.042us (0/50/99p, 256 byte packets)
# Port-to-port (switch hop)
#
# 680+/-20ns
#
# - Mellanox Spectrum SN2700 w/ Cumulus 5.7
@databento-bot
databento-bot / get_xnas_ohlcv1h_20240501.py
Created June 8, 2024 17:42
Extracts 1 day of OHLCV-1h data for XNAS.ITCH
# Extracts 1 day of OHLCV-1h data for XNAS.ITCH
import databento as db
client = db.Historical()
data = client.timeseries.get_range(
dataset='XNAS.ITCH',
schema='ohlcv-1h',
symbols='ALL_SYMBOLS',
@databento-bot
databento-bot / replay_snapshot.py
Last active March 6, 2025 15:20
Use intraday replay to synthetically generate a snapshot
#!/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.
@databento-bot
databento-bot / databento-extract-cme.py
Created April 15, 2024 04:39
Fetch data of options on futures through Databento, using COMEX Gold (GC) and CBOT US 5-Year T-Note (ZF) and including weeklies, as an example
#!/usr/bin/env python
#
# databento-extract-cme.py
#
# Fetch data of options on futures through Databento,
# using COMEX Gold (GC) and CBOT US 5-Year T-Note (ZF) and
# including weeklies, as an example
import databento as db
import itertools
# Reddit comment 2024-04-03
> See https://www.reddit.com/r/algotrading/comments/1bu59ql/comment/kxuil9a
There will always be some differences in the vendor’s infrastructure used to process real-time vs. historical.
It takes a bit of effort to make these as identical as possible. Non-exhaustive list:
The most common issue I’ve seen is that the vendor will retroactively clean and patch their historical data ex post in ways
that are not replicable in real-time. (The most obvious tell is if you report a data error and they tell you it’s patched
within the same day.) This is one area where Bloomberg is quite good despite doing it the “wrong” way - they have a
@databento-bot
databento-bot / extended_hours.py
Created February 28, 2024 09:20
Example of getting largest moves in US stocks during extended hours with Databento
import datetime
import databento as db
import pandas as pd
def get_df_move(date: str = '2023-06-06'):
end_dt = datetime.datetime.strptime(date+'T16:00', '%Y-%m-%dT%H:%M')
@databento-bot
databento-bot / energies.py
Created February 21, 2024 12:06
Example of fetching CME and ICE energy futures data on Databento
import databento as db
client = db.Historical()
# Heating oil, gasoline and crude futures on CME and ICE
# .FUT parent symbology notation fetches all expirations across
# all outrights and multi-legged spreads etc.
CME_SYMBOLS = ['HO.FUT', 'RB.FUT', 'CL.FUT']
ICE_SYMBOLS = ['UHO.FUT', 'UHU.FUT', 'BRN.FUT']
@databento-bot
databento-bot / premarket.py
Created February 21, 2024 11:05
Example of getting largest premarket moves in US stocks with Databento
import databento as db
client = db.Historical()
data = client.timeseries.get_range(
dataset='XNAS.ITCH',
schema='ohlcv-1m',
symbols='ALL_SYMBOLS',
start='20230606T00:00',
end='20230606T14:30',
)