microsoft/qlib · error · NotImplementedError

InfPosition doesn't support get_stock_amount_dict

Error message

InfPosition doesn't support get_stock_amount_dict

What it means

InfPosition.get_stock_amount_dict() raises NotImplementedError because an infinite-cash position has no finite per-stock amounts to map. The amount dict API exists for Position (used by weight reporting and order generation against real holdings) and is intentionally unsupported for InfPosition.

Source

Thrown at qlib/backtest/position.py:550

    def calculate_value(self) -> float:
        raise NotImplementedError(f"InfPosition doesn't support calculating value")

    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. Switch the position to Position where amount dicts are needed
  2. Gate the call: skip amount-dict logic for InfPosition instances
  3. Derive intended amounts from the strategy's target weights and prices instead of the position snapshot

Example fix

# before
amounts = position.get_stock_amount_dict()

# after
amounts = None if isinstance(position, InfPosition) else position.get_stock_amount_dict()
Defensive patterns

Strategy: type-guard

Validate before calling

from qlib.backtest.position import InfPosition
amounts = {} if isinstance(position, InfPosition) else position.get_stock_amount_dict()

Type guard

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

Try / catch

try:
    amounts = position.get_stock_amount_dict()
except NotImplementedError:
    amounts = {}

Prevention

When it happens

Trigger: Calling get_stock_amount_dict() on an InfPosition instance; commonly from code that snapshots current holdings to compute deltas for the next rebalance (e.g. exchange.get_amount_info or custom strategy logic).

Common situations: Order-generation code reused against an InfPosition-based exchange; reporters trying to serialize holdings after backtests run with infinite cash.

Related errors


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