QuantConnect/Lean · error · RegressionTestException

Quantity of order {_orderId} should be -1001, but was {order

Error message

Quantity of order {_orderId} should be -1001, but was {orderQuantity}

What it means

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.

Source

Thrown at Algorithm.CSharp/CustomShortableProviderRegressionAlgorithm.cs:63

        {
            var spyShortableQuantity = _spy.ShortableProvider.ShortableQuantity(_spy.Symbol, Time);
            if (spyShortableQuantity > 1000)
            {
                _orderId = Sell("SPY", (int)spyShortableQuantity);
            }
        }

        public override void OnEndOfAlgorithm()
        {
            var transactions = Transactions.OrdersCount;
            if (transactions != 1)
            {
                throw new RegressionTestException($"Algorithm should have just 1 order, but was {transactions}");
            }
            var orderQuantity = Transactions.GetOrderById(_orderId).Quantity;
            if (orderQuantity != -1001)
            {
                throw new RegressionTestException($"Quantity of order {_orderId} should be -1001, but was {orderQuantity}");
            }
            var feeRate = _spy.ShortableProvider.FeeRate(_spy.Symbol, Time);
            if (feeRate != 0.0025m)
            {
                throw new RegressionTestException($"Fee rate should be 0.0025, but was {feeRate}");
            }
            var rebateRate = _spy.ShortableProvider.RebateRate(_spy.Symbol, Time);
            if (rebateRate != 0.0507m)
            {
                throw new RegressionTestException($"Fee rate should be 0.0507, but was {rebateRate}");
            }
        }

        private class CustomSPYShortableProvider : IShortableProvider
        {
            public decimal FeeRate(Symbol symbol, DateTime localTime) => 0.0025m;

            public decimal RebateRate(Symbol symbol, DateTime localTime) => 0.0507m;

View on GitHub (pinned to d2c3659f87)

Solutions

  1. Ensure Sell receives (int)spyShortableQuantity so magnitude matches the provider's return value.
  2. 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).
  3. Confirm only one sell fired so _orderId points at the intended -1001 order, not a later one.
  4. Check for any order-sizing/rounding in the brokerage or fill model that could alter the quantity.

Example fix

// before: hardcoded quantity diverges from shortable
_orderId = Sell("SPY", 10);

// after: use the provider's shortable quantity
_orderId = Sell("SPY", (int)spyShortableQuantity);
Defensive patterns

Strategy: validation

Validate before calling

// Verify the quantity equals the shortable amount at sell time
var qty = (int)_spy.ShortableProvider.ShortableQuantity(_spy.Symbol, Time);
_orderId = Sell("SPY", qty);
// post-check
if (Transactions.GetOrderById(_orderId).Quantity != -qty)
    Log("Order quantity does not match shortable quantity");

Prevention

When it happens

Trigger: GetOrderById(_orderId).Quantity != -1001: the provider returned 10 (pre-16:00 branch), null, or a Sell used a hardcoded/other quantity.

Common situations: 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.

Related errors


AI-assisted analysis of QuantConnect/Lean@d2c3659f87 (2026-08-13). Data as JSON: /api/errors/97030daf6a1294d5. Report an issue: GitHub.