{"record":{"id":"c47e03df20fa7d27","repo":"nautechsystems/nautilus_trader","slug":"initial-margin-fraction-must-be-in-1-10-000-was","errorCode":null,"errorMessage":"initial_margin_fraction must be in 1..=10_000, was {initial_margin_fraction}","messagePattern":"initial_margin_fraction must be in 1\\.\\.=10_000, was (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/adapters/lighter/src/execution.rs","lineNumber":2083,"sourceCode":"    /// Returns an error if credentials are missing, the instrument is not\n    /// registered, `initial_margin_fraction` is outside `1..=10_000`, or\n    /// the dispatch pre-flight (nonce allocation, signing) fails. Transport\n    /// errors after dispatch are logged but not returned synchronously.\n    pub fn update_leverage(\n        &self,\n        instrument_id: InstrumentId,\n        initial_margin_fraction: u16,\n        margin_mode: LighterPositionMarginMode,\n    ) -> anyhow::Result<()> {\n        let credential = self.credential.as_ref().ok_or_else(|| {\n            anyhow::anyhow!(\"Lighter execution client cannot update leverage without credentials\")\n        })?;\n\n        let market_index = self.registry.market_index(&instrument_id).ok_or_else(|| {\n            anyhow::anyhow!(\"no Lighter market_index registered for instrument {instrument_id}\")\n        })?;\n\n        anyhow::ensure!(\n            (1..=10_000).contains(&initial_margin_fraction),\n            \"initial_margin_fraction must be in 1..=10_000, was {initial_margin_fraction}\",\n        );\n\n        let ReservedTxContext {\n            context,\n            mut send_reservation,\n        } = self.build_tx_context(credential)?;\n\n        let connection_epoch = send_reservation.connection_epoch;\n\n        let captured_nonce = context.nonce;\n        let captured_api_key_index = context.api_key_index;\n        let mut rollback_guard =\n            TxDispatchGuard::new(self.dispatch.clone(), credential, None, captured_nonce);\n\n        let tx = UpdateLeverageTxInfo {\n            context,","sourceCodeStart":2065,"sourceCodeEnd":2101,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/adapters/lighter/src/execution.rs#L2065-L2101","documentation":"Lighter's initial_margin_fraction is expressed in basis-point-like units and must be between 1 and 10,000 inclusive (i.e. leverage between 10,000x and 1x). update_leverage validates the requested value up front and rejects anything outside that range.","triggerScenarios":"update_leverage called with initial_margin_fraction of 0, negative (if passed as a wider integer), or > 10,000 — e.g. passing a percentage like 50 meaning 50% but which is actually valid only via inverse units, or confusing this field with leverage multiplier.","commonSituations":"Passing leverage (e.g. 20) where the API wants margin fraction, or margin fraction 0.05 where an integer 1..=10_000 is expected; copy-pasted defaults from another exchange adapter; UI sending percent values.","solutions":["Convert leverage to margin fraction: initial_margin_fraction = 10_000 / leverage (e.g. 20x -> 500).","Clamp/validate the value into 1..=10_000 before calling update_leverage.","If you meant to raise leverage, decrease the fraction, not increase it."],"exampleFix":"// before\nclient.update_leverage(iid, 25 /* intended 25x */, mode).await?; // out of range semantics\n// after\nlet imf = (10_000 / leverage).clamp(1, 10_000);\nclient.update_leverage(iid, imf, mode).await?;","handlingStrategy":"validation","validationCode":"let imf = 10_000u32.checked_div(leverage).ok_or(\"leverage must be >= 1\")?;\nassert!((1..=10_000).contains(&imf), \"imf {imf} out of 1..=10_000\");","typeGuard":null,"tryCatchPattern":"if let Err(e) = client.update_leverage(iid, imf, mode).await {\n    if e.to_string().contains(\"initial_margin_fraction must be\") {\n        let clamped = imf.clamp(1, 10_000);\n        client.update_leverage(iid, clamped, mode).await?;\n    }\n}","preventionTips":["Convert leverage to margin fraction (10_000 / leverage) once, in one helper.","Add a clamping helper so every caller passes an in-range value.","Document the units in your strategy config schema to avoid percent/multiplier mixups."],"tags":["leverage","margin","parameter-validation","lighter"],"backgroundTag":"argument-out-of-range","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"}