betting_combat.rounds.market.windows
Set 3: round-start market features from the Kalshi trades, one row per (fight_id, round_idx) like the round dataset, built only from what happened before the row’s time.
Ported from the research (ufc/dataset/set3.py: window_stats, Set3.row_times,
Set3.windows, Set3.main, Set3.side, Set3.implied, Set3.write).
Row time t: the market’s bell that ended round r (round_idx r >= 1: the live decision’s
instant, row_times); for round_idx 0 the end of the card’s previous listed fight
(pre_anchor). A row for every round that started and that the live rule can place (no
cut at the winner market’s snap, which is only known afterwards; the dataset keeps the
rounds that started). Windows, with edges E = [start-60, start-30, start, start+6,
start+12, …]:
last_ [E[r+1], t) the round just fought (round 1: the 30 minutes before the fight)prev_ [E[r], E[r+1]) the window before thatsofar_ [start, t) the fight so farpre_ [start-30, min(start, t)) (round 0 stops at its own time t)lastmin_ [t-1, t)Trades are read in (ts, trade_id) order (:func:~.trades.time_order): the last price of a
window never depends on how a sort breaks ties.
Main market (A-framed: a trade in B’s market is its mirror in A’s): mid_a, spread (cents), oi,
mins_since_trade, chg_since_pre, per window trades, contracts, dollars, avg_trade, big_trades
(>= $500), blocks, flow, vwap_a, chg_a, range_a, vol_a, jumps; surprise. Side markets
(:data:SIDE): implied) with their change over
the last window. A window with no trades: counts 0, moves 0, prices carried from the last
trade before it (else the book at t).
Functions
at_bell
at_bell(f: Any, round_idx: int) -> boolThe row of round_idx decides at the market’s bell (row_times: its time is the
bell’s fire).
bell_quote
bell_quote(m: pd.DataFrame | None, t: pd.Timestamp) -> pd.Series | NoneThe QUOTE a bell decision at t is priced at in the backtests: per market the DEARER of
the minute ended by t (book_at) and the break’s first close (book_after) — the
higher ask, the lower bid. The second review (2026-10-11), against the recorded books at
the fire + 0/2/5 s on 2026-10-03 and 10-10: the break’s close alone was 1.6-2.7c cheap on
the rounds market, the last print since the bell 1.2-1.75c cheap on distance YES; the
dearer of the two minutes is dearer than the live book on every leg (+0.4c rounds NO to
+3.5c pair legs). Features never read it (book_at / known_book).
book_after
book_after(m: pd.DataFrame | None, t: pd.Timestamp) -> pd.Series | NoneThe minute panel’s first minute that ends at or after t (its close: up to a minute
after t), else the last one before. The QUOTE a bell decision is priced at in the
backtests: the panel’s last minute before the fire mostly closed before the bell, in the
round, where a ‘lasts’ bet looks cheap (the post-card review, 2026-10-11: the distance ask
2.3c low, the rounds bid 6.4c high against the live books); the break’s first close is a
later, dearer price (the market moves toward the result as the break goes on) — a
conservative stand-in for the live book at the fire. Features never read it (book_at).
book_at
book_at(m: pd.DataFrame | None, t: pd.Timestamp) -> pd.Series | NoneThe minute panel’s row knowable at t: the last minute that ended at or before t (a row’s ts is its minute’s end); None without one. On the minute grid it is t’s own row.
decision_time
decision_time(f: Any, round_idx: int) -> pd.TimestampThe row’s (and its candidate bets’) decision time (placed_time); raises for a row
row_times does not place.
drop_unpriceable
drop_unpriceable(side: pd.DataFrame) -> tuple[pd.DataFrame, dict[str, list[str]]]The side set’s complete-core rule, as live applies it: a listed side market that had
no book or trade before a row cannot be priced, and the fight leaves the side set from
that row on (trading.rounds: side_ok turns off; its earlier rows keep their side
features). Until 2026-10-11 the whole fight left, its earlier rows too: a later row
decided an earlier one (the CV audit). Returns the kept rows and fight_id -> the columns
that were empty in its dropped rows.
implied
implied(mids: Mapping[str, Any], main_mid_a: float) -> dict[str, Any]Probabilities implied by the side markets: p_dec_dist (distance), p_dec_mov and p_a_mov (shares of all MOV), p_finish_mov, ko_share (of the finishes), gap_dec, gap_a.
known_book
known_book(m: pd.DataFrame | None, tr: pd.DataFrame | None, mk: str, t: pd.Timestamp) -> tuple[float, float, float, float]Market mk as known at t: (bid, ask, oi) from the minute panel’s last minute ended by
t (book_at), and last, the market’s own last trade (YES price) in [that minute’s
end, t) — NaN without. Its mid is last when there is one, else the book’s: the
training rows’ and the replay’s stand-in for the live book at t (live.windows.Book
carries last the same way). tr: the market’s trades (ts, yes_price), any order.
known_mid
known_mid(book: tuple[float, float, float, float]) -> floatThe mid of a known_book: its last trade, else the book’s (bid + ask) / 2.
known_quote
known_quote(m: pd.DataFrame | None, trades: pd.DataFrame | None, t: pd.Timestamp) -> pd.Series | NoneThe books as KNOWN at t (the decision’s view: the candidate’s side, edge and the meta
model’s price features): the minute panel’s last minute ended by t (book_at), each
market’s bid and ask moved with its own last trade since that minute (its mid = that
trade, the minute’s spread kept; known_book’s rule), the bid down and the ask up to
the cent, inside (0, 1).
trades: the fight’s tapes (market, ts, yes_price; any order). The canary audit
(2026-10-11): the bell rows took these from the minute ENDING after t (book_after),
up to 60 s past the decision; the PRICE paid stays bell_quote’s, dearer.
last_price_since
last_price_since(tr: pd.DataFrame, lo: pd.Timestamp, t: pd.Timestamp) -> floatThe last traded price in [lo, t) (tr: time-ordered, a price column), else NaN:
what the book learned after the panel’s last minute and before t.
placed_time
placed_time(f: Any, round_idx: int) -> pd.Timestamp | NoneThe row’s decision time as row_times places it; None for a row it does not place
(with BELL_CLOCK: round 0 without an anchor, a later round without its market bell).
prior_fill
prior_fill(frame: pd.DataFrame, cols: list[str], dates: pd.Series) -> pd.DataFrameframe[cols] with each missing value set to the column’s median over the rows dated
strictly before its row (dates, aligned to frame), else SPREAD_PRIOR.
round0_time
round0_time(f: Any) -> pd.Timestamp | NoneThe round-0 row’s time: the minute after the card’s previous listed fight ended
(pre_anchor, ceiled to the minute); None without one (no round-0 row). Raises when
the fights frame lacks the column (with_pre_anchor).
row_start
row_start(f: Any) -> pd.TimestampThe fight’s start as the rows place it: with BELL_CLOCK, the earliest market bell
j that fired, less round j’s end on the clock (bell_j - (6j - 1) min: round 1’s bell -
5:00 when it fired); else the fight clock’s start. The fight clock is anchored on how the
fight ENDED (its finish time, or the distance market at the final bell), so its start
leaks the outcome (the post-card review, 2026-10-11: decisions sat ~23 s earlier than
finishes against the bell); every row at or after round 1 has its own bell, so its start
never comes from that clock. The live run places it the same way (trading.rounds: the
round-1 bell - 5:00, else the feed’s anchor less FEED_LAG). f: a fights-frame row
(a Series, or a namedtuple).
row_times
row_times(f: Any) -> list[tuple[int, pd.Timestamp, list[pd.Timestamp]]](round_idx, row time, window edges E) for every row of a fight the live rule places,
in round order (the dataset keys keep the rounds that started). f needs start,
rounds, snap; with BELL_CLOCK also pre_anchor and its bell_k / fire_k.
With BELL_CLOCK: round 0 at round0_time (none without an anchor), its edges from
that time + 1 min; round k >= 1 only where the market’s bell of round k fired (the live
decision: its time is the bell’s fire, round k+1’s edge the bell + 60 s, the start
row_start). A round without its market bell has no row: the live run decides at the
bell, so a row timed by the result-anchored clock instead (the cards before the rounds
markets traded, Feb-Apr 2026) has no live counterpart (the CV audit, 2026-10-11).
set3_main
set3_main(trades_main: pd.DataFrame, minutes: pd.DataFrame, fights: pd.DataFrame, causal: bool | None = None) -> pd.DataFrameThe winner-market rows (research Set3.main), before the dataset keys are attached.
Args:
trades_main: :func:~.trades.trade_tables main table.
minutes: :func:~.minutes.set4_minutes.
fights: :func:~.listing.live_fights rows.
causal: no window of a row reaches past the row’s time (the round-0 sofar
window, see :func:window_bounds): the live rule, the default since the CV
audit (2026-10-11); False = the research; None: True, the research’s False
under RESEARCH.
set3_side
set3_side(trades_side: pd.DataFrame, minutes: pd.DataFrame, fights: pd.DataFrame, markets: pd.DataFrame, main: pd.DataFrame) -> pd.DataFrameThe side-market rows (research Set3.side) for the fights listing all of
:data:SIDE, before the dataset keys are attached.
Args:
trades_side: :func:~.trades.trade_tables side table.
minutes: :func:~.minutes.set4_minutes.
fights: :func:~.listing.live_fights rows.
markets: :func:~.listing.market_list rows (fight_id, name).
main: :func:set3_main (its mid_a).
window_bounds
window_bounds(r: int, t: pd.Timestamp, edges: list[pd.Timestamp], causal: bool = False) -> dict[str, Window]The row’s windows as (lo, hi), lo <= ts < hi. pre ends at min(start, t): the
round-0 row (t = the minute before the start) does not read [t, start) (research fixed
2026-09-30).
causal: the research’s round-0 sofar is the empty window at the start, so its
carried price (sofar_vwap_a) is the last trade before the START, not before t. True
makes it [min(start, t), t), the live rule: nothing at or after t. False reproduces the
research. Rows at or after the start are the same either way.
window_stats
window_stats(tr: pd.DataFrame, lo: Any, hi: Any, carry: float) -> dict[str, Any]Trade statistics for trades with lo <= ts < hi. tr columns: ts, price (A-framed or
the market’s own YES price), count, dollars, sign (+1 buying the priced side), block;
carry is the price before the window.
with_keys
with_keys(keys: pd.DataFrame, rows: pd.DataFrame) -> pd.DataFrameThe dataset’s keys (fight_id, round_idx, event_id, event_date) inner-joined to Set 3 rows, in the dataset’s order.
with_pre_anchor
with_pre_anchor(fights: pd.DataFrame) -> pd.DataFramefights with pre_anchor: the snap of the fight before it on its card (fights
of an event in start order; NaT for the card’s first listed fight). The round-0 row’s
clock (round0_time).