nautechsystems/nautilus_trader · error

Invalid bid quantity for OrderBook::filtered_view

Error message

Invalid bid quantity for OrderBook::filtered_view

What it means

In OrderBook::filtered_view, each synthetic bid quantity is built with Quantity::from_decimal(...).expect(...). Quantity::from_decimal returns None when the decimal is negative, non-finite, or exceeds the representable precision/range, and the expect converts that into this panic. Zero/negative quantities are already skipped by the `quantity <= 0` check, so this fires for non-finite or precision/range violations.

Source

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

        let mut filtered_book = Self::new(self.instrument_id, self.book_type);
        filtered_book.sequence = self.sequence;
        filtered_book.ts_last = self.ts_last;

        let sequence = self.sequence;
        let ts_event = self.ts_last;

        let mut order_id = 1_u64;

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

            let order = BookOrder::new(
                OrderSide::Buy,
                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,
            );

View on GitHub (pinned to 18893faf8b)

Solutions

  1. Validate quantities are finite and round them to the instrument size precision before calling filtered_view.
  2. Reuse the same Quantity normalization used elsewhere in the pipeline (from_raw / fixed-point conversion) rather than ad-hoc from_decimal.
  3. Filter out non-finite quantity entries when building the bids map.
  4. Sanitize external input (JSON/config) at parse time, rejecting non-finite values.
  5. Keep quantity precision aligned with the instrument definition whenever it changes.

Example fix

// before
let qty = Quantity::from_decimal(Decimal::from_f64_retain(raw_qty).unwrap()); // may exceed precision
// after
let qty_dec = Decimal::from_f64_retain(raw_qty).unwrap().round_dp(instrument.size_precision as u32);
assert!(qty_dec.is_finite() && qty_dec.is_sign_positive());
let qty = Quantity::from_decimal(qty_dec);
Defensive patterns

Strategy: validation

Validate before calling

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

Type guard

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

Try / catch

// Validate quantities before filtered_view; panics aren't catchable 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 a bids map whose quantity is NaN, infinite, or has more decimal places than the Quantity precision supports (e.g. unrounded floating-point results like 0.30000000000000004 at high precision).

Common situations: Feeding raw float math results into the quantity map; constructing quantities from strings with excessive precision; instrument precision changed but quantities not re-normalized.

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