Skip to content

betting_combat.features.price_bars

price_bars — information bars from a venue’s trades (López de Prado, AFML ch. 2; Sam 2026-11-20): the rows of a market sampled by what traded, not by the clock. One market’s trades in (time, YES price, contracts, the side that took), its bars out - every bar type the same columns, bar_type telling them apart.

BAR_TYPES tick / volume / dollar a bar closes when its trades / contracts / dollars reach a fixed size
*_imbalance ... when the SIGNED flow |sum b v| reaches E[T] |E[b v]| (b = +1 a YES
buyer, -1 a NO buyer: the venue prints the taker's side, no tick rule)
*_runs ... when the larger ONE-SIDED total max(sum v of YES buyers, sum v of NO
buyers) reaches E[T] max(P E[v | YES], (1 - P) E[v | NO])
(v: 1 a trade / its contracts / its dollars - what the taker paid: YES price for a YES buyer, 1 - it for NO)
the expectations (imbalance, runs) exponential averages, FIXED WITHIN A BAR and renewed at each close from the ticks
and bars so far (the book's E0): E[T] over the bars' lengths (ticks), E[b v] / P / E[v | side] over the
ticks. They start from the market's WARM-UP ticks (its trades before the fight) and ``t0`` (the ticks a
standard bar of this market is expected to hold). The known failure - E[T] running away (one bar a fight)
or collapsing (a bar a trade) - is bounded: E[T] kept in [t_lo, t_hi], |E[b v]| never under ``floor`` x E[v].
standard_sizes each market's tick / volume / dollar bar size, AS OF ITS BELL: its kind's in-fight flow on EARLIER
cards (an exponential average over cards of in-fight / pre-fight flow) x the market's own pre-fight flow
(a main event trades far more than a prelim), over the bars wanted a market (``target``); a card never
sees its own or a later card's in-fight flow. A kind's first card has no size (no bars).
make_bars one market's bars of every type (pure: no clock, no database)

A bar: bar_type, bar_no, complete (closed by its threshold; a market’s last bar may not be), threshold (the size it had to reach), start_at / end_at (its first and last trade), trades, contracts, dollars, yes_* / no_* (the YES buyers’ and NO buyers’ trades, contracts, dollars), imbalance_* (YES - NO), open / high / low / close / vwap (YES prices as printed), close_yes / close_no (the last YES price a YES buyer paid - the ask’s side - and a NO buyer took - the bid’s side - by the bar’s end, the warm-up’s too; close jumps between the two sides from trade to trade: a move is measured on ONE side or on close_mid, their middle), run_yes / run_no (the longest run of consecutive YES-buyer / NO-buyer trades).

Functions

between_events

between_events(bars: pd.DataFrame, members: pd.DataFrame | None = None) -> tuple[pd.DataFrame, pd.DataFrame | None]

ONE group’s bars (with cusum_events’ columns, in order) summed into one row an EVENT: the bars after the event before up to and including the event bar. Sums for the flow, the first bar’s open, the event bar’s close and carried prices, the highest high / lowest low, the longest runs, the dollar-weighted VWAPs; start_at the first bar’s, end_at the event bar’s; bars (how many went in). members the same per market. Keyed by event_no.

blocks_over

blocks_over(stream: pd.DataFrame, bars: pd.DataFrame, warm: pd.DataFrame | None = None, markets: pd.DataFrame | None = None) -> tuple[pd.DataFrame, pd.DataFrame]

ANOTHER group’s blocks over a DRIVER’s bars (one bar_type’s rows of make_group_bars / make_bars, in order): for each driver bar the group’s trades after the bar before’s end up to this bar’s end (the first bar: from its own start; the group’s trades before that only carry prices in, as warm does). The same blocks and members as the group’s own bars (_blocks), a row a driver bar whether the group traded in it or not, keyed by the driver’s bar_no; a carried price’s age is counted to the DRIVER bar’s end. markets: as make_group_bars.

cusum_events

cusum_events(close: np.ndarray, h: float = 2.0, span: int = 20, floor: float = 0.02, min_bars: int = 5) -> pd.DataFrame

The symmetric CUSUM filter on ONE group’s bars in order. close: the group’s price at each bar’s end (blank bars carry the last). Two running sums of the price’s changes (log-odds), up and down, each floored at 0; a bar where either reaches the threshold is an EVENT, and both reset. The threshold = h x the spread of the changes so far (an exponential standard deviation over span changes, from the bars BEFORE this one), never under floor (log-odds; a dead market cannot trigger on one tick); no event in the first min_bars bars (nothing to measure the spread from). Returns, a row a bar: chg (the change), vol (the spread used), cusum_h (the threshold), cusum_up, cusum_down (the sums AFTER the bar), is_event, event_no (how many events so far, the event bar counting itself), bars_since_event.

group_stream

group_stream(trades: pd.DataFrame, groups: pd.DataFrame) -> pd.DataFrame

Every group’s trades in one stream. trades: market_id, ts, p (the YES price), n (contracts), side (‘yes’ / ‘no’: the taker’s), in the venue’s order within a market; groups: market_groups.group_map. A trade of a market in two groups is in both streams. Returns, in each group’s time order (the same instant: by market, then the venue’s order): group_id, ts, market_id, slot, role, p, n, yes (the taker bought YES), b (+1 the taker bet TOWARD the group’s outcome, -1 against: the mirror’s sides turned), d (the dollars the taker paid), gp (the price in the GROUP’s terms: the main’s and a same’s own, 1 - a mirror’s; blank for a part - the parts’ prices sum to the group’s).

imbalance_ids

imbalance_ids(b: np.ndarray, v: np.ndarray, n_warm: int, t0: float, span_ticks: float, span_bars: float, t_lo: float, t_hi: float, floor: float) -> tuple[np.ndarray, np.ndarray, int]

Imbalance bars (module doc). b +1 / -1, v each tick’s size; the first n_warm ticks only seed the expectations (id -1). Returns (ids, each tick’s threshold as its bar had it, the bars closed).

make_bars

make_bars(trades: pd.DataFrame, sizes: dict[str, float], warm: pd.DataFrame | None = None, types: tuple[str, ...] = BAR_TYPES, span_ticks: float = 100.0, span_bars: float = 10.0, bound: float = 4.0, floor: float = 0.1) -> pd.DataFrame

ONE market’s bars. trades: its in-fight trades - ts, p (the YES price), n (contracts), side (‘yes’ / ‘no’: the taker’s) - in the order printed; warm: its trades before (they seed the imbalance / runs expectations, never a bar); sizes: its tick / volume / dollar bar sizes (standard_sizes; a type whose size is missing: no bars). The imbalance / runs bars’ E[T] starts at the ticks a standard TICK bar holds (sizes[‘tick’]) and stays within [that / bound, that x bound]. Returns the bars of every type asked (module doc), in (bar_type, bar_no) order.

make_group_bars

make_group_bars(stream: pd.DataFrame, sizes: dict[str, float], warm: pd.DataFrame | None = None, markets: pd.DataFrame | None = None, types: tuple[str, ...] = ('dollar', 'dollar_runs'), span_ticks: float = 100.0, span_bars: float = 10.0, bound: float = 4.0, floor: float = 0.1) -> tuple[pd.DataFrame, pd.DataFrame]

ONE group’s bars, cut on ALL its markets’ trades together (group_stream’s rows of the group, in order; warm: its rows before the fight - they seed the expectations and the carried prices, never a bar; markets: the group’s markets as the map lists them - market_id, slot, role - so a bar never depends on which of them trade LATER; None: the markets in the trades given). As make_bars for the sizes, the types and the expectations (b = toward / against the group’s outcome). Returns (bars, members).

bars - a row a bar: bar_type, bar_no, complete, threshold, start_at, end_at FLOW (every market of the group): trades, contracts, dollars; toward_* / against_* (the buyers betting toward the outcome / against it: trades, contracts, dollars); imbalance_* (toward - against); run_toward / run_against THE GROUP’S PRICE from its direct markets (main, mirror, same - their trades in the group’s terms): open, high, low, close, vwap_direct, direct_dollars; close_toward / close_against (the last price a toward-buyer paid / an against-buyer took, carried forward), close_mid THE PARTS’ PRICE (a group with parts): parts_close (the sum of the last price of every part that has printed so far), parts_vwap (the same sum with a part’s VWAP in the bar where it traded in it), parts_priced / parts_total (how many of the group’s parts that sum holds / the group has: a sum of fewer is not the whole price), parts_oldest_s (how long before the bar’s end the stalest of them last printed: a part nobody trades any more keeps its old price), parts_dollars vwap the whole group’s: vwap_direct and parts_vwap weighted by the dollars each side of the group traded in the bar members - a row a bar x market of the group (every one, traded in the bar or not): bar_type, bar_no, market_id, slot, role, trades, contracts, dollars, yes_ / no_ trades, contracts, dollars (the YES buyers’ and the NO buyers’), imbalance_* (YES - NO), run_yes / run_no (its own longest runs), open, high, low, close, vwap (its own YES prices; blank with no trade in the bar), last_yes / last_no (the last YES price a YES buyer paid / a NO buyer took there, carried forward - the warm-up’s too) and last_yes_at / last_no_at (when that was).

njit

njit(*a: Any, **k: Any) -> Any

runs_ids

runs_ids(b: np.ndarray, v: np.ndarray, n_warm: int, t0: float, span_ticks: float, span_bars: float, t_lo: float, t_hi: float) -> tuple[np.ndarray, np.ndarray, int]

Runs bars (module doc): the larger one-sided total against E[T] max(P E[v | YES], (1 - P) E[v | NO]). As imbalance_ids.

standard_ids

standard_ids(v: np.ndarray, size: float) -> tuple[np.ndarray, int]

Each tick’s bar: a bar closes ON the tick that takes its total to size (the next tick starts a new one, from zero). Returns (ids, the bars closed).

standard_sizes

standard_sizes(V: pd.DataFrame, target: dict[str, float], span: float = 10.0, scaled: bool = True, floors: dict[str, float] | None = None) -> pd.DataFrame

Each market’s standard bar sizes AS OF ITS BELL (module doc). V: a row a market - market_id, kind, date (its card’s day), pre_tick / pre_volume / pre_dollar (its flow before the fight), in_tick / in_volume / in_dollar (in the fight); target: the bars wanted a market, by kind. scaled: the kind’s in-fight / pre-fight ratio on earlier cards x the market’s own pre-fight flow (a market with none: the kind’s earlier mean in-fight flow); else that mean alone. Returns market_id, size_tick / size_volume / size_dollar (blank: the kind’s first card).

wide_rows

wide_rows(bars: pd.DataFrame, members: pd.DataFrame, prefix: str = '') -> pd.DataFrame

One row a bar with everything on it: the bar’s own columns as group_* and every market of the group as <slot>_* (main_close, part_a_last_no, part_b_r1_vwap …); prefix: put before both (the group’s name when several groups share a row: ko_group_close, ko_part_a_last_yes). One bar_type’s rows; keyed by bar_no.