From 23675b39b8055a8568cdcf71f66482b9d0cf90a9 Mon Sep 17 00:00:00 2001 From: Tyler Date: Sat, 28 Mar 2026 23:01:14 -0700 Subject: Initial commit — Prism financial analysis dashboard MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Streamlit app with market bar, price chart, financial statements, DCF valuation engine, comparable companies, and news feed. Co-Authored-By: Claude Sonnet 4.6 --- services/fmp_service.py | 65 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 services/fmp_service.py (limited to 'services/fmp_service.py') 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 [] -- cgit v1.3-2-g0d8e