quantlab.dataset.massive.resample.TradeBarResampler#

class quantlab.dataset.massive.resample.TradeBarResampler(bar_interval, *, close_auction=True)[source]#

Bases: object

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

Parameters:
bar_intervalstr

The bar size, a key of BAR_INTERVAL_SECONDS.

close_auctionbool, default True

Whether the sessions end at the exchange’s close, so that the closing auction printed after it belongs to their last bar. A window that ends earlier has none.

Raises:
ValueError

If bar_interval is unknown.

Examples

>>> TradeBarResampler("5m").interval_ns
300000000000
labels(sessions)[source]#

Return every bar label of sessions.

Parameters:
sessionspl.DataFrame

Columns date, open and close (naive UTC).

Returns:
pl.DataFrame

Columns date and timestamp (Datetime("ns")): for each session open + k * interval while before the close, then the close, sorted.

Examples

>>> TradeBarResampler("1m").labels(sessions)["timestamp"].head(1).to_list()
[datetime.datetime(2024, 1, 24, 14, 31)]
resample_with_stats(trades, conditions, sessions)[source]#

Resample trades onto bars and count what each rule dropped.

Parameters:
tradespl.DataFrame

Columns symbol, date (the session date), conditions (comma-separated ids, or null), correction, price, size, sip_timestamp (Int64 nanoseconds since the epoch, UTC), sequence_number, trf_id (0 or null when not reported through a TRF) and exchange.

conditionspl.DataFrame

The condition table, as read_conditions returns it.

sessionspl.DataFrame

Columns date, open and close (naive UTC) of the sessions the trades belong to.

Returns:
barspl.DataFrame

Columns symbol, date, timestamp and TRADE_BAR_VARIABLES: every bar of the session for each symbol with at least one record on that date, sorted by symbol and label. Prices are null where no trade was eligible; the volumes are Float64, n_trades Int64.

statspl.DataFrame

Columns date, symbol and TRADE_STATS_COUNTS (Int64), one row per (date, symbol) with a record.

Examples

With trades read by quantlab.dataset.massive.raw.read_trades (ticker renamed symbol, a date column added), the condition table and that day’s XNYS session bounds:

sessions = XnysSessionCalendar().session_bounds([date(2016, 11, 25)])
bars, stats = TradeBarResampler("1m").resample_with_stats(trades, conditions, sessions)