nautechsystems/nautilus_trader · error · anyhow::Error

Trade info is not initialized. Please call calculate_trade_i

Error message

Trade info is not initialized. Please call calculate_trade_info() first.

What it means

This error is thrown by `check_if_trade_info_initialized` on the quote wrapper when the internal `trade_info: Option<SwapTradeInfo>` is still `None`. Metrics such as price impact and slippage depend on precomputed swap trade info, so the library forces the caller to call `calculate_trade_info()` (which populates the field) before querying any metric.

Source

Thrown at crates/model/src/defi/pool_analysis/quote.rs:116

            instrument_id,
            amount0,
            amount1,
            sqrt_price_before_x96,
            sqrt_price_after_x96,
            tick_before,
            tick_after,
            liquidity_after,
            fee_growth_global_after,
            lp_fee,
            protocol_fee,
            crossed_ticks,
            trade_info: None,
        }
    }

    fn check_if_trade_info_initialized(&self) -> anyhow::Result<&SwapTradeInfo> {
        if self.trade_info.is_none() {
            anyhow::bail!(
                "Trade info is not initialized. Please call calculate_trade_info() first."
            );
        }

        Ok(self.trade_info.as_ref().unwrap())
    }

    /// Calculates and populates the `trade_info` field with market-oriented trade data.
    ///
    /// This method transforms the raw swap quote data (token0/token1 amounts, sqrt prices)
    /// into standard trading terminology (base/quote, order side, execution price).
    /// The computation uses the `sqrt_price_before_x96` to calculate price impact and slippage.
    ///
    /// # Errors
    ///
    /// Returns an error if trade info computation or price calculations fail.
    pub fn calculate_trade_info(&mut self, token0: &Token, token1: &Token) -> anyhow::Result<()> {
        let trade_info_calculator = SwapTradeInfoCalculator::new(

View on GitHub (pinned to 18893faf8b)

Solutions

  1. Call `quote.calculate_trade_info()` once after construction, before any metric getter.
  2. Ensure all code paths that read metrics first pass through trade-info initialization.
  3. Consider constructing the quote via an API that computes trade info eagerly, if available.
  4. In async/event-driven code, verify initialization order between quote creation and metric consumption.

Example fix

// before
let mut quote = Quote::new(pool, amount_in);
let impact = quote.get_price_impact_bps()?;
// after
let mut quote = Quote::new(pool, amount_in);
quote.calculate_trade_info()?;
let impact = quote.get_price_impact_bps()?;
Defensive patterns

Strategy: validation

Validate before calling

// ensure initialization before reading metrics
quote.calculate_trade_info()?;
// now get_price_impact_bps()/get_slippage_bps() are safe to call

Type guard

fn trade_info_ready(quote: &Quote) -> bool {
    !quote.trade_info.is_none() // or expose an is_initialized() helper
}

Try / catch

let impact = quote.get_price_impact_bps().map_err(|e| {
    if e.to_string().contains("not initialized") {
        // initialize and retry once
    }
    e
})?;

Prevention

When it happens

Trigger: Calling `get_price_impact_bps()` or `get_slippage_bps()` (and their dependents like `validate_slippage_tolerance`) on a quote created via the constructor that sets `trade_info: None`, without first invoking `calculate_trade_info()`.

Common situations: Constructing a quote object and immediately reading metrics in exploratory code; refactoring where a `calculate_trade_info()` call was dropped; conditionally calculating trade info on one code path but unconditionally reading metrics on another.

Understand the failure class

Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.

Related errors


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