fix: use actual invested amounts for avg_price in seed data
All checks were successful
Deploy to Production / deploy (push) Successful in 1m48s

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 <noreply@anthropic.com>
This commit is contained in:
zephyrdark 2026-02-18 22:01:01 +09:00
parent 238c4d1caf
commit 653fa08fa4

View File

@ -42,6 +42,15 @@ TARGETS = {
"411060": Decimal("15"), "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 # Historical snapshots from data.txt
SNAPSHOTS = [ SNAPSHOTS = [
{ {
@ -242,13 +251,18 @@ def seed(db: Session):
print(f"Created {tx_count} transactions from snapshot diffs") print(f"Created {tx_count} transactions from snapshot diffs")
# Set current holdings from latest snapshot # Set current holdings from latest snapshot
# avg_price = total invested amount / quantity (from actual brokerage records)
latest = SNAPSHOTS[-1] latest = SNAPSHOTS[-1]
for h in latest["holdings"]: 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( db.add(Holding(
portfolio_id=portfolio.id, portfolio_id=portfolio.id,
ticker=h["ticker"], ticker=ticker,
quantity=h["qty"], quantity=qty,
avg_price=h["price"], # Using current price as avg (best available) avg_price=avg_price,
)) ))
print(f"Set {len(latest['holdings'])} current holdings from {latest['date']}") print(f"Set {len(latest['holdings'])} current holdings from {latest['date']}")