From b24b745dbb047674c1ac05f2531cab83f45c2291 Mon Sep 17 00:00:00 2001 From: Tyler Hoang Date: Mon, 18 May 2026 02:21:03 -0700 Subject: Add ratios service and tests --- backend/app/services/data_service.py | 159 +++++++++++++++++++++++++++++++++++ 1 file changed, 159 insertions(+) (limited to 'backend/app/services/data_service.py') diff --git a/backend/app/services/data_service.py b/backend/app/services/data_service.py index 9fe7f67..220accc 100644 --- a/backend/app/services/data_service.py +++ b/backend/app/services/data_service.py @@ -28,6 +28,8 @@ BETA_CACHE = TTLCache(maxsize=256, ttl=3600) SHORT_CACHE = TTLCache(maxsize=256, ttl=3600) FINANCIALS_CACHE = TTLCache(maxsize=128, ttl=3600) VALUATION_CACHE = TTLCache(maxsize=128, ttl=3600) +HIST_RATIOS_CACHE: TTLCache = TTLCache(maxsize=128, ttl=3600) +RATIOS_ENDPOINT_CACHE: TTLCache = TTLCache(maxsize=128, ttl=3600) PERIODS = {"1m", "3m", "6m", "1y", "2y", "5y"} YF_PERIOD_MAP = {"1m": "1mo", "3m": "3mo", "6m": "6mo", "1y": "1y", "2y": "2y", "5y": "5y"} @@ -614,6 +616,163 @@ def _latest_share_count(balance_sheet: pd.DataFrame) -> float | None: return shares if shares is not None and shares > 0 else None +def _find_price_at_date(price_history: list[dict], target: "pd.Timestamp") -> float | None: + """Return closing price from price_history nearest to target date (within 45 days).""" + if not price_history: + return None + best_price: float | None = None + best_delta = float("inf") + for pt in price_history: + try: + delta = abs((pd.Timestamp(pt["date"]) - target).days) + if delta < best_delta: + best_delta = delta + best_price = _safe_float(pt.get("close")) + except Exception: + continue + return best_price if best_delta <= 45 else None + + +@cached(HIST_RATIOS_CACHE) +def compute_historical_ratios(symbol: str) -> dict[str, list[float | None]]: + """Per-fiscal-year ratios from annual statements, oldest-first (up to 4 points).""" + sym = normalize_symbol(symbol) + inc_a = get_income_statement(sym, quarterly=False) + bal_a = get_balance_sheet(sym, quarterly=False) + cf_a = get_cash_flow(sym, quarterly=False) + + if inc_a is None or inc_a.empty: + return {} + + years = list(inc_a.columns[: min(len(inc_a.columns), 4)]) + shares = get_shares_outstanding(sym) + price_history = get_price_history(sym, period="5y") + + result: dict[str, list[float | None]] = {k: [] for k in [ + "gross_margin", "operating_margin", "net_margin", "ebitda_margin", + "roe", "roa", "debt_to_equity", "current_ratio", + "trailing_pe", "ev_to_ebitda", "price_to_book", "price_to_sales", + ]} + + for col in years: + col_dt = pd.Timestamp(col) + + def _inc(label: str) -> float | None: + if label not in inc_a.index: + return None + return _safe_float(inc_a.loc[label, col]) if col in inc_a.columns else None + + def _bal(label: str) -> float | None: + if bal_a is None or bal_a.empty or label not in bal_a.index: + return None + return _safe_float(bal_a.loc[label, col]) if col in bal_a.columns else None + + revenue = _inc("Total Revenue") + gross_profit = _inc("Gross Profit") + operating_income = _inc("Operating Income") + net_income = _inc("Net Income") + ebitda = _inc("EBITDA") or _inc("Normalized EBITDA") + equity = _bal("Stockholders Equity") or _bal("Common Stock Equity") + total_assets = _bal("Total Assets") + total_debt = _bal("Total Debt") + current_assets = _bal("Current Assets") + current_liabilities = _bal("Current Liabilities") + cash = _bal("Cash And Cash Equivalents") or _bal("Cash Cash Equivalents And Short Term Investments") or 0.0 + + rev = revenue if revenue and revenue > 0 else None + result["gross_margin"].append(_cap_ratio(gross_profit / rev, -5, 5) if rev and gross_profit is not None else None) + result["operating_margin"].append(_cap_ratio(operating_income / rev, -5, 5) if rev and operating_income is not None else None) + result["net_margin"].append(_cap_ratio(net_income / rev, -5, 5) if rev and net_income is not None else None) + result["ebitda_margin"].append(_cap_ratio(ebitda / rev, -5, 5) if rev and ebitda is not None else None) + result["roe"].append(_cap_ratio(net_income / equity, -10, 10) if equity and equity > 0 and net_income is not None else None) + result["roa"].append(_cap_ratio(net_income / total_assets, -10, 10) if total_assets and total_assets > 0 and net_income is not None else None) + result["debt_to_equity"].append(_cap_ratio(total_debt / equity, -1, 100) if equity and equity > 0 and total_debt is not None else None) + result["current_ratio"].append(current_assets / current_liabilities if current_liabilities and current_liabilities > 0 and current_assets is not None else None) + + price = _find_price_at_date(price_history, col_dt) + market_cap = price * shares if price and shares else None + ev = market_cap + (total_debt or 0.0) - cash if market_cap else None + + result["trailing_pe"].append(_cap_ratio(market_cap / net_income, 0, 500) if market_cap and net_income and net_income > 0 else None) + result["ev_to_ebitda"].append(_cap_ratio(ev / ebitda, 0, 500) if ev and ebitda and ebitda > 1e6 else None) + result["price_to_book"].append(_cap_ratio(market_cap / equity, 0, 100) if market_cap and equity and equity > 0 else None) + result["price_to_sales"].append(_cap_ratio(market_cap / revenue, 0, 100) if market_cap and revenue and revenue > 0 else None) + + return {k: list(reversed(v)) for k, v in result.items()} + + +@cached(RATIOS_ENDPOINT_CACHE) +def get_ratios(symbol: str) -> dict: + """Build the full RatiosResponse dict for the /ratios endpoint.""" + sym = normalize_symbol(symbol) + ttm = compute_ttm_ratios(sym) + hist = compute_historical_ratios(sym) + info = get_company_info(sym) + + income = get_income_statement(sym, quarterly=True) + balance = get_balance_sheet(sym, quarterly=True) + cf = get_cash_flow(sym, quarterly=True) + + ebitda = _statement_ttm(income, "EBITDA", "Normalized EBITDA") + revenue = _statement_ttm(income, "Total Revenue") + current_assets = _balance_value(balance, "Current Assets") + current_liabilities = _balance_value(balance, "Current Liabilities") + inventory = _balance_value(balance, "Inventory") + ebit = _statement_ttm(income, "EBIT") + interest_expense = _statement_ttm(income, "Interest Expense") + op_cf = _statement_ttm(cf, "Operating Cash Flow", "Cash From Operations") + capex_raw = _statement_ttm(cf, "Capital Expenditure") + capex = abs(capex_raw) if capex_raw is not None else None + fcf = (op_cf - capex) if op_cf is not None and capex is not None else None + market_cap = ttm.get("market_cap") + + quick_ratio: float | None = None + if current_liabilities and current_liabilities > 0 and current_assets is not None: + quick_ratio = (current_assets - (inventory or 0.0)) / current_liabilities + + interest_coverage: float | None = None + if interest_expense and ebit is not None: + ie = abs(interest_expense) + if ie > 0 and ebit > 0: + interest_coverage = _cap_ratio(ebit / ie, 0, 1000) + + ebitda_margin = _cap_ratio(ebitda / revenue, -5, 5) if revenue and revenue > 0 and ebitda is not None else None + fcf_margin = _cap_ratio(fcf / revenue, -5, 5) if revenue and revenue > 0 and fcf is not None else None + p_fcf = _cap_ratio(market_cap / fcf, 0, 1000) if market_cap and fcf and fcf > 0 else None + + fwd_pe = _safe_float(info.get("forwardPE")) if info else None + forward_pe = fwd_pe if fwd_pe and 0 < fwd_pe < 500 else None + + def point(ttm_key: str | None, hist_key: str | None, override: float | None = None) -> dict: + val = override if override is not None else (ttm.get(ttm_key) if ttm_key else None) + spark = hist.get(hist_key, []) if hist_key else [] + return {"value": val, "spark": spark, "vs_sector": None} + + return { + "pe_ttm": point("trailing_pe", "trailing_pe"), + "ev_ebitda": point("ev_to_ebitda", "ev_to_ebitda"), + "gross_margin": point("gross_margin_ttm", "gross_margin"), + "net_margin": point("net_margin_ttm", "net_margin"), + "price_to_book": point("price_to_book", "price_to_book"), + "price_to_sales": point("price_to_sales", "price_to_sales"), + "ev_to_sales": point("ev_to_sales", None), + "p_fcf": point(None, None, p_fcf), + "forward_pe": point(None, None, forward_pe), + "operating_margin": point("operating_margin_ttm", "operating_margin"), + "ebitda_margin": point(None, "ebitda_margin", ebitda_margin), + "fcf_margin": point(None, None, fcf_margin), + "roe": point("roe_ttm", "roe"), + "roa": point("roa_ttm", "roa"), + "roic": point("roic_ttm", None), + "debt_to_equity": point("debt_to_equity", "debt_to_equity"), + "current_ratio": point("current_ratio", "current_ratio"), + "quick_ratio": point(None, None, quick_ratio), + "interest_coverage": point(None, None, interest_coverage), + "dividend_yield": point("dividend_yield_ttm", None), + "dividend_payout": point("dividend_payout_ratio_ttm", None), + } + + def _pick_search_match(symbol: str) -> dict[str, Any]: sym = normalize_symbol(symbol) results = search_tickers(sym) -- cgit v1.3-2-g0d8e From 38bef98ab1964bc9365d17c4a5ca17cdbd32673b Mon Sep 17 00:00:00 2001 From: Tyler Hoang Date: Mon, 18 May 2026 02:34:46 -0700 Subject: fix: correct historical ratios share and debt inputs --- backend/app/services/data_service.py | 48 ++++++++++++++++-- backend/tests/test_api.py | 96 ++++++++++++++++++++++++++++++++++++ 2 files changed, 141 insertions(+), 3 deletions(-) (limited to 'backend/app/services/data_service.py') diff --git a/backend/app/services/data_service.py b/backend/app/services/data_service.py index 220accc..e356cb4 100644 --- a/backend/app/services/data_service.py +++ b/backend/app/services/data_service.py @@ -645,8 +645,17 @@ def compute_historical_ratios(symbol: str) -> dict[str, list[float | None]]: return {} years = list(inc_a.columns[: min(len(inc_a.columns), 4)]) - shares = get_shares_outstanding(sym) price_history = get_price_history(sym, period="5y") + current_shares = get_shares_outstanding(sym) + + try: + shares_history_raw = yf.Ticker(sym).get_shares_full(start="2000-01-01") + if isinstance(shares_history_raw, pd.Series): + shares_history = pd.to_numeric(shares_history_raw, errors="coerce").dropna().sort_index() + else: + shares_history = pd.Series(dtype=float) + except Exception: + shares_history = pd.Series(dtype=float) result: dict[str, list[float | None]] = {k: [] for k in [ "gross_margin", "operating_margin", "net_margin", "ebitda_margin", @@ -654,6 +663,38 @@ def compute_historical_ratios(symbol: str) -> dict[str, list[float | None]]: "trailing_pe", "ev_to_ebitda", "price_to_book", "price_to_sales", ]} + def _balance_shares(period_date: pd.Timestamp) -> float | None: + if bal_a is None or bal_a.empty or period_date not in bal_a.columns: + return None + for label in _SHARE_LABELS: + if label not in bal_a.index: + continue + shares_value = _safe_float(bal_a.loc[label, period_date]) + if shares_value is not None and shares_value > 0: + return shares_value + return None + + def _historical_shares_for_date(period_date: pd.Timestamp) -> float | None: + direct_balance_shares = _balance_shares(period_date) + if direct_balance_shares is not None: + return direct_balance_shares + if not shares_history.empty: + target = pd.Timestamp(period_date) + index = shares_history.index + if getattr(index, "tz", None) is not None and target.tzinfo is None: + target = target.tz_localize(index.tz) + elif getattr(index, "tz", None) is None and target.tzinfo is not None: + target = target.tz_localize(None) + + deltas = pd.Series(index - target, index=index).abs() + if not deltas.empty: + nearest_idx = deltas.idxmin() + if abs(pd.Timestamp(nearest_idx) - target) <= pd.Timedelta(days=180): + shares_value = _safe_float(shares_history.loc[nearest_idx]) + if shares_value is not None and shares_value > 0: + return shares_value + return current_shares + for col in years: col_dt = pd.Timestamp(col) @@ -674,10 +715,11 @@ def compute_historical_ratios(symbol: str) -> dict[str, list[float | None]]: ebitda = _inc("EBITDA") or _inc("Normalized EBITDA") equity = _bal("Stockholders Equity") or _bal("Common Stock Equity") total_assets = _bal("Total Assets") - total_debt = _bal("Total Debt") + total_debt = _bal("Total Debt") or _bal("Long Term Debt And Capital Lease Obligation") current_assets = _bal("Current Assets") current_liabilities = _bal("Current Liabilities") cash = _bal("Cash And Cash Equivalents") or _bal("Cash Cash Equivalents And Short Term Investments") or 0.0 + period_shares = _historical_shares_for_date(col_dt) rev = revenue if revenue and revenue > 0 else None result["gross_margin"].append(_cap_ratio(gross_profit / rev, -5, 5) if rev and gross_profit is not None else None) @@ -690,7 +732,7 @@ def compute_historical_ratios(symbol: str) -> dict[str, list[float | None]]: result["current_ratio"].append(current_assets / current_liabilities if current_liabilities and current_liabilities > 0 and current_assets is not None else None) price = _find_price_at_date(price_history, col_dt) - market_cap = price * shares if price and shares else None + market_cap = price * period_shares if price and period_shares else None ev = market_cap + (total_debt or 0.0) - cash if market_cap else None result["trailing_pe"].append(_cap_ratio(market_cap / net_income, 0, 500) if market_cap and net_income and net_income > 0 else None) diff --git a/backend/tests/test_api.py b/backend/tests/test_api.py index 94c9c20..d0aafa4 100644 --- a/backend/tests/test_api.py +++ b/backend/tests/test_api.py @@ -993,6 +993,102 @@ def test_compute_historical_ratios_margins(monkeypatch) -> None: assert result["price_to_sales"] == [None, None, None, None] +def test_compute_historical_ratios_uses_period_share_counts_oldest_first(monkeypatch) -> None: + clear_service_caches() + monkeypatch.setattr( + data_service, + "get_income_statement", + lambda symbol, quarterly=False: annual_frame( + { + "Total Revenue": [40.0, 40.0, 40.0, 40.0], + "Gross Profit": [20.0, 20.0, 20.0, 20.0], + "Operating Income": [15.0, 15.0, 15.0, 15.0], + "Net Income": [10.0, 10.0, 10.0, 10.0], + "EBITDA": [2_000_000.0, 2_000_000.0, 2_000_000.0, 2_000_000.0], + } + ), + ) + monkeypatch.setattr( + data_service, + "get_balance_sheet", + lambda symbol, quarterly=False: annual_frame( + { + "Stockholders Equity": [20.0, 20.0, 20.0, 20.0], + "Total Assets": [50.0, 50.0, 50.0, 50.0], + "Total Debt": [100_000.0, 100_000.0, 100_000.0, 100_000.0], + "Current Assets": [10.0, 10.0, 10.0, 10.0], + "Current Liabilities": [5.0, 5.0, 5.0, 5.0], + "Cash And Cash Equivalents": [0.0, 0.0, 0.0, 0.0], + "Ordinary Shares Number": [10.0, 20.0, 40.0, 80.0], + } + ), + ) + monkeypatch.setattr(data_service, "get_cash_flow", lambda symbol, quarterly=False: pd.DataFrame()) + monkeypatch.setattr(data_service, "get_shares_outstanding", lambda symbol: 999.0) + monkeypatch.setattr( + data_service, + "get_price_history", + lambda symbol, period="5y": [ + {"date": "2024-09-30", "close": 10.0}, + {"date": "2023-09-30", "close": 10.0}, + {"date": "2022-09-30", "close": 10.0}, + {"date": "2021-09-30", "close": 10.0}, + ], + ) + + result = data_service.compute_historical_ratios("AAPL") + + assert result["trailing_pe"] == [80.0, 40.0, 20.0, 10.0] + assert result["price_to_book"] == [40.0, 20.0, 10.0, 5.0] + assert result["price_to_sales"] == [20.0, 10.0, 5.0, 2.5] + + +def test_compute_historical_ratios_uses_long_term_debt_fallback(monkeypatch) -> None: + clear_service_caches() + monkeypatch.setattr( + data_service, + "get_income_statement", + lambda symbol, quarterly=False: annual_frame( + { + "Total Revenue": [5_000_000.0, 5_000_000.0, 5_000_000.0, 5_000_000.0], + "Gross Profit": [2_500_000.0, 2_500_000.0, 2_500_000.0, 2_500_000.0], + "Operating Income": [2_100_000.0, 2_100_000.0, 2_100_000.0, 2_100_000.0], + "Net Income": [1_000_000.0, 1_000_000.0, 1_000_000.0, 1_000_000.0], + "EBITDA": [2_000_000.0, 2_000_000.0, 2_000_000.0, 2_000_000.0], + } + ), + ) + monkeypatch.setattr( + data_service, + "get_balance_sheet", + lambda symbol, quarterly=False: annual_frame( + { + "Stockholders Equity": [4_000_000.0, 4_000_000.0, 4_000_000.0, 4_000_000.0], + "Total Assets": [8_000_000.0, 8_000_000.0, 8_000_000.0, 8_000_000.0], + "Long Term Debt And Capital Lease Obligation": [2_000_000.0, 2_000_000.0, 2_000_000.0, 2_000_000.0], + "Cash And Cash Equivalents": [1_000_000.0, 1_000_000.0, 1_000_000.0, 1_000_000.0], + "Ordinary Shares Number": [1_000_000.0, 1_000_000.0, 1_000_000.0, 1_000_000.0], + } + ), + ) + monkeypatch.setattr(data_service, "get_cash_flow", lambda symbol, quarterly=False: pd.DataFrame()) + monkeypatch.setattr(data_service, "get_shares_outstanding", lambda symbol: None) + monkeypatch.setattr( + data_service, + "get_price_history", + lambda symbol, period="5y": [ + {"date": "2024-09-30", "close": 10.0}, + {"date": "2023-09-30", "close": 10.0}, + {"date": "2022-09-30", "close": 10.0}, + {"date": "2021-09-30", "close": 10.0}, + ], + ) + + result = data_service.compute_historical_ratios("AAPL") + + assert result["ev_to_ebitda"] == [5.5, 5.5, 5.5, 5.5] + + def test_compute_historical_ratios_empty_income(monkeypatch) -> None: clear_service_caches() monkeypatch.setattr(data_service, "get_income_statement", lambda symbol, quarterly=False: pd.DataFrame()) -- cgit v1.3-2-g0d8e From acb628932215338b7971f3051b1e5d89e58573a1 Mon Sep 17 00:00:00 2001 From: Tyler Hoang Date: Mon, 18 May 2026 22:31:48 -0700 Subject: fix: populate sector benchmark values for ratios tab --- backend/app/services/data_service.py | 101 +++++++++++++++++++++++++++++++++-- backend/tests/test_api.py | 42 +++++++++++++++ 2 files changed, 138 insertions(+), 5 deletions(-) (limited to 'backend/app/services/data_service.py') diff --git a/backend/app/services/data_service.py b/backend/app/services/data_service.py index e356cb4..ab06723 100644 --- a/backend/app/services/data_service.py +++ b/backend/app/services/data_service.py @@ -3,6 +3,7 @@ from __future__ import annotations import math import os +import statistics from typing import Any import httpx @@ -30,6 +31,7 @@ FINANCIALS_CACHE = TTLCache(maxsize=128, ttl=3600) VALUATION_CACHE = TTLCache(maxsize=128, ttl=3600) HIST_RATIOS_CACHE: TTLCache = TTLCache(maxsize=128, ttl=3600) RATIOS_ENDPOINT_CACHE: TTLCache = TTLCache(maxsize=128, ttl=3600) +SECTOR_BENCHMARK_CACHE: TTLCache = TTLCache(maxsize=128, ttl=3600) PERIODS = {"1m", "3m", "6m", "1y", "2y", "5y"} YF_PERIOD_MAP = {"1m": "1mo", "3m": "3mo", "6m": "6mo", "1y": "1y", "2y": "2y", "5y": "5y"} @@ -750,6 +752,7 @@ def get_ratios(symbol: str) -> dict: ttm = compute_ttm_ratios(sym) hist = compute_historical_ratios(sym) info = get_company_info(sym) + sector_bench = compute_sector_ratio_benchmarks(sym) income = get_income_statement(sym, quarterly=True) balance = get_balance_sheet(sym, quarterly=True) @@ -785,10 +788,17 @@ def get_ratios(symbol: str) -> dict: fwd_pe = _safe_float(info.get("forwardPE")) if info else None forward_pe = fwd_pe if fwd_pe and 0 < fwd_pe < 500 else None - def point(ttm_key: str | None, hist_key: str | None, override: float | None = None) -> dict: + def point( + ttm_key: str | None, + hist_key: str | None, + override: float | None = None, + sector_key: str | None = None, + ) -> dict: val = override if override is not None else (ttm.get(ttm_key) if ttm_key else None) spark = hist.get(hist_key, []) if hist_key else [] - return {"value": val, "spark": spark, "vs_sector": None} + skey = sector_key if sector_key is not None else ttm_key + vs_sector = sector_bench.get(skey) if skey else None + return {"value": val, "spark": spark, "vs_sector": vs_sector} return { "pe_ttm": point("trailing_pe", "trailing_pe"), @@ -799,22 +809,103 @@ def get_ratios(symbol: str) -> dict: "price_to_sales": point("price_to_sales", "price_to_sales"), "ev_to_sales": point("ev_to_sales", None), "p_fcf": point(None, None, p_fcf), - "forward_pe": point(None, None, forward_pe), + "forward_pe": point(None, None, forward_pe, "trailing_pe"), "operating_margin": point("operating_margin_ttm", "operating_margin"), - "ebitda_margin": point(None, "ebitda_margin", ebitda_margin), + "ebitda_margin": point(None, "ebitda_margin", ebitda_margin, "operating_margin_ttm"), "fcf_margin": point(None, None, fcf_margin), "roe": point("roe_ttm", "roe"), "roa": point("roa_ttm", "roa"), "roic": point("roic_ttm", None), "debt_to_equity": point("debt_to_equity", "debt_to_equity"), "current_ratio": point("current_ratio", "current_ratio"), - "quick_ratio": point(None, None, quick_ratio), + "quick_ratio": point(None, None, quick_ratio, "current_ratio"), "interest_coverage": point(None, None, interest_coverage), "dividend_yield": point("dividend_yield_ttm", None), "dividend_payout": point("dividend_payout_ratio_ttm", None), } +@cached(SECTOR_BENCHMARK_CACHE) +def compute_sector_ratio_benchmarks(symbol: str) -> dict[str, float]: + """Median TTM ratio benchmarks from same-sector peers (FMP-backed when available).""" + sym = normalize_symbol(symbol) + fmp_key = os.getenv("FMP_API_KEY") + if not fmp_key: + return {} + + info = get_company_info(sym) + sector_raw = info.get("sector") if isinstance(info, dict) else None + sector = str(sector_raw or "").strip() + if not sector: + enrichment = get_profile_enrichment(sym) + sector = str((enrichment or {}).get("sector") or "").strip() + if not sector: + return {} + + peer_symbols: list[str] = [] + try: + with httpx.Client(timeout=3.5) as client: + res = client.get( + "https://financialmodelingprep.com/api/v3/stock-screener", + params={ + "sector": sector, + "isEtf": "false", + "isActivelyTrading": "true", + "limit": 12, + "apikey": fmp_key, + }, + ) + rows = res.json() + if isinstance(rows, list): + for row in rows: + psym = normalize_symbol((row or {}).get("symbol")) + if not psym or psym == sym: + continue + peer_symbols.append(psym) + except Exception: + return {} + + if not peer_symbols: + return {} + + keys = [ + "trailing_pe", + "ev_to_ebitda", + "gross_margin_ttm", + "net_margin_ttm", + "price_to_book", + "price_to_sales", + "ev_to_sales", + "operating_margin_ttm", + "roe_ttm", + "roa_ttm", + "roic_ttm", + "debt_to_equity", + "current_ratio", + "dividend_yield_ttm", + "dividend_payout_ratio_ttm", + ] + buckets: dict[str, list[float]] = {k: [] for k in keys} + + for psym in peer_symbols[:6]: + try: + ratios = compute_ttm_ratios(psym) + except Exception: + continue + if not isinstance(ratios, dict): + continue + for key in keys: + val = _safe_float(ratios.get(key)) + if val is not None: + buckets[key].append(val) + + out: dict[str, float] = {} + for key, values in buckets.items(): + if values: + out[key] = float(statistics.median(values)) + return out + + def _pick_search_match(symbol: str) -> dict[str, Any]: sym = normalize_symbol(symbol) results = search_tickers(sym) diff --git a/backend/tests/test_api.py b/backend/tests/test_api.py index 2ec4a08..300069c 100644 --- a/backend/tests/test_api.py +++ b/backend/tests/test_api.py @@ -21,6 +21,7 @@ def clear_service_caches() -> None: data_service.VALUATION_CACHE.clear() data_service.HIST_RATIOS_CACHE.clear() data_service.RATIOS_ENDPOINT_CACHE.clear() + data_service.SECTOR_BENCHMARK_CACHE.clear() def quarterly_frame(rows: dict[str, list[float]]) -> pd.DataFrame: @@ -1170,6 +1171,47 @@ def test_get_ratios_interest_coverage(monkeypatch) -> None: assert result["interest_coverage"]["spark"] == [] +def test_get_ratios_sector_benchmark_fields(monkeypatch) -> None: + clear_service_caches() + monkeypatch.setattr( + data_service, + "compute_ttm_ratios", + lambda symbol: { + "market_cap": 1000.0, + "trailing_pe": 20.0, + "price_to_book": 5.0, + "current_ratio": 2.0, + "dividend_yield_ttm": 0.01, + }, + ) + monkeypatch.setattr(data_service, "compute_historical_ratios", lambda symbol: {}) + monkeypatch.setattr( + data_service, + "compute_sector_ratio_benchmarks", + lambda symbol: { + "trailing_pe": 18.0, + "price_to_book": 4.0, + "current_ratio": 1.7, + "dividend_yield_ttm": 0.012, + }, + ) + monkeypatch.setattr(data_service, "get_company_info", lambda symbol: {}) + monkeypatch.setattr( + data_service, + "get_income_statement", + lambda symbol, quarterly=True: quarterly_frame({"Total Revenue": [100.0, 100.0, 100.0, 100.0]}), + ) + monkeypatch.setattr(data_service, "get_balance_sheet", lambda symbol, quarterly=True: quarterly_frame({})) + monkeypatch.setattr(data_service, "get_cash_flow", lambda symbol, quarterly=True: quarterly_frame({})) + + result = data_service.get_ratios("AAPL") + + assert result["pe_ttm"]["vs_sector"] == 18.0 + assert result["price_to_book"]["vs_sector"] == 4.0 + assert result["current_ratio"]["vs_sector"] == 1.7 + assert result["dividend_yield"]["vs_sector"] == pytest.approx(0.012) + + def test_ticker_ratios_route(monkeypatch) -> None: """GET /api/tickers/{symbol}/ratios returns a valid RatiosResponse shape.""" clear_service_caches() -- cgit v1.3-2-g0d8e From 52635efd7d435b091b4f13897511ca8e2c48f0b9 Mon Sep 17 00:00:00 2001 From: Tyler Hoang Date: Mon, 18 May 2026 22:43:16 -0700 Subject: fix: add no-key fallback for sector ratio benchmarks --- backend/app/services/data_service.py | 61 ++++++++++++++++++++++-------------- backend/tests/test_api.py | 43 +++++++++++++++++++++++++ 2 files changed, 81 insertions(+), 23 deletions(-) (limited to 'backend/app/services/data_service.py') diff --git a/backend/app/services/data_service.py b/backend/app/services/data_service.py index ab06723..f913ec5 100644 --- a/backend/app/services/data_service.py +++ b/backend/app/services/data_service.py @@ -830,8 +830,6 @@ def compute_sector_ratio_benchmarks(symbol: str) -> dict[str, float]: """Median TTM ratio benchmarks from same-sector peers (FMP-backed when available).""" sym = normalize_symbol(symbol) fmp_key = os.getenv("FMP_API_KEY") - if not fmp_key: - return {} info = get_company_info(sym) sector_raw = info.get("sector") if isinstance(info, dict) else None @@ -843,27 +841,44 @@ def compute_sector_ratio_benchmarks(symbol: str) -> dict[str, float]: return {} peer_symbols: list[str] = [] - try: - with httpx.Client(timeout=3.5) as client: - res = client.get( - "https://financialmodelingprep.com/api/v3/stock-screener", - params={ - "sector": sector, - "isEtf": "false", - "isActivelyTrading": "true", - "limit": 12, - "apikey": fmp_key, - }, - ) - rows = res.json() - if isinstance(rows, list): - for row in rows: - psym = normalize_symbol((row or {}).get("symbol")) - if not psym or psym == sym: - continue - peer_symbols.append(psym) - except Exception: - return {} + if fmp_key: + try: + with httpx.Client(timeout=3.5) as client: + res = client.get( + "https://financialmodelingprep.com/api/v3/stock-screener", + params={ + "sector": sector, + "isEtf": "false", + "isActivelyTrading": "true", + "limit": 12, + "apikey": fmp_key, + }, + ) + rows = res.json() + if isinstance(rows, list): + for row in rows: + psym = normalize_symbol((row or {}).get("symbol")) + if not psym or psym == sym: + continue + peer_symbols.append(psym) + except Exception: + peer_symbols = [] + + # No-key or FMP failure fallback: search by sector term, then filter by exact sector. + if not peer_symbols: + try: + candidates = search_tickers(sector) + except Exception: + candidates = [] + target_sector = sector.lower() + for row in candidates[:24]: + psym = normalize_symbol((row or {}).get("symbol")) + if not psym or psym == sym: + continue + pinfo = get_company_info(psym) + psector = str((pinfo or {}).get("sector") or "").strip().lower() + if psector and psector == target_sector: + peer_symbols.append(psym) if not peer_symbols: return {} diff --git a/backend/tests/test_api.py b/backend/tests/test_api.py index 300069c..345c0a3 100644 --- a/backend/tests/test_api.py +++ b/backend/tests/test_api.py @@ -1212,6 +1212,49 @@ def test_get_ratios_sector_benchmark_fields(monkeypatch) -> None: assert result["dividend_yield"]["vs_sector"] == pytest.approx(0.012) +def test_compute_sector_ratio_benchmarks_without_fmp_key(monkeypatch) -> None: + clear_service_caches() + monkeypatch.delenv("FMP_API_KEY", raising=False) + monkeypatch.setattr( + data_service, + "get_company_info", + lambda symbol: ( + {"sector": "Technology"} + if symbol == "AAPL" + else {"sector": "Technology"} + if symbol == "MSFT" + else {"sector": "Technology"} + if symbol == "NVDA" + else {"sector": "Healthcare"} + ), + ) + monkeypatch.setattr( + data_service, + "search_tickers", + lambda query: [ + {"symbol": "MSFT", "name": "Microsoft", "exchange": "NASDAQ"}, + {"symbol": "NVDA", "name": "NVIDIA", "exchange": "NASDAQ"}, + {"symbol": "UNH", "name": "UnitedHealth", "exchange": "NYSE"}, + ], + ) + monkeypatch.setattr( + data_service, + "compute_ttm_ratios", + lambda symbol: ( + {"trailing_pe": 30.0, "current_ratio": 2.0} + if symbol == "MSFT" + else {"trailing_pe": 20.0, "current_ratio": 1.5} + if symbol == "NVDA" + else {"trailing_pe": 10.0, "current_ratio": 1.0} + ), + ) + + result = data_service.compute_sector_ratio_benchmarks("AAPL") + + assert result["trailing_pe"] == pytest.approx(25.0) + assert result["current_ratio"] == pytest.approx(1.75) + + def test_ticker_ratios_route(monkeypatch) -> None: """GET /api/tickers/{symbol}/ratios returns a valid RatiosResponse shape.""" clear_service_caches() -- cgit v1.3-2-g0d8e