This commit is contained in:
wvr committed 2026-11-21 20:00:22 -06:00
1 parent 421050292d
commit ce36cba8e9
2978 files changed
+35317 -85

No files matched your search

+44
View File
@@ -0,0 +1,44 @@
#!/usr/bin/env python3
"""Download NQ front-month trades (aggressor side) for a short window each weekday. One request per day,
saved under raw_of/ and skipped on re-run (safe to restart after a timeout). Then builds one CSV.
export DATABENTO_API_KEY=db-xxxxxxxx
python3 orderflow_get.py # NQ, 09:29-09:32 ET, 2010-06-07 -> 2015-01-01 (~$13)
python3 orderflow_get.py --start 2016-01-01 --end 2026-11-21 # later, only if the test passes
"""
import argparse, glob, os, sys, time
import databento as db
import pandas as pd
p = argparse.ArgumentParser()
p.add_argument("--symbol", default="NQ"); p.add_argument("--window", nargs=2, default=["09:29", "09:32"])
p.add_argument("--start", default="2010-06-07"); p.add_argument("--end", default="2015-01-01")
p.add_argument("--yes", action="store_true", help="skip the confirmation prompt")
a = p.parse_args()
key = os.environ.get("DATABENTO_API_KEY") or sys.exit("Set DATABENTO_API_KEY first.")
c = db.Historical(key); os.makedirs("raw_of", exist_ok=True)
days = [d for d in pd.bdate_range(a.start, a.end) if d < pd.Timestamp(a.end)]
todo = [d for d in days if not os.path.exists(f"raw_of/{a.symbol}_{d.date()}.dbn.zst")]
print(f"{len(days)} weekdays, {len(todo)} still to download, window {a.window[0]}-{a.window[1]} ET")
if todo and not a.yes and input("Download (billed by Databento)? [y/N] ").strip().lower() != "y": sys.exit("cancelled")
for k, d in enumerate(todo, 1):
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")
out, tmp = f"raw_of/{a.symbol}_{d.date()}.dbn.zst", f"raw_of/{a.symbol}_{d.date()}.part"
for attempt in range(4):
try:
c.timeseries.get_range(dataset="GLBX.MDP3", schema="trades", stype_in="continuous", symbols=[f"{a.symbol}.v.0"],
start=s.isoformat(), end=e.isoformat(), path=tmp)
os.replace(tmp, out); break
except Exception as ex:
if os.path.exists(tmp): os.remove(tmp)
if attempt == 3: sys.exit(f"stopped at {d.date()} ({ex}); just run the command again to resume")
time.sleep(5 * (attempt + 1))
if k % 50 == 0 or k == len(todo): print(f" {k}/{len(todo)} done", flush=True)
frames = []
for f in sorted(glob.glob(f"raw_of/{a.symbol}_*.dbn.zst")):
if not (a.start <= f.split("_")[-1][:10] < a.end): continue
df = db.DBNStore.from_file(f).to_df()
if len(df): frames.append(df.reset_index()[["ts_event", "price", "size", "side", "instrument_id"]])
X = pd.concat(frames); X["ts_et"] = pd.to_datetime(X.pop("ts_event"), utc=True).dt.tz_convert("America/New_York")
name = f"{a.symbol}_trades_{a.window[0].replace(':','')}-{a.window[1].replace(':','')}_{a.start[:4]}-{str(int(a.end[:4]) - (1 if a.end[5:] == '01-01' else 0))}.csv.gz"
X.to_csv(name, index=False); print(f"wrote {name}: {len(X):,} trades, {X.ts_et.dt.date.nunique()} days")
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loaded 100 of 2978 files, more files were not shown because too many files have changed in this diff. Show more