quantlab.dataset.base.MarketDataset#

class quantlab.dataset.base.MarketDataset(config)[source]#

Bases: BaseDataset

Dataset 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 BaseDataset this class adds the market rules a backtest trades by (tradable_bars, delisting_bars) and to_kunquant, which returns contiguous [time, symbol] float32 arrays for KunQuant (the compiled factor engine). A subclass implements _to_kunquant and sets COLUMN_MAP (inherited from BaseDataset) when its variable names differ from the shared ones. A market dataset converted from raw files derives from quantlab.dataset.raw.RawMarketDataset; the market views (RosterDataset, MergedDataset, the Trade bar views) and FrameDataset derive from this class and build nothing.

Parameters:
configDatasetConfig

The dataset config.

Examples

Using the DemoDataset described on BaseDataset:

>>> 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_column price 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) where inputs maps 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_column price 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]])