From 421050292d9d5f296f05ff2d9345ff59475579b4 Mon Sep 17 00:00:00 2001 From: wvr Date: Sat, 21 Nov 2026 02:50:55 -0600 Subject: [PATCH] ya --- ...low_cost_first_five_min_CHECK-COST-ONLY.py | 45 +++++++++++++++++++ scripts/set_api_key.sh | 4 ++ 2 files changed, 49 insertions(+) create mode 100644 scripts/orderflow_cost_first_five_min_CHECK-COST-ONLY.py create mode 100644 scripts/set_api_key.sh diff --git a/scripts/orderflow_cost_first_five_min_CHECK-COST-ONLY.py b/scripts/orderflow_cost_first_five_min_CHECK-COST-ONLY.py new file mode 100644 index 0000000..de7954c --- /dev/null +++ b/scripts/orderflow_cost_first_five_min_CHECK-COST-ONLY.py @@ -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)") diff --git a/scripts/set_api_key.sh b/scripts/set_api_key.sh new file mode 100644 index 0000000..e2af95e --- /dev/null +++ b/scripts/set_api_key.sh @@ -0,0 +1,4 @@ +#!/bin/sh + +export DATABENTO_API_KEY="$(cat api.key)" +