microsoft/qlib · error · NotImplementedError

InfPosition doesn't support get_stock_weight_dict

Error message

InfPosition doesn't support get_stock_weight_dict

What it means

InfPosition.get_stock_weight_dict() raises NotImplementedError: weights are amount/value ratios, and with infinite amounts and infinite value they are undefined (inf/inf), so qlib blocks the API rather than returning NaNs. Only Position (finite holdings) supports weight dicts.

Source

Thrown at qlib/backtest/position.py:553

    def get_stock_list(self) -> List[str]:
        raise NotImplementedError(f"InfPosition doesn't support stock list position")

    def get_stock_price(self, code: str) -> float:
        """the price of the inf position is meaningless"""
        return np.nan

    def get_stock_amount(self, code: str) -> float:
        return np.inf

    def get_cash(self, include_settle: bool = False) -> float:
        return np.inf

    def get_stock_amount_dict(self) -> dict:
        raise NotImplementedError(f"InfPosition doesn't support get_stock_amount_dict")

    def get_stock_weight_dict(self, only_stock: bool = False) -> dict:
        raise NotImplementedError(f"InfPosition doesn't support get_stock_weight_dict")

    def add_count_all(self, bar: str) -> None:
        raise NotImplementedError(f"InfPosition doesn't support add_count_all")

    def update_weight_all(self) -> None:
        raise NotImplementedError(f"InfPosition doesn't support update_weight_all")

    def settle_start(self, settle_type: str) -> None:
        pass

    def settle_commit(self) -> None:
        pass

View on GitHub (pinned to 79633dd950)

Solutions

  1. Use Position when weight-based logic or reporting is involved
  2. Skip weight reads for InfPosition: isinstance(position, InfPosition) guard
  3. Track target weights in the strategy itself instead of reading them back from the position

Example fix

# before
weights = position.get_stock_weight_dict(only_stock=True)

# after
weights = ({}) if isinstance(position, InfPosition) else position.get_stock_weight_dict(only_stock=True)
Defensive patterns

Strategy: type-guard

Validate before calling

from qlib.backtest.position import InfPosition
weights = {} if isinstance(position, InfPosition) else position.get_stock_weight_dict(only_stock)

Type guard

def has_weight_dict(pos) -> bool:
    from qlib.backtest.position import InfPosition
    return not isinstance(pos, InfPosition)

Try / catch

try:
    weights = position.get_stock_weight_dict()
except NotImplementedError:
    weights = {}

Prevention

When it happens

Trigger: Calling get_stock_weight_dict(only_stock=...) on an InfPosition; typical callers are performance reporting, order sizing from current weights, and nested-executor weight-rebalancing code.

Common situations: Reusing a strategy or report pipeline that reads current weights with an exchange/account configured for infinite cash (e.g. randomized order generation runs).

Related errors


AI-assisted analysis of microsoft/qlib@79633dd950 (2026-08-15). Data as JSON: /api/errors/e2af07af5b49b63e. Report an issue: GitHub.