nautechsystems/nautilus_trader · error

market-buy balance must be positive

Error message

market-buy balance must be positive

What it means

adjust_market_buy_amount caps the fee-adjusted BUY amount by the user's pUSD balance, so the balance must be strictly positive. A zero or negative balance is rejected with "market-buy balance must be positive".

Source

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

/// or the adjusted amount truncates to zero.
pub fn adjust_market_buy_amount(
    amount: Decimal,
    user_pusd_balance: Decimal,
    price: Decimal,
    fee_rate: Decimal,
    fee_exponent: Decimal,
    builder_taker_fee_rate: Decimal,
) -> anyhow::Result<Decimal> {
    if price <= Decimal::ZERO || price >= Decimal::ONE {
        anyhow::bail!(
            "invalid market-buy price {price}: must satisfy 0 < price < 1 for fee adjustment",
        );
    }

    let platform_fee_rate = fee_curve_rate(fee_rate, price, fee_exponent)?;

    anyhow::ensure!(amount > Decimal::ZERO, "market-buy amount must be positive");
    anyhow::ensure!(
        user_pusd_balance > Decimal::ZERO,
        "market-buy balance must be positive"
    );
    anyhow::ensure!(
        builder_taker_fee_rate >= Decimal::ZERO,
        "builder fee rate must be non-negative"
    );
    let platform_fee = amount
        .checked_div(price)
        .and_then(|shares| shares.checked_mul(platform_fee_rate))
        .context("market-buy platform fee overflow")?;
    let builder_fee = amount
        .checked_mul(builder_taker_fee_rate)
        .context("market-buy builder fee overflow")?;
    let total_cost = amount
        .checked_add(platform_fee)
        .and_then(|cost| cost.checked_add(builder_fee))
        .context("market-buy total cost overflow")?;

View on GitHub (pinned to 18893faf8b)

Solutions

  1. Check the account's actual pUSD/USDC balance on the venue and deposit funds if it is zero.
  2. Verify the wallet address used to fetch the balance is the funded trading account.
  3. In the caller, skip order submission when balance <= 0 instead of invoking the adjustment.

Example fix

// before
adjust_market_buy_amount(amount, price, Decimal::ZERO /* balance */)?;
// after
if user_pusd_balance > Decimal::ZERO {
    adjust_market_buy_amount(amount, price, user_pusd_balance)?;
}
Defensive patterns

Strategy: validation

Validate before calling

let balance = fetch_pusd_balance(wallet).await?;
if balance <= Decimal::ZERO { tracing::warn!("no collateral; skipping market buy"); return Ok(()); }

Type guard

fn has_collateral(balance: Decimal) -> bool { balance > Decimal::ZERO }

Try / catch

match adjust_market_buy_amount(amount, price, balance, ...) {
    Err(e) if e.to_string().contains("balance must be positive") => {
        tracing::warn!("insufficient/zero pUSD balance — deposit or fix wallet config");
    }
    other => other?,
}

Prevention

When it happens

Trigger: Calling adjust_market_buy_amount with user_pusd_balance <= 0 — typically when the wallet reports no collateral, balance fetch failed/returned zero, or the balance was queried before funding/deposits settled.

Common situations: Fresh account with no USDC deposited; balance cache populated before the deposit confirmed; wrong wallet address configured so the fetched balance is zero.

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