1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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 []
|