{"record":{"id":"d45eab055d9533e8","repo":"nautechsystems/nautilus_trader","slug":"cannot-calculate-cfd-swap-for-position-currenc","errorCode":null,"errorMessage":"cannot calculate CFD swap for position {}: currency conversion overflow","messagePattern":"cannot calculate CFD swap for position (.+?): currency conversion overflow","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/backtest/src/modules/cfd_swap.rs","lineNumber":326,"sourceCode":"                                ),\n                            );\n                            return Ok(None);\n                        }\n                        Err(e) => {\n                            self.log_calculation_failure(\n                                booking_date,\n                                instrument_id,\n                                CfdSwapFailureKind::Xrate,\n                                &format!(\n                                    \"Cannot calculate CFD swap for {instrument_id}: exchange rate from {} to {base_currency}: {e}\",\n                                    notional.currency\n                                ),\n                            );\n                            return Ok(None);\n                        }\n                    };\n                    let amount = amount.checked_mul(xrate).ok_or_else(|| {\n                        anyhow::anyhow!(\n                            \"cannot calculate CFD swap for position {}: currency conversion overflow\",\n                            position.id\n                        )\n                    })?;\n                    (amount, base_currency)\n                } else {\n                    (amount, notional.currency)\n                };\n                adjustments.push(Money::from_decimal(amount, currency)?);\n            }\n        }\n\n        Ok(Some(adjustments))\n    }\n\n    fn log_totals(label: &str, totals: &AHashMap<Currency, Decimal>) -> anyhow::Result<()> {\n        let mut currencies = totals.keys().copied().collect::<Vec<_>>();\n        currencies.sort_unstable_by_key(|currency| currency.code);","sourceCodeStart":308,"sourceCodeEnd":344,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/backtest/src/modules/cfd_swap.rs#L308-L344","documentation":"After computing the swap adjustment in the position's quote currency, the module converts it to the account base currency by multiplying with the exchange rate. If amount * xrate overflows Decimal, the conversion is impossible and this error is raised for that position.","triggerScenarios":"calculate_adjustments (via process) converts a swap amount using an exchange rate from cache.try_get_xrate where the product exceeds Decimal::MAX — caused by an inflated adjustment (huge notional/rate) or a wildly wrong stored exchange rate.","commonSituations":"Bad FX rate in the cache (scale error, e.g. rate stored as 1e15); base currency misconfigured so conversion uses an inappropriate rate; upstream overflow from 3713's causes propagating into conversion.","solutions":["Verify cached exchange rates for the quote->base pair are sane (order of magnitude ~1 for major pairs)","Fix the swap rate/multiplier/notional so the pre-conversion amount is realistic","Correct the account base_currency configuration if an unintended pair is being converted"],"exampleFix":"// before: trusting any cached rate blindly\nlet xrate = ctx.cache.try_get_xrate(quote, base)?.unwrap();\n// after: sanity-check magnitude first\nlet xrate = ctx.cache.try_get_xrate(quote, base)?.unwrap();\nassert!(xrate > Decimal::ZERO && xrate < Decimal::from(1_000_000));","handlingStrategy":"validation","validationCode":"// sanity-check cached FX rate before conversion\nif let Some(xrate) = ctx.cache.try_get_xrate(quote, base)? {\n    assert!(xrate > Decimal::ZERO && xrate < Decimal::from(1_000_000), \"implausible xrate\");\n}","typeGuard":null,"tryCatchPattern":"match module.process(ts_now) {\n    Err(e) if e.to_string().contains(\"currency conversion overflow\") => {\n        log::error!(\"bad FX rate or inflated amount: {e:#}\");\n        SimulationModuleResult::NotReady\n    }\n    other => other,\n}","preventionTips":["Seed the cache with correct-magnitude exchange rates","Confirm account base_currency matches your conversion expectations","Fix upstream amount overflow (rate/multiplier scale) first"],"tags":["backtest","decimal-overflow","cfd","currency-conversion"],"backgroundTag":"value-out-of-range","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"}