{"record":{"id":"44a1c3a2f75e4d68","repo":"nautechsystems/nautilus_trader","slug":"strategy-not-registered-portfolio-not-initialized","errorCode":null,"errorMessage":"Strategy not registered: Portfolio not initialized","messagePattern":"Strategy not registered: Portfolio not initialized","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/trading/src/strategy/core.rs","lineNumber":142,"sourceCode":"    /// 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    ///\n    /// # Panics\n    ///\n    /// Panics if the strategy has not been registered.\n    fn portfolio_rc(&self) -> Rc<RefCell<Portfolio>> {\n        self.strategy_core()\n            .portfolio\n            .as_ref()\n            .expect(\"Strategy not registered: Portfolio not initialized\")\n            .clone()\n    }\n}\n\nimpl StrategyCore {\n    /// Creates a new [`StrategyCore`] instance with correctness checking.\n    ///\n    /// # Errors\n    ///\n    /// Returns an error if the configured order ID tag contains the '-' strategy ID separator,\n    /// or if composing it into the strategy ID does not produce a valid [`StrategyId`].\n    pub fn new_checked(config: StrategyConfig) -> CorrectnessResult<Self> {\n        if let Some(order_id_tag) = config.order_id_tag.as_deref() {\n            check_order_id_tag(order_id_tag)?;\n        }\n\n        let configured_strategy_id = config.strategy_id;\n        let configured_order_id_tag = normalize_order_id_tag(config.order_id_tag.as_deref());","sourceCodeStart":124,"sourceCodeEnd":160,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/trading/src/strategy/core.rs#L124-L160","documentation":"`portfolio_rc()` returns a clone of the strategy's shared `Rc<RefCell<Portfolio>>`. The portfolio handle is injected during strategy registration; if the strategy is unregistered the `Option` is `None` and the `.expect` panics with 'Strategy not registered: Portfolio not initialized'.","triggerScenarios":"Calling `self.portfolio_rc()` before registration — accessing portfolio in the constructor, in `on_start` before the kernel injects dependencies, or in a standalone test instance.","commonSituations":"Caching portfolio references at construction; querying account/portfolio state too early in the lifecycle; running a strategy outside a full trading node (e.g. sandbox without portfolio wiring).","solutions":["Move portfolio access into post-registration lifecycle handlers (`on_start` or later)","Ensure the strategy is registered with a trader/kernel that injects the Portfolio","For tests, construct via the provided harness that registers the strategy with a Portfolio"],"exampleFix":"// before: too early\nfn new(...) -> Self { Self { net: self.portfolio_rc().borrow().net_position(instrument) } }\n// after: query after registration\nfn on_start(&mut self) {\n    self.net = self.portfolio_rc().borrow().net_position(self.instrument_id);\n}","handlingStrategy":"validation","validationCode":"if core.portfolio.is_none() {\n    return Err(MyError::StrategyNotRegistered(strategy_id));\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Read portfolio state only after registration (on_start onward)","Ensure the trading node wires a Portfolio for every registered strategy","In sandbox/test rigs, include the portfolio component or skip portfolio-dependent logic"],"tags":["rust","lifecycle","strategy","panic","portfolio"],"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"}