{"record":{"id":"10daeea416c18b9e","repo":"nautechsystems/nautilus_trader","slug":"cannot-add-an-instrument-object-without-first-ad","errorCode":null,"errorMessage":"Cannot add an `Instrument` object without first adding its associated venue {}","messagePattern":"Cannot add an `Instrument` object without first adding its associated venue (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/backtest/src/engine.rs","lineNumber":377,"sourceCode":"            exchange.borrow_mut().add_instrument(instrument.clone())?;\n            if let Some(expiration_ns) = instrument.expiration_ns() {\n                self.set_instrument_expiration_timer(exchange, instrument_id, expiration_ns)?;\n            }\n\n            if let Some(previous_expiration_ns) = previous_expiration_ns\n                && instrument.expiration_ns() != Some(previous_expiration_ns)\n                && !exchange\n                    .borrow()\n                    .has_unprocessed_instrument_expiration(previous_expiration_ns)\n            {\n                let timer_name = Self::instrument_expiration_timer_name(\n                    instrument_id.venue,\n                    previous_expiration_ns,\n                );\n                self.kernel.clock.borrow_mut().cancel_timer(&timer_name);\n            }\n        } else {\n            anyhow::bail!(\n                \"Cannot add an `Instrument` object without first adding its associated venue {}\",\n                instrument.id().venue\n            )\n        }\n\n        self.add_market_data_client_if_not_exists(instrument.id().venue);\n\n        self.kernel\n            .data_engine\n            .borrow_mut()\n            .process(instrument as &dyn Any);\n        log::info!(\n            \"Added instrument {} to exchange {}\",\n            instrument_id,\n            instrument_id.venue\n        );\n        Ok(())\n    }","sourceCodeStart":359,"sourceCodeEnd":395,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/backtest/src/engine.rs#L359-L395","documentation":"The backtest engine requires the venue for an instrument to be registered before the instrument itself is added. add_instrument checks that the instrument's venue exists in the kernel; if not, it aborts so market data routing has an exchange to attach to. This preserves the invariant that every instrument belongs to a known simulated venue.","triggerScenarios":"Calling engine.add_instrument(instrument) without a prior add_venue(...) for instrument.id().venue, or adding an instrument whose venue string does not exactly match the configured venue name.","commonSituations":"Copying an instrument from a live config whose venue name differs (e.g. 'BINANCE' vs 'BINANCE_FUTURES'); forgetting add_venue in a new script; instrument built for a venue that was never configured.","solutions":["Call engine.add_venue(...) with a venue matching instrument.id().venue before add_instrument","Verify instrument.id().venue string equals the venue passed to add_venue (case-sensitive exact match)","Check that the instrument was built with the intended Venue, not a default or stale value"],"exampleFix":"// before\nengine.add_instrument(instrument)?;\n// after\nengine.add_venue(venue, execution_engine_config, risk_engine_config)?;\nengine.add_instrument(instrument)?;","handlingStrategy":"validation","validationCode":"if !added_venues.contains(&instrument.id().venue) {\n    anyhow::bail!(\"venue {} not added; call add_venue first\", instrument.id().venue);\n}\nengine.add_instrument(instrument)?;","typeGuard":null,"tryCatchPattern":"match engine.add_instrument(instrument) {\n    Ok(()) => {},\n    Err(e) if e.to_string().contains(\"without first adding its associated venue\") => {\n        eprintln!(\"add the venue before the instrument: {e}\");\n    }\n    Err(e) => return Err(e),\n}","preventionTips":["Always call add_venue before any add_instrument call in setup code","Assert venue registration in a setup helper that adds both together","Compare venue strings exactly (case and suffix) between venue config and instrument"],"tags":["backtest","venue-registration","ordering"],"backgroundTag":"resource-not-found","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"}