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 |
False
|
Returns:
| Type | Description |
|---|---|
BookSnapshot
|
The book at at: Each order's |
BookSnapshot
dataclass
¶
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 ¶
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
|
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
One row per order id:
|
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: |
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
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
One row per bin, from the most negative distance (deepest in the book) to the most positive:
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. |