{"record":{"id":"f18fe08f5bf7e246","repo":"nautechsystems/nautilus_trader","slug":"latency-model-should-be-initialized","errorCode":null,"errorMessage":"Latency model should be initialized","messagePattern":"Latency model should be initialized","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/backtest/src/exchange.rs","lineNumber":869,"sourceCode":"                    command.ts_init() + latency_model.get_update_latency()\n                }\n                TradingCommand::CancelOrder(_)\n                | TradingCommand::CancelOrders(_)\n                | TradingCommand::CancelAllOrders(_) => {\n                    command.ts_init() + latency_model.get_delete_latency()\n                }\n                _ => panic!(\"Cannot handle command: {command:?}\"),\n            };\n\n            let counter = self\n                .inflight_counter\n                .entry(ts)\n                .and_modify(|e| *e += 1)\n                .or_insert(1);\n\n            (ts, *counter)\n        } else {\n            panic!(\"Latency model should be initialized\");\n        }\n    }\n\n    /// Processes a single order book delta.\n    ///\n    /// # Errors\n    ///\n    /// Returns an error if module pre-processing or matching engine processing fails.\n    pub fn process_order_book_delta(&mut self, delta: OrderBookDelta) -> anyhow::Result<()> {\n        self.pre_process_modules(&Data::BookDelta(delta))?;\n\n        if !self.matching_engines.contains_key(&delta.instrument_id) {\n            let instrument = {\n                let cache = self.cache.as_ref().borrow();\n                cache.instrument(&delta.instrument_id).cloned()\n            };\n\n            if let Some(instrument) = instrument {","sourceCodeStart":851,"sourceCodeEnd":887,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/backtest/src/exchange.rs#L851-L887","documentation":"The backtest exchange stores its latency model in an Option-like holder and unwraps it when computing inflight command latencies. If no latency model was configured on the exchange before processing trading commands, the unwrap site panics with 'Latency model should be initialized'. It is an internal precondition violation: commands are being processed before setup completed.","triggerScenarios":"Submitting/routing any trading command through the backtest exchange when its latency model was never set (e.g. exchange built without latency config, or command processed before initialization).","commonSituations":"Backtest config omits latency model parameters; a custom exchange builder skips the latency-model step; ordering bug where commands are processed during setup before initialization finishes.","solutions":["Configure a latency model on the backtest exchange before running the backtest (latency_model field/config)","Ensure the exchange is fully initialized (including latency model) before any commands are processed","Check the exchange construction/builder path for a skipped latency-model assignment","Consider making the latency model a required constructor parameter to move the failure to build time"],"exampleFix":"// before\nlet exchange = BacktestExchange::new(...); // latency model never set\nexchange.process(command); // panics\n// after\nlet exchange = BacktestExchange::new(...).with_latency_model(LatencyModel::new(...));\nexchange.process(command);","handlingStrategy":"type-guard","validationCode":"if exchange.latency_model().is_none() {\n    return Err(\"backtest exchange requires a latency model before processing commands\".into());\n}","typeGuard":"fn is_ready(exchange: &BacktestExchange) -> bool {\n    exchange.latency_model().is_some()\n}","tryCatchPattern":null,"preventionTips":["Always configure a latency model in backtest configs; treat it as mandatory setup","Make the latency model a required constructor argument so omission fails at build time","Initialize the exchange fully before feeding it any trading commands","Add an integration test that runs a minimal backtest to catch setup-order regressions"],"tags":["rust","panic","backtest","latency-model","initialization"],"backgroundTag":"internal-invariant-violation","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"}