{"record":{"id":"3cff387b75566cf3","repo":"nautechsystems/nautilus_trader","slug":"executionalgorithm-not-registered-portfolio-not-i","errorCode":null,"errorMessage":"ExecutionAlgorithm not registered: Portfolio not initialized","messagePattern":"ExecutionAlgorithm not registered: Portfolio not initialized","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/trading/src/algorithm/core.rs","lineNumber":109,"sourceCode":"/// plug-in authoring surface. Native borrows, `Rc<RefCell<_>>`, and core\n/// references do not cross those boundaries.\npub trait ExecutionAlgorithmNative: DataActorNative {\n    /// Returns the execution algorithm core.\n    fn exec_algorithm_core(&self) -> &ExecutionAlgorithmCore;\n\n    /// Returns the mutable execution algorithm core.\n    fn exec_algorithm_core_mut(&mut self) -> &mut ExecutionAlgorithmCore;\n\n    /// Returns a clone of the reference-counted portfolio.\n    ///\n    /// # Panics\n    ///\n    /// Panics if the execution algorithm has not been registered.\n    fn portfolio_rc(&self) -> Rc<RefCell<Portfolio>> {\n        self.exec_algorithm_core()\n            .portfolio\n            .as_ref()\n            .expect(\"ExecutionAlgorithm not registered: Portfolio not initialized\")\n            .clone()\n    }\n}\n\nimpl Debug for ExecutionAlgorithmCore {\n    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {\n        f.debug_struct(stringify!(ExecutionAlgorithmCore))\n            .field(\"actor\", &self.actor)\n            .field(\"config\", &self.config)\n            .field(\"exec_algorithm_id\", &self.exec_algorithm_id)\n            .field(\"exec_spawn_ids\", &self.exec_spawn_ids.len())\n            .field(\"subscribed_strategies\", &self.subscribed_strategies.len())\n            .field(\n                \"pending_spawn_reductions\",\n                &self.pending_spawn_reductions.len(),\n            )\n            .field(\"submit_params\", &self.submit_params.len())\n            .field(","sourceCodeStart":91,"sourceCodeEnd":127,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/trading/src/algorithm/core.rs#L91-L127","documentation":"ExecutionAlgorithm's portfolio_rc accessor unwraps the Option<Rc<RefCell<Portfolio>>> held by ExecutionAlgorithmCore. It is Some only after the algorithm has been registered with a system/kernel that owns an initialized Portfolio; otherwise any portfolio access panics.","triggerScenarios":"Calling any ExecutionAlgorithm method that reaches portfolio_rc (e.g. order/position queries, portfolio state access) before the algorithm was registered via a trading node/kernel, or when the node started without a Portfolio configured.","commonSituations":"Using an ExecutionAlgorithm standalone (outside a TradingNode) or in unit tests without registering it; constructing a node with portfolio disabled/misconfigured; calling algorithm methods in on_start before the kernel wires the portfolio.","solutions":["Run the algorithm inside a fully built TradingNode so registration injects the Portfolio.","Verify the node's portfolio configuration is present and enabled.","Defer portfolio access until after the algorithm is registered (e.g. post on_start wiring).","In tests, register the algorithm with a core that has a Portfolio set up first."],"exampleFix":"// before\nfn on_start(&mut self) {\n    let pos = self.portfolio_rc().borrow().is_flat(&instrument_id); // panics if unregistered\n}\n// after\nfn on_start(&mut self) {\n    if !self.is_registered() {\n        log::warn(\"portfolio unavailable; skipping check\");\n        return;\n    }\n    let pos = self.portfolio_rc().borrow().is_flat(&instrument_id);\n}","handlingStrategy":"type-guard","validationCode":"// guard before portfolio access\nif !algorithm.is_registered() {\n    return; // or log and defer\n}","typeGuard":"fn has_portfolio(core: &ExecutionAlgorithmCore) -> bool { core.portfolio.is_some() }","tryCatchPattern":"let portfolio = std::panic::catch_unwind(|| self.portfolio_rc())\n    .ok()\n    .map(|p| p.borrow());","preventionTips":["Only run ExecutionAlgorithms inside a fully constructed TradingNode","Confirm portfolio is enabled/configured in the node config","Defer portfolio-dependent logic until after kernel registration (post on_start)","In unit tests, register the algorithm with an initialized Portfolio first"],"tags":["rust","trading","portfolio","initialization","lifecycle"],"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-14T00:17:10.932Z"}