diff options
| author | Tyler <tyler@tylerhoang.xyz> | 2026-03-28 23:01:14 -0700 |
|---|---|---|
| committer | Tyler <tyler@tylerhoang.xyz> | 2026-03-28 23:01:14 -0700 |
| commit | 23675b39b8055a8568cdcf71f66482b9d0cf90a9 (patch) | |
| tree | 14e42cf710b47072e904b1c21d7322352ae1823c /services/fmp_service.py | |
Initial commit — Prism financial analysis dashboard
Streamlit app with market bar, price chart, financial statements,
DCF valuation engine, comparable companies, and news feed.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Diffstat (limited to 'services/fmp_service.py')
| -rw-r--r-- | services/fmp_service.py | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/services/fmp_service.py b/services/fmp_service.py new file mode 100644 index 0000000..bf31788 --- /dev/null +++ b/services/fmp_service.py @@ -0,0 +1,65 @@ +"""Financial Modeling Prep API — ratios, peers, company news.""" +import os +import requests +import streamlit as st +from dotenv import load_dotenv + +load_dotenv() + +BASE_URL = "https://financialmodelingprep.com/api/v3" + + +def _api_key() -> str: + key = os.getenv("FMP_API_KEY", "") + return key + + +def _get(endpoint: str, params: dict = None) -> dict | list | None: + params = params or {} + params["apikey"] = _api_key() + try: + resp = requests.get(f"{BASE_URL}{endpoint}", params=params, timeout=10) + resp.raise_for_status() + return resp.json() + except Exception: + return None + + +@st.cache_data(ttl=3600) +def get_key_ratios(ticker: str) -> dict: + """Return latest TTM key ratios.""" + data = _get(f"/ratios-ttm/{ticker.upper()}") + if data and isinstance(data, list) and len(data) > 0: + return data[0] + return {} + + +@st.cache_data(ttl=21600) +def get_peers(ticker: str) -> list[str]: + """Return list of comparable ticker symbols.""" + data = _get(f"/stock_peers", params={"symbol": ticker.upper()}) + if data and isinstance(data, list) and len(data) > 0: + return data[0].get("peersList", []) + return [] + + +@st.cache_data(ttl=3600) +def get_ratios_for_tickers(tickers: list[str]) -> list[dict]: + """Return TTM ratios for a list of tickers (for comps table).""" + results = [] + for t in tickers: + data = _get(f"/ratios-ttm/{t}") + if data and isinstance(data, list) and len(data) > 0: + row = data[0] + row["symbol"] = t + results.append(row) + return results + + +@st.cache_data(ttl=600) +def get_company_news(ticker: str, limit: int = 20) -> list[dict]: + """Return recent news articles for a ticker.""" + data = _get("/stock_news", params={"tickers": ticker.upper(), "limit": limit}) + if data and isinstance(data, list): + return data + return [] |
