# `curate-etl` Dedicated zero-dependency Rust crate for the `M1.2a` split-factor lookup table / bar un-adjustment core, the `M1.2b` pure `adj_factors` builder, and the `M1.2c` pure `corporate_actions` event-log builder. ## 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. ## `adj_factors` Builder (`M1.2b`) `build_adj_factors` (`src/adj.rs`) turns split (`qmSplits`) and cash-dividend (`qmDividends`) event streams into sorted, unique per-`(key, ex_date)` rows matching the `adj_factors/part.parquet` columns in `design/DATA-CONTRACTS.md` (`listing_id, ex_date, split_factor, div_cash, div_currency, cum_split_factor, cum_total_factor`), omitting `usdFxRate` per contract. > **Contract interpretation note:** `design/DATA-CONTRACTS.md` names `split_factor`, `div_cash`, `div_currency`, `cum_split_factor`, and `cum_total_factor` without writing formulas. Rules 3–5 below document this crate's back-adjustment divisor definitions so downstream consumers and reviewers can verify parity with `SplitTable::cum_factor_after`. Rules: 1. **Split normalization:** Drop `ratio == 1.0` no-ops (counted in `stats.split_noops_dropped`). Multiply same-day splits in `f64::total_cmp` order; if they cancel back to `1.0` (e.g. `2.0 * 0.5`), drop the split component (and omit the row if no dividend is present on that date). 2. **Dividend normalization:** Drop `cash == 0.0` events (counted in `stats.dividend_zero_cash_dropped`). Sum same-day same-currency cash dividends in `f64::total_cmp` order into `div_cash`. Split-only rows carry `div_cash = 0.0` and `div_currency = None`; dividend-only rows carry `split_factor = 1.0`. Two different dividend currencies on the same `(key, ex_date)` return `AdjError::ConflictingDividendCurrency`. 3. **`cum_split_factor` (back-adjustment divisor):** Product of `split_factor` over this row and all later rows of the same key (inclusive of the row's own `ex_date`). For any bar dated `d`, the first row with `ex_date > d` (`adj_factors_after(rows, key, d).0`) matches `SplitTable::cum_factor_after(key, d)` bitwise. 4. **Per-row dividend factor:** `prev_close / (prev_close - div_cash)` when `prev_close` (the raw close of the last session strictly before `ex_date`) is present, finite, `> div_cash > 0.0`, and `prev_close_currency == div_currency`. Otherwise falls back to `1.0` in strict priority order while preserving `div_cash` and `div_currency` on the row and incrementing the matching counter: - `dividends_missing_prev_close` (`prev_close` is `None` or non-finite) - `dividends_currency_mismatch` (`prev_close_currency != Some(div_currency)`) - `dividends_not_below_prev_close` (`prev_close <= div_cash`) 5. **`cum_total_factor`:** `cum_split_factor` multiplied by the product of per-row dividend factors over this row and all later rows of the same key (inclusive of the row's own `ex_date`). 6. **Validation & overflow guards:** Non-finite or `<= 0.0` split ratios (`InvalidSplitRatio`), non-finite or `< 0.0` dividend cash (`InvalidDividendCash`), empty/whitespace currency on positive cash (`EmptyDividendCurrency`), conflicting same-day dividend currencies (`ConflictingDividendCurrency`), and overflow/underflow of any combined or cumulative factor (`FactorOverflow`) return a typed `AdjError` without panicking. Deferred out of `src/adj.rs`: cross-currency dividend FX conversion (waits for `fx_1d`). Point-in-time `known_at` clamping and delisting / Shumway rows live in `src/corporate_actions.rs`. ## `corporate_actions` Builder (`M1.2c`) `build_corporate_actions` (`src/corporate_actions.rs`) turns split (`qmSplits`), cash-dividend (`qmDividends`), and Pass 6 delisting outcome streams into a deterministically sorted event log matching the `corporate_actions/part.parquet` columns in `design/DATA-CONTRACTS.md` (`listing_id, ex_date, declared_date, kind, ratio, cash_amount, currency, successor_listing_id, terminal_return, known_at, known_at_source`). Rules: 1. **Splits (`kind = "split"`):** Same `ratio = new_shares / old_shares` convention as `src/split.rs`. `ratio == 1.0` no-ops are dropped and counted in `stats.split_noops_dropped`. Non-finite or `<= 0.0` ratios return `CorporateActionError::InvalidSplitRatio`. 2. **Dividends (`kind = "dividend"`):** Local `currency` and `cash` are preserved unchanged (deliberately omitting `usdFxRate`). `cash == 0.0` events are dropped and counted in `stats.dividend_zero_cash_dropped`. Multiple dividends on the same `(key, ex_date)` remain separate event-log rows. Non-finite or `< 0.0` cash returns `InvalidDividendCash`; empty/whitespace currency on positive cash returns `EmptyDividendCurrency`. 3. **Point-in-time `known_at` clamping:** Uses `declared_date` when present and `<= ex_date` (`known_at_source = "declared_date"`). Otherwise falls back to `ex_date` (`known_at_source = "ex_date_fallback"`), preserving the raw `declared_date` on the row and counting `declared_date > ex_date` in `stats.declared_after_ex_date` so `known_at_date <= ex_date` always holds. 4. **Delistings (Pass 6):** - `successor_key = Some(s), terminal_return = None` → one `otc_delist_transfer` row (`ex_date = known_at_date = delist_date`, `known_at_source = "delist_date"`). Callers must pass the Pass 6 continuity `effectiveDate` (the successor's first observed OTC trading date) as `delist_date`, not the primary listing's last exchange print / halt date. - `successor_key = None, terminal_return = Some(r)` → one `delisting` row (`r` must be finite and in `[-1.0, 0.0]`, normalizing `-0.0` to `0.0`, else `InvalidTerminalReturn`). - Both `None` → dropped and counted in `stats.delist_without_outcome`. - Both `Some` → `CorporateActionError::ConflictingDelistingOutcome`. 5. **Determinism & accounting:** Output rows are sorted by `(key, ex_date, kind, declared_date, ratio_bits, cash_bits, currency, successor_key, terminal_return_bits, known_at_date, known_at_source)` so any input permutation yields byte-identical output, and `stats` satisfies `in == out + dropped` per kind and `known_at_declared_date + known_at_ex_date_fallback + known_at_delist_date == rows_out`. ## Local Benchmarks ### `bench-split` 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] ``` 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` ### `bench-adj` Fixed-seed (`std`-only SplitMix64) benchmark over `20,000` `i32` `listing_id` keys spanning `1990-01-01..=2025-12-31` with ~4% splits and ~96% dividends (default `1,000,000` mixed events), timing only `build_adj_factors`: ```bash cargo run --release -p curate-etl --offline -- bench-adj [events] ``` Command: `cargo run --release -p curate-etl --offline -- bench-adj` - Input: `events=1000000`, `splits=39861`, `dividends=960139`, `keys=20000` - Build: `rows_out=986702`, `keys_out=20000`, `elapsed=242.94 ms`, `throughput=4116183 events/s`, `latency=242.94 ns/event`, `checksum=17888280.062 (0x41710f4180ff5d60)` (bitwise identical across back-to-back runs) - Stats: `split_noops=1988`, `zero_div=9527`, `missing_prev_close=19089`, `currency_mismatch=19147`, `not_below_prev_close=9693` ### `bench-corporate-actions` Fixed-seed (`std`-only SplitMix64) benchmark over `20,000` `i32` `listing_id` keys spanning `1990-01-01..=2025-12-31` with ~4% splits, ~92% USD/CAD dividends, and ~4% delisting outcomes (default `1,000,000` mixed events), timing only `build_corporate_actions`: ```bash cargo run --release -p curate-etl --offline -- bench-corporate-actions [events] ``` Command: `cargo run --release -p curate-etl --offline -- bench-corporate-actions` - Input: `events=1000000`, `splits=39872`, `dividends=920134`, `delistings=39994`, `keys=20000` - Build: `rows_out=980694`, `keys_out=20000`, `elapsed=107.48 ms`, `throughput=9303979 events/s`, `latency=107.48 ns/event`, `checksum=0xbfd2ceda373762f5` (identical across back-to-back runs) - Stats: `split_noops=1963`, `zero_div=9302`, `delist_no_outcome=8041`, `otc_transfers=11942`, `delistings=20011`, `declared=758098`, `ex_fallback=190643`, `declared_after_ex=19108`, `delist_date=31953` ## Deferred Follow-Ups (`M1.2d+` / `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`) - `rayon` parallel sharding across symbol/date partitions - Zstd-compressed Parquet writer for curated daily/intraday datasets - QuestDB ILP v2 writer for time-series ingestion