""" Fundamental Features — Value, growth, quality scores from yfinance data. """ import logging from backend.data.fundamentals import compute_fundamental_score, fetch_fundamentals from backend.data.store import get_store logger = logging.getLogger(__name__) def compute_fundamental_features(ticker: str) -> dict: """ Fetch fundamentals and compute a composite score. Uses SQLite cache to avoid refetching within 24 hours. Returns: dict with all fundamental metrics + composite score """ store = get_store() # Check cache (24h TTL for fundamentals) cached = store.load_features(ticker, "fundamentals", max_age_hours=24) if cached: logger.debug(f"{ticker}: Using cached fundamentals") return cached # Fetch fresh fundamentals = fetch_fundamentals(ticker) if "error" in fundamentals: return {"ticker": ticker, "fundamental_score": 50.0, "error": fundamentals["error"]} # Compute composite score fundamentals["fundamental_score"] = compute_fundamental_score(fundamentals) # Cache store.save_features(ticker, "fundamentals", fundamentals) logger.info(f"{ticker}: Fundamental score = {fundamentals['fundamental_score']:.1f}") return fundamentals