microsoft/qlib · error · NotImplementedError

InfPosition doesn't support update_weight_all

Error message

InfPosition doesn't support update_weight_all

What it means

InfPosition.update_weight_all() raises NotImplementedError. This method recomputes each holding's weight = stock_value / total_value after trades; with infinite amounts and value the weights are undefined, so qlib refuses rather than producing inf/inf NaNs. Only finite Position instances support it.

Source

Thrown at qlib/backtest/position.py:559

        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 whenever weight bookkeeping is required
  2. Guard with isinstance(position, InfPosition) and skip the refresh
  3. In custom executors, only call update_weight_all when the position is a finite Position

Example fix

# before
position.update_weight_all()

# after
if not isinstance(position, InfPosition):
    position.update_weight_all()
Defensive patterns

Strategy: type-guard

Validate before calling

from qlib.backtest.position import InfPosition
if not isinstance(position, InfPosition):
    position.update_weight_all()

Type guard

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

Try / catch

try:
    position.update_weight_all()
except NotImplementedError:
    pass  # InfPosition: weights undefined

Prevention

When it happens

Trigger: Executor/report code calls position.update_weight_all() after processing orders while the account holds an InfPosition; the base-class docstring even notes weight state is awkward around order handling, and InfPosition opts out entirely.

Common situations: Infinite-cash order-generation runs passed through code paths that refresh weights for reporting; custom strategies calling update_weight_all before get_stock_weight_dict.

Related errors


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