nautechsystems/nautilus_trader · error · anyhow::Error

command instrument ID {} does not match cached order {}

Error message

command instrument ID {} does not match cached order {}

What it means

Companion check in `validate_cancel_order_target`: the `CancelOrder` command's `instrument_id` must equal the cached order's `instrument_id`. A mismatch means the cancel command targets an order resolved on a different instrument, which would route the cancel to the wrong IB contract, so `ensure!` aborts with this error.

Source

Thrown at crates/adapters/interactive_brokers/src/execution/core.rs:2112

            order_id = exec_data.execution.order_id,
            order_ref = exec_data.execution.order_reference.as_str(),
            execution_id = exec_data.execution.execution_id.as_str(),
            error = %error,
            "Failed to parse IBKR historical fill report",
        );
    }

    fn validate_cancel_order_target(
        cmd: &CancelOrder,
        target_order: &OrderAny,
    ) -> anyhow::Result<()> {
        anyhow::ensure!(
            cmd.client_order_id == target_order.client_order_id(),
            "command client order ID {} does not match cached order {}",
            cmd.client_order_id,
            target_order.client_order_id()
        );
        anyhow::ensure!(
            cmd.instrument_id == target_order.instrument_id(),
            "command instrument ID {} does not match cached order {}",
            cmd.instrument_id,
            target_order.instrument_id()
        );

        // Command actor IDs identify the requester and are not ownership evidence
        if let (Some(command_venue_order_id), Some(target_venue_order_id)) =
            (cmd.venue_order_id.as_ref(), target_order.venue_order_id())
        {
            anyhow::ensure!(
                command_venue_order_id == &target_venue_order_id,
                "command venue order ID {command_venue_order_id} does not match cached order {target_venue_order_id}"
            );
        }

        Ok(())
    }

View on GitHub (pinned to 18893faf8b)

Solutions

  1. Verify the instrument ID (symbol and venue) on the command matches the order's instrument, including suffix conventions.
  2. Re-resolve the cached order by `cmd.client_order_id` instead of by instrument to avoid cross-instrument lookups.
  3. If instrument IDs legitimately changed (contract roll), update cached orders via reconciliation before cancelling.

Example fix

// before
anyhow::ensure!(cmd.instrument_id == target_order.instrument_id(), ...);
// after (fix at call site: resolve by client_order_id)
let target_order = cache.order(&cmd.client_order_id)?;
anyhow::ensure!(cmd.instrument_id == target_order.instrument_id(), ...);
Defensive patterns

Strategy: validation

Validate before calling

// Ensure instrument IDs match before cancelling
anyhow::ensure!(
    cmd.instrument_id == order.instrument_id(),
    "instrument mismatch: cmd={} order={}", cmd.instrument_id, order.instrument_id()
);

Type guard

fn same_instrument(cmd: &CancelOrder, order: &OrderAny) -> bool {
    cmd.instrument_id == order.instrument_id()
}

Try / catch

if !same_instrument(&cmd, &order) {
    return Err(anyhow::anyhow!(
        "cancel {} refused: instrument {} != order instrument {}",
        cmd.client_order_id, cmd.instrument_id, order.instrument_id()
    ));
}

Prevention

When it happens

Trigger: A `CancelOrder` whose `cmd.instrument_id` differs from `target_order.instrument_id()` — typically a cache lookup that returned an order from another instrument, or a command built with the wrong/synthetic instrument ID.

Common situations: Instrument ID symbol/venue suffix mismatches after symbol mapping changes; cache keyed incorrectly; using a continuation/rolled contract ID while the cached order carries the original ID.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


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