nautechsystems/nautilus_trader · error

invalid {name} quantity: {e}

Error message

invalid {name} quantity: {e}

What it means

Thrown when a level's quantity mantissa (with the snapshot's `qty_exponent`) cannot be converted into a Nautilus `Quantity` at the instrument's `size_precision`. The `name` is "bid" or "ask", identifying the failing side.

Source

Thrown at crates/adapters/binance/src/spot/http/client.rs:3085

                             side: OrderSide,
                             order_id: usize,
                             name: &str|
         -> anyhow::Result<()> {
            let price = Price::from_mantissa_exponent_checked(
                level.price_mantissa,
                snapshot.price_exponent,
                instrument.price_precision(),
            )
            .map_err(|e| anyhow::anyhow!("invalid {name} price: {e}"))?;
            anyhow::ensure!(price.is_positive(), "invalid non-positive {name} price");
            let qty_mantissa = u64::try_from(level.qty_mantissa)
                .map_err(|_| anyhow::anyhow!("invalid negative {name} quantity"))?;
            let quantity = Quantity::from_mantissa_exponent_checked(
                qty_mantissa,
                snapshot.qty_exponent,
                instrument.size_precision(),
            )
            .map_err(|e| anyhow::anyhow!("invalid {name} quantity: {e}"))?;
            anyhow::ensure!(
                quantity.is_positive(),
                "invalid non-positive {name} quantity"
            );
            let order = BookOrder::new(
                side,
                price,
                quantity,
                u64::try_from(order_id)
                    .map_err(|_| anyhow::anyhow!("order-book level index overflow"))?,
            );
            book.add(order, 0, sequence, ts_event);
            Ok(())
        };

        for (index, level) in snapshot.bids.iter().enumerate() {
            add_level(level, OrderSide::Buy, index, "bid")?;
        }

View on GitHub (pinned to 18893faf8b)

Solutions

  1. Refresh the instruments cache so `size_precision` matches current LOT_SIZE filters.
  2. Re-fetch the snapshot to rule out a transient malformed payload.
  3. Check the inner `{e}` message for the exact Quantity conversion failure.
  4. Update the adapter if Binance changed the qty-exponent encoding.

Example fix

// before
let book = client.request_book_snapshot(instrument_id, depth).await?;
// after: ensure instrument definitions are fresh first
client.reload_instruments().await?;
let book = client.request_book_snapshot(instrument_id, depth).await?;
Defensive patterns

Strategy: validation

Validate before calling

fn qty_fits_precision(qty: i64, exponent: i32, precision: u8) -> bool {
    exponent >= -(precision as i32) || qty % 10 == 0
}

Type guard

fn valid_qty_level(l: &BinancePriceLevel) -> bool { l.qty_mantissa >= 0 }

Try / catch

match client.request_book_snapshot(id, depth).await {
    Ok(book) => book,
    Err(e) if e.to_string().contains("invalid bid quantity") || e.to_string().contains("invalid ask quantity") => {
        reload_instruments_and_retry(id, depth).await?
    }
    Err(e) => return Err(e),
}

Prevention

When it happens

Trigger: Calling `request_book_snapshot` when `Quantity::from_mantissa_exponent_checked` fails for a level — a quantity not representable at `instrument.size_precision()`, typically from an instrument-definition/cache mismatch with the snapshot's `qty_exponent`.

Common situations: Stale cached instrument definitions whose size precision no longer matches exchange LOT_SIZE filters; corrupted depth payloads; exchange precision changes after an instrument update.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of nautechsystems/nautilus_trader@18893faf8b (2026-09-08). Data as JSON: /api/errors/126a4226514029d6. Report an issue: GitHub.