nautechsystems/nautilus_trader · error · anyhow::Error

Invalid Interactive Brokers account_id: {e}

Error message

Invalid Interactive Brokers account_id: {e}

What it means

When the configured account_id already contains a dash, resolve_account_id validates it directly with AccountId::new_checked. If validation fails (bad characters or structure), the error is wrapped as 'Invalid Interactive Brokers account_id'. This branch treats the given ID as a fully-qualified issuer-account string.

Source

Thrown at crates/adapters/interactive_brokers/src/factories.rs:204

        );

        let client = InteractiveBrokersExecutionClient::new(core, ib_config, instrument_provider)?;
        Ok(Box::new(client))
    }

    fn name(&self) -> &'static str {
        IB
    }

    fn config_type(&self) -> &'static str {
        stringify!(InteractiveBrokersExecutionClientConfig)
    }
}

fn resolve_account_id(name: &str, account_id: &str) -> anyhow::Result<AccountId> {
    if account_id.contains('-') {
        return AccountId::new_checked(account_id)
            .map_err(|e| anyhow::anyhow!("Invalid Interactive Brokers account_id: {e}"));
    }

    let issuer = if name.is_empty() { IB } else { name };
    AccountId::new_checked(format!("{issuer}-{account_id}"))
        .map_err(|e| anyhow::anyhow!("Invalid Interactive Brokers account_id: {e}"))
}

fn seed_provider_from_cache(
    instrument_provider: &InteractiveBrokersInstrumentProvider,
    cache: &CacheView,
) {
    let instruments = {
        let cache = cache.borrow();
        cache
            .instrument_ids(None)
            .into_iter()
            .filter_map(|instrument_id| cache.instrument(instrument_id).cloned())
            .collect::<Vec<_>>()

View on GitHub (pinned to 18893faf8b)

Solutions

  1. Inspect the wrapped error `e` to see why AccountId::new_checked rejected the string.
  2. Use the exact format expected: '<ISSUER>-<ACCOUNT>' with valid identifier characters, e.g. 'IB-DU12345'.
  3. Strip whitespace or extra separators before configuring account_id.
  4. If you only have the bare account number, drop the dash-containing form and supply the raw ID so the issuer is prepended automatically.

Example fix

// before
account_id = "IB--DU12345" // double dash, invalid
// after
account_id = "IB-DU12345"
Defensive patterns

Strategy: validation

Validate before calling

// only pass dash-containing ids that already validate
let _ = AccountId::new_checked(&account_id).expect("fully-qualified account_id must be valid");

Type guard

fn is_valid_account_id(s: &str) -> bool {
    AccountId::new_checked(s).is_ok()
}

Try / catch

match resolve_account_id(name, account_id) {
    Ok(id) => use(id),
    Err(e) if e.to_string().contains("Invalid Interactive Brokers account_id") => {
        return Err(anyhow!("fix account_id format in config, expected 'ISSUER-ACCOUNT': {e}"));
    }
    Err(e) => return Err(e),
}

Prevention

When it happens

Trigger: Setting account_id to a dash-containing string that is not a valid AccountId — e.g. 'IB--ABC', 'my-broker/account', trailing dash 'IB-', or containing spaces.

Common situations: Copy-pasting an account label from a broker statement that includes extra separators or whitespace; confusing the gateway's account name with a Nautilus AccountId.

Understand the failure class

Background: "invalid id" errors: invalid identifier format — why libraries reject IDs before lookup, and how to fix them — this error's family across 37 libraries.

Related errors


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