{"record":{"id":"1771e3ccf3215c22","repo":"nautechsystems/nautilus_trader","slug":"cannot-rebuild-wallet-reservations-for-account-ac","errorCode":null,"errorMessage":"cannot rebuild Wallet reservations for account {account_id} and instrument {instrument_id}","messagePattern":"cannot rebuild Wallet reservations for account (.+?) and instrument (.+?)","errorType":"exception","errorClass":"anyhow::Error","httpStatus":null,"severity":"error","filePath":"crates/portfolio/src/portfolio.rs","lineNumber":1797,"sourceCode":"                    anyhow::anyhow!(\n                        \"cannot rebuild Wallet reservations: account {account_id} not found\"\n                    )\n                })?;\n                let instrument = cache.instrument(&instrument_id).cloned().ok_or_else(|| {\n                    anyhow::anyhow!(\n                        \"cannot rebuild Wallet reservations: instrument {instrument_id} not found\"\n                    )\n                })?;\n                (account, instrument)\n            };\n            let order_refs = orders.iter().collect::<Vec<_>>();\n            let Some((updated_account, _)) = self.inner.borrow().accounts.update_orders(\n                &account,\n                &instrument,\n                &order_refs,\n                self.clock.borrow().timestamp_ns(),\n            ) else {\n                anyhow::bail!(\n                    \"cannot rebuild Wallet reservations for account {account_id} and instrument {instrument_id}\"\n                );\n            };\n            self.cache.borrow_mut().update_account(&updated_account)?;\n        }\n\n        log::info!(\n            color = if total_orders > 0 { LogColor::Blue as u8 } else { LogColor::Normal as u8 };\n            \"Initialized {} Wallet reservation{}\",\n            total_orders,\n            if total_orders == 1 { \"\" } else { \"s\" }\n        );\n        Ok(())\n    }\n\n    /// Initializes account margin based on existing open positions.\n    ///\n    /// # Panics","sourceCodeStart":1779,"sourceCodeEnd":1815,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/portfolio/src/portfolio.rs#L1779-L1815","documentation":"During initialize_wallet_orders, the Portfolio delegates reservation rebuilding to the account's update_orders(), which returns None when it cannot apply the orders. Because None carries no reason, the Portfolio raises this bail identifying the account and instrument. It means the Wallet account state could not be updated with the given orders for that instrument (e.g. insufficient/debit balance data or an invariant failure inside the account update).","triggerScenarios":"initialize_wallet_orders() calls accounts.update_orders(...) for a grouped (account, instrument) set of open orders and the method returns None — typically when the Wallet account lacks the balances needed to compute reservations (e.g. a missing debit balance) for that instrument's currency.","commonSituations":"Rebuilding state after restart where the account snapshot is missing a balance currency that open orders need; balance events lost or partially persisted; orders persisted for an instrument whose quote/base balances were never reported by the venue.","solutions":["Check why update_orders returned None: verify the Wallet account holds the base and quote balances required for each open order's instrument","Re-sync the account with a fresh account state (account_state event) before rebuilding reservations","Remove or cancel stale open orders that reference balances no longer present","Enable balance consistency checks in the venue client so balances are persisted with the account"],"exampleFix":"// before: rebuilding without account balance snapshot\nlet portfolio = Portfolio::new(cache, clock, ...);\nportfolio.initialize_wallet_orders()?;\n// after: ensure account state (with balances) is in cache first\nlet account_state = venue_client.account_state(ts_ns)?;\ncache.update_account_from_state(account_state);\nlet portfolio = Portfolio::new(cache, clock, ...);\nportfolio.initialize_wallet_orders()?;","handlingStrategy":"validation","validationCode":"// Ensure wallet balances exist for every open order's instrument currencies\nfor order in cache.orders_open() {\n    if let Some(inst) = cache.instrument(&order.instrument_id()) {\n        let acct = cache.account_for_venue(inst.id.venue).unwrap();\n        assert!(acct.balance(&inst.quote_currency).is_some(), \"missing quote balance\");\n    }\n}","typeGuard":null,"tryCatchPattern":"if let Err(e) = portfolio.initialize_wallet_orders() {\n    if e.to_string().starts_with(\"cannot rebuild Wallet reservations for account\") {\n        // re-sync account state from venue, then retry once\n    }\n    return Err(e);\n}","preventionTips":["Persist account state snapshots together with the order store","Keep wallet balances synchronized via account state events before shutdown","Purge stale open orders whose balances no longer exist"],"tags":["rust","portfolio","wallet","balance","state-rebuild"],"backgroundTag":"invalid-state-transition","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"}