{"record":{"id":"9dfa95a0a168c41c","repo":"QuantConnect/Lean","slug":"expected-stock-assignment-but-got-orderevent","errorCode":null,"errorMessage":"Expected stock assignment but got: {orderEvent}","messagePattern":"Expected stock assignment but got: (.+?)","errorType":"exception","errorClass":"RegressionTestException","httpStatus":null,"severity":"error","filePath":"Algorithm.CSharp/DuplicateOptionAssignmentRegressionAlgorithm.cs","lineNumber":96,"sourceCode":"                    LimitOrder(_option, 1, Securities[_option].Price * 0.1m);\n\n                    _optionSold = true;\n                }\n                // This is the assignment\n                else if (!_optionAssigned)\n                {\n                    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)","sourceCodeStart":78,"sourceCodeEnd":114,"githubUrl":"https://github.com/QuantConnect/Lean/blob/d2c3659f877bfc2b5d9dc0fc89a9c7566f45e892/Algorithm.CSharp/DuplicateOptionAssignmentRegressionAlgorithm.cs#L78-L114","documentation":"After the option assignment, the next expected Filled event is the stock (underlying) leg of the assignment: OrderType.OptionExercise, IsAssignment == false, Symbol == _stock. If the event is an assignment (duplicate), has the wrong symbol, or is a non-exercise fill, the regression fails.","triggerScenarios":"A second option-assignment event arrives instead of the underlying leg, the underlying delivery uses a different symbol than _stock (e.g., a mapped symbol), or an unexpected order fill interrupts the sequence.","commonSituations":"Symbol mapping (GOOG vs GOOCV) causing orderEvent.Symbol != _stock, a Lean change in how the underlying delivery event is represented, or a duplicate assignment event.","solutions":["Verify _stock (AddEquity(\"GOOG\").Symbol) matches the symbol on the exercise-delivery OrderEvent — account for mapped symbols.","Log orderEvent.Symbol, IsAssignment, and OrderType for the failing event.","Check for a Lean regression in option-exercise underlying-delivery event representation.","Compare the symbol using SID/canonical rather than raw Value if mapping is involved."],"exampleFix":"// before\nif (orderEvent.Ticket.OrderType != OrderType.OptionExercise || orderEvent.IsAssignment || orderEvent.Symbol != _stock)\n\n// after — compare canonical symbol to handle mapping\nif (orderEvent.Ticket.OrderType != OrderType.OptionExercise || orderEvent.IsAssignment || !orderEvent.Symbol.SID().Equals(_stock.SID()))","handlingStrategy":"type-guard","validationCode":"else if (!_stockAssigned)\n{\n    var isExerciseDelivery = orderEvent.Ticket.OrderType == OrderType.OptionExercise\n        && !orderEvent.IsAssignment\n        && orderEvent.Symbol == _stock;\n    if (!isExerciseDelivery)\n    {\n        Debug($\"Symbol mismatch? event={orderEvent.Symbol} stock={_stock}\");\n        throw new RegressionTestException($\"Expected stock assignment but got: {orderEvent}\");\n    }\n    _stockAssigned = true;\n}","typeGuard":"static bool IsStockDeliveryLeg(OrderEvent e, Symbol stock) =>\n    e.Ticket.OrderType == OrderType.OptionExercise && !e.IsAssignment && e.Symbol == stock;","tryCatchPattern":null,"preventionTips":["Compare symbols by SID/canonical form when mapping (GOOG vs GOOCV) is involved.","Log the event symbol, IsAssignment, and OrderType for every fill.","Extend the state machine explicitly if Lean adds new exercise-delivery event types."],"tags":["quantconnect","options","assignment","order-events","symbol-mapping","regression-test"],"backgroundTag":null,"analyzedSha":"d2c3659f877bfc2b5d9dc0fc89a9c7566f45e892","analyzedAt":"2026-08-13T13:52:21.013Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}