{"record":{"id":"691ec90a76fef777","repo":"nautechsystems/nautilus_trader","slug":"strategy-strategy-id-is-already-registered-691ec9","errorCode":null,"errorMessage":"Strategy {strategy_id} is already registered","messagePattern":"Strategy (.+?) is already registered","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/system/src/trader.rs","lineNumber":588,"sourceCode":"            ensure_unique_order_id_tag(&existing_order_id_tags, strategy_id.get_tag())?;\n            StrategyNative::strategy_core_mut(strategy).change_id(strategy_id)?;\n            strategy_id\n        } else {\n            let order_id_tag = runtime_order_id_tag.map_or_else(\n                || format!(\"{:03}\", existing_order_id_tags.len()),\n                str::to_string,\n            );\n            ensure_unique_order_id_tag(&existing_order_id_tags, &order_id_tag)?;\n\n            let base_id = strategy_registration_id::<T>(strategy);\n            let strategy_id =\n                StrategyId::new_checked(format!(\"{}-{order_id_tag}\", base_strategy_id(&base_id)))?;\n            StrategyNative::strategy_core_mut(strategy).change_id(strategy_id)?;\n            strategy_id\n        };\n\n        if self.strategy_ids.contains(&strategy_id) {\n            anyhow::bail!(\"Strategy {strategy_id} is already registered\");\n        }\n\n        Ok(strategy_id)\n    }\n\n    /// Adds a strategy to the trader.\n    ///\n    /// Strategies are registered in both the component registry (for lifecycle management)\n    /// and the actor registry (for data callbacks via msgbus). The strategy's `StrategyCore`\n    /// is also registered with the portfolio for order management.\n    ///\n    /// # Errors\n    ///\n    /// Returns an error if:\n    /// - The trader is not in a valid state for adding components.\n    /// - A strategy with the same ID is already registered.\n    pub fn add_strategy<T>(&mut self, mut strategy: T) -> anyhow::Result<()>\n    where","sourceCodeStart":570,"sourceCodeEnd":606,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/system/src/trader.rs#L570-L606","documentation":"prepare_strategy_for_registration derives the final StrategyId (applying a per-strategy order id tag if given) and refuses to proceed if that id is already registered in trader.strategy_ids. This guard makes registration idempotent-safe: a strategy can only be registered once with the trader. It is the internal pre-check used by add_strategy.","triggerScenarios":"Calling trader.add_strategy (directly or via prepare_strategy_for_registration) with a strategy whose resolved StrategyId already exists — e.g. adding the same strategy twice, or two strategies whose base name and order_id_tag resolve to the same id.","commonSituations":"Adding the same strategy object twice by accident in a loop over configs; duplicate order_id_tag values combined with identical strategy names producing colliding ids; re-running an init function without guarding against prior registration.","solutions":["Check that the strategy's StrategyId is not already in trader.strategy_ids before calling add_strategy.","Give each strategy a unique name and unique order_id_tag so resolved ids cannot collide.","If re-registering after a restart, create a fresh trader or remove/dispose the old strategy first."],"exampleFix":"// before\ntrader.add_strategy(strategy)?;\ntrader.add_strategy(strategy)?; // panics/bails second time\n\n// after\nlet strategy_id = StrategyNative::strategy_core(&strategy).id().clone();\nif !trader.strategy_ids.contains(&strategy_id) {\n    trader.add_strategy(strategy)?;\n}","handlingStrategy":"validation","validationCode":"let strategy_id = StrategyNative::strategy_core(&strategy).id().clone();\nif trader.strategy_ids.contains(&strategy_id) {\n    // skip or error early\n}","typeGuard":null,"tryCatchPattern":"match trader.add_strategy(strategy) {\n    Err(e) if e.to_string().contains(\"already registered\") => { /* idempotent skip */ }\n    other => other?,\n}","preventionTips":["Ensure each strategy has a unique name and order_id_tag","Deduplicate strategy configs before constructing strategies","Wrap registration in an idempotent helper that checks strategy_ids first"],"tags":["rust","duplicate-registration","strategy"],"backgroundTag":"duplicate-registration","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"}