stock-bot/tools/curate-etl/README.md

46 lines
2.9 KiB
Markdown

# `curate-etl`
Dedicated zero-dependency Rust crate for the `M1.2a` split-factor lookup table and bar un-adjustment core.
## Scope & Sign Conventions
Aligned with `design/DATA-CONTRACTS.md` (§1 `qmSplits`, `eodPrices`, `eodIntraday1M`):
- **Split ratio**: `ratio = new_shares / old_shares`.
- Forward split `10:1` (e.g., NVDA on `2024-06-10`) → `ratio = 10.0`.
- Reverse split `1:20` (e.g., NEXM.V on `2025-06-18`) → `ratio = 0.05`.
- No-op rows (`ratio == 1.0`) are dropped during `SplitTable::from_events`.
- Duplicate events on the same `ex_date` are multiplied (and dropped if reciprocal pairs cancel to `1.0`).
- Non-finite or `<= 0.0` ratios and compounding overflow/underflow return a typed `SplitError` rather than panicking.
- **Ex-date boundary (strict `>`):** `cum_factor_after(key, bar_date)` returns the product of all split ratios with `ex_date > bar_date`. A bar dated on an ex-date excludes that day's split (it is already post-split for that event) while still including any later splits (for example, NVDA on its `2021-07-20` 4:1 ex-date still has `factor = 10.0` from the later `2024-06-10` 10:1 split). Unknown keys and dates with no later splits return `1.0`.
- **`eodPrices` (daily bars):** OHLC is raw and only volume is retroactively split-adjusted:
- `raw_volume = unadjust_volume(adj_volume, factor) = adj_volume / factor`
- **`eodIntraday1M` (1m bars, reused in M2):** both OHLC and volume are retroactively split-adjusted:
- `raw_price = unadjust_price(adj_price, factor) = adj_price * factor`
- `raw_volume = unadjust_volume(adj_volume, factor) = adj_volume / factor`
- `unadjust_ohlcv(bar, factor)` applies both so dollar volume (`raw_close * raw_volume == adj_close * adj_volume`) is invariant.
## Local Benchmark
Fixed-seed (`std`-only SplitMix64), zero-allocation hot-loop benchmark over `20,000` symbol keys and `19,000` forward/reverse split events spanning `1990-01-01..=2025-12-31` (default `10,000,000` bar lookups + `unadjust_volume`):
```bash
cargo run --release -p curate-etl --offline -- bench-split [rows]
```
### Recorded Local Result
Command: `cargo run --release -p curate-etl --offline -- bench-split`
- Table: `keys=20000`, `keys_with_splits=12153`, `events=18999`, `build=1.73 ms`
- Run: `rows=10000000`, `elapsed=367.40 ms`, `throughput=27218476 rows/s`, `latency=36.74 ns/row`, `checksum=259124314244145.531`
## Deferred Follow-Ups (`M1.2b` / `M2`)
This crate currently uses `std` only so it builds strictly offline. Follow-up tasks (requiring crates.io access) will add:
- Read-only MongoDB `RawDocumentBuf` streaming cursors (`eodPrices`, `eodIntraday1M`, `qmSplits`)
- PostgreSQL `provider_mapping` preload keyed by `listing_id: i32` (supported via generic `SplitTable<K>`)
- `rayon` parallel sharding across symbol/date partitions
- Zstd-compressed Parquet writer for curated daily/intraday datasets
- QuestDB ILP v2 writer for time-series ingestion