{"record":{"id":"7fecbac43213162f","repo":"QuantConnect/Lean","slug":"expectedorderscount-method-is-not-implemented","errorCode":null,"errorMessage":"ExpectedOrdersCount method is not implemented","messagePattern":"ExpectedOrdersCount method is not implemented","errorType":"exception","errorClass":"NotImplementedError","httpStatus":null,"severity":"error","filePath":"Algorithm.Python/OptionStrategyFactoryMethodsBaseAlgorithm.py","lineNumber":66,"sourceCode":"            self.assert_strategy_position_group(position_group, self._option_symbol)\n\n            # Now we should be able to close the position\n            self.liquidate_strategy()\n\n            # We can quit now, no more testing required\n            self.quit()\n\n    def on_end_of_algorithm(self):\n        if self.portfolio.invested:\n            raise AssertionError(\"Expected no holdings at end of algorithm\")\n\n        orders_count = len(list(self.transactions.get_orders(lambda order: order.status == OrderStatus.FILLED)))\n        if orders_count != self.expected_orders_count():\n            raise AssertionError(f\"Expected {self.expected_orders_count()} orders to have been submitted and filled, \"\n                            f\"half for buying the strategy and the other half for the liquidation. Actual {orders_count}\")\n\n    def expected_orders_count(self) -> int:\n        raise NotImplementedError(\"ExpectedOrdersCount method is not implemented\")\n\n    def trade_strategy(self, chain: OptionChain, option_symbol: Symbol) -> None:\n        raise NotImplementedError(\"TradeStrategy method is not implemented\")\n\n    def assert_strategy_position_group(self, position_group: IPositionGroup, option_symbol: Symbol) -> None:\n        raise NotImplementedError(\"AssertStrategyPositionGroup method is not implemented\")\n\n    def liquidate_strategy(self) -> None:\n        raise NotImplementedError(\"LiquidateStrategy method is not implemented\")\n","sourceCodeStart":48,"sourceCodeEnd":76,"githubUrl":"https://github.com/QuantConnect/Lean/blob/d2c3659f877bfc2b5d9dc0fc89a9c7566f45e892/Algorithm.Python/OptionStrategyFactoryMethodsBaseAlgorithm.py#L48-L76","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Run a concrete subclass (e.g. one of the OptionStrategyFactoryMethods*RegressionAlgorithm classes), not the base.","Add `def expected_orders_count(self) -> int: return <2 * leg_count>` to your subclass.","Mark the base class abstract (ABC / @abstractmethod) so the omission is caught at instantiation rather than at runtime."],"exampleFix":"# before: base raises NotImplementedError\n# after in subclass\nclass MyStratRegression(OptionStrategyFactoryMethodsBaseAlgorithm):\n    def expected_orders_count(self) -> int:\n        return 4","handlingStrategy":"validation","validationCode":"# Guard against calling the unimplemented base method\nif self.expected_orders_count.__func__ is OptionStrategyFactoryMethodsBaseAlgorithm.expected_orders_count:\n    raise NotImplementedError('Subclass must override expected_orders_count()')","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Make the base class abstract with @abstractmethod so missing overrides fail at construction.","Run concrete subclasses, never the base.","When adding a subclass, implement every abstract template method."],"tags":["options","template-method","not-implemented","quantconnect","option-strategy","inheritance"],"backgroundTag":null,"analyzedSha":"d2c3659f877bfc2b5d9dc0fc89a9c7566f45e892","analyzedAt":"2026-08-13T13:52:21.013Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}