QuantConnect/Lean · error · NotImplementedError

LiquidateStrategy method is not implemented

Error message

LiquidateStrategy method is not implemented

What it means

liquidate_strategy is the abstract close method on OptionStrategyFactoryMethodsBaseAlgorithm. on_data calls it after verification so the subclass can close the combo it opened (e.g. via self.liquidate() or an opposing strategy order). The base raises NotImplementedError because the liquidation mechanism depends on the concrete strategy. Hitting it means the algorithm cannot close its position.

Source

Thrown at Algorithm.Python/OptionStrategyFactoryMethodsBaseAlgorithm.py:75

        if self.portfolio.invested:
            raise AssertionError("Expected no holdings at end of algorithm")

        orders_count = len(list(self.transactions.get_orders(lambda order: order.status == OrderStatus.FILLED)))
        if orders_count != self.expected_orders_count():
            raise AssertionError(f"Expected {self.expected_orders_count()} orders to have been submitted and filled, "
                            f"half for buying the strategy and the other half for the liquidation. Actual {orders_count}")

    def expected_orders_count(self) -> int:
        raise NotImplementedError("ExpectedOrdersCount method is not implemented")

    def trade_strategy(self, chain: OptionChain, option_symbol: Symbol) -> None:
        raise NotImplementedError("TradeStrategy method is not implemented")

    def assert_strategy_position_group(self, position_group: IPositionGroup, option_symbol: Symbol) -> None:
        raise NotImplementedError("AssertStrategyPositionGroup method is not implemented")

    def liquidate_strategy(self) -> None:
        raise NotImplementedError("LiquidateStrategy method is not implemented")

View on GitHub (pinned to d2c3659f87)

Solutions

  1. Implement liquidate_strategy in the subclass to close every leg (e.g. self.liquidate() or self.sell(strategy, quantity)).
  2. Run a concrete subclass.
  3. Use @abstractmethod to catch the omission at instantiation.

Example fix

# before: base raises NotImplementedError
# after
def liquidate_strategy(self):
    self.liquidate()
Defensive patterns

Strategy: validation

Validate before calling

if self.liquidate_strategy.__func__ is OptionStrategyFactoryMethodsBaseAlgorithm.liquidate_strategy:
    raise NotImplementedError('Subclass must override liquidate_strategy()')

Prevention

When it happens

Trigger: on_data, after verifying the invested group, calls self.liquidate_strategy() on an instance whose class did not override the method.

Common situations: Running the base class; a new subclass missing the override; renaming the method in the base without updating subclasses.

Related errors


AI-assisted analysis of QuantConnect/Lean@d2c3659f87 (2026-08-13). Data as JSON: /api/errors/4cc9a7736af13da0. Report an issue: GitHub.