quantlab.dataset.base.MarketDataset#
- class quantlab.dataset.base.MarketDataset(config)[source]#
Bases:
BaseDatasetDataset of market price bars, with a KunQuant export.
A bar is one period’s open, high, low, close and volume for a symbol. On top of
BaseDatasetthis class adds the market rules a backtest trades by (tradable_bars,delisting_bars) andto_kunquant, which returns contiguous[time, symbol]float32 arrays for KunQuant (the compiled factor engine). A subclass implements_to_kunquantand setsCOLUMN_MAP(inherited fromBaseDataset) when its variable names differ from the shared ones. A market dataset converted from raw files derives fromquantlab.dataset.raw.RawMarketDataset; the market views (RosterDataset,MergedDataset, the Trade bar views) andFrameDatasetderive from this class and build nothing.- Parameters:
- configDatasetConfig
The dataset config.
Examples
Using the
DemoDatasetdescribed onBaseDataset:>>> DemoDataset(config).from_raw_data().save() >>> ds = DemoDataset(config) >>> inputs, symbols, timestamps = ds.to_kunquant( ... ("open", "close"), panel=ds.panel("2024-01-02", "2024-01-05") ... ) >>> inputs["close"].shape (4, 3)
- config_cls#
The config class used to rebuild this dataset from a saved config.
alias of
DatasetConfig
- delisting_bars(prices, valuation_column)[source]#
Mark, in a price panel, the last bar of each symbol that delisted in it.
A backtest settles a delisted holding into cash at its last valuation on the bar after this one. The default marks a symbol’s last bar with a valid
valuation_columnprice in the panel when the dataset has no valid price for it on any later bar, inside the panel or after it; a symbol halted through the end of the panel that trades again later is not delisted. On a CRSP store the marked bar is the delisting row, whose adjusted close already carries the delisting return (or, when CRSP gives no return on a no-price delisting row, the last priced day before it). A dataset that knows more (a halt without a later price, say) overrides this.- Parameters:
- pricesxr.Dataset
A panel of this dataset on
(timestamp, symbol).- valuation_columnstr
The price the portfolio is valued at.
- Returns:
- xr.DataArray
Booleans on the panel’s
(timestamp, symbol).
Examples
>>> panel = xr.Dataset( ... {"close": (("timestamp", "symbol"), [[1.0, 2.0], [1.1, np.nan], [1.2, np.nan]])}, ... coords={"timestamp": pd.bdate_range("2024-01-01", periods=3), "symbol": ["A", "B"]}, ... ) >>> dataset.delisting_bars(panel, "close").values # B's last price is bar 0 array([[False, True], [False, False], [False, False]])
- to_kunquant(data_columns, panel)[source]#
Convert a panel of this dataset to KunQuant input arrays.
- Parameters:
- data_columnstuple[str, …]
Columns to export, named as KunQuant names them (
open,high,low,close,volume,amount).- panelxr.Dataset
A panel of this dataset, such as one
panel(start, end)returned.
- Returns:
- tuple[dict, np.ndarray, np.ndarray]
A tuple
(inputs, symbols, timestamps)whereinputsmaps each column to a contiguous[time, symbol]float32 array.
Examples
>>> inputs, symbols, timestamps = ds.to_kunquant( ... ("open", "close"), panel=ds.panel("2024-01-03", "2024-01-04") ... ) >>> inputs["close"].shape, inputs["close"].dtype ((2, 3), dtype('float32')) >>> symbols.tolist() ['AAA', 'BBB', 'CCC']
- tradable_bars(prices, fill_column)[source]#
Mark, in a price panel, which symbols can be traded at each bar.
Judged from nothing later than the bar, so a backtest and a live engine agree (ADR 0014). The default: a real, not forward-filled,
fill_columnprice at the bar. A dataset that knows more, such as a halt flag, overrides this.- Parameters:
- pricesxr.Dataset
A panel of this dataset on
(timestamp, symbol).- fill_columnstr
The price orders fill at.
- Returns:
- xr.DataArray
Booleans on the panel’s
(timestamp, symbol).
Examples
>>> panel = xr.Dataset( ... {"open": (("timestamp", "symbol"), [[1.0, 2.0], [1.1, np.nan]])}, ... coords={"timestamp": pd.bdate_range("2024-01-01", periods=2), "symbol": ["A", "B"]}, ... ) >>> dataset.tradable_bars(panel, "open").values array([[ True, True], [ True, False]])