{"record":{"id":"09bc9fb9693ca6c7","repo":"QuantConnect/Lean","slug":"expected-to-liquidate-option-order-event-symbol","errorCode":null,"errorMessage":"Expected to liquidate option {order_event.symbol} at {self.expected_liquidation_time}, instead liquidated at {self.time}","messagePattern":"Expected to liquidate option (.+?) at (.+?), instead liquidated at (.+?)","errorType":"exception","errorClass":"AssertionError","httpStatus":null,"severity":"error","filePath":"Algorithm.Python/FuturesAndFuturesOptionsExpiryTimeAndLiquidationRegressionAlgorithm.py","lineNumber":86,"sourceCode":"        if not self.invested and \\\n            (self.es_future in data.bars or self.es_future in data.quote_bars) and \\\n            (self.es_future_option in data.bars or self.es_future_option in data.quote_bars):\n\n            self.invested = True\n\n            self.market_order(self.es_future, 1)\n            self.market_order(self.es_future_option, 1)\n\n    def on_order_event(self, order_event: OrderEvent):\n        if order_event.direction != OrderDirection.SELL or order_event.status != OrderStatus.FILLED:\n            return\n\n        # * Future Liquidation\n        # * Future Option Exercise\n        # * We expect NO Underlying Future Liquidation because we already hold a Long future position so the FOP Put selling leaves us breakeven\n        self.liquidated += 1\n        if order_event.symbol.security_type == SecurityType.FUTURE_OPTION and self.expected_liquidation_time != self.time:\n            raise AssertionError(f\"Expected to liquidate option {order_event.symbol} at {self.expected_liquidation_time}, instead liquidated at {self.time}\")\n\n        if order_event.symbol.security_type == SecurityType.FUTURE and \\\n            (self.expected_liquidation_time - timedelta(minutes=1)) != self.time and \\\n            self.expected_liquidation_time != self.time:\n\n            raise AssertionError(f\"Expected to liquidate future {order_event.symbol} at {self.expected_liquidation_time} (+1 minute), instead liquidated at {self.time}\")\n\n    def on_end_of_algorithm(self):\n        if not self.invested:\n            raise AssertionError(\"Never invested in ES futures and FOPs\")\n\n        if self.delistings_received != 4:\n            raise AssertionError(f\"Expected 4 delisting events received, found: {self.delistings_received}\")\n\n        if self.liquidated != 2:\n            raise AssertionError(f\"Expected 3 liquidation events, found {self.liquidated}\")\n","sourceCodeStart":68,"sourceCodeEnd":103,"githubUrl":"https://github.com/QuantConnect/Lean/blob/d2c3659f877bfc2b5d9dc0fc89a9c7566f45e892/Algorithm.Python/FuturesAndFuturesOptionsExpiryTimeAndLiquidationRegressionAlgorithm.py#L68-L103","documentation":"Liquidation-timing assertion for FUTURE_OPTION sell/fill events. When an order event for a SecurityType.FUTURE_OPTION completes, the test requires self.time == expected_liquidation_time (2020-06-20). A mismatch indicates the option was liquidated/exercised on the wrong bar.","triggerScenarios":"FOP exercise or auto-liquidation firing a bar earlier or later than the expiry day; timezone skew between order_event processing time and the expected datetime; data changes shifting exercise timing.","commonSituations":"Engine changes to FOP exercise scheduling; data refreshes with different expiry timestamps; algorithm time timezone differing from expected_liquidation_time's naive datetime.","solutions":["Print self.time and self.expected_liquidation_time to locate the offset.","Verify the FOP data file's expiry/exercise timestamp.","Update expected_liquidation_time if exercise timing intentionally changed.","Confirm self.time tz matches the naive datetime assumption."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"# Validate FOP liquidation time before raising\nif order_event.symbol.security_type == SecurityType.FUTURE_OPTION:\n    if self.expected_liquidation_time != self.time:\n        self.debug(f\"FOP liquidation at {self.time} != expected {self.expected_liquidation_time}\")","typeGuard":"def fop_liquidated_on_time(order_event, algo_time: datetime, expected: datetime) -> bool:\n    return (order_event.symbol.security_type == SecurityType.FUTURE_OPTION\n            and algo_time == expected)","tryCatchPattern":null,"preventionTips":["Keep expected_liquidation_time synced with FOP exercise timing.","Confirm self.time tz matches the naive expected datetime.","Log order_event details when debugging.","Re-baseline after engine exercise-scheduling changes."],"tags":["quantconnect","future-options","liquidation","exercise","expiry","regression-test"],"backgroundTag":null,"analyzedSha":"d2c3659f877bfc2b5d9dc0fc89a9c7566f45e892","analyzedAt":"2026-08-13T13:52:21.013Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}