{"record":{"id":"05284e535daef698","repo":"nautechsystems/nautilus_trader","slug":"instrument-first-instrument-id-for-the-given-dat","errorCode":null,"errorMessage":"Instrument {first_instrument_id} for the given data not found in the cache. Add the instrument through `add_instrument()` prior to adding related data.","messagePattern":"Instrument (.+?) for the given data not found in the cache\\. Add the instrument through `add_instrument\\(\\)` prior to adding related data\\.","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/backtest/src/engine.rs","lineNumber":477,"sourceCode":"    ) -> anyhow::Result<String> {\n        #[cfg(not(feature = \"defi\"))]\n        let _ = client_id;\n\n        let Some(first) = items.clone().next() else {\n            anyhow::bail!(\"data was empty\");\n        };\n\n        if validate {\n            // Validate against the first element only and assume the batch is\n            // homogeneous (documented contract on add_data).\n            #[cfg(feature = \"defi\")]\n            let first_is_defi = matches!(first, DataRef::Defi(_));\n            #[cfg(not(feature = \"defi\"))]\n            let first_is_defi = false;\n\n            if !first_is_defi && !matches!(first, DataRef::Custom(_)) {\n                let first_instrument_id = first.instrument_id();\n                anyhow::ensure!(\n                    self.kernel\n                        .cache\n                        .borrow()\n                        .instrument(&first_instrument_id)\n                        .is_some(),\n                    \"Instrument {first_instrument_id} for the given data not found in the cache. \\\n                     Add the instrument through `add_instrument()` prior to adding related data.\"\n                );\n\n                if let DataRef::Bar(bar) = first {\n                    anyhow::ensure!(\n                        bar.bar_type.aggregation_source() == AggregationSource::External,\n                        \"bar_type.aggregation_source must be External, was {:?}\",\n                        bar.bar_type.aggregation_source(),\n                    );\n                }\n            }\n        }","sourceCodeStart":459,"sourceCodeEnd":495,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/backtest/src/engine.rs#L459-L495","documentation":"Before running, the backtest engine registers all added data and requires that the instrument each data item refers to already exists in the cache. Data must be preceded by add_instrument() for its instrument id; otherwise register_added_data fails so the data cannot be priced or simulated without its instrument definition.","triggerScenarios":"Calling engine.add_data(...) or engine.add_data_batch(...) with quotes/trades/bars/deltas whose first item's instrument_id() is not in the cache — i.e. no matching add_instrument() call was made (or was made after adding data).","commonSituations":"Forgetting add_instrument when loading data-only backtests; instrument id mismatch between the data (e.g. 'EUR/USD.SIM') and the added instrument ('EUR/USD.IDEALPRO'); instruments added on a different engine instance.","solutions":["Call engine.add_instrument(...) for the instrument referenced by the data before add_data/add_data_batch.","Verify the instrument_id in your data matches exactly (symbol and venue casing/format) the added instrument's id.","If batch-loading, add all instruments first, then add data, then sort and run.","For custom data (DataRef::Custom) or DeFi data, note these are exempt from the check — confirm you didn't accidentally wrap domain data as non-custom."],"exampleFix":"// before\nengine.add_data(quote_ticks);\nengine.add_instrument(instrument);\n// after\nengine.add_instrument(instrument);\nengine.add_data(quote_ticks);","handlingStrategy":"validation","validationCode":"// before adding data, ensure every referenced instrument is in the cache\nfor id in data_instrument_ids {\n    if engine.cache.instrument(&id).is_none() {\n        panic!(\"instrument {id} missing from cache; call add_instrument first\");\n    }\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always add all instruments before any data.","Derive instrument ids from the data files and assert each resolves to a cached instrument.","Watch for id format mismatches (symbol casing, venue suffix) between datasets and instruments."],"tags":["rust","backtest","cache","instrument","ordering"],"backgroundTag":"entity-not-found","analyzedSha":"18893faf8b356be3320add8de2f861b0b647cf06","analyzedAt":"2026-09-08T20:49:34.690Z","contentChangedAt":"2026-09-08T20:49:34.690Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}