nautechsystems/nautilus_trader · error

unapplied CFD swap total overflow

Error message

unapplied CFD swap total overflow

What it means

Raised when accumulating a failed (non-retryable) CFD swap adjustment amount into the per-currency `unapplied_swap_totals` diagnostic would overflow. Like the applied-total overflow, it protects the integrity of diagnostic aggregation for swap adjustments that could not be applied.

Solutions

  1. Fix the root cause making adjustments fail (funding, account config, rate data) so failures stop accumulating.
  2. Run a shorter backtest window or reset diagnostics periodically.
  3. Validate input amounts and account currencies before starting the run.
Defensive patterns

Strategy: validation

Validate before calling

anyhow::ensure!(account.is_funded_for(currency), "account must hold {currency} before swaps accrue");

Try / catch

if let Err(e) = engine.acknowledge(&outcomes) {
    if e.to_string().contains("unapplied CFD swap total overflow") {
        log::error!("too many failed adjustments accumulated; fix underlying failures");
    } else { return Err(e); }
}

Prevention

When it happens

Trigger: Many failed swap adjustments (e.g. persistent account/funding errors) whose summed amounts exceed the Decimal accumulator bound during a backtest.

Common situations: Long backtests where every rollover fails (e.g. account currency never funded, missing rate data) so unapplied amounts pile up daily; corrupted amount data inflating each adjustment.

Understand the failure class

Background: "value must be between 0 and 1" / "out of range" / "must not be negative" errors: fixing range-validation failures across open-source libraries — this error's family across 42 libraries.

Related errors


AI-assisted analysis of nautechsystems/nautilus_trader@18893faf8b (2026-09-08). Data as JSON: /api/errors/94a1b8d88083b8b3. Report an issue: GitHub.

Appendix: source

Thrown at crates/backtest/src/modules/cfd_swap.rs:505

                AccountAdjustmentOutcome::Failed(error) if error.is_retryable() => {
                    log::warn!(
                        "Cannot apply CFD swap adjustment for {} on {}: {error}",
                        adjustment.amount.currency,
                        adjustment.booking_date
                    );
                    failed.push(adjustment);
                }
                AccountAdjustmentOutcome::Failed(error) => {
                    log::warn!(
                        "CFD swap adjustment {} on {} is recorded as unapplied: {error}",
                        adjustment.amount,
                        adjustment.booking_date
                    );
                    let mut totals = self.unapplied_swap_totals.borrow_mut();
                    let total = totals.entry(adjustment.amount.currency).or_default();
                    *total = total
                        .checked_add(adjustment.amount.as_decimal())
                        .ok_or_else(|| anyhow::anyhow!("unapplied CFD swap total overflow"))?;
                }
            }
        }

        let mut day = self.rollover_day.borrow_mut();
        let day = day
            .as_mut()
            .ok_or_else(|| anyhow::anyhow!("CFD swap rollover day is not initialized"))?;
        if failed.is_empty() {
            day.date = batch_end_date;
            day.pending_end_date = None;
            day.warned_failures.clear();
            self.rollover_completed.set(true);
        } else {
            day.pending_adjustments = Some(failed);
        }
        Ok(())
    }

View on GitHub (pinned to 18893faf8b)