fix: update_workbook_sector_insights.py 인덱스 접근을 딕셔너리 접근으로 교체
- account_rows/universe_rows raw 인덱스 접근(row[3], row[10] 등) -> dict 기반(row.get("ticker"), row.get("market_value") 등)
- 헤더 컬럼 순서 변경에 강건한 구조
- sector_map 빌드: row[0]/row[2] -> row.get("Ticker")/row.get("Sector")
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user