{"record":{"id":"aa5bdecc64f30a5f","repo":"nautechsystems/nautilus_trader","slug":"cannot-update-position-position-id-not-found-in","errorCode":null,"errorMessage":"Cannot update position {position_id}: not found in cache","messagePattern":"Cannot update position (.+?): not found in cache","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/common/src/cache/mod.rs","lineNumber":5466,"sourceCode":"        Ok(())\n    }\n\n    /// Updates a cached position by applying an order fill in place.\n    ///\n    /// Returns a transient copy of the updated state without stored history. The canonical cached\n    /// position retains its complete history.\n    ///\n    /// # Errors\n    ///\n    /// Returns an error if the position is not already held in the cache, or if updating the\n    /// position in the database fails.\n    pub fn update_position_from_fill(\n        &mut self,\n        position_id: PositionId,\n        fill: &OrderFilled,\n    ) -> anyhow::Result<Position> {\n        let Some(position_cell) = self.positions.get(&position_id).cloned() else {\n            anyhow::bail!(\"Cannot update position {position_id}: not found in cache\");\n        };\n\n        let position = {\n            let mut position = position_cell.borrow_mut();\n            position.apply(fill);\n            position.clone_without_events()\n        };\n\n        self.refresh_position_indexes(&position);\n\n        if let Some(database) = &mut self.database {\n            database.update_position(&position_cell.borrow())?;\n        }\n\n        Ok(position)\n    }\n\n    fn refresh_position_indexes(&mut self, position: &Position) {","sourceCodeStart":5448,"sourceCodeEnd":5484,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/common/src/cache/mod.rs#L5448-L5484","documentation":"`update_position_from_fill` looks up the position by PositionId and applies an OrderFilled event to it in place. If the position is not in the cache, the fill cannot be applied, so the method bails — it never fabricates a position from a fill alone.","triggerScenarios":"Applying a fill whose `position_id` references a position not cached (e.g. the position was never opened through the cache, was purged, or the fill's position_id is stale/mismatched after a snapshot restore).","commonSituations":"Backtesting/live runs where position events arrive after cache purging; desynchronized OMS state where fills reference positions from a previous session; incomplete snapshot restore missing the position record.","solutions":["Ensure the opening event that creates the position is processed before its fills.","Check `cache.position(&position_id)` and, if absent, rebuild the position from the fill per your OMS policy instead of calling this API.","Disable/adjust aggressive position purging, or restore positions from snapshots before applying fills."],"exampleFix":"// before\nlet position = cache.update_position_from_fill(position_id, &fill)?;\n// after\nif cache.position(&position_id).is_none() {\n    // rebuild or open the position from the fill per OMS policy\n}\nlet position = cache.update_position_from_fill(position_id, &fill)?;","handlingStrategy":"validation","validationCode":"if cache.position(&position_id).is_none() {\n    // position missing: rebuild/open it per OMS policy before applying the fill\n}","typeGuard":null,"tryCatchPattern":"match cache.update_position_from_fill(position_id, &fill) {\n    Ok(p) => {},\n    Err(e) if e.to_string().contains(\"not found in cache\") => { /* recover: rebuild position or open new */ },\n    Err(e) => return Err(e),\n}","preventionTips":["Guarantee the position-open event is processed before fills referencing it.","Align purge schedules with expected event arrival.","Validate position_id in fills against restored snapshot state."],"tags":["rust","cache","position","order-fill","not-found"],"backgroundTag":"entity-not-found","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"}