{"record":{"id":"4177dd84f05a7ee0","repo":"QuantConnect/Lean","slug":"algorithm-should-have-just-1-order-but-was-trans","errorCode":null,"errorMessage":"Algorithm should have just 1 order, but was {transactions}","messagePattern":"Algorithm should have just 1 order, but was (.+?)","errorType":"exception","errorClass":"RegressionTestException","httpStatus":null,"severity":"error","filePath":"Algorithm.CSharp/CustomShortableProviderRegressionAlgorithm.cs","lineNumber":58,"sourceCode":"            _spy = AddEquity(\"SPY\", Resolution.Daily);\n            _spy.SetShortableProvider(new CustomSPYShortableProvider());\n        }\n\n        public override void OnData(Slice slice)\n        {\n            var spyShortableQuantity = _spy.ShortableProvider.ShortableQuantity(_spy.Symbol, Time);\n            if (spyShortableQuantity > 1000)\n            {\n                _orderId = Sell(\"SPY\", (int)spyShortableQuantity);\n            }\n        }\n\n        public override void OnEndOfAlgorithm()\n        {\n            var transactions = Transactions.OrdersCount;\n            if (transactions != 1)\n            {\n                throw new RegressionTestException($\"Algorithm should have just 1 order, but was {transactions}\");\n            }\n            var orderQuantity = Transactions.GetOrderById(_orderId).Quantity;\n            if (orderQuantity != -1001)\n            {\n                throw new RegressionTestException($\"Quantity of order {_orderId} should be -1001, but was {orderQuantity}\");\n            }\n            var feeRate = _spy.ShortableProvider.FeeRate(_spy.Symbol, Time);\n            if (feeRate != 0.0025m)\n            {\n                throw new RegressionTestException($\"Fee rate should be 0.0025, but was {feeRate}\");\n            }\n            var rebateRate = _spy.ShortableProvider.RebateRate(_spy.Symbol, Time);\n            if (rebateRate != 0.0507m)\n            {\n                throw new RegressionTestException($\"Fee rate should be 0.0507, but was {rebateRate}\");\n            }\n        }\n","sourceCodeStart":40,"sourceCodeEnd":76,"githubUrl":"https://github.com/QuantConnect/Lean/blob/d2c3659f877bfc2b5d9dc0fc89a9c7566f45e892/Algorithm.CSharp/CustomShortableProviderRegressionAlgorithm.cs#L40-L76","documentation":"Asserts exactly one order was placed. The Sell fires only when ShortableQuantity > 1000, which (per the custom provider) is true only after 2013-10-04 16:00 returning 1001. With Daily resolution over 2013-10-04..06 the condition is met exactly once. != 1 means it never fired (0) or fired multiple times (lack of Portfolio.Invested guard).","triggerScenarios":"OnData sells when shortable > 1000; if that branch never runs you get 0 orders, if it runs every bar you get several. The test expects precisely one sell of 1001 shares.","commonSituations":"ShortableQuantity returns <= 1000 for the whole window (date logic wrong, wrong Time zone); no Portfolio.Invested guard so Sell fires on every bar after the threshold; or resolution/date-range change shifts how many bars exceed 1000.","solutions":["Add a Portfolio.Invested / Transactions.OrdersCount guard so the sell fires once, not every qualifying bar.","Confirm ShortableQuantity's date threshold (2013-10-04 16:00) falls inside the backtest window in the security's exchange time zone.","Match the expected resolution (Daily) and date range so exactly one bar returns > 1000.","Re-check the custom provider returns 1001 (not null) for the qualifying dates."],"exampleFix":"// before: sells on every bar where shortable > 1000\nif (spyShortableQuantity > 1000) _orderId = Sell(\"SPY\", (int)spyShortableQuantity);\n\n// after: sell exactly once\nif (!Portfolio.Invested && spyShortableQuantity > 1000)\n    _orderId = Sell(\"SPY\", (int)spyShortableQuantity);","handlingStrategy":"validation","validationCode":"// Guard so the sell fires exactly once\nif (!Portfolio.Invested && Transactions.OrdersCount == 0 && spyShortableQuantity > 1000)\n    _orderId = Sell(\"SPY\", (int)spyShortableQuantity);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Add a Portfolio.Invested / OrdersCount guard so condition-true bars do not stack orders.","Ensure ShortableQuantity's date threshold falls in-window in exchange time.","Match the expected resolution and date range."],"tags":["regression-test","shortable-provider","orders","shorting"],"backgroundTag":null,"analyzedSha":"d2c3659f877bfc2b5d9dc0fc89a9c7566f45e892","analyzedAt":"2026-08-13T13:52:21.013Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}