{"record":{"id":"00fbe04712b4a051","repo":"QuantConnect/Lean","slug":"unexpected-order-fill-event-orderevent","errorCode":null,"errorMessage":"Unexpected order fill event: {orderEvent}","messagePattern":"Unexpected order fill event: (.+?)","errorType":"exception","errorClass":"RegressionTestException","httpStatus":null,"severity":"error","filePath":"Algorithm.CSharp/DuplicateOptionAssignmentRegressionAlgorithm.cs","lineNumber":103,"sourceCode":"                    if (orderEvent.Ticket.OrderType != OrderType.OptionExercise || !orderEvent.IsAssignment)\n                    {\n                        throw new RegressionTestException($\"Expected option assignment but got: {orderEvent}\");\n                    }\n\n                    _optionAssigned = true;\n                }\n                else if (!_stockAssigned)\n                {\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","sourceCodeStart":85,"sourceCodeEnd":121,"githubUrl":"https://github.com/QuantConnect/Lean/blob/d2c3659f877bfc2b5d9dc0fc89a9c7566f45e892/Algorithm.CSharp/DuplicateOptionAssignmentRegressionAlgorithm.cs#L85-L121","documentation":"After the three expected Filled events (option sell, option assignment, stock assignment), any additional Filled event is unexpected and triggers this error. It guards against duplicate or spurious fills beyond the defined sequence.","triggerScenarios":"A fourth fill arrives — e.g., the LimitOrder from line 78 actually filled after assignment, a duplicate assignment event, or an extra exercise-delivery fill. The _stockAssigned flag is already true so the else-branch catches it.","commonSituations":"The never-fill LimitOrder eventually fills (price gap, or Lean fill logic change), a duplicate option-exercise event, or a Lean change generating an extra fill for the assignment.","solutions":["Log the extra orderEvent fully (Status, Symbol, OrderType, IsAssignment, FillQuantity, FillPrice) to identify its source.","Confirm the LimitOrder was canceled before it could fill (it should be canceled on delisting).","Check for a Lean regression producing duplicate exercise fills.","If an additional fill is now legitimately expected, extend the state machine rather than silently ignoring it."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"else\n{\n    Debug($\"Extra fill after expected sequence: {orderEvent}\");\n    // Determine if it is the limit order filling unexpectedly\n    throw new RegressionTestException($\"Unexpected order fill event: {orderEvent}\");\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Ensure the never-fill LimitOrder is canceled before it can fill.","Log the full event sequence to identify spurious fills.","Extend the state machine rather than silently ignoring new legitimate fills."],"tags":["quantconnect","options","order-events","state-machine","regression-test"],"backgroundTag":null,"analyzedSha":"d2c3659f877bfc2b5d9dc0fc89a9c7566f45e892","analyzedAt":"2026-08-13T13:52:21.013Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}