quantlab.factor.predefined.market.MarketFeatures#

class quantlab.factor.predefined.market.MarketFeatures(config)[source]#

Bases: Factor

Index or ETF features, identical for every symbol that has a bar.

config.dataset is the target panel: its symbols receive the features, and warmup_bars (60 by default, the longest window) is counted on its calendar. config.series maps a name to a single-symbol dataset; each series gives the 21 features listed in the module docstring, named <name>_ret, <name>_ret_mean_<d>, <name>_ret_std_<d>, <name>_amount_mean_<d> and <name>_amount_std_<d>, in that order for d in 5, 10, 20, 30, 60.

The features are computed on each series’ own bars and placed on the target’s timestamps; a target bar the series lacks is NaN. A window is defined only when every bar in it is, and the standard deviations use ddof=1, as pandas and Qlib do. An amount of 0 gives NaN rather than an infinite ratio. On each bar the values go to every target symbol whose presence_column is non-missing there; a symbol without a bar (not yet listed, delisted) stays NaN, so the market features do not add it to a model’s cross-section. The panel is float32, like a KunQuant factor’s.

config.kwargs may set the column names:

close_column (default "adjClose")

Series variable the return is computed from.

volume_column (default "adjVolume")

Series variable multiplied by the close into the amount.

amount_column (default None)

Series variable holding the amount itself, used instead of volume times close.

presence_column (default "close")

Target variable that is non-missing where a symbol has a bar.

Names are looked up after the dataset’s COLUMN_MAP renaming, so a crypto spot series is read as close, volume and amount.

Parameters:
configMarketFeatureConfig

The factor config.

Examples

stocks is a daily stock dataset of six symbols from 2024-01-01, the last of which, FFF, lists on 1 April; spy and qqq are datasets over one ETF each on the same days:

>>> factor = MarketFeatures(MarketFeatureConfig(
...     dataset=stocks, series={"spy": spy, "qqq": qqq},
...     file_path="data/factors/market.zarr",
... ))
>>> factor.warmup_bars, factor.num_factors
(60, 42)
>>> factor.get_factor_names()[:5]
('spy_ret', 'spy_ret_mean_5', 'spy_ret_std_5', 'spy_amount_mean_5', 'spy_amount_std_5')
>>> panel = factor.compute("2024-03-01", "2024-04-30")
>>> dict(panel.sizes)
{'timestamp': 43, 'symbol': 6}
>>> panel["spy_ret_mean_20"].sel(timestamp="2024-03-01").values.round(5)
array([0.00035, 0.00035, 0.00035, 0.00035, 0.00035,     nan],
      dtype=float32)
>>> panel["spy_ret_mean_20"].sel(timestamp="2024-04-01").values.round(5)
array([-0.00302, -0.00302, -0.00302, -0.00302, -0.00302, -0.00302],
      dtype=float32)
config_cls#

The config class from_config rebuilds this factor with.

alias of MarketFeatureConfig

property options#

The column names in use: config.kwargs over the defaults.

Examples

>>> factor.options
{'close_column': 'adjClose', 'volume_column': 'adjVolume', 'amount_column': None, 'presence_column': 'close'}