quantlab.backtest.predefined.us_equity.USEquityCrossectionSelectStockVectorBt#

class quantlab.backtest.predefined.us_equity.USEquityCrossectionSelectStockVectorBt(config)[source]#

Bases: VectorBtBacktester

Daily US-equity backtester: a portfolio construction rule simulated with vectorbt.

On every rebalance bar the config’s constructor turns the predictions of that bar into target weights, for example the top top_n symbols with TopNConstructor, and the targets are held until the next rebalance bar. A symbol is tradable at a bar as the price dataset’s tradable_bars says (by default, when it has a fill price at that bar), and a held symbol that is not tradable stays locked. The class only wires the pieces together: the market conventions are MARKET, the rule is the config’s constructor, and every engine behaviour comes from VectorBtBacktester.

Parameters:
configCrossSectionBacktestConfig

The backtest configuration: price dataset, model, date window and output directory, plus rebalance_periods and constructor.

Raises:
TypeError

If constructor is not a PortfolioConstructor.

ValueError

If the constructor refuses the model’s labels.

Examples

>>> from quantlab.backtest.config import CrossSectionBacktestConfig
>>> from quantlab.portfolio.config import TopNConfig
>>> from quantlab.portfolio.predefined.top_n import TopNConstructor
>>> backtester = USEquityCrossectionSelectStockVectorBt(
...     CrossSectionBacktestConfig(
...         price_dataset=prices,  # a StockDataset over a Zarr store
...         model=model,  # a model whose checkpoint is given below
...         model_mode="load",
...         checkpoint="models/first_feature/checkpoint.joblib",
...         start_date="2024-02-12",
...         end_date="2024-03-11",
...         output_dir="runs",
...         rebalance_periods=5,
...         constructor=TopNConstructor(TopNConfig(direction="long_only", top_n=2)),
...     )
... )
>>> result = backtester.run()
>>> BacktestRun.open(result.run_dir).kind
'run'
>>> result.weights["weight"].dims
('timestamp', 'symbol')
config_cls#

alias of CrossSectionBacktestConfig