{"record":{"id":"02ad6690b8ce6258","repo":"nautechsystems/nautilus_trader","slug":"strategy-not-registered-orderfactory-not-initiali","errorCode":null,"errorMessage":"Strategy not registered: OrderFactory not initialized","messagePattern":"Strategy not registered: OrderFactory not initialized","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/trading/src/strategy/core.rs","lineNumber":116,"sourceCode":"/// as `order()` and `portfolio()`, because native borrows, `Rc<RefCell<_>>`, and\n/// core references do not cross those boundaries.\npub trait StrategyNative {\n    /// Returns the strategy core.\n    fn strategy_core(&self) -> &StrategyCore;\n\n    /// Returns the mutable strategy core.\n    fn strategy_core_mut(&mut self) -> &mut StrategyCore;\n\n    /// Returns a mutable borrow of the order factory.\n    ///\n    /// # Panics\n    ///\n    /// Panics if the strategy has not been registered.\n    fn order_factory(&mut self) -> RefMut<'_, OrderFactory> {\n        self.strategy_core_mut()\n            .order_factory\n            .as_ref()\n            .expect(\"Strategy not registered: OrderFactory not initialized\")\n            .borrow_mut()\n    }\n\n    /// Returns a clone of the reference-counted order factory.\n    ///\n    /// # Panics\n    ///\n    /// Panics if the strategy has not been registered.\n    fn order_factory_rc(&self) -> Rc<RefCell<OrderFactory>> {\n        self.strategy_core()\n            .order_factory\n            .as_ref()\n            .expect(\"Strategy not registered: OrderFactory not initialized\")\n            .clone()\n    }\n\n    /// Returns a clone of the reference-counted portfolio.\n    ///","sourceCodeStart":98,"sourceCodeEnd":134,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/trading/src/strategy/core.rs#L98-L134","documentation":"`order_factory()` on the Strategy trait returns a `RefMut` to the strategy's `OrderFactory`. The factory is only populated when the strategy has been registered with a trading node/kernel; before registration the `Option` is `None`, and the `.expect` panics with 'Strategy not registered: OrderFactory not initialized'. This is a lifecycle misuse error, not a runtime failure.","triggerScenarios":"Calling `self.order_factory()` from within strategy lifecycle methods that run before registration completes — typically the constructor, `on_start` before the kernel wires dependencies, or any code path that touches the strategy before `Strategy::register`/trader registration.","commonSituations":"Creating an OrderFactory-dependent state in the strategy's constructor; submitting orders in `on_start` before registration; unit-testing a strategy in isolation without a trading kernel; spawning the strategy without attaching it to a node.","solutions":["Defer any order-factory use until after registration — do work in `on_start`/event handlers, never in the constructor","Ensure the strategy is actually registered with a trader/node (`strategy.register(...)` or adding it to the kernel) before running it","In tests, use the test harness/builder that registers the strategy and injects an OrderFactory"],"exampleFix":"// before: panics — factory not yet injected\nimpl MyStrategy {\n    fn new(...) -> Self { Self { cache: self.order_factory().make_cache() } }\n}\n// after: initialize lazily on start, after registration\nfn on_start(&mut self) {\n    self.cache = self.order_factory().make_cache();\n}","handlingStrategy":"validation","validationCode":"// Guard before touching the factory (if you have access to the core's Option)\nif core.order_factory.is_none() {\n    return Err(MyError::StrategyNotRegistered(strategy_id));\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Never call order-factory APIs in the strategy constructor; use on_start or later handlers","Always register the strategy with a trader/node before running or testing it","Use the provided test harness that performs registration + dependency injection"],"tags":["rust","lifecycle","strategy","panic","initialization-order"],"backgroundTag":"resource-not-initialized","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"}