{"record":{"id":"4d5f8186ebb80c43","repo":"nautechsystems/nautilus_trader","slug":"market-amount-must-be-positive","errorCode":null,"errorMessage":"market amount must be positive","messagePattern":"market amount must be positive","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/adapters/polymarket/src/execution/parse.rs","lineNumber":754,"sourceCode":"///\n/// For BUY: walks asks best-first, accumulates `size * price` (pUSD) until >= amount.\n///          Also accumulates the exact shares at each level for precise base qty.\n/// For SELL: walks bids best-first, accumulates `size` (shares) until >= amount.\n///\n/// Returns the crossing price and expected base quantity. If insufficient liquidity,\n/// uses all available levels. If the book side is empty, returns an error.\npub fn calculate_market_price(\n    book_levels: &[ClobBookLevel],\n    amount: Decimal,\n    side: PolymarketOrderSide,\n) -> anyhow::Result<MarketPriceResult> {\n    if book_levels.is_empty() {\n        anyhow::bail!(\"Empty order book: no liquidity available for market order\");\n    }\n\n    // Parse and sort levels deterministically so we never depend on API ordering.\n    // BUY: asks ascending (best/lowest first). SELL: bids descending (best/highest first).\n    anyhow::ensure!(amount > Decimal::ZERO, \"market amount must be positive\");\n    let mut parsed_levels = Vec::with_capacity(book_levels.len());\n    for level in book_levels {\n        let price = parse_decimal_exact(&level.price).context(\"invalid market-book price\")?;\n        let size = parse_decimal_exact(&level.size).context(\"invalid market-book size\")?;\n        anyhow::ensure!(\n            price > Decimal::ZERO && price < Decimal::ONE,\n            InvalidMarketPriceError(\"market-book price must be in (0, 1)\".to_string())\n        );\n        anyhow::ensure!(\n            size >= Decimal::ZERO,\n            \"market-book size must be non-negative\"\n        );\n\n        if !size.is_zero() {\n            parsed_levels.push((price, size));\n        }\n    }\n","sourceCodeStart":736,"sourceCodeEnd":772,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/adapters/polymarket/src/execution/parse.rs#L736-L772","documentation":"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.","triggerScenarios":"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.","commonSituations":"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).","solutions":["Guard the amount before submission: skip or reject the market order if amount <= 0.","Check the computation that produces the amount for truncation/rounding to zero.","Verify the correct quantity variable is passed to bench_submit_market."],"exampleFix":"// before\nlet amount = qty.round_dp(0); // may round to 0\nlet res = calculate_market_price(book, amount, side, precision)?;\n// after\nlet amount = qty.round_dp(0);\nanyhow::ensure!(amount > Decimal::ZERO, \"nothing to execute\");\nlet res = calculate_market_price(book, amount, side, precision)?;","handlingStrategy":"validation","validationCode":"if amount <= Decimal::ZERO { return Ok(()); /* nothing to execute */ }","typeGuard":"fn is_positive_amount(a: Decimal) -> bool { a > Decimal::ZERO }","tryCatchPattern":null,"preventionTips":["Guard quantity computations before submitting market orders","Watch for round-to-zero on small notionals","Skip market-close attempts when position size is 0"],"tags":["rust","validation","market-order","polymarket"],"backgroundTag":"invalid-argument-value","analyzedSha":"18893faf8b356be3320add8de2f861b0b647cf06","analyzedAt":"2026-09-08T20:49:34.690Z","contentChangedAt":"2026-09-08T20:49:34.690Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}