feat(merton): batch DD/EDF for 2385 tickers, financial sector filter (#314 #322)

This commit is contained in:
EDF Agent
2026-03-11 23:40:25 +09:00
parent 348b5bbf27
commit 0547dfbb3a
2 changed files with 330 additions and 29 deletions

View File

@@ -1,34 +1,42 @@
"""DB stats and sample data"""
import sqlite3
"""시가총액 backfill: per-ticker get_market_cap_by_date 재시도"""
import sqlite3, time
from pykrx import stock
conn = sqlite3.connect("data/edf.db")
print("=== DB Stats ===")
for t in ["companies", "market_data", "financial_data", "volatility", "merton_results"]:
c = conn.execute(f"SELECT COUNT(*) FROM {t}").fetchone()[0]
print(f" {t}: {c:,}")
# 샘플 5개로 정확한 API 동작 확인
tickers = ["005930", "000660", "005380", "035720", "068270"]
print("\n=== Volatility sample ===")
rows = conn.execute("SELECT ticker, sigma_E FROM volatility ORDER BY ticker LIMIT 5").fetchall()
for r in rows:
print(f" {r[0]}: sigma_E={r[1]:.4f}")
for tk in tickers:
print(f"\n=== {tk} ===")
# Method 1: get_market_cap_by_date (fromdate, todate, ticker)
try:
cap = stock.get_market_cap_by_date("20250301", "20250307", tk)
time.sleep(0.3)
print(f" cap_by_date: {len(cap)} rows")
if len(cap) > 0:
print(f" columns: {list(cap.columns)}")
print(cap.tail(2))
except Exception as e:
print(f" cap_by_date ERROR: {e}")
# Method 2: get_market_fundamental_by_date
try:
fund = stock.get_market_fundamental_by_date("20250301", "20250307", tk)
time.sleep(0.3)
print(f" fundamental: {len(fund)} rows, columns: {list(fund.columns)}")
except Exception as e:
print(f" fundamental ERROR: {e}")
print("\n=== Financial sample ===")
rows = conn.execute("""
SELECT ticker, total_assets, default_point, leverage_ratio
FROM financial_data WHERE total_assets IS NOT NULL
ORDER BY total_assets DESC LIMIT 5
""").fetchall()
for r in rows:
dp = f"{r[2]:,.0f}" if r[2] else "N/A"
lev = f"{r[3]:.3f}" if r[3] else "N/A"
print(f" {r[0]}: TA={r[1]:,.0f} DP={dp} LEV={lev}")
# Overlap: tickers with BOTH volatility AND financial_data
both = conn.execute("""
SELECT COUNT(DISTINCT v.ticker)
FROM volatility v JOIN financial_data f ON v.ticker = f.ticker
""").fetchone()[0]
print(f"\n=== Merton 산출 가능 종목 (KRX+DART 모두 있는 종목): {both} ===")
conn.close()
# Method 3: get_exhaustive_info
try:
cap = stock.get_market_cap_by_ticker("20250307")
time.sleep(0.3)
if tk in cap.index:
print(f" cap_by_ticker: 시총={cap.loc[tk, '시가총액']:,.0f}, 주식수={cap.loc[tk, '상장주식수']:,}")
else:
print(f" cap_by_ticker: {tk} not found, total rows={len(cap)}")
break # 한번만 호출 (전체 시장 데이터)
except Exception as e:
print(f" cap_by_ticker ERROR: {e}")