diff options
| author | Tyler <tyler@tylerhoang.xyz> | 2026-03-29 18:04:14 -0700 |
|---|---|---|
| committer | Tyler <tyler@tylerhoang.xyz> | 2026-03-29 18:04:14 -0700 |
| commit | 425874931ffd3e3cef60262f7f0b7cb678629278 (patch) | |
| tree | b99dcbba9dba249ee94f9d7caa9986ffe2c23f6f /services/fmp_service.py | |
| parent | 678d3290e87a2043b4cb338df6cb93307e91ffc1 (diff) | |
Add yfinance fallback for historical ratios when FMP quota is exhausted
FMP free tier caps at 250 req/day; hitting the limit caused the Historical
Ratios tab to show an error. get_historical_ratios_yfinance now computes
margins, ROE, ROA, D/E, P/E, P/B, P/S, and EV/EBITDA directly from
yfinance income statements, balance sheets, and price history. FMP
functions fall back to this automatically when they receive an empty
response.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Diffstat (limited to 'services/fmp_service.py')
| -rw-r--r-- | services/fmp_service.py | 18 |
1 files changed, 13 insertions, 5 deletions
diff --git a/services/fmp_service.py b/services/fmp_service.py index bba6c85..3bfa5c1 100644 --- a/services/fmp_service.py +++ b/services/fmp_service.py @@ -3,7 +3,7 @@ import os import requests import streamlit as st from dotenv import load_dotenv -from services.data_service import get_company_info +from services.data_service import get_company_info, get_historical_ratios_yfinance load_dotenv() @@ -121,16 +121,24 @@ def get_company_news(ticker: str, limit: int = 20) -> list[dict]: @st.cache_data(ttl=86400) def get_historical_ratios(ticker: str, limit: int = 10) -> list[dict]: - """Annual historical valuation ratios (P/E, P/B, P/S, EV/EBITDA, etc.).""" + """Annual historical valuation ratios (P/E, P/B, P/S, EV/EBITDA, etc.). + Falls back to yfinance-computed ratios if FMP returns empty (e.g. rate limit).""" data = _get(STABLE_BASE, "/ratios", params={"symbol": ticker.upper(), "limit": limit}) - return data if isinstance(data, list) else [] + if isinstance(data, list) and data: + return data + return get_historical_ratios_yfinance(ticker.upper()) @st.cache_data(ttl=86400) def get_historical_key_metrics(ticker: str, limit: int = 10) -> list[dict]: - """Annual historical key metrics (ROE, ROA, margins, debt/equity, etc.).""" + """Annual historical key metrics (ROE, ROA, margins, debt/equity, etc.). + Falls back to yfinance-computed metrics if FMP returns empty (e.g. rate limit).""" data = _get(STABLE_BASE, "/key-metrics", params={"symbol": ticker.upper(), "limit": limit}) - return data if isinstance(data, list) else [] + if isinstance(data, list) and data: + return data + # yfinance fallback already covers all key metrics — return empty to avoid duplication + # (get_historical_ratios will have already provided the full merged dataset) + return [] @st.cache_data(ttl=3600) |
