asf
This commit is contained in:
1 parent
3898609a52
commit
1cfbf11176
3003 files changed
+109
-6
No files matched your search
@@ -0,0 +1,6 @@
|
||||
pip install --user duckdb python3-zstd databento
|
||||
|
||||
#### pip3 install --user --break-system-packages databento
|
||||
|
||||
python csv_to_parquet.py "raw/*.gz" data
|
||||
|
||||
@@ -0,0 +1,103 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Convert 1-minute futures bar CSVs (Databento-style continuous contracts) to Parquet.
|
||||
|
||||
Input columns : ts_et,open,high,low,close,volume,instrument_id
|
||||
ts_et like "2015-01-01 18:00:00-05:00" (bar OPEN time, US/Eastern with offset)
|
||||
Input files : e.g. NQ_1m_2015-2026_csv.gz, ES_1m_....csv (symbol = text before the first "_")
|
||||
|
||||
Output : <out>/symbol=NQ/year=2015/*.parquet (year = session year)
|
||||
|
||||
Columns written
|
||||
ts TIMESTAMPTZ bar open time (UTC internally; displays in your session TZ)
|
||||
ts_et TIMESTAMP same instant as naive US/Eastern wall-clock time
|
||||
session_date DATE CME trading date: the 18:00 ET Sunday bar belongs to Monday's session
|
||||
open/high/low/close/volume UNADJUSTED prices of the contract trading at that moment
|
||||
instrument_id BIGINT contract id; changes at each roll
|
||||
is_roll BOOLEAN first bar of a new contract -> the bar-to-bar return INTO this bar is
|
||||
a contract switch, not a market move. Exclude it from return calcs.
|
||||
roll_gap DOUBLE on roll bars: new open - old close (approx. calendar spread), else 0
|
||||
adj_add DOUBLE back-adjustment, additive: adj_price = price + adj_add
|
||||
adj_mult DOUBLE back-adjustment, multiplicative: adj_price = price * adj_mult
|
||||
(both = 0 / 1 on the latest contract; they change when you append data)
|
||||
|
||||
Usage
|
||||
pip install duckdb
|
||||
python csv_to_parquet.py "raw/*.gz" data
|
||||
python csv_to_parquet.py "raw/*.csv" data
|
||||
"""
|
||||
import argparse
|
||||
import time
|
||||
|
||||
import duckdb
|
||||
|
||||
|
||||
def main() -> None:
|
||||
ap = argparse.ArgumentParser()
|
||||
ap.add_argument("inputs", help='glob of CSV / CSV.gz files, e.g. "raw/*_1m_*.gz"')
|
||||
ap.add_argument("out", help="output directory for partitioned Parquet")
|
||||
args = ap.parse_args()
|
||||
|
||||
t0 = time.time()
|
||||
con = duckdb.connect()
|
||||
con.sql("SET TimeZone = 'America/New_York'") # makes ts::TIMESTAMP = ET wall clock
|
||||
|
||||
con.sql(f"""
|
||||
CREATE TEMP TABLE bars AS
|
||||
SELECT
|
||||
split_part(parse_filename(filename), '_', 1) AS symbol,
|
||||
CAST(ts_et AS TIMESTAMPTZ) AS ts,
|
||||
open, high, low, close, volume, instrument_id
|
||||
FROM read_csv('{args.inputs}', header = true, filename = true,
|
||||
columns = {{'ts_et':'VARCHAR','open':'DOUBLE','high':'DOUBLE','low':'DOUBLE',
|
||||
'close':'DOUBLE','volume':'BIGINT','instrument_id':'BIGINT'}})
|
||||
""")
|
||||
|
||||
# Data checks that should never fail; stop rather than write bad data.
|
||||
dup = con.sql("SELECT count(*) FROM (SELECT 1 FROM bars GROUP BY symbol, ts HAVING count(*) > 1)").fetchall()[0][0]
|
||||
bad = con.sql("""SELECT count(*) FROM bars WHERE high < greatest(open, close) OR low > least(open, close)
|
||||
OR open <= 0 OR volume < 0""").fetchall()[0][0]
|
||||
if dup or bad:
|
||||
raise SystemExit(f"Data check failed: {dup} duplicate timestamps, {bad} invalid OHLC rows")
|
||||
|
||||
con.sql("""
|
||||
CREATE TEMP TABLE out AS
|
||||
WITH r AS (
|
||||
SELECT *,
|
||||
ts::TIMESTAMP AS ts_et,
|
||||
CAST(ts::TIMESTAMP + INTERVAL 6 HOUR AS DATE) AS session_date,
|
||||
coalesce(instrument_id <> lag(instrument_id) OVER w, false) AS is_roll,
|
||||
lag(close) OVER w AS prev_close
|
||||
FROM bars
|
||||
WINDOW w AS (PARTITION BY symbol ORDER BY ts)
|
||||
), g AS (
|
||||
SELECT *,
|
||||
CASE WHEN is_roll THEN open - prev_close ELSE 0 END AS roll_gap,
|
||||
CASE WHEN is_roll THEN ln(open / prev_close) ELSE 0 END AS roll_lr
|
||||
FROM r
|
||||
)
|
||||
SELECT symbol, ts, ts_et, session_date, year(session_date) AS year,
|
||||
open, high, low, close, volume, instrument_id, is_roll, roll_gap,
|
||||
coalesce(sum(roll_gap) OVER f, 0) AS adj_add,
|
||||
exp(coalesce(sum(roll_lr) OVER f, 0)) AS adj_mult
|
||||
FROM g
|
||||
WINDOW f AS (PARTITION BY symbol ORDER BY ts ROWS BETWEEN 1 FOLLOWING AND UNBOUNDED FOLLOWING)
|
||||
ORDER BY symbol, ts
|
||||
""")
|
||||
|
||||
con.sql(f"""
|
||||
COPY out TO '{args.out}'
|
||||
(FORMAT parquet, PARTITION_BY (symbol, year), COMPRESSION zstd, OVERWRITE_OR_IGNORE)
|
||||
""")
|
||||
|
||||
print(con.sql("""
|
||||
SELECT symbol, count(*) AS bars, min(ts_et) AS first_bar_et, max(ts_et) AS last_bar_et,
|
||||
count(DISTINCT session_date) AS sessions, sum(is_roll::INT) AS rolls,
|
||||
round(arg_min(adj_mult, ts), 4) AS first_bar_adj_mult
|
||||
FROM out GROUP BY symbol ORDER BY symbol
|
||||
"""))
|
||||
print(f"done in {time.time() - t0:.1f}s -> {args.out}/")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Binary file not shown.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loaded 100 of 3003 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user