nautechsystems/nautilus_trader · error

market amount must be positive

Error message

market amount must be positive

What it means

calculate_market_price walks the market order book for a market order and requires the order amount to be strictly positive. Zero or negative amounts have no meaning for a market execution, so the function bails before parsing levels. Called by bench_submit_market when submitting market orders.

Source

Thrown at crates/adapters/polymarket/src/execution/parse.rs:754

///
/// For BUY: walks asks best-first, accumulates `size * price` (pUSD) until >= amount.
///          Also accumulates the exact shares at each level for precise base qty.
/// For SELL: walks bids best-first, accumulates `size` (shares) until >= amount.
///
/// Returns the crossing price and expected base quantity. If insufficient liquidity,
/// uses all available levels. If the book side is empty, returns an error.
pub fn calculate_market_price(
    book_levels: &[ClobBookLevel],
    amount: Decimal,
    side: PolymarketOrderSide,
) -> anyhow::Result<MarketPriceResult> {
    if book_levels.is_empty() {
        anyhow::bail!("Empty order book: no liquidity available for market order");
    }

    // Parse and sort levels deterministically so we never depend on API ordering.
    // BUY: asks ascending (best/lowest first). SELL: bids descending (best/highest first).
    anyhow::ensure!(amount > Decimal::ZERO, "market amount must be positive");
    let mut parsed_levels = Vec::with_capacity(book_levels.len());
    for level in book_levels {
        let price = parse_decimal_exact(&level.price).context("invalid market-book price")?;
        let size = parse_decimal_exact(&level.size).context("invalid market-book size")?;
        anyhow::ensure!(
            price > Decimal::ZERO && price < Decimal::ONE,
            InvalidMarketPriceError("market-book price must be in (0, 1)".to_string())
        );
        anyhow::ensure!(
            size >= Decimal::ZERO,
            "market-book size must be non-negative"
        );

        if !size.is_zero() {
            parsed_levels.push((price, size));
        }
    }

View on GitHub (pinned to 18893faf8b)

Solutions

  1. Guard the amount before submission: skip or reject the market order if amount <= 0.
  2. Check the computation that produces the amount for truncation/rounding to zero.
  3. Verify the correct quantity variable is passed to bench_submit_market.

Example fix

// before
let amount = qty.round_dp(0); // may round to 0
let res = calculate_market_price(book, amount, side, precision)?;
// after
let amount = qty.round_dp(0);
anyhow::ensure!(amount > Decimal::ZERO, "nothing to execute");
let res = calculate_market_price(book, amount, side, precision)?;
Defensive patterns

Strategy: validation

Validate before calling

if amount <= Decimal::ZERO { return Ok(()); /* nothing to execute */ }

Type guard

fn is_positive_amount(a: Decimal) -> bool { a > Decimal::ZERO }

Prevention

When it happens

Trigger: Calling calculate_market_price with amount <= 0 — e.g. a market buy computed from a zero-size position, a notional that rounded down to 0, or an uninitialized quantity field.

Common situations: Closing a position whose size was already 0; integer/Decimal truncation shrinking a small notional to zero; passing the wrong variable (price instead of qty).

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