{"record":{"id":"82e728910cfaccaf","repo":"QuantConnect/Lean","slug":"unexpected-open-order-order","errorCode":null,"errorMessage":"Unexpected open order {order}","messagePattern":"Unexpected open order (.+?)","errorType":"exception","errorClass":"RegressionTestException","httpStatus":null,"severity":"error","filePath":"Algorithm.CSharp/EmitInsightNoAlphaModelAlgorithm.cs","lineNumber":72,"sourceCode":"\n            // Order margin value has to have a minimum of 0.5% of Portfolio value, allows filtering out small trades and reduce fees.\n            // Commented so regression algorithm is more sensitive\n            //Settings.MinimumOrderMarginPortfolioPercentage = 0.005m;\n        }\n\n        /// <summary>\n        /// OnData event is the primary entry point for your algorithm. Each new data point will be pumped in here.\n        /// </summary>\n        /// <param name=\"data\">Slice object keyed by symbol containing the stock data</param>\n        public override void OnData(Slice slice)\n        {\n            if (!Portfolio.Invested)\n            {\n                var order = Transactions.GetOpenOrders(_symbol).FirstOrDefault();\n\n                if (order != null)\n                {\n                    throw new RegressionTestException($\"Unexpected open order {order}\");\n                }\n\n                EmitInsights(Insight.Price(_symbol, Resolution.Daily, 10, InsightDirection.Down));\n\n                // emitted insight should have triggered a new order\n                order = Transactions.GetOpenOrders(_symbol).FirstOrDefault();\n\n                if (order == null)\n                {\n                    throw new RegressionTestException(\"Expected open order for emitted insight\");\n                }\n                if (order.Direction != OrderDirection.Sell\n                    || order.Symbol != _symbol)\n                {\n                    throw new RegressionTestException($\"Unexpected open order for emitted insight: {order}\");\n                }\n\n                SetHoldings(_symbol, 1);","sourceCodeStart":54,"sourceCodeEnd":90,"githubUrl":"https://github.com/QuantConnect/Lean/blob/d2c3659f877bfc2b5d9dc0fc89a9c7566f45e892/Algorithm.CSharp/EmitInsightNoAlphaModelAlgorithm.cs#L54-L90","documentation":"In this algorithm there is no AlphaModel; insights are emitted manually via EmitInsights. Before emitting, the test asserts there is no open order for _symbol. A pre-existing open order would mean a prior loop iteration left an order dangling before the insight-driven order is created. This is a pre-emission cleanliness check.","triggerScenarios":"OnData is called a second time while a previous order is still open (not yet filled), so GetOpenOrders returns it. Or the Portfolio.Invested guard did not prevent re-entry.","commonSituations":"The !Portfolio.Invested guard not sufficient (a pending order does not yet flip Invested); order fill delayed across time steps; a previous EmitInsights created an order that is still open.","solutions":["Confirm the order from a prior OnData call has filled (Invested becomes true) before re-entering the block.","Add an explicit open-orders guard at the top of OnData to skip re-processing.","Verify the Portfolio Construction Model does not create duplicate orders from repeated insights."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"var open = Transactions.GetOpenOrders(_symbol);\nif (open.Count > 0)\n{\n    // a prior order is still open; do not emit again this step\n    Log($\"Skipping EmitInsights; open order exists: {open[0]}\");\n    return;\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Guard OnData against re-entrancy when a prior order is still open.","Use a 'position/price managed' flag rather than relying solely on Portfolio.Invested.","Log open-order state at each OnData entry."],"tags":["quantconnect","lean","regression-test","insights","orders","alpha","precondition"],"backgroundTag":null,"analyzedSha":"d2c3659f877bfc2b5d9dc0fc89a9c7566f45e892","analyzedAt":"2026-08-13T13:52:21.013Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}