Merge pull request 'fix: update_workbook_sector_insights dict-based row access (robust to column order changes)' (#58) from feature/fix-workbook-sector-dict-access into main

fix: update_workbook_sector_insights dict-based row access
This commit is contained in:
2026-06-14 21:00:48 +09:00
+15 -11
View File
@@ -230,26 +230,30 @@ def build_portfolio_sector_exposure(wb) -> None:
account_headers, account_rows = extract_sheet_rows(wb, "account_snapshot")
universe_headers, universe_rows = extract_sheet_rows(wb, "universe")
account_dicts = [dict(zip(account_headers, row)) for row in account_rows if any(v not in (None, "") for v in row)]
universe_dicts = [dict(zip(universe_headers, row)) for row in universe_rows if any(v not in (None, "") for v in row)]
sector_map: dict[str, str] = {}
for row in universe_rows:
if len(row) >= 3 and row[0] and row[2]:
ticker = str(row[0]).zfill(6)
sector_map[ticker] = str(row[2])
for row in universe_dicts:
ticker = str(row.get("Ticker", "") or "").zfill(6)
sector = str(row.get("Sector", "") or "").strip()
if ticker and sector:
sector_map[ticker] = sector
latest_capture = ""
for row in account_rows:
cap = str(row[0] or "")
for row in account_dicts:
cap = str(row.get("captured_at", "") or "")
if cap and cap >= latest_capture:
latest_capture = cap
latest_rows = [r for r in account_rows if str(r[0] or "") == latest_capture]
latest_rows = [r for r in account_dicts if str(r.get("captured_at", "") or "") == latest_capture]
exposure: dict[str, dict[str, float]] = {}
for row in latest_rows:
ticker = str(row[3] or "").zfill(6)
ticker = str(row.get("ticker", "") or "").zfill(6)
sector = sector_map.get(ticker, "미분류")
mv = float(row[10] or 0)
pl = float(row[11] or 0)
cost = float(row[8] or 0)
mv = float(row.get("market_value", 0) or 0)
pl = float(row.get("profit_loss", 0) or 0)
cost = float(row.get("total_cost", 0) or 0)
bucket = exposure.setdefault(sector, {"market_value": 0.0, "profit_loss": 0.0, "cost": 0.0, "count": 0.0})
bucket["market_value"] += mv
bucket["profit_loss"] += pl