nautechsystems/nautilus_trader · error

Failed to parse base quantity for ord_id={}, sz='{}': {e}

Error message

Failed to parse base quantity for ord_id={}, sz='{}': {e}

What it means

For base-quantity orders (not quote-quantity per tgt_ccy/heuristic), parse_order_status_report parses sz directly as the base-currency quantity with the instrument's size precision. This error means the sz string from OKX failed decimal parsing or precision normalization.

Source

Thrown at crates/adapters/okx/src/common/parse.rs:806

                    order.ord_id.as_str(),
                    order.sz
                )
            })?
        };

        let filled_qty_dec = parse_quantity(&order.acc_fill_sz, size_precision).map_err(|e| {
            anyhow::anyhow!(
                "Failed to parse filled quantity for ord_id={}, acc_fill_sz='{}': {e}",
                order.ord_id.as_str(),
                order.acc_fill_sz
            )
        })?;

        (quantity_base, filled_qty_dec)
    } else {
        // Base-quantity order: both sz and acc_fill_sz are in base currency
        let quantity_dec = parse_quantity(&order.sz, size_precision).map_err(|e| {
            anyhow::anyhow!(
                "Failed to parse base quantity for ord_id={}, sz='{}': {e}",
                order.ord_id.as_str(),
                order.sz
            )
        })?;
        let filled_qty_dec = parse_quantity(&order.acc_fill_sz, size_precision).map_err(|e| {
            anyhow::anyhow!(
                "Failed to parse filled quantity for ord_id={}, acc_fill_sz='{}': {e}",
                order.ord_id.as_str(),
                order.acc_fill_sz
            )
        })?;

        (quantity_dec, filled_qty_dec)
    };

    // For quote-quantity orders marked as FILLED, adjust quantity to match filled_qty
    // to avoid precision mismatches from quote-to-base conversion

View on GitHub (pinned to 18893faf8b)

Solutions

  1. Log order.sz and ord_id to inspect the failing value
  2. Align the Nautilus instrument definition's size_precision with the OKX instrument lotSz
  3. Normalize empty sz to "0" or skip the order upstream
  4. Upgrade the OKX adapter in case newer versions handle the format

Example fix

// before
let qty = parse_quantity(&order.sz, size_precision)?;
// after
let qty = match parse_quantity(order.sz.trim(), size_precision) {
    Ok(q) => q,
    Err(e) => { log::warn!("Skipping order {}: bad sz '{}': {e}", order.ord_id, order.sz); return Ok(None); }
};
Defensive patterns

Strategy: validation

Validate before calling

let sz = order.sz.trim();
if sz.is_empty() || rust_decimal::Decimal::from_str(sz).is_err() { /* skip order */ }

Type guard

fn parseable_base_sz(s: &str, precision: u8) -> bool {
    rust_decimal::Decimal::from_str(s.trim()).map(|d| d.fract_digits() as u8 <= precision).unwrap_or(false)
}

Try / catch

match parse_order_status_report(&order, &instrument, ts_init) {
    Ok(r) => handle(r),
    Err(e) if e.to_string().contains("base quantity") => { log::warn!("bad sz for {}: {e}", order.ord_id); Ok(None) }
    Err(e) => Err(e),
}

Prevention

When it happens

Trigger: A limit order, SELL market order, or non-spot order whose sz field is empty, non-numeric, or has more decimal places than the instrument's size_precision allows.

Common situations: Mismatched instrument definition (size_precision does not match OKX lotSz); empty sz on cancelled legacy orders; OKX returning unexpected formats (e.g. exponent notation); wrong instrument mapping for the inst_id.

Understand the failure class

Background: "Invalid ... format", "must be in format X", "does not look like a ..." — invalid argument format errors across CLI tools and libraries — this error's family across 17 libraries.

Related errors


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