betting_combat.providers.kalshi.stream
stream.py — Kalshi’s WebSocket (market data only: no trading over it): the books, the trades and the market lifecycle of a card’s markets, as typed messages, in order.
BookSnapshot · BookDelta · TradePrint · Lifecycle · TickerUpdate market messagesFillUpdate · OrderUpdate · PositionUpdate · GroupUpdate our account's (signed)Reconnected the connection rebuiltKalshiStream ``messages()`` yields them, checking every subscription's sequence and rebuilding the connection on a gap (then ``Reconnected``: every book is re-snapshotted after it)BookMirror a card's books kept from the messages: top of book, the offers a buy can takeResync the stream lost its placeThe handshake is signed (GET /trade-api/ws/v2), public channels too. Every message on a
subscription carries its seq; a gap is a lost message. market_lifecycle_v2 is the
whole exchange (it cannot be filtered by market): lifecycle_prefix keeps ours.
Classes
BookDelta
One level’s change: delta contracts at price on side (yes | no bids).
BookMirror
A set of markets’ books kept from the stream’s messages. Kalshi books hold bids only: the YES ask is 1 - the best NO bid; buying YES takes the NO bids, buying NO the YES bids.
apply
apply(message: Message) -> NoneFold one message in; Resync on a delta before its snapshot or a negative size.
has
has(ticker: str) -> boollevel
level(ticker: str, side: str, price: float) -> floatThe contracts resting at price on side (yes | no bids); 0 when none.
offers
offers(ticker: str, side: str, limit: float) -> list[tuple[float, float]] | NoneWhat buying side can take at or below limit: (price, contracts), cheapest
first; None when the book is not mirrored.
top
top(ticker: str) -> tuple[float | None, float, float | None, float] | None(YES bid, its size, YES ask, its size); None when the book is not mirrored.
BookSnapshot
A market’s whole book: each side’s bids (price, contracts).
FillUpdate
One of our fills, pushed (fill): faster than polling /portfolio/fills.
GroupUpdate
An order group changed (order_group_updates): its limit hit, triggered, reset …
KalshiStream
The WebSocket of a set of markets: books and trades; the lifecycle, the ticker and our account’s channels when asked.
add
add(tickers: Iterable[str]) -> NoneFollow more markets on the open connection (a late-listed market): its book, its trades (and its ticker when followed). A connection not open yet subscribes them all.
close
close() -> Nonemessages
messages() -> AsyncIterator[Message]Every message in order, until close(); a break rebuilds the connection (1 s, 2 s,
… 15 s) and yields Reconnected first.
resnapshot
resnapshot(tickers: Iterable[str]) -> NoneA fresh book snapshot of tickers (a book out of step): dropped from the book
subscription and added back.
Lifecycle
A market’s lifecycle event: created, close_date_updated, determined (result,
settlement_value), settled, deactivated / activated (a pause: resting orders are
cancelled on reactivation), metadata_updated …
OrderUpdate
One of our orders changed (user_orders): resting, filled, cancelled …
PositionUpdate
Our position in a market changed (market_positions).
Reconnected
The connection was rebuilt: drop every book until its next snapshot.
Resync
Bases: Exception
The stream lost its place (a sequence gap, a delta before its snapshot, an error).
TickerUpdate
A market’s top of book and stats (ticker channel).
TradePrint
Functions
parse
parse(msg: dict[str, Any], received_at: dt.datetime) -> _Parsed | NoneOne raw message as a typed one (None: a reply or a channel we ignore).