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, closecand bar lengthd, barkcovers(o + (k-1)d, min(o + kd, c)]fork = 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; whenddoes not divide the session, the last bar is the part up to the close, labelled at the close (quantlab.utils.resample.session_labelscuts 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.openandcloseare the first and last open/close-eligible trade bysip_timestamp, ties broken bysequence_number;highandlowthe extremes of the high/low-eligible trades;volumeandn_tradesthe size and count of the volume-eligible ones.Corrected and cancelled trades are dropped: every
correctionbut 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 nullcorrectionis 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_volumeis price times size (VWAP isdollar_volume / volume),offexchange_volumethe size of trades reported through a TRF (trf_idnon-zero),oddlot_volumeof 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_volumenorsell_volume, so their sum may be less thanvolume.The auctions are variables of their own. An auction is the
OPENING_TRADEorCLOSING_PRINTSprints 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 setsopen_auction_priceandopen_auction_volumeon 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 toCLOSE_AUCTION_CUTOFF_NSlater setclose_auction_priceandclose_auction_volumeon 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
Resample trade records onto right-closed Trade bars; see the module docstring. |