{"record":{"id":"400c518e08dfb010","repo":"nautechsystems/nautilus_trader","slug":"cannot-modify-order-e","errorCode":null,"errorMessage":"Cannot modify order: {e}","messagePattern":"Cannot modify order: (.+?)","errorType":"exception","errorClass":"anyhow::Error","httpStatus":null,"severity":"error","filePath":"crates/trading/src/strategy/mod.rs","lineNumber":363,"sourceCode":"        client_id: Option<ClientId>,\n        params: Option<Params>,\n    ) -> anyhow::Result<()>\n    where\n        Self: StrategyNative,\n    {\n        let (trader_id, strategy_id) = {\n            let core = StrategyNative::strategy_core_mut(self);\n            (registered_trader_id(core)?, registered_strategy_id(core)?)\n        };\n\n        let params = params.filter(|params| !params.is_empty());\n\n        // TODO: Snapshot the order from the cache. See `cancel_order` for the rationale.\n        let order = StrategyNative::strategy_core_mut(self)\n            .cache_rc()\n            .borrow()\n            .try_order_owned(&client_order_id)\n            .map_err(|e| anyhow::anyhow!(\"Cannot modify order: {e}\"))?;\n\n        let mut updating = false;\n\n        if quantity.is_some_and(|q| q != order.quantity() || order.is_pending_update()) {\n            updating = true;\n        }\n\n        if let Some(price) = price {\n            if !LIMIT_ORDER_TYPES.contains(&order.order_type()) {\n                anyhow::bail!(\"{} orders do not have a LIMIT price\", order.order_type());\n            }\n\n            if Some(price) != order.price() {\n                updating = true;\n            }\n        }\n\n        if let Some(trigger_price) = trigger_price {","sourceCodeStart":345,"sourceCodeEnd":381,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/trading/src/strategy/mod.rs#L345-L381","documentation":"`modify_order` snapshots the order from the Cache using `try_order_owned(&client_order_id)`; any cache lookup failure (order not found, or a cache borrow/consistency error) is wrapped as `Cannot modify order: {e}` and propagates up (reached via dispatch_manager_actions in Python strategies).","triggerScenarios":"Calling `self.modify_order(client_order_id, ...)` with a client_order_id that is not in the cache (order already filled/canceled/never submitted), or a malformed ID; the inner `{e}` carries the specific cache error.","commonSituations":"Modifying an order after it was filled or canceled; racing an execution event that removed the order; wrong client_order_id (using venue order id instead); modifying in `on_order_filled`/`on_order_canceled` after the order left the open set; typo in strategy state mapping IDs to orders.","solutions":["Verify the order is still open before modifying: `self.cache.order_exists(client_order_id)` and check its status.","Use the exact StrategyClientOrderId returned by the OrderFactory when the order was submitted.","Move modify calls to `on_order_accepted`/event-driven points where the order is guaranteed open; cancel-and-replace if it may already be terminal.","Read the inner `{e}` to distinguish not-found from borrow errors and handle each accordingly."],"exampleFix":"// before\ndef on_bar(self, bar):\n    self.modify_order(self._order_id, quantity=self._new_qty)  # may already be filled\n// after\ndef on_bar(self, bar):\n    if self.cache.order_exists(self._order_id):\n        order = self.cache.order(self._order_id)\n        if order.is_open:\n            self.modify_order(self._order_id, quantity=self._new_qty)","handlingStrategy":"validation","validationCode":"if not self.cache.order_exists(client_order_id):\n    self.log.warning(f'{client_order_id} not in cache — cannot modify')\n    return\norder = self.cache.order(client_order_id)\nif not order.is_open:\n    self.log.warning(f'{client_order_id} is {order.status} — cannot modify')\n    return","typeGuard":"def is_modifiable(strategy, client_order_id):\n    order = strategy.cache.order(client_order_id) if strategy.cache.order_exists(client_order_id) else None\n    return order is not None and order.is_open","tryCatchPattern":"try:\n    self.modify_order(client_order_id, quantity=new_qty)\nexcept Exception as e:\n    self.log.warning(f'Modify failed for {client_order_id}: {e}')\n    # fall back to cancel-and-replace if still open","preventionTips":["Confirm the order exists and is open before modify calls","Store and reuse the client_order_id returned by the OrderFactory","Avoid modifying from terminal-event callbacks (on_order_filled/canceled)","Prefer cancel-and-replace when the order state may have changed"],"tags":["order-modify","cache","orders","strategy"],"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"}