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
This commit is contained in:
@@ -294,12 +294,19 @@ class SimulateEngine:
|
|||||||
return
|
return
|
||||||
|
|
||||||
reason = exit_info["reason"]
|
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(
|
self.trades.append(
|
||||||
{
|
{
|
||||||
"type": "sell",
|
"type": "sell",
|
||||||
"token": self.position_token,
|
"token": self.position_token,
|
||||||
"price": price,
|
"price": price,
|
||||||
"quantity": self.position,
|
"quantity": quantity,
|
||||||
|
"amount": sale_proceeds,
|
||||||
"timestamp": timestamp,
|
"timestamp": timestamp,
|
||||||
"exit_reason": reason,
|
"exit_reason": reason,
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user