quantlab.portfolio.predefined.approximate.top_n.decide#

quantlab.portfolio.predefined.approximate.top_n.decide(rule, panel, prices=None, stopping=Stopping(tolerance=1e-05, max_iterations=20000))[source]#

Return every bar’s top-n book; top-n reads neither the holdings nor a stopping rule.

Parameters:
ruleTopNConstructor

The rule: its config’s direction and top_n, and the label it ranks by (ranking_label: score_label, else the first predicted label).

panelDecisionPanel

The bars’ predictions and tradability.

pricesPanelPrices, optional

Not read.

stoppingStopping

Not read.

Returns:
PanelSolution

[bars, symbols] weights, no iterations, every bar converged.

Examples

>>> import numpy as np, pandas as pd, xarray as xr
>>> from quantlab.portfolio.base import DecisionPanel
>>> from quantlab.portfolio.config import TopNConfig
>>> from quantlab.portfolio.predefined.top_n import TopNConstructor
>>> bars, symbols = pd.DatetimeIndex(["2024-01-02", "2024-01-03"]), ["AAA", "BBB", "CCC", "DDD"]
>>> panel = DecisionPanel(
...     timestamps=bars,
...     symbols=np.array(symbols),
...     predictions=xr.Dataset(
...         {"ret": (("timestamp", "symbol"), [[0.3, 0.1, np.nan, 0.2], [0.1, 0.1, 0.4, 0.1]])},
...         coords={"timestamp": bars, "symbol": symbols},
...     ),
...     tradable=np.ones((2, 4), dtype=bool),
... )
>>> decide(TopNConstructor(TopNConfig(direction="long_only", top_n=2)), panel).weights
array([[0.5, 0. , 0. , 0.5],
       [0.5, 0. , 0.5, 0. ]])