This commit is contained in:
wvr committed 2026-11-26 08:32:04 -06:00
1 parent 3898609a52
commit 1cfbf11176
3003 files changed
+109 -6

No files matched your search

+45
View File
@@ -0,0 +1,45 @@
#!/usr/bin/env python3
"""Free cost estimate (downloads nothing) for opening-window trade data (aggressor side) on CME futures.
Samples ~30 trading days across the range, prices each day's window, and extrapolates to all trading days.
export DATABENTO_API_KEY=db-xxxxxxxx
python3 orderflow_cost.py # NQ, 9:29-9:34 ET, trades + tbbo, 2010-06 -> now
python3 orderflow_cost.py --symbols NQ GC CL --window 09:29 09:34
python3 orderflow_cost.py --symbols GC --window 08:19 08:24
"""
import argparse, os, sys
import databento as db
import pandas as pd
p = argparse.ArgumentParser()
p.add_argument("--symbols", nargs="+", default=["NQ"])
p.add_argument("--window", nargs=2, default=["09:29", "09:34"], help="ET start/end, e.g. 09:29 09:34")
p.add_argument("--schemas", nargs="+", default=["trades", "tbbo"])
p.add_argument("--start", default="2010-06-07")
p.add_argument("--end", default=None)
p.add_argument("--samples", type=int, default=30)
a = p.parse_args()
key = os.environ.get("DATABENTO_API_KEY") or sys.exit("Set DATABENTO_API_KEY first.")
c = db.Historical(key)
end = a.end or str(pd.Timestamp(c.metadata.get_dataset_range(dataset="GLBX.MDP3")["end"]).date())
days = pd.bdate_range(a.start, end) # weekdays (holidays make this a slight overestimate)
idx = [int(i) for i in pd.Series(range(len(days))).quantile([k / (a.samples - 1) for k in range(a.samples)]).round()]
sample = days[idx]
print(f"{len(days):,} weekdays {days[0].date()} -> {days[-1].date()}, window {a.window[0]}-{a.window[1]} ET, {len(sample)} sampled days\n")
total = 0.0
for sym in a.symbols:
for sch in a.schemas:
costs = []
for d in sample:
s = pd.Timestamp(f"{d.date()} {a.window[0]}", tz="America/New_York").tz_convert("UTC")
e = pd.Timestamp(f"{d.date()} {a.window[1]}", tz="America/New_York").tz_convert("UTC")
try:
costs.append(c.metadata.get_cost(dataset="GLBX.MDP3", schema=sch, stype_in="continuous",
symbols=[f"{sym}.v.0"], start=s.isoformat(), end=e.isoformat()))
except Exception as ex:
print(f" {sym} {sch} {d.date()}: {ex}"); continue
if not costs: continue
early, late = sum(costs[: len(costs) // 2]) / max(1, len(costs) // 2), sum(costs[len(costs) // 2:]) / max(1, len(costs) - len(costs) // 2)
est = sum(costs) / len(costs) * len(days); total += est
print(f"{sym:3s} {sch:7s} ~${est:8.2f} for all days (avg ${sum(costs)/len(costs):.4f}/day; earlier years ${early:.4f}, later ${late:.4f})")
print(f"\nEstimated total: ~${total:,.2f} (one request per day when downloading; front month = volume-based continuous contract)")