{"record":{"id":"907d2fcb41907a19","repo":"QuantConnect/Lean","slug":"expected-option-assignment-but-got-orderevent","errorCode":null,"errorMessage":"Expected option assignment but got: {orderEvent}","messagePattern":"Expected option assignment but got: (.+?)","errorType":"exception","errorClass":"RegressionTestException","httpStatus":null,"severity":"error","filePath":"Algorithm.CSharp/DuplicateOptionAssignmentRegressionAlgorithm.cs","lineNumber":87,"sourceCode":"        public override void OnOrderEvent(OrderEvent orderEvent)\n        {\n            if (orderEvent.Status == OrderStatus.Filled)\n            {\n                // This is the fill for the option sell order\n                if (!_optionSold)\n                {\n                    // Let's close the position but with a limit order that won't ever fill (limit price too low)\n                    // just so we keep it open until the brokerage tries to assign it\n                    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            }","sourceCodeStart":69,"sourceCodeEnd":105,"githubUrl":"https://github.com/QuantConnect/Lean/blob/d2c3659f877bfc2b5d9dc0fc89a9c7566f45e892/Algorithm.CSharp/DuplicateOptionAssignmentRegressionAlgorithm.cs#L69-L105","documentation":"In OnOrderEvent, after the initial option sell fill, the next expected Filled event must be the option assignment: OrderType.OptionExercise with IsAssignment == true. If the event is not an assignment (e.g., the limit order from line 78 filled, or an unexpected fill arrived), the regression fails.","triggerScenarios":"The LimitOrder placed at 10% of price unexpectedly filled (it is designed never to fill), or an order event arrives out of the expected sequence (sell -> option assignment -> stock assignment), or assignment did not fire by expiry.","commonSituations":"A Lean change where low-priced limit orders fill against the bid, a change in option-exercise/assignment event ordering, or the option not being assigned at expiry due to a data/exercise-logic regression.","solutions":["Confirm the LimitOrder price (10% of current) is far enough from market that it cannot fill; lower the factor if needed.","Log every OrderEvent with its Status, OrderType, IsAssignment, and Symbol to trace the sequence.","Verify the option (GOOG Put $800 expiring 2015-12-24) is in-the-money at expiry so assignment occurs.","Check for a Lean regression in option-exercise event generation."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"else if (!_optionAssigned)\n{\n    if (orderEvent.Ticket.OrderType != OrderType.OptionExercise || !orderEvent.IsAssignment)\n    {\n        Debug($\"Unexpected event: {orderEvent.Status} {orderEvent.Ticket.OrderType} IsAssign={orderEvent.IsAssignment}\");\n        throw new RegressionTestException($\"Expected option assignment but got: {orderEvent}\");\n    }\n    _optionAssigned = true;\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Place limit orders far enough from market price that they cannot fill.","Log every OrderEvent to trace the fill sequence.","Verify the option is ITM at expiry so assignment occurs."],"tags":["quantconnect","options","assignment","order-events","regression-test"],"backgroundTag":null,"analyzedSha":"d2c3659f877bfc2b5d9dc0fc89a9c7566f45e892","analyzedAt":"2026-08-13T13:52:21.013Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}