quantlab.backtest.predefined.us_equity.USEquityCrossectionSelectStockVectorBt#
- class quantlab.backtest.predefined.us_equity.USEquityCrossectionSelectStockVectorBt(config)[source]#
Bases:
VectorBtBacktesterDaily US-equity backtester: a portfolio construction rule simulated with vectorbt.
On every rebalance bar the config’s
constructorturns the predictions of that bar into target weights, for example the toptop_nsymbols withTopNConstructor, and the targets are held until the next rebalance bar. A symbol is tradable at a bar as the price dataset’stradable_barssays (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 areMARKET, the rule is the config’sconstructor, and every engine behaviour comes fromVectorBtBacktester.- Parameters:
- configCrossSectionBacktestConfig
The backtest configuration: price dataset, model, date window and output directory, plus
rebalance_periodsandconstructor.
- Raises:
- TypeError
If
constructoris not aPortfolioConstructor.- 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