quantlab.portfolio.base#
The root classes of portfolio construction: the rule from one bar’s scores to weights.
A portfolio construction rule turns the scores of one bar, and the weights
currently held, into the weights to hold after that bar (ADR 0012). Every
rule derives from PortfolioConstructor, whose one abstract method,
construct(context), is handed a PortfolioContext holding only what is
known at that bar. decide runs construct, checks the row and holds
a bar the rule cannot solve. A rule only decides: its contexts are
assembled by quantlab.portfolio.decision_inputs.DecisionInputs, for the
vectorised backtest (a whole panel, the holdings replayed between
rebalances) and for an event-driven executor (one bar, with the holdings it
really has).
The output follows the weights contract (D-03): on a rebalance bar every
symbol gets a finite weight, an unselected one exactly 0.0, with gross
exposure at most one; an all-NaN row means “hold the current position”. A
locked position, a symbol held but not tradable at the bar, keeps its
current weight, and a symbol neither tradable nor held gets 0.0 (ADR 0014);
the loop checks both. A rule holds no state between bars. A bar the rule
cannot solve raises PortfolioConstructionError, and the loop holds it
instead.
The approximate backtest (quantlab.backtest.approximate, ADR 0045) reads
the same rules’ configs and decides a block of bars at once with its own
implementations (quantlab.portfolio.predefined.approximate, chosen by
quantlab.portfolio.approximate); a rule here knows
nothing of them. DecisionPanel is what
DecisionInputs.panel reads for it: a block of bars’ decision inputs.
A covariance estimator (CovarianceEstimator) estimates the covariance
of one-bar returns at a bar, as a quantlab.risk.base.ReturnCovariance
(the dense CovarianceEstimate here, or LowRankCovarianceEstimate,
low rank plus a diagonal, which Ledoit-Wolf returns); a rule that prices
risk, such as a mean-variance optimiser, holds one. It is not a factor risk
model (quantlab.risk), which is estimated ahead as stores; one estimator,
FactorRiskStoreEstimator, reads those stores: its estimate is the
model’s forecast (quantlab.risk.base.FactorRiskForecast), whose
factor_form() lets an optimiser build a low-rank risk term and whose
factor_exposure gives a risk factor’s exposures, and it
declares its risk model, whose exposures at the bar the decision inputs take
from the model itself (read or computed per its exposure_data_strategy)
and put in each bar’s context beside the bar’s row of its estimate store, so
the estimator reads nothing but the context.
What a rule reads at each bar besides its predictions and holdings is one
value, its InputDeclaration (declared_inputs()): the return window
and the prices behind it, the Factor panels, the factor risk model. A
rule holding a covariance estimator merges the estimator’s into its own.
Shipped rules and covariance estimators live in
quantlab/portfolio/predefined; this module imports no solver.
Classes
A covariance estimator's covariance of returns at one bar, over the symbols it covers. |
|
Base class of every covariance estimator: the covariance of returns at one bar. |
|
What a rule decided at one bar: |
|
What a rule may read at a block of decision bars at once: the panel counterpart of a context. |
|
Base class of every expected return model: one prediction column to the |
|
What a rule reads at each bar besides its predictions and holdings: its decision inputs, declared. |
|
A covariance estimate |
|
Base class of every rule that turns one bar's scores into target weights. |
|
Everything a portfolio construction rule may read at one bar. |