microsoft/qlib · error · NotImplementedError

Please implement the `settle_commit` method

Error message

Please implement the `settle_commit` method

What it means

BasePosition.settle_commit() is the second abstract settlement stub: it should flush pending settlement state (e.g. move cash_delay into cash) at transaction commit. If a subclass does not override it, the base implementation raises NotImplementedError. Together with settle_start it forms the transactional settlement protocol executors rely on.

Source

Thrown at qlib/backtest/position.py:222

        It will act like start and commit a transaction

        Parameters
        ----------
        settle_type : str
            Should we make delay the settlement in each execution (each execution will make the executor a step forward)
            - "cash": make the cash settlement delayed.
                - The cash you get can't be used in current step (e.g. you can't sell a stock to get cash to buy another
                        stock)
            - None: not settlement mechanism
            - TODO: other assets will be supported in the future.
        """
        raise NotImplementedError(f"Please implement the `settle_conf` method")

    def settle_commit(self) -> None:
        """
        settlement commit
        """
        raise NotImplementedError(f"Please implement the `settle_commit` method")

    def __str__(self) -> str:
        return self.__dict__.__str__()

    def __repr__(self) -> str:
        return self.__dict__.__repr__()


class Position(BasePosition):
    """Position

    current state of position
    a typical example is :{
      <instrument_id>: {
        'count': <how many days the security has been hold>,
        'amount': <the amount of the security>,
        'price': <the close price of security in the last trading day>,
        'weight': <the security weight of total position value>,

View on GitHub (pinned to 79633dd950)

Solutions

  1. Use the concrete Position class which implements settle_commit
  2. Implement settle_commit in your subclass: fold cash_delay back into cash and reset _settle_type to ST_NO
  3. If settlement is not needed, ensure executors are configured with settle_type None so commit is skipped

Example fix

// before
class MyPosition(BasePosition):
    def settle_start(self, settle_type): ...
    # missing settle_commit

# after
class MyPosition(BasePosition):
    def settle_commit(self) -> None:
        if self._settle_type == self.ST_CASH:
            self.position["cash"] += self.position.pop("cash_delay", 0.0)
        self._settle_type = self.ST_NO
Defensive patterns

Strategy: type-guard

Validate before calling

assert type(position).settle_commit is not BasePosition.settle_commit, "settle_commit not implemented"

Type guard

def supports_settlement(pos) -> bool:
    return type(pos).settle_commit is not BasePosition.settle_commit

Try / catch

try:
    position.settle_commit()
except NotImplementedError as e:
    raise RuntimeError(f"position {type(position).__name__} cannot commit settlement") from e

Prevention

When it happens

Trigger: An executor finishes a nested step and calls position.settle_commit() on a position object whose class did not implement it; typically after settle_start succeeded, or when BasePosition/InfPosition-unsupported flows are used with settlement enabled.

Common situations: Custom position class implementing settle_start but not settle_commit (partial port of the API); upgrading qlib where executors newly call settle_commit on positions.

Related errors


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