From 10a74ea4f76c6137b8454e9a85bdf82f936205f8 Mon Sep 17 00:00:00 2001 From: Gahow Wang Date: Fri, 25 Sep 2026 02:05:15 +0800 Subject: [PATCH] fix(trader): record trade and snapshot prices at full precision Rounding to cents made trade logs irreproducible for low-priced lines (HK ETFs in USD, A-shares): replaying a log drifted up to $200 from the recorded cash. Co-Authored-By: Claude Opus 5.5 (1M context) --- trader.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/trader.py b/trader.py index 8f0cc67..76d98a3 100644 --- a/trader.py +++ b/trader.py @@ -523,7 +523,7 @@ def compute_trades(holdings: dict, cash: float, target_weights: dict, "shares_delta": int(round(delta)) if integer_shares else round(delta, 4), "direction": "BUY" if delta > 0 else "SELL", "est_value": round(trade_value, 2), - "price": round(price, 2), + "price": round(price, 6), "target_shares": int(round(target_shares)) if integer_shares else round(target_shares, 4), "current_shares": int(current_shares) if integer_shares else round(current_shares, 4), }) @@ -620,7 +620,7 @@ def execute_trades(state: dict, trades: list[dict], prices: dict, "action": "BUY" if delta > 0 else "SELL", "ticker": ticker, "shares": abs(delta) if integer_shares else round(abs(delta), 4), - "price": round(price, 2), + "price": round(price, 6), "value": round(cost, 2), "commission": round(commission, 2), }) @@ -650,7 +650,7 @@ def record_daily_snapshot(state: dict, date_str: str, prices: dict, p = prices.get(ticker, 0.0) positions[ticker] = { "shares": shares, - "price": round(p, 2), + "price": round(p, 6), "value": round(shares * p, 2), } @@ -662,7 +662,7 @@ def record_daily_snapshot(state: dict, date_str: str, prices: dict, "action": t["direction"], "ticker": t["ticker"], "shares": abs(t["shares_delta"]), - "price": round(p, 2), + "price": round(p, 6), "value": round(abs(t["shares_delta"]) * p, 2), })