QuantConnect/Lean · error · NotImplementedError

ExpectedOrdersCount method is not implemented

Error message

ExpectedOrdersCount method is not implemented

What it means

OptionStrategyFactoryMethodsBaseAlgorithm is an abstract template: expected_orders_count() raises NotImplementedError to force subclasses to declare how many FILLED orders their specific strategy should produce (open + liquidation legs). The base cannot know the leg count, so calling it directly (i.e., running the base class or a subclass that did not override it) is treated as a programmer error.

Source

Thrown at Algorithm.Python/OptionStrategyFactoryMethodsBaseAlgorithm.py:66

            self.assert_strategy_position_group(position_group, self._option_symbol)

            # Now we should be able to close the position
            self.liquidate_strategy()

            # We can quit now, no more testing required
            self.quit()

    def on_end_of_algorithm(self):
        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. Run a concrete subclass (e.g. one of the OptionStrategyFactoryMethods*RegressionAlgorithm classes), not the base.
  2. Add `def expected_orders_count(self) -> int: return <2 * leg_count>` to your subclass.
  3. Mark the base class abstract (ABC / @abstractmethod) so the omission is caught at instantiation rather than at runtime.

Example fix

# before: base raises NotImplementedError
# after in subclass
class MyStratRegression(OptionStrategyFactoryMethodsBaseAlgorithm):
    def expected_orders_count(self) -> int:
        return 4
Defensive patterns

Strategy: validation

Validate before calling

# Guard against calling the unimplemented base method
if self.expected_orders_count.__func__ is OptionStrategyFactoryMethodsBaseAlgorithm.expected_orders_count:
    raise NotImplementedError('Subclass must override expected_orders_count()')

Prevention

When it happens

Trigger: on_end_of_algorithm reaches the orders-count comparison and calls self.expected_orders_count() on an instance whose class did not override the method. Occurs when the base algorithm is run directly, or when a subclass omits the override.

Common situations: Instantiating the base class instead of a concrete subclass; adding a new strategy subclass and forgetting to implement expected_orders_count(); 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/7fecbac43213162f. Report an issue: GitHub.