quantlab.portfolio.base.PortfolioContext#

class quantlab.portfolio.base.PortfolioContext(timestamp, predictions, tradable, current_weights, returns=None, factors=None, staleness=None, risk_exposures=None, risk_estimate=None)[source]#

Bases: object

Everything a portfolio construction rule may read at one bar.

Nothing in it reaches past the bar it describes, so a rule handed a context cannot look ahead.

Attributes:
timestamppd.Timestamp

The bar the rule decides on; the weights it returns fill at the next bar.

predictionsxr.Dataset

Every label’s prediction at the bar: one variable per label name, in the predictor’s label order, on symbol.

tradablexr.DataArray

Booleans on symbol: whether the symbol can be traded at the bar, judged from nothing later than the bar (the price dataset’s tradable_bars: by default, a real fill price at the bar). A rule treats a symbol without a finite prediction of the label it reads as not selectable too.

current_weightsxr.DataArray

The weights currently held, on symbol, valued at this bar’s valuation price: what the orders of the earlier rebalances left, rejected orders and delisting settlements included; 0.0 where nothing is held, all 0.0 before the first rebalance.

returnsxr.DataArray or None

The trailing window of one-bar returns ending at the bar, on (timestamp, symbol), of the rule’s declared lookback_bars length (no bars for a rule that needs none), read from its last declared history_bars raw valuation prices up to and including the bar. Each return is computed from the last valuation price known at its bar within those, so a halt shows as zero returns and then the whole gap on the bar the symbol trades again; NaN before a symbol’s first price in them and on that price’s bar. None in a context built by hand for a rule that reads none.

stalenessxr.DataArray or None

Bars since each symbol’s last real valuation price within the rule’s last declared history_bars bars, on symbol: 0 when it has one at the bar, NaN when it has none in those bars. None without prices.

factorsxr.Dataset or None

The values at the bar of the Factor panels the rule declares (InputDeclaration.factors; a benchmark beta, for example): one variable per factor name, on symbol, NaN where a symbol has none. None when the rule declares none.

risk_exposuresxr.Dataset or None

The exposures at the bar of the factor risk model the rule declares (InputDeclaration.risk_model), as FactorRiskModel.exposures gives them (read from the exposures factor’s store or computed, per the model’s exposure_data_strategy): its exposure_names and estimation-universe flag, on symbol, NaN where a symbol has none. None when the rule declares no risk model.

risk_estimatexr.Dataset or None

The estimate store’s row at the bar of the same factor risk model, its forecast made at the bar of the next bar’s risk (quantlab.risk.base.ESTIMATE_VARIABLES): factor_covariance on (factor_i, factor_j) and specific_risk on symbol, NaN where a symbol has none. With risk_exposures it is everything FactorRiskModel.forecast reads. None when the rule declares no risk model or the store has no row at the bar.

Examples

>>> import numpy as np, xarray as xr
>>> symbols = ["AAA", "BBB", "CCC"]
>>> context = PortfolioContext(
...     timestamp=pd.Timestamp("2024-01-02"),
...     predictions=xr.Dataset({"ret": ("symbol", [0.3, 0.1, 0.2])}, coords={"symbol": symbols}),
...     tradable=xr.DataArray([True, True, False], dims="symbol", coords={"symbol": symbols}),
...     current_weights=xr.DataArray([0.0, 0.4, 0.6], dims="symbol", coords={"symbol": symbols}),
... )
>>> context.symbols.tolist()
['AAA', 'BBB', 'CCC']
>>> context.locked.values
array([False, False,  True])
property locked#

Booleans on symbol: held and not tradable, so kept at the current weight.

Examples

>>> context.locked.values
array([False, False,  True])
property symbols#

The symbols of the bar, the axis the returned weights must be on.

Examples

>>> context.symbols.tolist()
['AAA', 'BBB', 'CCC']