{"record":{"id":"cfd6b6e11b54fe2f","repo":"nautechsystems/nautilus_trader","slug":"last-price-was-zero-when-calculating-base-quanti","errorCode":null,"errorMessage":"`last_price` was zero when calculating base quantity","messagePattern":"`last_price` was zero when calculating base quantity","errorType":"validation","errorClass":"anyhow::Error","httpStatus":null,"severity":"error","filePath":"crates/model/src/instruments/mod.rs","lineNumber":545,"sourceCode":"                ),\n            });\n        }\n\n        Quantity::from_raw_checked(quantity.raw, precision)\n    }\n\n    /// # Errors\n    ///\n    /// Returns an error if `last_price` is zero, or if the value cannot be converted to a\n    /// `Quantity`.\n    fn try_calculate_base_quantity(\n        &self,\n        quantity: Quantity,\n        last_price: Price,\n    ) -> anyhow::Result<Quantity> {\n        let last_px = last_price.as_decimal();\n        if last_px.is_zero() {\n            anyhow::bail!(\"`last_price` was zero when calculating base quantity\");\n        }\n        let precision = u32::from(self.min_size_increment_precision());\n        let value = (quantity.as_decimal() / last_px)\n            .round_dp_with_strategy(precision, RoundingStrategy::MidpointNearestEven);\n        Quantity::from_decimal_dp(value, self.size_precision()).map_err(Into::into)\n    }\n\n    /// # Panics\n    ///\n    /// Panics if `last_price` is zero, or if the value cannot be converted to a `Quantity`\n    /// (see `try_calculate_base_quantity`).\n    fn calculate_base_quantity(&self, quantity: Quantity, last_price: Price) -> Quantity {\n        self.try_calculate_base_quantity(quantity, last_price)\n            .unwrap()\n    }\n\n    /// Calculates the notional value for the given quantity and price.\n    ///","sourceCodeStart":527,"sourceCodeEnd":563,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/model/src/instruments/mod.rs#L527-L563","documentation":"Instrument::try_calculate_base_quantity converts a quote-currency quantity into a base quantity by dividing by last_price; a zero last_price would divide by zero, so the method bails with this error before computing.","triggerScenarios":"Calling calculate_base_quantity(quantity, Price::zero()) — typically when the latest trade/price feed has not initialized and a default zero Price is passed.","commonSituations":"Strategy startup before the first quote/trade arrives, stale market data after a halt, or a price field default-initialized to zero.","solutions":["Guard that last_price.is_positive() before calling calculate_base_quantity.","Wait for the first price update event before sizing orders.","Use a fallback reference price (e.g. mid or last known good) when last is zero."],"exampleFix":"// before\nlet base_qty = instrument.calculate_base_quantity(quote_qty, last_price)?;\n// after\nif last_price.as_decimal().is_zero() {\n    anyhow::bail!(\"no valid last price yet; skipping sizing\");\n}\nlet base_qty = instrument.calculate_base_quantity(quote_qty, last_price)?;","handlingStrategy":"validation","validationCode":"if last_price.as_decimal().is_zero() { return Err(anyhow::anyhow!(\"last price not initialized\")); }","typeGuard":null,"tryCatchPattern":"match instrument.try_calculate_base_quantity(qty, last_price) {\n    Ok(base) => base,\n    Err(_) => { /* skip sizing until a price arrives */ }\n}","preventionTips":["Gate order sizing behind receipt of the first market-data update.","Use a positive fallback reference price during data outages."],"tags":["price","division-by-zero","market-data"],"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-14T05:17:10.506Z"}