.
This commit is contained in:
1 parent
c38665cde0
commit
2670041747
11 files changed
+235
No files matched your search
@@ -0,0 +1,72 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Download NQ front-month 1-minute bars from Databento into a gzipped CSV.
|
||||
|
||||
Setup (one time):
|
||||
1. Sign up at https://databento.com (new accounts get $125 in free data credits)
|
||||
2. Copy your API key from the portal (API Keys page)
|
||||
3. pip install -U databento pandas
|
||||
|
||||
Run:
|
||||
export DATABENTO_API_KEY=db-xxxxxxxxxxxxxxxx # Windows: set DATABENTO_API_KEY=...
|
||||
python get_nq_1m.py # 2018-01-01 -> latest available
|
||||
python get_nq_1m.py --start 2020-01-01 --symbol MNQ.v.0
|
||||
|
||||
The script prints the estimated cost and asks before downloading anything.
|
||||
Output columns: ts_et (US/Eastern, bar open time), open, high, low, close, volume, instrument_id.
|
||||
instrument_id changes on roll days, so contract switches are visible in the data.
|
||||
"""
|
||||
import argparse
|
||||
import os
|
||||
import sys
|
||||
|
||||
import databento as db
|
||||
import pandas as pd
|
||||
|
||||
DATASET = "GLBX.MDP3" # CME Globex
|
||||
SCHEMA = "ohlcv-1m"
|
||||
|
||||
p = argparse.ArgumentParser()
|
||||
p.add_argument("--start", default="2018-01-01")
|
||||
p.add_argument("--end", default=None, help="exclusive end date; default = latest available")
|
||||
p.add_argument("--symbol", default="NQ.v.0",
|
||||
help="continuous front month, rolled by volume (default NQ.v.0; MNQ.v.0 for micros)")
|
||||
p.add_argument("--out", default="nq_1m.csv.gz")
|
||||
p.add_argument("--all-hours", action="store_true",
|
||||
help="keep the full overnight session instead of trimming to 08:00-16:15 ET")
|
||||
p.add_argument("-y", "--yes", action="store_true", help="skip the cost confirmation")
|
||||
a = p.parse_args()
|
||||
|
||||
key = os.environ.get("DATABENTO_API_KEY")
|
||||
if not key:
|
||||
sys.exit("Set DATABENTO_API_KEY first (see the top of this file).")
|
||||
client = db.Historical(key)
|
||||
|
||||
end = a.end
|
||||
if end is None:
|
||||
rng = client.metadata.get_dataset_range(dataset=DATASET)
|
||||
end = (rng.get("schema", {}).get(SCHEMA, {}) or {}).get("end") or rng["end"]
|
||||
|
||||
params = dict(dataset=DATASET, schema=SCHEMA, stype_in="continuous",
|
||||
symbols=[a.symbol], start=a.start, end=end)
|
||||
|
||||
cost = client.metadata.get_cost(**params)
|
||||
print(f"{a.symbol} {SCHEMA} {a.start} -> {end}: estimated cost ${cost:.2f}")
|
||||
if not a.yes and input("Download? [y/N] ").strip().lower() != "y":
|
||||
sys.exit("Cancelled.")
|
||||
|
||||
print("Downloading...")
|
||||
df = client.timeseries.get_range(**params).to_df(tz="America/New_York")
|
||||
df = df[["open", "high", "low", "close", "volume", "instrument_id"]]
|
||||
df.index.name = "ts_et"
|
||||
|
||||
if not a.all_hours:
|
||||
t = df.index.strftime("%H:%M")
|
||||
df = df[(t >= "08:00") & (t <= "16:15")]
|
||||
|
||||
df.to_csv(a.out, float_format="%.2f")
|
||||
|
||||
days = df.index.normalize().nunique()
|
||||
rolls = int((df["instrument_id"].diff().fillna(0) != 0).sum())
|
||||
print(f"Wrote {a.out}: {len(df):,} bars, {days} trading days, "
|
||||
f"{df.index[0]} -> {df.index[-1]}, {rolls} contract rolls")
|
||||
print("Attach that file in the chat.")
|
||||
Reference in new issue
Block a user