nautechsystems/nautilus_trader · error

invalid quantity `{value}` at precision {precision}: {e}

Error message

invalid quantity `{value}` at precision {precision}: {e}

What it means

After the string parses as a Decimal and passes the non-negative check, parse_quantity calls Quantity::from_decimal_dp. This error means the value is out of Quantity's representable fixed-precision range at the requested precision (or the precision conversion failed) — e.g. a magnitude far beyond ~10^18, or a value that cannot be represented at the given precision. Value and precision are included in the message.

Source

Thrown at crates/adapters/lighter/src/common/parse.rs:121

///
/// Zero is allowed because Lighter sends zero-size book levels to delete
/// existing orders.
///
/// # Errors
///
/// Returns an error if the string is not a decimal, if `precision` exceeds
/// [`MAX_DECIMALS`], if the value is negative, or if the resulting quantity
/// is out of range.
pub fn parse_quantity(value: &str, precision: u8) -> anyhow::Result<Quantity> {
    anyhow::ensure!(
        precision <= MAX_DECIMALS,
        "size precision {precision} exceeds maximum {MAX_DECIMALS}",
    );
    let decimal =
        Decimal::from_str(value).map_err(|e| anyhow::anyhow!("invalid quantity `{value}`: {e}"))?;
    anyhow::ensure!(decimal.is_sign_positive(), "negative quantity `{value}`");
    Quantity::from_decimal_dp(decimal, precision)
        .map_err(|e| anyhow::anyhow!("invalid quantity `{value}` at precision {precision}: {e}"))
}

/// Converts a [`Decimal`] into a Nautilus [`Price`] at the requested precision.
///
/// Use this when the wire value has already been deserialized as a [`Decimal`]
/// (the standard pattern for model fields tagged with `deserialize_decimal`).
///
/// # Errors
///
/// Returns an error if `precision` exceeds [`MAX_DECIMALS`] or if the value
/// is out of [`Price`] range.
pub fn price_from_decimal(value: Decimal, precision: u8) -> anyhow::Result<Price> {
    anyhow::ensure!(
        precision <= MAX_DECIMALS,
        "price precision {precision} exceeds maximum {MAX_DECIMALS}",
    );
    Price::from_decimal_dp(value, precision)
        .map_err(|e| anyhow::anyhow!("invalid price `{value}` at precision {precision}: {e}"))

View on GitHub (pinned to 18893faf8b)

Solutions

  1. Verify size_decimals for the market matches orderBookDetails — a wrong precision mis-scales the value.
  2. Range-check the magnitude against plausible instrument maximums before parsing and reject out-of-band values upstream.
  3. Inspect the raw payload: overflow-sized quantities indicate a corrupted or fabricated message; drop and log.
  4. Ensure you are not passing an unscaled mantissa string (e.g. "500000000000") where a human-readable decimal was expected — use parse_quantity_from_ticks for mantissas.

Example fix

// before
let qty = parse_quantity(&ticks.to_string(), precision)?; // raw mantissa as string
// after
let qty = parse_quantity_from_ticks(ticks, precision)?;
Defensive patterns

Strategy: validation

Validate before calling

fn quantity_plausible(value: &str, max_qty: f64) -> bool {
    value.parse::<f64>().map(|v| v.is_finite() && v >= 0.0 && v <= max_qty).unwrap_or(false)
}

Try / catch

match parse_quantity(raw, precision) {
    Ok(q) => q,
    Err(e) if e.to_string().contains("at precision") => {
        tracing::error!(raw = %raw, "quantity out of Quantity range; dropping update");
        return Ok(());
    }
    Err(e) => return Err(e.into()),
}

Prevention

When it happens

Trigger: Calling parse_quantity with a valid, non-negative decimal string whose magnitude overflows the Quantity fixed-point range, or whose representation conflicts with the requested precision.

Common situations: Malicious or corrupt feed payloads with absurd sizes; a market whose size_decimals is misreported so values scale wrongly; unit tests probing Quantity bounds; accidentally passing a raw mantissa (unscaled tick count) as a decimal string.

Understand the failure class

Background: "value must be between 0 and 1" / "out of range" / "must not be negative" errors: fixing range-validation failures across open-source libraries — this error's family across 42 libraries.

Related errors


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