#!/usr/bin/env python3 """Fetch XSP (mini-SPX) options daily OHLCV for the equity-index VRP test. HARD-CAPPED. get_cost-gated: prints the exact cost and ABORTS if above CAP — no surprise charges. Single dataset/schema/symbol/window. ohlcv-1d only (NOT definition). """ import os import sys import databento as db CAP = 70.00 # hard cap — abort if cost exceeds this DS, SCH, SYM = "OPRA.PILLAR", "ohlcv-1d", "XSP.OPT" START, END = "2013-04-01", "2026-06-05" OUT = "data/surfer/xsp_ohlcv1d.dbn" def main(): c = db.Historical(os.environ["DATABENTO_API_KEY"]) if os.path.exists(OUT): print(f"already downloaded: {OUT}"); return 0 cost = c.metadata.get_cost(dataset=DS, symbols=[SYM], schema=SCH, start=START, end=END, stype_in="parent") gb = c.metadata.get_billable_size(dataset=DS, symbols=[SYM], schema=SCH, start=START, end=END, stype_in="parent") / 1e9 print(f"EXACT get_cost = ${cost:.2f} ({gb:.2f} GB) CAP = ${CAP:.2f}") if cost > CAP: print(f"ABORT: ${cost:.2f} exceeds cap ${CAP:.2f} — NOTHING downloaded.") return 1 print(f"under cap by ${CAP - cost:.2f} -> downloading {SYM} {SCH} year-chunked (same total cost)") os.makedirs("data/surfer/xsp", exist_ok=True) for yr in range(2013, 2027): ys = f"{yr}-01-01" if yr > 2013 else "2013-04-01" ye = f"{yr+1}-01-01" if yr < 2026 else "2026-06-05" out = f"data/surfer/xsp/xsp_{yr}.dbn" if os.path.exists(out): print(f" {yr}: exists"); continue for attempt in range(3): try: d = c.timeseries.get_range(dataset=DS, symbols=[SYM], schema=SCH, start=ys, end=ye, stype_in="parent") d.to_file(out) print(f" {yr}: saved ({os.path.getsize(out)/1e6:.1f} MB)") break except Exception as e: print(f" {yr}: attempt {attempt+1} {type(e).__name__}; retry") print(f"DONE -> data/surfer/xsp/") return 0 if __name__ == "__main__": sys.exit(main())