Skip to content

Analytics

Format-agnostic analytics on a run's frames. These functions work with the output of any format's pipeline run (Bitstamp, LOBSTER, or custom). The book and lifecycle frames are on the Order book page, and the data-quality checks are on the Data quality page.

Trade Analysis

order_aggressiveness

order_aggressiveness(
    events: DataFrame, quotes: DataFrame
) -> pd.DataFrame

Calculate order aggressiveness with respect to the best bid or ask in BPS.

Each new limit order is measured against the best price on its own side of the book as it stood just before the order arrived: the last row of quotes strictly earlier in the canonical event order (:func:~ob_analytics.schemas.time_order_keys -- timestamp, then event_id between rows at the same instant), found by :func:~ob_analytics.trade_sign.quote_before. A positive value means the order improved on that price, a negative one sat behind it.

Parameters:

Name Type Description Default
events DataFrame

The events DataFrame (must contain direction, action, type, timestamp, event_id, price columns).

required
quotes DataFrame

The best bid and ask over time, such as the run's depth summary. It must have timestamp, best_bid_price and best_ask_price; an event_id column orders rows that share a timestamp.

required

Returns:

Type Description
DataFrame

The events DataFrame with an added aggressiveness_bps column.

trade_impacts

trade_impacts(trades: DataFrame) -> pd.DataFrame

Generate a DataFrame containing order book impact summaries.

Aggregates trade records by taker order ID to summarise how each aggressive order swept through the book (price range, number of fills, total volume, VWAP, duration).

Parameters:

Name Type Description Default
trades DataFrame

The trades DataFrame (must contain taker, price, volume, timestamp, direction columns).

required

Returns:

Type Description
DataFrame

One row per taker order, with columns id (the taker's order id), low and high (its lowest and highest fill price), vwap, n_trades (how many fills), volume, timestamp_start and timestamp_end (its first and last fill), and direction. The price-valued columns (low, high, vwap) are in the quote currency and volume in the instrument's own units, whether the trades hold integer ticks and lots (with tick_size and lot_size on the frame's attrs, as on a pipeline result) or display prices and sizes.

Raises:

Type Description
ConfigError

If a required column is missing, or the prices or sizes are integers and the frame carries no tick_size or lot_size. Also if trades is empty.

maker_resting_times

maker_resting_times(
    events: DataFrame, trades: DataFrame
) -> pd.DataFrame

Return how long the maker order behind each trade had rested in the book.

Each trade is matched to its maker order through maker_event_id, and the maker's placement time is the earliest created row of that order.

Parameters:

Name Type Description Default
events DataFrame

Events with event_id, id, action and timestamp.

required
trades DataFrame

Trades with timestamp, price, volume, direction and maker_event_id.

required

Returns:

Type Description
DataFrame

One row per trade, in the order of trades, with columns timestamp, price (in the quote currency), volume (in the instrument's units), direction (the aggressor's side), maker_event_id, id (the maker's order id), created_ts (when the maker order was placed) and resting_s (seconds from placement to the trade). A trade whose maker order has no created row, such as an order from the book that was there before the data starts, is left out.

Raises:

Type Description
ConfigError

If a required column is missing, events has no rows (an L2 run has no per-order events), or the prices or sizes are integers and trades carries no tick_size or lot_size.

Cancelled Orders

cancelled_orders

cancelled_orders(
    events: DataFrame,
    *,
    lifecycles: DataFrame | None = None,
) -> pd.DataFrame

Return each cancelled order's age and distance from the touch.

A cancelled order here is a cancelled-limit order (see :func:set_order_types): placed, then deleted with the size it came with, with no fill or change in between. Its placement and its end are read from :func:~ob_analytics.book.order_lifecycles.

Parameters:

Name Type Description Default
events DataFrame

Events with the columns :func:~ob_analytics.book.order_lifecycles needs, the type column of :func:set_order_types and the aggressiveness_bps column of :func:order_aggressiveness.

required
lifecycles DataFrame

order_lifecycles(events), when you already have it (for example from :meth:~ob_analytics.pipeline.PipelineResult.lifecycles). None (the default) builds it from events.

None

Returns:

Type Description
DataFrame

One row per cancelled order, in placement order, with columns id, direction, price (in the quote currency), volume (the size cancelled, in the instrument's units), placed_ts, timestamp (when it was cancelled), age_s (seconds from placement to cancellation) and distance_bps (how far the order was placed from the best price on its own side, in basis points, with the sign of :func:order_aggressiveness: positive when the order improved on that price, negative when it sat behind it, and NaN when there was no best price to measure from).

Raises:

Type Description
ConfigError

If a required column is missing, or the prices or sizes are integers and events carries no tick_size or lot_size.

Order Type Classification

set_order_types

set_order_types(
    events: DataFrame, trades: DataFrame
) -> pd.DataFrame

Determine limit order types.

Classifies each order as one of: market, resting-limit, cancelled-limit, or market-limit, based on how the order interacts with the book over its lifetime.

market and market-limit mark takers, so they need the trades' taker ids. A feed that shows resting orders only (see :class:~ob_analytics.protocols.TradeAttribution) has none, and no order is labelled either; that is the feed, not a session without market orders. LOBSTER fills its taker ids with a guess, so its labels are only as good as the guess.

Parameters:

Name Type Description Default
events DataFrame

The limit order events DataFrame.

required
trades DataFrame

The executions DataFrame.

required

Returns:

Type Description
DataFrame

The events DataFrame with an updated 'type' column indicating order types.