{"record":{"id":"0dd86758f7f658d6","repo":"nautechsystems/nautilus_trader","slug":"invalid-leverage-leverage-for","errorCode":null,"errorMessage":"Invalid leverage {leverage} for {}","messagePattern":"Invalid leverage (.+?) for (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/model/src/accounts/margin_model.rs","lineNumber":326,"sourceCode":"    pyo3_stub_gen::derive::gen_stub_pyclass(module = \"nautilus_trader.model\")\n)]\npub struct LeveragedMarginModel;\n\nimpl MarginModel for LeveragedMarginModel {\n    fn name(&self) -> &'static str {\n        \"leveraged\"\n    }\n\n    fn calculate_initial_margin(\n        &self,\n        instrument: &dyn Instrument,\n        quantity: Quantity,\n        price: Price,\n        leverage: Decimal,\n        use_quote_for_inverse: Option<bool>,\n    ) -> anyhow::Result<Money> {\n        if leverage <= Decimal::ZERO {\n            anyhow::bail!(\"Invalid leverage {leverage} for {}\", instrument.id());\n        }\n        let use_quote = use_quote_for_inverse.unwrap_or(false);\n        let notional = instrument.try_calculate_notional_value(quantity, price, Some(use_quote))?;\n        let margin = notional\n            .as_decimal()\n            .abs()\n            .checked_div(leverage)\n            .and_then(|adjusted| adjusted.checked_mul(instrument.margin_init()))\n            .ok_or_else(|| anyhow::anyhow!(\"initial margin calculation overflow\"))?;\n        let currency = margin_currency(instrument, use_quote)?;\n        Money::from_decimal(margin, currency).map_err(Into::into)\n    }\n\n    fn calculate_maintenance_margin(\n        &self,\n        instrument: &dyn Instrument,\n        quantity: Quantity,\n        price: Price,","sourceCodeStart":308,"sourceCodeEnd":344,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/model/src/accounts/margin_model.rs#L308-L344","documentation":"Leveraged initial margin calculation requires a strictly positive leverage; leverage <= 0 is meaningless (division by zero or negative notional-per-unit margin). The error includes the offending leverage and the instrument id.","triggerScenarios":"Calling calculate_initial_margin on the leveraged margin model with leverage <= Decimal::ZERO (zero or negative Decimal).","commonSituations":"Leverage loaded from config or an exchange API as 0/0.0 before the instrument is fully initialized; sign flips when computing leverage from notional/margin ratios; unset optional fields defaulting to zero.","solutions":["Validate leverage > 0 before calling calculate_initial_margin","Fix the source of leverage (config, exchange metadata) so it carries a positive value","Use the margin model matching the instrument's actual leverage setting"],"exampleFix":"// before\nmodel.calculate_initial_margin(&instrument, qty, price, Decimal::ZERO, None)?; // bails\n// after\nlet leverage = Decimal::from(10); // or instrument.leverage()\nanyhow::ensure!(leverage > Decimal::ZERO);\nmodel.calculate_initial_margin(&instrument, qty, price, leverage, None)?;","handlingStrategy":"validation","validationCode":"anyhow::ensure!(leverage > Decimal::ZERO, \"leverage must be positive\");\nlet margin = model.calculate_initial_margin(&instrument, qty, price, leverage, None)?;","typeGuard":"fn is_valid_leverage(l: Decimal) -> bool { l > Decimal::ZERO }","tryCatchPattern":"match model.calculate_initial_margin(&instrument, qty, price, leverage, None) {\n    Err(e) if e.to_string().starts_with(\"Invalid leverage\") => { /* fix leverage source */ }\n    other => other?,\n}","preventionTips":["Default leverage to instrument.leverage() rather than a literal zero","Validate config at load time (leverage > 0)","Check exchange metadata is loaded before margin math"],"tags":["rust","margin","leverage","validation"],"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"}