quantlab.dataset.massive.resample#

Resample SIP trades onto Trade bars, by the SIP’s rules per trade condition.

A Trade bar is quantlab’s own bar built from every trade of the consolidated tape (ADR 0030), not the vendor’s minute aggregate. TradeBarResampler turns one or more sessions of trade records into one row per (symbol, bar label). It is pure polars with no file access, as quantlab.dataset.nbbo.resample.NbboResampler is; quantlab.dataset.massive.trade_bars feeds it the raw files and puts its output on the (timestamp, symbol) panel.

The rules:

  • Time is sip_timestamp, when the SIP published the trade, so a bar holds only trades the market could see by its end.

  • Bars are right-closed and labelled at their end. With session open o, close c and bar length d, bar k covers (o + (k-1)d, min(o + kd, c)] for k = 1..ceil((c - o) / d): a trade exactly on a boundary belongs to the bar ending there; one exactly at the open is before the session; when d does not divide the session, the last bar is the part up to the close, labelled at the close (quantlab.utils.resample.session_labels cuts coarser bars the same way).

  • Each trade counts towards high/low, open/close and volume separately, by the consolidated update rules of its conditions (the condition table, quantlab.dataset.massive.raw.read_conditions): it counts towards one only if every one of its conditions allows it. A trade without conditions counts towards all three. open and close are the first and last open/close-eligible trade by sip_timestamp, ties broken by sequence_number; high and low the extremes of the high/low-eligible trades; volume and n_trades the size and count of the volume-eligible ones.

  • Corrected and cancelled trades are dropped: every correction but 0 (a regular trade) and 12 (the corrected print that replaces a trade later corrected, code 1), so a correction keeps the trade at its corrected values. A null correction is a regular trade. A trade with a condition the table does not know is dropped too; both are counted.

  • Volumes are over the volume-eligible trades, as floats (recent years carry fractional shares): dollar_volume is price times size (VWAP is dollar_volume / volume), offexchange_volume the size of trades reported through a TRF (trf_id non-zero), oddlot_volume of trades under 100 shares.

  • The tick rule signs each volume-eligible trade against the price of the volume-eligible trade before it in the same session: an up-tick is a buy, a down-tick a sell, and a zero tick takes the side of the last non-zero tick. The previous price carries across bars but not across sessions; a session’s first trade, and zero ticks before its first non-zero tick, count in neither buy_volume nor sell_volume, so their sum may be less than volume.

  • The auctions are variables of their own. An auction is the OPENING_TRADE or CLOSING_PRINTS prints of one market: the one with the most volume under that condition that day, which is the listing market (a few percent of tickers also get such a print from a second market). Its largest print sets the price, its prints together (odd-lot portions included) the volume. The opening auction sets open_auction_price and open_auction_volume on the bar that holds that print, and stays in the bar’s OHLCV. The closing auction reaches the SIP after the session’s close, Nasdaq’s within a second and NYSE’s in 2016 up to minutes later, so it is in no OHLCV: its prints after the close and up to CLOSE_AUCTION_CUTOFF_NS later set close_auction_price and close_auction_volume on the session’s last bar, which therefore holds information published after its label. Only a window that ends at the exchange’s close has a closing auction (close_auction), and only one that starts by its open an opening auction.

  • A bar without an eligible trade has null prices and zero volumes and n_trades, never a carried price.

Examples#

>>> bars, stats = TradeBarResampler("1m").resample_with_stats(trades, conditions, sessions)
>>> bars.columns[3:]
['open', 'high', 'low', 'close', 'volume', 'dollar_volume', 'n_trades', 'buy_volume', 'sell_volume', 'offexchange_volume', 'oddlot_volume', 'open_auction_price', 'open_auction_volume', 'close_auction_price', 'close_auction_volume']

Classes

TradeBarResampler

Resample trade records onto right-closed Trade bars; see the module docstring.