quantdata.massive.processing#

quantdata.massive.processing#

Some simple tools to make life easier when working with Massive data. These are not meant to be comprehensive utilities, but rather simple examples of how to filter and align trades and quotes.

Function in this file:

quantdata.massive.processing.filter_valid_trades(ticker, d, within_session=True)[source]#

Returns trades for a given underlying and date within session hours (optionally). Removes trades with NORMAL_TRADE_EXCLUDE_FLAGS and price or size zero or negative. For trades at the sime sip time stamp, computes aggregate trade size.

Returns a polars data frame with MassiveClient.SCHEMA_TRADES schema with the following columns:

ts_ns Datetime[ns,tz] price Float64 size Float64 trade_code UInt32 (semantic mask) sequence_number UInt64. Sequence number of the trade for a given timestamp, useful for ordering trades with the same timestamp.

quantdata.massive.processing.filter_valid_quotes(ticker, d, within_session=True)[source]#

Returns quotes for a given underlying and date within session hours (optionally). Removes quotes with invalid timestamps, prices, sizes, or crossed/locked markets.

Returns a polars data frame with MassiveClient.SCHEMA_QUOTES schema with the following columns:

ts_ns Datetime[ns,tz] bid_price Float64 bid_size Float64 ask_price Float64 ask_size Float64

class quantdata.massive.processing.TradeQuoteAlignmentConfig(lookback_ms=250, lookfwd_ms=250)[source]#

Bases: object

Configuration for matching trades to nearby valid quotes. Times are in milliseconds.

lookback_ms: int = 250#
lookfwd_ms: int = 250#
quantdata.massive.processing.align_quotes_to_trades(ticker, d, drop_unmatched, config=TradeQuoteAlignmentConfig(lookback_ms=250, lookfwd_ms=250), *, within_session=True)[source]#

Fetch valid trades and quotes for ticker on d and align them on a common timeline.

For each trade, this first finds the latest quote between config.lookback_ms and the trade timestamp where bid_price <= price <= ask_price. If no such quote exists, it finds the first matching quote up to config.lookfwd_ms after the trade.

The output keeps all trade columns and adds the matched quote columns. If drop_unmatched is False, unmatched trades have null quote fields.

Parameters:
  • ticker (str) – The underlying ticker to fetch trades and quotes for.

  • d (date) – The date to fetch trades and quotes for.

  • drop_unmatched (bool) – If True, only keep trades with a matched quote. If False, keep all trades and have null quote fields for unmatched trades.

  • config (TradeQuoteAlignmentConfig) – Configuration for matching trades to nearby valid quotes.

  • within_session (bool) – Whether to only consider trades and quotes within session hours when aligning.

quantdata.massive.processing.align_trades_to_quotes(ticker, d, drop_unmatched, config=TradeQuoteAlignmentConfig(lookback_ms=250, lookfwd_ms=250), *, within_session=True)[source]#

Fetch valid trades and quotes for ticker on d and align trades onto quotes.

For each quote, this first finds the latest trade between config.lookback_ms and the quote timestamp where bid_price <= price <= ask_price. If no such trade exists, it finds the first matching trade up to config.lookfwd_ms after the quote. The output keeps all quote columns and adds matched trade columns. If drop_unmatched is False, unmatched quotes have null trade fields.

Parameters:
tickerstr

The underlying ticker to fetch trades and quotes for.

ddatetime.date

The date to fetch trades and quotes for.

drop_unmatchedbool

If True, only keep quotes with a matched trade. If False, keep all quotes and have null trade fields for unmatched quotes.

configTradeQuoteAlignmentConfig, default TradeQuoteAlignmentConfig()

Configuration for matching trades to nearby valid quotes.

within_sessionbool, default True

Whether to only consider trades and quotes within session hours when aligning.