From 653fa08fa47921dbebc4ca5bebc26c57c327a1bf Mon Sep 17 00:00:00 2001 From: zephyrdark Date: Wed, 18 Feb 2026 22:01:01 +0900 Subject: [PATCH] fix: use actual invested amounts for avg_price in seed data The seed script was incorrectly using the latest snapshot's market price as avg_price, resulting in inflated average costs. Now computes avg_price from actual total invested amounts per ticker. Co-Authored-By: Claude Opus 4.6 --- backend/scripts/seed_data.py | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/backend/scripts/seed_data.py b/backend/scripts/seed_data.py index 24a078b..800efc3 100644 --- a/backend/scripts/seed_data.py +++ b/backend/scripts/seed_data.py @@ -42,6 +42,15 @@ TARGETS = { "411060": Decimal("15"), } +# Actual total invested amounts per ticker (from brokerage records) +TOTAL_INVESTED = { + "069500": Decimal("541040"), + "148070": Decimal("15432133"), + "284430": Decimal("18375975"), + "360750": Decimal("7683515"), + "411060": Decimal("6829620"), +} + # Historical snapshots from data.txt SNAPSHOTS = [ { @@ -242,13 +251,18 @@ def seed(db: Session): print(f"Created {tx_count} transactions from snapshot diffs") # Set current holdings from latest snapshot + # avg_price = total invested amount / quantity (from actual brokerage records) latest = SNAPSHOTS[-1] for h in latest["holdings"]: + ticker = h["ticker"] + qty = h["qty"] + invested = TOTAL_INVESTED[ticker] + avg_price = (invested / qty).quantize(Decimal("0.01")) db.add(Holding( portfolio_id=portfolio.id, - ticker=h["ticker"], - quantity=h["qty"], - avg_price=h["price"], # Using current price as avg (best available) + ticker=ticker, + quantity=qty, + avg_price=avg_price, )) print(f"Set {len(latest['holdings'])} current holdings from {latest['date']}")