nautechsystems/nautilus_trader · error

Invalid ask quantity for OrderBook::filtered_view

Error message

Invalid ask quantity for OrderBook::filtered_view

What it means

In OrderBook::filtered_view, each synthetic ask quantity is converted with Quantity::from_decimal(...).expect(...) and panics with this message on failure. from_decimal returns None for negative, non-finite, or precision/range-violating decimals; the surrounding code already skips quantity <= 0, so this panic indicates a non-finite quantity or a precision violation.

Source

Thrown at crates/model/src/orderbook/book.rs:841

                Price::from_decimal(price).expect("Invalid bid price for OrderBook::filtered_view"),
                Quantity::from_decimal(quantity)
                    .expect("Invalid bid quantity for OrderBook::filtered_view"),
                order_id,
            );
            order_id += 1;
            filtered_book.add(order, 0, sequence, ts_event);
        }

        for (price, quantity) in asks_map {
            if quantity <= Decimal::ZERO {
                continue;
            }

            let order = BookOrder::new(
                OrderSide::Sell,
                Price::from_decimal(price).expect("Invalid ask price for OrderBook::filtered_view"),
                Quantity::from_decimal(quantity)
                    .expect("Invalid ask quantity for OrderBook::filtered_view"),
                order_id,
            );
            order_id += 1;
            filtered_book.add(order, 0, sequence, ts_event);
        }

        Ok(filtered_book)
    }

    /// Groups bid quantities into price buckets, truncating to a maximum depth, excluding own orders.
    ///
    /// With `own_book`, subtracts own order sizes, filtered by `status` if provided.
    /// When `now` is provided, only subtracts orders whose acceptance time plus
    /// `accepted_buffer_ns` is at or before `now`. When `now` is `None`, acceptance-time
    /// filtering is disabled.
    ///
    /// # Panics
    ///

View on GitHub (pinned to 18893faf8b)

Solutions

  1. Round ask quantities to the instrument size precision and assert finiteness before calling filtered_view.
  2. Filter out non-finite entries when constructing the asks map.
  3. Use the project's Quantity/from_raw fixed-point path instead of ad-hoc decimal conversion.
  4. Sanitize external inputs at parse time.
  5. Mirror the same validation used for the bids side to keep behavior consistent.

Example fix

// before
asks.insert(price, Quantity::from_decimal(unrounded_qty_dec));
// after
let q = unrounded_qty_dec.round_dp(instrument.size_precision as u32);
debug_assert!(q.is_finite() && q.is_sign_positive());
asks.insert(price, Quantity::from_decimal(q));
Defensive patterns

Strategy: validation

Validate before calling

fn valid_ask_qty(q: &Quantity) -> bool {
    let d = q.as_decimal();
    d.is_finite() && d.is_sign_positive()
}
assert!(ask_map.iter().all(|(_, q)| valid_ask_qty(q)));

Type guard

fn is_valid_quantity(q: &Quantity) -> bool {
    q.as_decimal().is_finite()
}

Try / catch

// Validate before the call; panics are not recoverable in Rust. Python:
try:
    view = book.filtered_view(bids, asks, None)
except BaseException as e:
    log.warning("filtered_view failed: %s", e)

Prevention

When it happens

Trigger: Calling filtered_view / py_filtered_view with an asks map whose quantity is NaN, infinite, or exceeds the supported decimal precision (unrounded float artifacts, oversized values).

Common situations: Raw floating-point computations inserted directly as quantities; parsing external data with more decimal places than allowed; size precision changed on the instrument without renormalizing the map.

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/0c714b6775e333db. Report an issue: GitHub.