Skip to content

Queue positions

Frame-level functions for the engine's queue reconstruction: each resting order's place in its price level's queue. The arrays underneath are on the Order-book engine page.

queue_positions is also exported from the package root (from ob_analytics import queue_positions). On a run, result.queue() returns the same table and keeps it (see PipelineResult).

queue_positions

queue_positions(
    events: DataFrame, *, levels: str = "touch"
) -> pd.DataFrame

Reconstruct FIFO queue position for each visible limit order over time.

Parameters:

Name Type Description Default
events DataFrame

Classified canonical events (event_id, id, timestamp, price, volume = outstanding-after-event, direction, action, fill, and the type column of :func:~ob_analytics.analytics.set_order_types).

required
levels ('touch', 'all')

"touch" (default) keeps only rows where the order rests at the best bid/ask at that instant — the input to the touch-queue faces. "all" keeps every visible level.

"touch"

Returns:

Type Description
DataFrame

One row per order event (two for a move) with columns :data:QUEUE_COLUMNS. price is the price level the order is queued at. rank is 1-based from the front; ahead_volume sums the remaining size of the orders ahead; age_s is seconds since the order's creation.

Notes

Price-time priority: created 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 changed row with no fill and a new price moves the order to the back of its new level. It is reported twice: as deleted at the old level, then as created at the new one, where age_s still counts from the order's placement. A row that reports a fill never moves the order, whatever price it carries. Hidden orders (id == 0) and market orders are skipped. While an order stays at one price, rank is monotone non-increasing (FIFO: newcomers join the back), which the tests assert.

queue_age_grid

queue_age_grid(
    events: DataFrame,
    *,
    side: str = "bid",
    n_time: int = 200,
) -> pd.DataFrame

Touch-queue composition over time: the age of the order at each rank.

Replays one side's events and snapshots its touch (best) level at n_time evenly spaced instants, recording each resting order's age by FIFO rank. The table feeds the liquidity_at_touch L3 composition strip (a pcolormesh/Heatmap of age over time x rank).

Parameters:

Name Type Description Default
events DataFrame

Classified canonical events, with the columns :func:queue_positions reads. Orders are queued as :func:queue_positions queues them.

required
side ('bid', 'ask')

Which touch to compose (default "bid" -- the front HFT queue-position research lives in).

"bid"
n_time int

Number of time columns.

200

Returns:

Type Description
DataFrame

One row per queue rank and one column per sample instant. The index is rank, 1-based from the front of the queue, up to the longest queue seen. The columns are the n_time sample timestamps. Each cell is the age in seconds of the order at that rank at that instant, or NaN where the queue is shorter. Visible orders only: hidden orders and market orders are absent. When no order is queued, the table has no rows.