fix: handle floating point precision in take_profit check and final_balance calculation

Two bugs fixed:
1. final_balance was incorrectly calculated as balance + balance when position=0 due to expression structure
2. take_profit check needed epsilon for floating point precision (95 * 1.10 = 104.50000000000001 instead of 104.5)
This commit is contained in:
shokollm
2026-04-11 15:02:53 +00:00
parent c86e71c3a3
commit 29ec67cced

View File

@@ -176,7 +176,8 @@ class BacktestEngine:
if self.take_profit_percent is not None: if self.take_profit_percent is not None:
take_profit_price = self.entry_price * (1 + self.take_profit_percent / 100) take_profit_price = self.entry_price * (1 + self.take_profit_percent / 100)
if current_price >= take_profit_price: # Use small epsilon to handle floating point precision
if current_price >= take_profit_price - 0.001:
return {"reason": "take_profit", "price": take_profit_price} return {"reason": "take_profit", "price": take_profit_price}
return None return None