{"record":{"id":"2abade0cb7e29a92","repo":"QuantConnect/Lean","slug":"unexpected-cancel-pending-event-orderevent","errorCode":null,"errorMessage":"Unexpected cancel pending event: {orderEvent}","messagePattern":"Unexpected cancel pending event: (.+?)","errorType":"exception","errorClass":"RegressionTestException","httpStatus":null,"severity":"error","filePath":"Algorithm.CSharp/DuplicateOptionAssignmentRegressionAlgorithm.cs","lineNumber":111,"sourceCode":"                {\n                    if (orderEvent.Ticket.OrderType != OrderType.OptionExercise || orderEvent.IsAssignment || orderEvent.Symbol != _stock)\n                    {\n                        throw new RegressionTestException($\"Expected stock assignment but got: {orderEvent}\");\n                    }\n\n                    _stockAssigned = true;\n                }\n                else\n                {\n                    throw new RegressionTestException($\"Unexpected order fill event: {orderEvent}\");\n                }\n            }\n            else if (orderEvent.Status == OrderStatus.CancelPending)\n            {\n                // We receive the delisting warning before the order cancel is requested\n                if (!_optionSold || !_optionAssigned || !_stockAssigned || !_optionDelistedWarningReceived)\n                {\n                    throw new RegressionTestException($\"Unexpected cancel pending event: {orderEvent}\");\n                }\n            }\n            else if (orderEvent.Status == OrderStatus.Canceled)\n            {\n                // The delisted event is received before the order is canceled\n                if (!_optionSold || !_optionAssigned || !_stockAssigned || !_optionDelistedWarningReceived || !_optionDelisted)\n                {\n                    throw new RegressionTestException($\"Unexpected cancel event: {orderEvent}\");\n                }\n\n                _orderCanceled = true;\n            }\n        }\n\n        public override void OnDelistings(Delistings delistings)\n        {\n            if (!delistings.TryGetValue(_option, out var delisting))\n            {","sourceCodeStart":93,"sourceCodeEnd":129,"githubUrl":"https://github.com/QuantConnect/Lean/blob/d2c3659f877bfc2b5d9dc0fc89a9c7566f45e892/Algorithm.CSharp/DuplicateOptionAssignmentRegressionAlgorithm.cs#L93-L129","documentation":"When an OrderEvent with Status == CancelPending arrives, all prior steps must be complete: _optionSold, _optionAssigned, _stockAssigned, and _optionDelistedWarningReceived. The delisting warning triggers the cancel of the open LimitOrder, so a CancelPending before these flags are set indicates an out-of-order event sequence.","triggerScenarios":"The cancel-pending event arrives before the option was assigned or before the delisting warning — e.g., the order is canceled for a different reason (margin call, manual cancel, delisting without warning), or events arrive out of order.","commonSituations":"A Lean change in order-cancellation timing during delisting, the LimitOrder being canceled before assignment completes, or a delisting flow that skips the warning step.","solutions":["Log the full event sequence (all OrderEvents and Delistings in order) to identify which flag is false when CancelPending arrives.","Verify the delisting warning fires before the order cancel is requested.","Check for a Lean regression in delisting-driven order cancellation ordering.","If the sequence legitimately changed, update the flag-gating to match the new flow."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"else if (orderEvent.Status == OrderStatus.CancelPending)\n{\n    var ready = _optionSold && _optionAssigned && _stockAssigned && _optionDelistedWarningReceived;\n    if (!ready)\n    {\n        Debug($\"CancelPending before sequence complete: sold={_optionSold} optAssign={_optionAssigned} stockAssign={_stockAssigned} warn={_optionDelistedWarningReceived}\");\n        throw new RegressionTestException($\"Unexpected cancel pending event: {orderEvent}\");\n    }\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Log the timestamped sequence of all OrderEvents and Delistings.","Verify the delisting warning precedes order cancellation.","Update flag-gating only after confirming the new event ordering is correct."],"tags":["quantconnect","options","order-cancellation","delisting","state-machine","regression-test"],"backgroundTag":null,"analyzedSha":"d2c3659f877bfc2b5d9dc0fc89a9c7566f45e892","analyzedAt":"2026-08-13T13:52:21.013Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}