betting_combat.feeds.legacy
legacy.py — what the legacy card runs recorded in prod (betting.betting_combat), taken over
onto our ids: the Kalshi books of the live cards and the trading ledger.
LegacyFeed ``build`` = ``books`` + ``ledger`` (prod read-only; the local tables written) books ``kalshi_orderbook`` (+ its snapshots and streams) → ``books``, one market of one stream at a time, each recorded in ``legacy_imports`` once landed whole: a rerun skips it, a unit cut short is cleared and landed again (``books`` is append-only: nothing doubles) ledger ``decisions`` / ``orders`` / ``fills`` / ``settlements`` → ours (upserted on the legacy's own deterministic ids), every ticker or fight not on our ids reportedbook_rows one market's legacy rows as ``books`` rows (pure: what the import writes)The legacy book (store.orderbook): a snapshot level’s size is the resting contracts, a
delta’s is the SIGNED CHANGE (found 2026-11-10 on prod’s rows: deltas run negative, e.g. -77.52),
so each delta is replayed onto the market’s book and lands as the level’s new size, as the live
recorder writes it (live.recorder: 0 removes the level). A snapshot of an empty book (1,862
of prod’s 5,079: a market subscribed with no orders, or emptied) lands as one marker row,
yes_bid at price 0 size 0 (no level of a real book): a reader resets the book at every
snapshot group (stream, seq), the marker included. A delta that drives a level below zero (a
lost message) leaves the market’s book unknown: its deltas are skipped, counted (resyncs),
until its next snapshot. stream_id is the legacy’s (random, one per subscription), so its
seq is in order within it.
The ledger maps the legacy’s strategy run onto our contracts: every legacy order is a buy (of
YES or NO, outcome), sent IOC (the legacy runs sent immediate-or-cancel only); a decision’s
round_idx is the fight minute // 5 (the break after round k is k; a mid-round decision, the
round under way’s breaks so far), its mode its orders’ (no order: its strategy’s on that card,
else paper); its scalars (model and market chances, edge, sizes) in chances, the book it
priced in prices, its legs in legs.
Classes
LegacyFeed
Bases: Feed
Prod’s legacy recordings onto our ids (see the module doc).
books
books(cards: Iterable[dt.date] | None = None) -> dict[str, dict[str, int]]Every (stream, market) of the cards (all recorded cards: None) not imported yet.
build
build(cards: Iterable[dt.date] | None = None) -> dict[str, Any]ledger
ledger(cards: Iterable[dt.date] | None = None) -> dict[str, dict[str, int]]The legacy runs’ decisions, orders, fills and settlements, on our ids.
load
load() -> IdsFunctions
book_rows
book_rows(market_id: str, stream_id: int, rows: Iterable[tuple[Any, ...]], empty: Iterable[tuple[int, dt.datetime]] = ()) -> tuple[list[dict[str, Any]], int]One market’s legacy book rows (seq, side, price, kind, size, received_at, exchange_ts, in seq order) and its empty snapshots (seq, received_at) as
books rows (the level’s size after each change), and the deltas skipped out of step.