{"record":{"id":"97030daf6a1294d5","repo":"QuantConnect/Lean","slug":"quantity-of-order-orderid-should-be-1001-but","errorCode":null,"errorMessage":"Quantity of order {_orderId} should be -1001, but was {orderQuantity}","messagePattern":"Quantity of order (.+?) should be -1001, but was (.+?)","errorType":"exception","errorClass":"RegressionTestException","httpStatus":null,"severity":"error","filePath":"Algorithm.CSharp/CustomShortableProviderRegressionAlgorithm.cs","lineNumber":63,"sourceCode":"        {\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\n        private class CustomSPYShortableProvider : IShortableProvider\n        {\n            public decimal FeeRate(Symbol symbol, DateTime localTime) => 0.0025m;\n\n            public decimal RebateRate(Symbol symbol, DateTime localTime) => 0.0507m;","sourceCodeStart":45,"sourceCodeEnd":81,"githubUrl":"https://github.com/QuantConnect/Lean/blob/d2c3659f877bfc2b5d9dc0fc89a9c7566f45e892/Algorithm.CSharp/CustomShortableProviderRegressionAlgorithm.cs#L45-L81","documentation":"Asserts the single order's quantity is -1001 (negative = sell/short, magnitude = ShortableQuantity). The expected -1001 comes from the custom provider returning 1001 after 2013-10-04 16:00. A different quantity means ShortableQuantity returned the wrong value at sell time, or the wrong quantity was passed to Sell.","triggerScenarios":"GetOrderById(_orderId).Quantity != -1001: the provider returned 10 (pre-16:00 branch), null, or a Sell used a hardcoded/other quantity.","commonSituations":"ShortableQuantity date comparison off so the early branch (return 10) fired; Sell called with a literal instead of (int)spyShortableQuantity; or Time zone mismatch shifts which branch executes.","solutions":["Ensure Sell receives (int)spyShortableQuantity so magnitude matches the provider's return value.","Verify the provider returns 1001 for the bar on which the sell executes (i.e., the sell bar is >= 2013-10-04 16:00 in local time).","Confirm only one sell fired so _orderId points at the intended -1001 order, not a later one.","Check for any order-sizing/rounding in the brokerage or fill model that could alter the quantity."],"exampleFix":"// before: hardcoded quantity diverges from shortable\n_orderId = Sell(\"SPY\", 10);\n\n// after: use the provider's shortable quantity\n_orderId = Sell(\"SPY\", (int)spyShortableQuantity);","handlingStrategy":"validation","validationCode":"// Verify the quantity equals the shortable amount at sell time\nvar qty = (int)_spy.ShortableProvider.ShortableQuantity(_spy.Symbol, Time);\n_orderId = Sell(\"SPY\", qty);\n// post-check\nif (Transactions.GetOrderById(_orderId).Quantity != -qty)\n    Log(\"Order quantity does not match shortable quantity\");","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Pass (int)spyShortableQuantity to Sell rather than a hardcoded value.","Confirm the provider returns 1001 for the bar where the sell executes.","Ensure only one sell fires so _orderId points at the intended order."],"tags":["regression-test","shortable-provider","order-quantity","shorting"],"backgroundTag":null,"analyzedSha":"d2c3659f877bfc2b5d9dc0fc89a9c7566f45e892","analyzedAt":"2026-08-13T13:52:21.013Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}