microsoft/qlib · error · NotImplementedError

InfPosition doesn't support add_count_all

Error message

InfPosition doesn't support add_count_all

What it means

InfPosition.add_count_all(bar) raises NotImplementedError. add_count_all increments per-stock holding-duration counters (position[stock][f'count_{bar}']) each executor step; InfPosition has no per-stock state to count, so the API is intentionally unimplemented.

Source

Thrown at qlib/backtest/position.py:556

    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. Run those executor configurations with Position instead of InfPosition
  2. Override add_count_all as a no-op in an InfPosition subclass used purely for order generation
  3. Disable bar-count tracking (count_{bar} keys) in the executor config if supported

Example fix

# before
class MyInf(InfPosition):
    ...
# executor calls add_count_all -> raises

# after
class MyInf(InfPosition):
    def add_count_all(self, bar: str) -> None:
        pass  # counting is meaningless with infinite amounts
Defensive patterns

Strategy: fallback

Validate before calling

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

Type guard

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

Try / catch

try:
    position.add_count_all(bar)
except NotImplementedError:
    pass  # counting meaningless for InfPosition

Prevention

When it happens

Trigger: An executor's per-bar update loop calls position.add_count_all(bar) while the account uses InfPosition; e.g. nested executor setups (time-level executors incrementing daily counts) combined with an infinite-cash position.

Common situations: Running randomized/order-generation backtests (which use InfPosition) through executor stacks that assume the full Position API; custom executors ported from Position flows.

Related errors


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