betting_combat.trading.working
Strategy 4’s execution: every bet is a working order, not one shot.
A decision sizes each bet once (rounds.live.decision: Kelly x the meta factor, the
order, fight and card caps; no volume cap) and reserves that room. The card run then works
the bet until it is filled or its window closes (trading.session: while the round’s bell
is not known, portfolio.exec_window_seconds after the decision, 20 s by default, 0: one
order at the decision only; once the market’s bell is known, up to
exec_window_bell_seconds but never past the bell + 60 s - break_margin_seconds), one
SLICE a second (SLICE_EVERY; slices run while the window is open, the first one always):
1. the books are read again (the WebSocket mirror)2. a decision pair's second leg behind its first is caught up first: priced alone (``price_catch_up``), against what the first leg actually cost, at most its own limit3. the bet's instruments are taken cheapest all-in first AT THE BOOKS NOW (rounds: the rounds market; distance: the distance market or the decision pair), each only while its side and edge still hold at the price shown (``price_instrument``)4. each leg is an immediate-or-cancel buy at its limit, which never moves for the whole window: the decision's quote + the cushion (``LegPlan.cap``), and never above the dearest price that still clears ``min_edge`` after the fee5. contracts: whole, sized on the limits (the worst fill), inside the room left; a pair's second leg never more than the first has filled6. what filled is spent; the next slice works what is left. A live order whose fate is unknown counts at its limit and ends the bet (nothing is sent next to it)So a fill is never above the decision’s price + the cushion, the reserved room is never
passed, and a book that refills inside the window is bought again. The window stops early
when the room is spent, the fight’s winner books price the result, the bell market jumps
back above 50c (CONTRARY_BELL: the fight ended between rounds), the fight ends, the kill
switch is set, the strategy is switched off or changes mode, or a slice fails (the strategy
is then off for the card, as after a failed decision).
Paper orders fill against the live book with what the slices already took: a price level
gives at most the most it ever showed (PaperBook), never the same contracts twice (an
offer that moves to another price counts as new: paper is slightly optimistic there, and
its slices after the first fill on the book of the same instant, with no round trip).
Classes
BetPlan
One bet of one decision: the room it reserved and every instrument it may buy.
InstrumentPlan
One way to hold the bet: one market, or the decision pair’s two legs. p_yes: a
single market’s P(YES) (its side and edge are checked again on every book); None for
the pair (its edge is its legs’ chances against both asks). contracts: what the
decision allotted it (the research’s route; the cheapest takes the room).
The joint execution (rounds.live.joint): max_contracts caps what the instrument
may fill (the rebalance’s contracts; a pair: per leg); action ‘sell’ sells a holding
— on Kalshi, a buy of the other side, which nets against it — each leg’s side the side
BOUGHT and its cap the dearest price paid for it (the decision’s bid less the cushion
is the lowest sold at), with no edge test (the rebalance chose to sell).
LegPlan
One market an instrument buys: its dataset name, Kalshi role and ticker, the side, the price it showed at the decision and the limit held for the whole window (that price + the cushion, whole cents, at most 99c), and the chance its contracts pay.
PaperBook
What paper slices took from each price level: a level gives at most the most it ever showed in the window, less what was taken from it (a level that refills above that is new liquidity; one that shows the same contracts again is not).
take
take(market_id: str, side: str, limit: float, count: int, levels: Sequence[tuple[float, float]]) -> tuple[float, float | None]Fill up to count contracts from levels (price, size on our side, cheapest
first) at or below limit: (contracts, their average price).
Priced
An instrument on the books now: each leg’s limit, the price and size shown, and the most one contract (of each leg) can cost filled at the limits, fees in.
WorkingBet
A bet being worked: what it has filled per market and spent, all fees in.
add
add(market_id: str, contracts: float, cost: float) -> NoneContracts filled and their cost; an order of unknown fate adds its most it can
cost with no contracts (spent only).
deficit
deficit(inst: InstrumentPlan) -> floatA pair’s second leg behind its first (contracts); 0 for one market.
paid
paid(market_id: str) -> floatWhat one filled contract of the market cost on average, fees in (0: none).
pending
pending() -> floatDollars held back for pair legs still to catch up (at their limits).
remaining
remaining(inst: InstrumentPlan) -> floatContracts the instrument may still fill (its first leg; no cap: infinite).
Functions
child_summary
child_summary(client_order_id: str, slice_no: int, leg: LegPlan, instrument: str, limit: float, shown: float, count: int, filled: float, avg: float | None, cost: float, at: dt.datetime) -> dict[str, Any]One child order as the decision’s record keeps it (bought[].orders): the price
its market showed at the decision (shown) and when it was sent (sent_price), its
limit and contracts, what filled.
contracts_for
contracts_for(dollars: float, worst: float) -> intWhole contracts whose most they can cost (worst each) stays inside dollars.
price_catch_up
price_catch_up(inst: InstrumentPlan, books: Mapping[str, BookTop | None], paid: float, now: dt.datetime, stale_seconds: float, min_edge: float) -> Priced | strA decision pair’s second leg alone, to match what the first leg filled: its book now
(fresh, two-sided), its price at most its own limit, and the pair still clearing
min_edge with the first leg at what it actually cost (paid per contract, the fee
in) — the first leg is sunk, so its own book no longer matters. The limit never passes
the leg’s cap nor the dearest price that keeps that edge.
price_instrument
price_instrument(inst: InstrumentPlan, books: Mapping[str, BookTop | None], now: dt.datetime, stale_seconds: float, min_edge: float) -> Priced | strThe instrument on the books now (books: Kalshi role -> its book), or why it
cannot be bought this slice: a book missing or stale, the price already priced (the
instrument’s mid outside 3-97c), the side changed or the edge gone at the price shown,
or the price above the limit.
price_sell
price_sell(inst: InstrumentPlan, books: Mapping[str, BookTop | None], now: dt.datetime, stale_seconds: float) -> Priced | strA sell on the books now: each leg’s book fresh and two-sided, and the side bought shown at or below the leg’s cap (the floor of the sale). No edge test: the rebalance decided to sell; no price band (a holding near 0 or 1 still sells, as the backtest’s; the winner books’ result gate stops a decided fight).
worked_legs
worked_legs(legs: Sequence[Mapping[str, Any]], bets: Sequence[WorkingBet], children: Sequence[Mapping[str, Any]], ended: str, window_s: int, slices: int, timing: Mapping[str, Any] | None, unsent: str = 'edge_gone_at_send', decided_by: str | None = None, bell_at: dt.datetime | None = None) -> tuple[dict[str, Any], ...]The decision’s legs once its working orders ended: each instrument’s orders become
the children it sent (orders), and the bet gains exec — what decided the break
(decided_by: the feed or the market’s bell) and the bell the market showed, the
window, the slices, why it ended, the room, what was spent and filled per market. A bet that sent nothing
takes unsent as its reason (why nothing went out).