Skip to content

Order book

The book at an instant and the per-order lifecycles, as DataFrames. The reconstructions themselves live in the order-book engine; these are their frame-level faces.

Both functions are also exported from the package root (from ob_analytics import order_book, order_lifecycles). On a run, result.book(at=...) and result.lifecycles() return the same tables and keep them, so a second call costs nothing (see PipelineResult).

order_book

order_book(
    events: DataFrame,
    *,
    at: datetime | None = None,
    max_levels: int | None = None,
    bps_range: int = 0,
    uncross: bool = False,
) -> BookSnapshot

Reconstruct the order book at a specific point in time.

The reconstruction itself is :func:ob_analytics.engine.order_book; this function is its frame adapter, and owns the display window (max_levels, bps_range) the engine has no opinion about.

Parameters:

Name Type Description Default
events DataFrame

DataFrame containing order events.

required
at datetime or Timestamp

The point in time at which to evaluate the order book. If None, uses the latest event timestamp in the data.

None
max_levels int

The maximum number of price levels to include for bids and asks.

None
bps_range int

Basis points range to filter the bids and asks. Default is 0.

0
uncross bool

When True, evict crossed resting orders so the snapshot satisfies best_bid < best_ask — a display convenience, by the rule the depth summary uncrosses its touch with. At the crossed touch, the order whose latest event is older goes first, and orders at one price are walked in that order. This is the library's only way to uncross a per-order book: to draw an uncrossed book_snapshot or depth_chart, pass the book it returns. The default is False: the reconstruction stays faithful to the feed, so a diff feed's genuinely crossed resting orders (see :class:~ob_analytics.protocols.FeedType) are replayed as-is rather than silently uncrossed. Has no effect on a matched-book feed, which is never crossed.

False

Returns:

Type Description
BookSnapshot

The book at at: timestamp (the evaluation instant), bids (active bid orders, best first) and asks (active ask orders, best last).

Each order's price is the price it rests at: the price of its latest created row, or of a later changed row with no fill. A row that reports a fill does not move the order, so price can differ from the price on the order's latest row. The depth table and the queue tables place orders at the same price.

BookSnapshot dataclass

BookSnapshot(
    timestamp: datetime | Timestamp,
    bids: DataFrame,
    asks: DataFrame,
)

The order book at one instant, as :func:order_book returns it.

Attributes:

Name Type Description
timestamp datetime or Timestamp

The instant the book was evaluated at.

bids DataFrame

Active bid orders, best first.

asks DataFrame

Active ask orders, best last.

order_lifecycles

order_lifecycles(events: DataFrame) -> pd.DataFrame

Collapse events into one row per order: placement → outcome.

The canonical lifecycle table (one derivation, shared by the L3 faces and the order-book reconstruction). A thin frame wrapper over :func:ob_analytics.engine.order_lifecycles, which relies on the schemas.py volume contract: volume is the outstanding size after each event and fill the executed delta, so an order is terminated when a deleted row arrives or its outstanding size reaches zero — the latter is how fully-executed LOBSTER orders end, which never emit a deleted event.

Parameters:

Name Type Description Default
events DataFrame

Events satisfying the schemas.py contract. Orders without a created row (pre-existing book, hidden executions) are excluded — their placement is unknown.

required

Returns:

Type Description
DataFrame

One row per order id:

  • id, direction, price — placement identity.
  • type — classifier label (when the column is present).
  • placed_ts, placed_vol — from the created row.
  • filled_vol — total executed quantity (Σ fill).
  • end_ts — termination time (NaT while still resting; callers clip to their window end for display).
  • outcome — filled / partial / cancelled / resting. When type comes from the classifier, every cancelled-limit order has the outcome cancelled.
  • aggressiveness_bps — placement distance (when present).

fill_rate_by_distance

fill_rate_by_distance(
    lifecycles: DataFrame,
    *,
    distance_bins: int | Sequence[float] = 10,
) -> pd.DataFrame

Measure how often orders filled, by how far from the touch they were placed.

Orders are grouped by their placement distance (aggressiveness_bps: the distance from the best price on their own side, in basis points, positive when the order improved the touch). Each group counts how its orders ended. Orders still resting are left out, because their outcome is not known yet; so are orders with no placement distance.

Parameters:

Name Type Description Default
lifecycles DataFrame

One row per order, as :func:order_lifecycles returns it, with outcome and aggressiveness_bps.

required
distance_bins int or sequence of float

An int is the number of bins. Their edges are cut where each bin holds about the same number of orders, and rounded to two significant figures. Bins whose edges round to the same value merge, so there can be fewer. A sequence gives the edges between bins, in basis points. Either way the first bin holds every distance below the first edge, and the last bin every distance at or above the last edge. Default 10.

10

Returns:

Type Description
DataFrame

One row per bin, from the most negative distance (deepest in the book) to the most positive:

  • distance_low / distance_high — the bin's edges, in basis points. A bin holds distances from distance_low up to, but not including, distance_high. The first bin's distance_low is -inf and the last bin's distance_high is inf.
  • n_orders — orders in the bin whose life ended.
  • n_filled / n_partial / n_cancelled — of those, the orders that filled in full, filled in part and were then removed, and were removed with no fill.
  • fill_rate / partial_rate — n_filled and n_partial as a share of n_orders, from 0 to 1. NaN for a bin with no orders.

An empty frame with these columns when no order has both an ended outcome and a placement distance.

Raises:

Type Description
ConfigError

If required columns are missing, distance_bins is an int below one, or one of its edges is not a finite number.