feat(stock): get_holdings (현재가·손익·KRX판별)

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-05-31 21:37:01 +09:00
parent 885d52d8f5
commit 0ef7d414b7
2 changed files with 54 additions and 0 deletions

View File

@@ -0,0 +1,37 @@
"""보유종목 인텔리전스 — 순수연산 중심 (advisory). KIS 실주문 미사용."""
from __future__ import annotations
import datetime as dt
from typing import Any, Optional
from . import db
from . import price_fetcher
def _krx_tickers() -> set:
"""krx_master에 존재하는 ticker 집합 (KRX 판별용)."""
with db._conn() as conn:
try:
rows = conn.execute("SELECT ticker FROM krx_master").fetchall()
except Exception:
return set()
return {r["ticker"] for r in rows}
def get_holdings() -> list[dict]:
"""portfolio + 현재가 + pnl_rate + is_krx."""
items = db.get_all_portfolio()
tickers = [it["ticker"] for it in items]
prices = price_fetcher.get_current_prices(tickers) if tickers else {}
krx = _krx_tickers()
out = []
for it in items:
cur = prices.get(it["ticker"])
avg = it["avg_price"]
pnl = ((cur - avg) / avg * 100.0) if (cur and avg) else None
out.append({
**it,
"current_price": cur,
"pnl_rate": pnl,
"is_krx": it["ticker"] in krx,
})
return out