{"record":{"id":"89ddba98cdbc58f4","repo":"nautechsystems/nautilus_trader","slug":"strategy-strategy-id-is-already-tracked-by-tra","errorCode":null,"errorMessage":"Strategy '{strategy_id}' is already tracked by trader","messagePattern":"Strategy '(.+?)' is already tracked by trader","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/system/src/trader.rs","lineNumber":453,"sourceCode":"    /// Adds an externally-registered strategy to the trader for lifecycle management\n    /// and installs its order/position event subscriptions, stop hook, and control endpoint.\n    ///\n    /// The strategy must already be registered in the global component and actor\n    /// registries. The generic parameter `T` must match the concrete type stored\n    /// in those registries so that the typed event handlers can retrieve it.\n    ///\n    /// # Errors\n    ///\n    /// Returns an error if the strategy ID is already tracked by this trader.\n    pub fn add_strategy_id_with_subscriptions<T>(\n        &mut self,\n        strategy_id: StrategyId,\n    ) -> anyhow::Result<()>\n    where\n        T: Strategy + StrategyNative + DataActorNative + Component + Debug + 'static,\n    {\n        if self.strategy_ids.contains(&strategy_id) {\n            anyhow::bail!(\"Strategy '{strategy_id}' is already tracked by trader\");\n        }\n\n        let existing_order_id_tags: Vec<&str> =\n            self.strategy_ids.iter().map(StrategyId::get_tag).collect();\n        ensure_unique_order_id_tag(&existing_order_id_tags, strategy_id.get_tag())?;\n\n        let actor_id = strategy_id.inner();\n\n        // Subscribe to order events for this strategy\n        let order_topic = get_event_order_topic(strategy_id);\n        let order_actor_id = actor_id;\n        let order_handler = TypedHandler::from(move |event: &OrderEventAny| {\n            if let Some(mut strategy) = try_get_actor_unchecked::<T>(&order_actor_id) {\n                strategy.handle_order_event(event.clone());\n            } else {\n                log::error!(\"Strategy {order_actor_id} not found for order event handling\");\n            }\n        });","sourceCodeStart":435,"sourceCodeEnd":471,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/system/src/trader.rs#L435-L471","documentation":"Trader.add_strategy_id_with_subscriptions rejects a StrategyId that is already present in the trader's tracked strategy_ids set. The trader maintains one registration per strategy so subscriptions and order routing stay unambiguous. Registering the same strategy twice would double subscriptions and corrupt id-based lookups, so it is refused up front. A related uniqueness check follows for order id tags.","triggerScenarios":"Calling add_strategy_id_with_subscriptions with a StrategyId whose inner value already exists in trader.strategy_ids — e.g. adding the same strategy instance twice, or re-running an add path after a partially failed setup that already recorded the id.","commonSituations":"Application bootstrap code that calls trader.add_strategy on restart without checking whether the strategy was already added; idempotency wrappers that retry after an ambiguous failure; tests re-adding a strategy with the same StrategyId.","solutions":["Check trader.strategy_ids (or keep your own registry) before calling add_strategy_id_with_subscriptions and skip if the id is present.","Assign each strategy a distinct StrategyId (unique name + order id tag) when constructing it.","If this follows a failed add, reset or rebuild the trader instead of re-adding onto a partially initialized state."],"exampleFix":"// before\ntrader.add_strategy_id_with_subscriptions(strategy_id, strategy)?;\n\n// after\nif !trader.strategy_ids.contains(&strategy_id) {\n    trader.add_strategy_id_with_subscriptions(strategy_id, strategy)?;\n} else {\n    // strategy already tracked; skip registration\n}","handlingStrategy":"validation","validationCode":"if trader.strategy_ids.contains(&strategy_id) {\n    // skip registration or use a fresh StrategyId\n}","typeGuard":null,"tryCatchPattern":"match trader.add_strategy_id_with_subscriptions(strategy_id, strategy) {\n    Err(e) if e.to_string().contains(\"already tracked\") => { /* idempotent skip */ }\n    other => other?,\n}","preventionTips":["Keep a single application-level registry of strategy ids and check it before any add call","Make add calls idempotent by checking membership first","Never re-add strategies after partial setup failures; rebuild the trader"],"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"}