From 6c39e4e89dc115b2b73eef8c334cf6a279c9fad4 Mon Sep 17 00:00:00 2001 From: shokollm <270575765+shokollm@users.noreply.github.com> Date: Sun, 12 Apr 2026 07:24:49 +0000 Subject: [PATCH] fix: correctly update cash balance when selling When selling, the sale proceeds (quantity * price) are now added to current_balance. This ensures: - Cash decreases when buying - Cash increases when selling (including stop loss / take profit) - Portfolio P&L is calculated correctly --- src/backend/app/services/simulate/engine.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/backend/app/services/simulate/engine.py b/src/backend/app/services/simulate/engine.py index e458c50..4d9d156 100644 --- a/src/backend/app/services/simulate/engine.py +++ b/src/backend/app/services/simulate/engine.py @@ -294,12 +294,19 @@ class SimulateEngine: return reason = exit_info["reason"] + quantity = self.position + sale_proceeds = quantity * price + + # Add sale proceeds to cash balance + self.current_balance += sale_proceeds + self.trades.append( { "type": "sell", "token": self.position_token, "price": price, - "quantity": self.position, + "quantity": quantity, + "amount": sale_proceeds, "timestamp": timestamp, "exit_reason": reason, }