Order-book engine¶
The stateful core: order events in, book snapshots and order lifecycles out. This is the only part of the library that knows how a book is rebuilt, and the only part that holds no pandas — everything crosses its interface as NumPy arrays, so the inside can be made faster without anything above it moving. Both replay loops, the L2 depth replay and the queue reconstruction, are compiled with numba this way.
Most users never call it directly. Its frame-level faces are
order_book,
order_lifecycles,
and
the functions in ob_analytics.queue; they convert to and from these arrays.
What crosses the interface¶
OrderEvents is the shared event schema (data contracts) in
column form. Results carry a row index back into those arrays rather than
copying the event's own columns out, so a caller reads the
exchange_timestamp, the classifier type, the venue and symbol, or anything
else it tracks off its own table at that row. Adding a column to the schema
therefore never widens this interface, and the engine never has to learn a
vocabulary — order types, venue names — that belongs to the layer above.
Timestamps are int64 nanoseconds since the epoch, UTC: a time zone is a presentation detail, so it is stripped on the way in and re-attached on the way out. Prices are integer ticks; the engine only compares and subtracts them, so a float column from a pre-tick frame still works.
Categorical columns arrive as integer codes — Direction, Action, Outcome.
Each derives the schema's string from its own member name, so the integer the
engine compares and the label a frame carries cannot fall out of step.
Input¶
OrderEvents
dataclass
¶
OrderEvents(
order_id: ndarray,
timestamp: ndarray,
price: ndarray,
volume: ndarray,
direction: ndarray,
action: ndarray,
fill: ndarray | None = None,
is_market: ndarray | None = None,
is_hidden: ndarray | None = None,
resting_price: ndarray | None = None,
)
One order-event stream as parallel numpy columns.
Every array has the same length: one entry per event, in stream order.
Ordering contract
Rows must arrive in the venue's canonical event order — the total order
:func:ob_analytics.schemas.time_order_keys defines (timestamp, then
the tie-breaks the frame carries). At minimum, the rows belonging to one
order_id must be chronological: the reconstructions read the last row
per order as that order's current state, and replay the stream front to
back. Sorting is the caller's job, so the engine never has to guess which
tie-break columns a venue publishes.
Attributes:
| Name | Type | Description |
|---|---|---|
order_id |
ndarray
|
The venue's per-order identifier ( |
timestamp |
ndarray
|
Receive-clock time as int64 nanoseconds since the epoch, UTC. The zone is dropped on the way in and re-attached on the way out, so the engine compares plain integers. |
price |
ndarray
|
Price as a whole number of ticks ( |
volume |
ndarray
|
The order's outstanding size after the event ( |
direction |
ndarray
|
:class: |
action |
ndarray
|
:class: |
fill |
ndarray or None
|
Quantity executed at this event ( |
is_market |
ndarray or None
|
|
is_hidden |
ndarray or None
|
|
resting_price |
ndarray or None
|
The price the row's order rests at once the row is applied, in the
same units as :attr: |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the columns are not all one-dimensional and the same length. |
visible ¶
Rows of orders that can join the visible queue, optionally on one side.
Hidden orders (:attr:is_hidden) have no public identity, and market
orders never rest, so neither joins the visible queue. Both
queue reconstructions select rows through here, and so does any caller
that needs to size a window over the same rows the engine will replay —
the rule lives in one place.
require_fill ¶
Return :attr:fill, or raise when the caller omitted it.
market_mask ¶
Return :attr:is_market, or an all-False mask when it is absent.
Codes¶
Code ¶
Bases: IntEnum
An integer code that also knows the schema's string for itself.
The engine stores and compares the integer; a caller converting to or from a DataFrame needs the string. Deriving one from the other here means the two can never drift apart, which a hand-maintained parallel list of labels would eventually do.
DIRECTIONS
module-attribute
¶
Direction labels, indexed by :class:Direction code.
ACTIONS
module-attribute
¶
Action labels, indexed by :class:Action code.
OUTCOMES
module-attribute
¶
Outcome labels, indexed by :class:Outcome code.
NAT_NS
module-attribute
¶
Sentinel for "no time" in an int64-nanosecond column.
It is the integer NumPy stores for datetime64 NaT, so a caller turns a
nanosecond column into a datetime column — sentinels and all — with a plain
values.view("datetime64[ns]").
Reconstructions¶
order_book ¶
Reconstruct the order book at one instant.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
events
|
OrderEvents
|
The event stream, in canonical order. |
required |
at
|
int
|
The instant to evaluate the book at, in nanoseconds since the epoch (UTC). Events after it are ignored. |
required |
uncross
|
bool
|
When |
False
|
Returns:
| Type | Description |
|---|---|
BookSides
|
Both sides, best price first, each carrying the row that put every resting order in its current state. |
order_lifecycles ¶
order_lifecycles(
events: OrderEvents,
*,
fill_tolerance: float = DEFAULT_FILL_TOLERANCE,
) -> OrderLifecycles
Collapse events into one row per order.
Termination follows the schema's volume contract: an order ends when a
deleted row arrives or its outstanding size reaches zero. The
second is how a fully executed order ends on a venue that emits no delete
for it (LOBSTER); the created row itself is excluded from the test so a
zero-size placement does not terminate itself.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
events
|
OrderEvents
|
The event stream, in canonical order, carrying |
required |
fill_tolerance
|
float
|
How far short of its placed size an order's executed total may fall and
still count as fully filled. Defaults to
:data: |
DEFAULT_FILL_TOLERANCE
|
Returns:
| Type | Description |
|---|---|
OrderLifecycles
|
One row per submitted order, ordered by first placement. |
DEFAULT_FILL_TOLERANCE
module-attribute
¶
Default tolerance on "fully executed" (:func:order_lifecycles).
Canonical sizes are integer lots, where the tolerance does nothing: two distinct lot counts differ by at least 1. It earns its keep on a frame carrying float sizes in the base asset — the display frame the gallery builds, or a pre-4.0 file — where the fills summed over one order can fall an epsilon short of the placed size. Such an order counts as filled when its executed total reaches its placed size to within this much.
queue_positions ¶
Reconstruct the FIFO queue position of each visible limit order over time.
Price-time priority: a created event appends to the back of its level; a
size reduction (partial fill or partial cancel) keeps the order's place; a
deleted — or a reduction to zero — removes it. A move to a new resting
price takes the order to the back of its new level. Market orders never
join a queue.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
events
|
OrderEvents
|
The event stream, in canonical order. Order matters here more than anywhere else in the engine: it is the queue's priority. |
required |
touch_only
|
bool
|
Keep only the events where the order rests at the best bid or ask at
that instant — the input to the touch-queue faces. |
True
|
Returns:
| Type | Description |
|---|---|
QueuePositions
|
One row per surviving order event, two for a move. |
queue_age_grid ¶
Snapshot one side's touch queue at each of the instants at.
Replays the side's events and, at every sample instant, records the age of
each order resting at the best price by FIFO rank. Orders are queued as
:func:queue_positions queues them: at their resting price, with market
orders left out.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
events
|
OrderEvents
|
The event stream, in canonical order. |
required |
side
|
Direction
|
Which touch to compose. |
required |
at
|
ndarray
|
Sample instants in int64 nanoseconds, ascending. The caller chooses the window and the spacing; the engine only replays to them. |
required |
Returns:
| Type | Description |
|---|---|
QueueAgeGrid
|
The age-by-rank grid and its depth. |
crossed_prefix_counts ¶
crossed_prefix_counts(
bid_prices: ndarray,
bid_ts: ndarray,
ask_prices: ndarray,
ask_ts: ndarray,
) -> tuple[int, int]
How many best-end bids / asks to evict to uncross two book sides.
bid_prices descend from the best bid, ask_prices ascend from the best
ask, each paired with its order timestamp. Walks the touch: while the top
bid is priced at or above the top ask (crossed, or locked when equal), evict
the older of the two touching orders, so the fresher quote is trusted.
This is the library's one copy of the rule.
:func:~ob_analytics.engine.order_book calls it with orders, and
:class:~ob_analytics.engine.PriceLevelBook with price levels and the
order they were set in, which is how the depth summary uncrosses its touch.
The evicted orders are exactly the contiguous best-end prefixes, so the two
returned counts describe the eviction completely.
Results¶
BookSides
dataclass
¶
The book at one instant: both sides, best price first.
The engine's array form of a book snapshot.
:func:ob_analytics.book.order_book turns it into the
:class:~ob_analytics.book.BookSnapshot a user gets.
The instant itself is not repeated here — the caller supplied it as
:func:order_book's at and still holds it.
Attributes:
| Name | Type | Description |
|---|---|---|
bids |
BookSide
|
Resting bids, highest price first. |
asks |
BookSide
|
Resting asks, lowest price first. |
BookSide
dataclass
¶
One side of a reconstructed book, best price first.
The identity of each resting order — its id, both clocks, and outstanding
size — is not copied here: :attr:row points at the event that left the
order in this state, so a caller reads any column it wants straight off its
own event table. Only the derived quantities travel.
Attributes:
| Name | Type | Description |
|---|---|---|
row |
ndarray
|
For each resting order, the index in the :class: |
price |
ndarray
|
The price each order rests at: its
:attr: |
liquidity |
ndarray
|
Cumulative outstanding volume from the touch down to and including this
order ( |
bps |
ndarray
|
Distance from this side's own touch, in basis points ( |
OrderLifecycles
dataclass
¶
OrderLifecycles(
order_id: ndarray,
created_row: ndarray,
filled_vol: ndarray,
end_ts: ndarray,
outcome: ndarray,
)
One row per order, in the order the orders were placed.
As with :class:~ob_analytics.engine.BookSide, the placement columns are
not copied: :attr:created_row points at the order's created event, so
a caller reads the placement price, size, direction, and any label it
attached (the classifier type, the placement aggressiveness) off its own
event table at that row.
Orders with no created row are absent — a pre-existing opening book and
hidden executions have no placement to anchor a lifecycle to.
Attributes:
| Name | Type | Description |
|---|---|---|
order_id |
ndarray
|
The order's identifier. |
created_row |
ndarray
|
Index in the :class: |
filled_vol |
ndarray
|
Total quantity executed over the order's life, in the units the events
carried: integer lots ( |
end_ts |
ndarray
|
Termination time in int64 nanoseconds, or :data: |
outcome |
ndarray
|
:class: |
QueuePositions
dataclass
¶
QueuePositions(
row: ndarray,
price: ndarray,
action: ndarray,
rank: ndarray,
queue_len: ndarray,
ahead_volume: ndarray,
remaining: ndarray,
age_s: ndarray,
)
One row per order event, reporting that order's place in its level.
A move to a new price gives two rows for its event: the order leaving its old level, then joining its new one.
The event's own columns — time, order id, direction — are not copied:
:attr:row points back at the event in the :class:OrderEvents arrays.
Attributes:
| Name | Type | Description |
|---|---|---|
row |
ndarray
|
Index in the :class: |
price |
ndarray
|
The price level the order is queued at: its resting price, which can differ from the price on the event's row. |
action |
ndarray
|
What the event did to the queue, as
:class: |
rank |
ndarray
|
1-based position from the front of the level ( |
queue_len |
ndarray
|
Number of orders resting at the level ( |
ahead_volume |
ndarray
|
Outstanding size of the orders ahead of this one, in the size dtype the
events carried — |
remaining |
ndarray
|
This order's own outstanding size after the event, in that same dtype. |
age_s |
ndarray
|
Seconds since the order was placed ( |
QueueAgeGrid
dataclass
¶
Touch-queue composition over time: the age of the order at each rank.
Attributes:
| Name | Type | Description |
|---|---|---|
ages |
ndarray
|
A |
max_rank |
int
|
The deepest the touch queue got over the sampled window — the number of
rows in :attr: |