yf176 commited on
Commit ·
2f0f2ac
1
Parent(s): e414ccd
fix: remove unavailable Finnhub candle path
Browse files- README.md +1 -1
- docs/V8.0_IMPLEMENTATION_PLAN.md +5 -5
- src/polyhedra/services/finnhub.py +7 -109
- src/polyhedra/services/provider_smoke.py +5 -15
- tests/test_finnhub_service.py +8 -81
- tests/test_provider_smoke.py +16 -17
README.md
CHANGED
|
@@ -113,7 +113,7 @@ export OPENROUTER_API_KEY=sk-or-your-key
|
|
| 113 |
export POLYHEDRA_LLM_PROVIDER=openrouter
|
| 114 |
export POLYHEDRA_LLM_MODEL=nvidia/nemotron-3.5-super-vl:free
|
| 115 |
|
| 116 |
-
# Market data (Yahoo daily OHLCV
|
| 117 |
export FINNHUB_API_KEY=your-finnhub-key # https://finnhub.io/dashboard
|
| 118 |
|
| 119 |
# Optional preferred read-only options source; Yahoo remains a fallback.
|
|
|
|
| 113 |
export POLYHEDRA_LLM_PROVIDER=openrouter
|
| 114 |
export POLYHEDRA_LLM_MODEL=nvidia/nemotron-3.5-super-vl:free
|
| 115 |
|
| 116 |
+
# Market data (Yahoo daily OHLCV; Finnhub quote/news/earnings)
|
| 117 |
export FINNHUB_API_KEY=your-finnhub-key # https://finnhub.io/dashboard
|
| 118 |
|
| 119 |
# Optional preferred read-only options source; Yahoo remains a fallback.
|
docs/V8.0_IMPLEMENTATION_PLAN.md
CHANGED
|
@@ -226,17 +226,17 @@ parsing, artifact publication, baseline context, and declared output rendering.
|
|
| 226 |
Verify the agent's exact input/output model and ACL using representative data
|
| 227 |
and an explicit missing-input case. Keep existing reasoning duties intact.
|
| 228 |
|
| 229 |
-
- [x] M01 DataFetcher: retain fetch/
|
| 230 |
indicators; persist component quality and actual timestamps through its ACL.
|
| 231 |
- [x] M01a Add typed execution to the existing DataFetcher class. Reuse the
|
| 232 |
-
configured Finnhub and
|
| 233 |
normalize raw values, preserve actual OHLCV source provenance, publish one
|
| 234 |
immutable typed artifact per ticker plus the manifest, and represent provider
|
| 235 |
loss as typed availability. Keep M01 open until coordinator integration proves
|
| 236 |
this entry point is the active product path.
|
| 237 |
-
Approved 2026-10-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
- [x] M02 OptionsFlowWatcher: preserve normalized contracts and bounded sampling;
|
| 241 |
distinguish market context, candidates, and evidenced unusual flow. Retain
|
| 242 |
missing versus zero values and record unsupported flow capabilities.
|
|
|
|
| 226 |
Verify the agent's exact input/output model and ACL using representative data
|
| 227 |
and an explicit missing-input case. Keep existing reasoning duties intact.
|
| 228 |
|
| 229 |
+
- [x] M01 DataFetcher: retain fetch/normalization and computed
|
| 230 |
indicators; persist component quality and actual timestamps through its ACL.
|
| 231 |
- [x] M01a Add typed execution to the existing DataFetcher class. Reuse the
|
| 232 |
+
configured Finnhub context and Yahoo daily-price path without provider-side V8 writes;
|
| 233 |
normalize raw values, preserve actual OHLCV source provenance, publish one
|
| 234 |
immutable typed artifact per ticker plus the manifest, and represent provider
|
| 235 |
loss as typed availability. Keep M01 open until coordinator integration proves
|
| 236 |
this entry point is the active product path.
|
| 237 |
+
Approved 2026-10-24 provider scope: use Yahoo daily OHLCV; report an explicit
|
| 238 |
+
data gap if it has no valid bars. Keep Finnhub quote/news/earnings, but do not
|
| 239 |
+
call or smoke-test the inaccessible Finnhub candle endpoint.
|
| 240 |
- [x] M02 OptionsFlowWatcher: preserve normalized contracts and bounded sampling;
|
| 241 |
distinguish market context, candidates, and evidenced unusual flow. Retain
|
| 242 |
missing versus zero values and record unsupported flow capabilities.
|
src/polyhedra/services/finnhub.py
CHANGED
|
@@ -2,7 +2,7 @@
|
|
| 2 |
|
| 3 |
Free tier: 60 API calls/minute.
|
| 4 |
Technical indicators (SMA-20, EMA-20, RSI-14, MACD 12/26/9) are computed
|
| 5 |
-
locally from OHLCV candle data.
|
| 6 |
|
| 7 |
Raw data is persisted in Alpha Vantage-compatible JSON so existing quant
|
| 8 |
agents (TrendAnalyst, SentimentAnalyst, etc.) parse it without modification.
|
|
@@ -115,40 +115,12 @@ class FinnhubService:
|
|
| 115 |
ohlcv_result = await asyncio.get_event_loop().run_in_executor(
|
| 116 |
None, self._fetch_ohlcv_yfinance, ticker
|
| 117 |
)
|
| 118 |
-
ohlcv_provider: str | None = "yfinance" if ohlcv_result is not None else None
|
| 119 |
if ohlcv_result is None:
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
ohlcv_result = await self._fetch_ohlcv_finnhub(ticker)
|
| 123 |
-
if ohlcv_result is not None:
|
| 124 |
-
ohlcv_provider = "finnhub"
|
| 125 |
-
except httpx.HTTPStatusError as exc:
|
| 126 |
-
if exc.response.status_code == 403:
|
| 127 |
-
try:
|
| 128 |
-
payload = exc.response.json()
|
| 129 |
-
except ValueError:
|
| 130 |
-
payload = None
|
| 131 |
-
reason = (payload.get("error") or payload.get("message")) if isinstance(payload, dict) else None
|
| 132 |
-
safe_error = sanitize_error_text(
|
| 133 |
-
reason or "The configured key cannot access /stock/candle."
|
| 134 |
-
)
|
| 135 |
-
combined["_CANDLE_error"] = f"Finnhub candle access denied (HTTP 403): {safe_error}"
|
| 136 |
-
else:
|
| 137 |
-
combined["_CANDLE_error"] = sanitize_error_text(exc)
|
| 138 |
-
logger.warning("Finnhub OHLCV fallback for %s failed: %s", ticker, combined["_CANDLE_error"])
|
| 139 |
-
except Exception as exc:
|
| 140 |
-
safe_error = sanitize_error_text(exc)
|
| 141 |
-
logger.warning("Finnhub OHLCV fallback for %s failed: %s", ticker, safe_error)
|
| 142 |
-
combined["_CANDLE_error"] = safe_error
|
| 143 |
-
else:
|
| 144 |
-
combined["_CANDLE_error"] = "Yahoo returned no valid OHLCV; FINNHUB_API_KEY is not configured"
|
| 145 |
-
if ohlcv_result is None and "_CANDLE_error" not in combined:
|
| 146 |
-
combined["_CANDLE_error"] = "Yahoo and Finnhub returned no valid OHLCV"
|
| 147 |
-
|
| 148 |
-
# FR-5.2: check return value; None means the fallback also failed.
|
| 149 |
if ohlcv_result is not None:
|
| 150 |
ohlcv, sorted_dates, closes = ohlcv_result
|
| 151 |
-
combined["_OHLCV_provider"] =
|
| 152 |
combined["TIME_SERIES_DAILY_ADJUSTED"] = ohlcv
|
| 153 |
combined["SMA"] = self._compute_sma(closes, sorted_dates, period=20)
|
| 154 |
combined["EMA"] = self._compute_ema(closes, sorted_dates, period=20)
|
|
@@ -156,7 +128,7 @@ class FinnhubService:
|
|
| 156 |
combined["MACD"] = self._compute_macd(closes, sorted_dates)
|
| 157 |
else:
|
| 158 |
logger.warning(
|
| 159 |
-
"
|
| 160 |
)
|
| 161 |
|
| 162 |
# --- Company news (last 30 days) ---
|
|
@@ -243,36 +215,6 @@ class FinnhubService:
|
|
| 243 |
response.raise_for_status()
|
| 244 |
return response.json()
|
| 245 |
|
| 246 |
-
# ------------------------------------------------------------------ #
|
| 247 |
-
# Finnhub OHLCV
|
| 248 |
-
# ------------------------------------------------------------------ #
|
| 249 |
-
|
| 250 |
-
async def _fetch_ohlcv_finnhub(
|
| 251 |
-
self,
|
| 252 |
-
ticker: str,
|
| 253 |
-
) -> tuple[dict, list[str], list[float]] | None:
|
| 254 |
-
"""Fetch daily OHLCV via Finnhub /stock/candle."""
|
| 255 |
-
end = datetime.now(tz=timezone.utc)
|
| 256 |
-
start = end - timedelta(days=_CANDLE_DAYS)
|
| 257 |
-
candle = await self._call("/stock/candle", {
|
| 258 |
-
"symbol": ticker.upper(),
|
| 259 |
-
"resolution": "D",
|
| 260 |
-
"from": str(int(start.timestamp())),
|
| 261 |
-
"to": str(int(end.timestamp())),
|
| 262 |
-
})
|
| 263 |
-
if not isinstance(candle, dict) or candle.get("s") != "ok":
|
| 264 |
-
status = candle.get("s", "unknown") if isinstance(candle, dict) else "invalid"
|
| 265 |
-
logger.warning("Finnhub candle returned status %s for %s", status, ticker)
|
| 266 |
-
return None
|
| 267 |
-
ohlcv = self._candle_to_av(candle)
|
| 268 |
-
series = ohlcv.get("Time Series (Daily)", {})
|
| 269 |
-
if not series:
|
| 270 |
-
logger.warning("Finnhub candle returned empty series for %s", ticker)
|
| 271 |
-
return None
|
| 272 |
-
sorted_dates = sorted(series.keys())
|
| 273 |
-
closes = [float(series[d]["4. close"]) for d in sorted_dates]
|
| 274 |
-
return ohlcv, sorted_dates, closes
|
| 275 |
-
|
| 276 |
# ------------------------------------------------------------------ #
|
| 277 |
# yfinance OHLCV (free, no API key, runs in thread executor)
|
| 278 |
# ------------------------------------------------------------------ #
|
|
@@ -283,8 +225,7 @@ class FinnhubService:
|
|
| 283 |
) -> tuple[dict, list[str], list[float]] | None:
|
| 284 |
"""Download daily OHLCV via yfinance and return AV-compatible dict.
|
| 285 |
|
| 286 |
-
|
| 287 |
-
a None return as a signal that both Finnhub and yfinance are unavailable.
|
| 288 |
"""
|
| 289 |
try:
|
| 290 |
import yfinance as yf
|
|
@@ -336,52 +277,9 @@ class FinnhubService:
|
|
| 336 |
return None
|
| 337 |
|
| 338 |
# ------------------------------------------------------------------ #
|
| 339 |
-
# Format converters (Finnhub → Alpha Vantage-compatible keys)
|
| 340 |
# ------------------------------------------------------------------ #
|
| 341 |
|
| 342 |
-
@staticmethod
|
| 343 |
-
def _candle_to_av(candle: dict) -> dict:
|
| 344 |
-
"""Convert Finnhub /stock/candle response to TIME_SERIES_DAILY_ADJUSTED format."""
|
| 345 |
-
ts_list = candle.get("t", [])
|
| 346 |
-
opens = candle.get("o", [])
|
| 347 |
-
highs = candle.get("h", [])
|
| 348 |
-
lows = candle.get("l", [])
|
| 349 |
-
closes = candle.get("c", [])
|
| 350 |
-
volumes = candle.get("v", [])
|
| 351 |
-
series: dict[str, dict] = {}
|
| 352 |
-
excluded_rows: list[dict[str, Any]] = []
|
| 353 |
-
for i, ts in enumerate(ts_list):
|
| 354 |
-
d = datetime.fromtimestamp(ts, tz=timezone.utc).strftime("%Y-%m-%d")
|
| 355 |
-
row = {
|
| 356 |
-
"1. open": opens[i] if i < len(opens) else None,
|
| 357 |
-
"2. high": highs[i] if i < len(highs) else None,
|
| 358 |
-
"3. low": lows[i] if i < len(lows) else None,
|
| 359 |
-
"4. close": closes[i] if i < len(closes) else None,
|
| 360 |
-
"5. adjusted close": closes[i] if i < len(closes) else None,
|
| 361 |
-
"6. volume": volumes[i] if i < len(volumes) else None,
|
| 362 |
-
}
|
| 363 |
-
reason = _invalid_ohlcv_reason(row)
|
| 364 |
-
if reason:
|
| 365 |
-
excluded_rows.append({
|
| 366 |
-
"date": d,
|
| 367 |
-
"reason": reason,
|
| 368 |
-
"provider_values": {key: str(value) for key, value in row.items()},
|
| 369 |
-
})
|
| 370 |
-
continue
|
| 371 |
-
series[d] = {
|
| 372 |
-
"1. open": str(row["1. open"]),
|
| 373 |
-
"2. high": str(row["2. high"]),
|
| 374 |
-
"3. low": str(row["3. low"]),
|
| 375 |
-
"4. close": str(row["4. close"]),
|
| 376 |
-
"5. adjusted close": str(row["5. adjusted close"]),
|
| 377 |
-
"6. volume": str(int(float(row["6. volume"]))),
|
| 378 |
-
}
|
| 379 |
-
payload: dict[str, Any] = {"Time Series (Daily)": series}
|
| 380 |
-
if excluded_rows:
|
| 381 |
-
payload["_data_quality"] = _ohlcv_quality_payload(excluded_rows)
|
| 382 |
-
return payload
|
| 383 |
-
|
| 384 |
-
|
| 385 |
@staticmethod
|
| 386 |
def _news_to_av(news: list[dict]) -> dict:
|
| 387 |
"""Convert Finnhub /company-news list to NEWS_SENTIMENT feed format."""
|
|
|
|
| 2 |
|
| 3 |
Free tier: 60 API calls/minute.
|
| 4 |
Technical indicators (SMA-20, EMA-20, RSI-14, MACD 12/26/9) are computed
|
| 5 |
+
locally from Yahoo OHLCV candle data.
|
| 6 |
|
| 7 |
Raw data is persisted in Alpha Vantage-compatible JSON so existing quant
|
| 8 |
agents (TrendAnalyst, SentimentAnalyst, etc.) parse it without modification.
|
|
|
|
| 115 |
ohlcv_result = await asyncio.get_event_loop().run_in_executor(
|
| 116 |
None, self._fetch_ohlcv_yfinance, ticker
|
| 117 |
)
|
|
|
|
| 118 |
if ohlcv_result is None:
|
| 119 |
+
combined["_CANDLE_error"] = "Yahoo returned no valid daily OHLCV"
|
| 120 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
if ohlcv_result is not None:
|
| 122 |
ohlcv, sorted_dates, closes = ohlcv_result
|
| 123 |
+
combined["_OHLCV_provider"] = "yfinance"
|
| 124 |
combined["TIME_SERIES_DAILY_ADJUSTED"] = ohlcv
|
| 125 |
combined["SMA"] = self._compute_sma(closes, sorted_dates, period=20)
|
| 126 |
combined["EMA"] = self._compute_ema(closes, sorted_dates, period=20)
|
|
|
|
| 128 |
combined["MACD"] = self._compute_macd(closes, sorted_dates)
|
| 129 |
else:
|
| 130 |
logger.warning(
|
| 131 |
+
"Yahoo returned no valid OHLCV for %s — marking ticker as failed", ticker
|
| 132 |
)
|
| 133 |
|
| 134 |
# --- Company news (last 30 days) ---
|
|
|
|
| 215 |
response.raise_for_status()
|
| 216 |
return response.json()
|
| 217 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 218 |
# ------------------------------------------------------------------ #
|
| 219 |
# yfinance OHLCV (free, no API key, runs in thread executor)
|
| 220 |
# ------------------------------------------------------------------ #
|
|
|
|
| 225 |
) -> tuple[dict, list[str], list[float]] | None:
|
| 226 |
"""Download daily OHLCV via yfinance and return AV-compatible dict.
|
| 227 |
|
| 228 |
+
Returns None on failure so callers can report an explicit OHLCV gap.
|
|
|
|
| 229 |
"""
|
| 230 |
try:
|
| 231 |
import yfinance as yf
|
|
|
|
| 277 |
return None
|
| 278 |
|
| 279 |
# ------------------------------------------------------------------ #
|
| 280 |
+
# Format converters (Finnhub context → Alpha Vantage-compatible keys)
|
| 281 |
# ------------------------------------------------------------------ #
|
| 282 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 283 |
@staticmethod
|
| 284 |
def _news_to_av(news: list[dict]) -> dict:
|
| 285 |
"""Convert Finnhub /company-news list to NEWS_SENTIMENT feed format."""
|
src/polyhedra/services/provider_smoke.py
CHANGED
|
@@ -88,7 +88,7 @@ async def _finnhub_smoke_call(service: FinnhubService, path: str, params: dict[s
|
|
| 88 |
|
| 89 |
|
| 90 |
async def _probe_finnhub(ticker: str) -> list[dict[str, Any]]:
|
| 91 |
-
endpoints = ("quote", "
|
| 92 |
if not os.getenv("FINNHUB_API_KEY"):
|
| 93 |
return [
|
| 94 |
_result(f"Finnhub {endpoint}", "not_configured", "FINNHUB_API_KEY is missing.", 0)
|
|
@@ -100,12 +100,6 @@ async def _probe_finnhub(ticker: str) -> list[dict[str, Any]]:
|
|
| 100 |
today = now.date()
|
| 101 |
checks = (
|
| 102 |
("quote", "/quote", {"symbol": ticker}, lambda data: bool(isinstance(data, dict) and data.get("c"))),
|
| 103 |
-
("candle", "/stock/candle", {
|
| 104 |
-
"symbol": ticker,
|
| 105 |
-
"resolution": "D",
|
| 106 |
-
"from": str(int((now - timedelta(days=90)).timestamp())),
|
| 107 |
-
"to": str(int(now.timestamp())),
|
| 108 |
-
}, lambda data: bool(isinstance(data, dict) and data.get("s") == "ok" and data.get("c"))),
|
| 109 |
("company-news", "/company-news", {
|
| 110 |
"symbol": ticker,
|
| 111 |
"from": str(today - timedelta(days=30)),
|
|
@@ -191,20 +185,16 @@ async def run_provider_smoke(ticker: str) -> dict[str, Any]:
|
|
| 191 |
_result("Yahoo options", "not_applicable", "MarketData worked; Yahoo fallback was not called.", 0)
|
| 192 |
if marketdata["status"] == "ok" else await _probe_yahoo_options(ticker)
|
| 193 |
)
|
| 194 |
-
candle = next(check for check in finnhub if check["name"] == "Finnhub candle")
|
| 195 |
yahoo_price = yahoo[0]
|
| 196 |
-
price_source = yahoo_price if yahoo_price["status"] == "ok" else candle
|
| 197 |
if yahoo_price["status"] == "ok":
|
| 198 |
-
price_detail = "Yahoo daily prices are usable
|
| 199 |
-
elif candle["status"] == "ok":
|
| 200 |
-
price_detail = "Finnhub daily candles are usable as fallback; Yahoo price history failed (see diagnostic below)."
|
| 201 |
else:
|
| 202 |
-
price_detail = "No usable daily prices; see Yahoo price
|
| 203 |
price_coverage = _result(
|
| 204 |
"Daily prices used by DataFetcher",
|
| 205 |
-
"ok" if
|
| 206 |
price_detail,
|
| 207 |
-
yahoo_price["elapsed_ms"]
|
| 208 |
)
|
| 209 |
options_source = marketdata if marketdata["status"] == "ok" else yahoo_options
|
| 210 |
if marketdata["status"] == "ok":
|
|
|
|
| 88 |
|
| 89 |
|
| 90 |
async def _probe_finnhub(ticker: str) -> list[dict[str, Any]]:
|
| 91 |
+
endpoints = ("quote", "company-news", "stock/earnings")
|
| 92 |
if not os.getenv("FINNHUB_API_KEY"):
|
| 93 |
return [
|
| 94 |
_result(f"Finnhub {endpoint}", "not_configured", "FINNHUB_API_KEY is missing.", 0)
|
|
|
|
| 100 |
today = now.date()
|
| 101 |
checks = (
|
| 102 |
("quote", "/quote", {"symbol": ticker}, lambda data: bool(isinstance(data, dict) and data.get("c"))),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
("company-news", "/company-news", {
|
| 104 |
"symbol": ticker,
|
| 105 |
"from": str(today - timedelta(days=30)),
|
|
|
|
| 185 |
_result("Yahoo options", "not_applicable", "MarketData worked; Yahoo fallback was not called.", 0)
|
| 186 |
if marketdata["status"] == "ok" else await _probe_yahoo_options(ticker)
|
| 187 |
)
|
|
|
|
| 188 |
yahoo_price = yahoo[0]
|
|
|
|
| 189 |
if yahoo_price["status"] == "ok":
|
| 190 |
+
price_detail = "Yahoo daily prices are usable."
|
|
|
|
|
|
|
| 191 |
else:
|
| 192 |
+
price_detail = "No usable daily prices; see Yahoo price history diagnostic below."
|
| 193 |
price_coverage = _result(
|
| 194 |
"Daily prices used by DataFetcher",
|
| 195 |
+
"ok" if yahoo_price["status"] == "ok" else "failed",
|
| 196 |
price_detail,
|
| 197 |
+
yahoo_price["elapsed_ms"],
|
| 198 |
)
|
| 199 |
options_source = marketdata if marketdata["status"] == "ok" else yahoo_options
|
| 200 |
if marketdata["status"] == "ok":
|
tests/test_finnhub_service.py
CHANGED
|
@@ -3,7 +3,6 @@ from __future__ import annotations
|
|
| 3 |
import logging
|
| 4 |
from typing import Any
|
| 5 |
|
| 6 |
-
import httpx
|
| 7 |
import pytest
|
| 8 |
|
| 9 |
from polyhedra.agents.quant.datafetcher import _build_ticker_summary
|
|
@@ -11,18 +10,15 @@ from polyhedra.services.finnhub import FinnhubService
|
|
| 11 |
|
| 12 |
|
| 13 |
class _FakeFinnhub(FinnhubService):
|
| 14 |
-
def __init__(self,
|
| 15 |
super().__init__()
|
| 16 |
self._api_key = "test-key"
|
| 17 |
-
self.candle = candle
|
| 18 |
self.yfinance_result = yfinance_result
|
| 19 |
self.paths: list[str] = []
|
| 20 |
self.used_yfinance = False
|
| 21 |
|
| 22 |
async def _call(self, path: str, params: dict[str, str]) -> Any:
|
| 23 |
self.paths.append(path)
|
| 24 |
-
if path == "/stock/candle":
|
| 25 |
-
return self.candle
|
| 26 |
if path == "/company-news":
|
| 27 |
return []
|
| 28 |
if path == "/stock/earnings":
|
|
@@ -36,7 +32,7 @@ class _FakeFinnhub(FinnhubService):
|
|
| 36 |
|
| 37 |
class _NoKeyFinnhub(_FakeFinnhub):
|
| 38 |
def __init__(self, yfinance_result: Any) -> None:
|
| 39 |
-
super().__init__(
|
| 40 |
self._api_key = ""
|
| 41 |
|
| 42 |
async def _call(self, path: str, params: dict[str, str]) -> Any:
|
|
@@ -52,18 +48,6 @@ class _CredentialErrorFinnhub(_FakeFinnhub):
|
|
| 52 |
)
|
| 53 |
|
| 54 |
|
| 55 |
-
def _candle_payload() -> dict[str, Any]:
|
| 56 |
-
return {
|
| 57 |
-
"s": "ok",
|
| 58 |
-
"t": [1788048000, 1788134400],
|
| 59 |
-
"o": [650.0, 651.0],
|
| 60 |
-
"h": [655.0, 656.0],
|
| 61 |
-
"l": [648.0, 649.0],
|
| 62 |
-
"c": [654.0, 655.0],
|
| 63 |
-
"v": [1000000, 1100000],
|
| 64 |
-
}
|
| 65 |
-
|
| 66 |
-
|
| 67 |
def _yfinance_result():
|
| 68 |
return (
|
| 69 |
{
|
|
@@ -85,7 +69,7 @@ def _yfinance_result():
|
|
| 85 |
|
| 86 |
@pytest.mark.asyncio
|
| 87 |
async def test_yahoo_daily_prices_are_primary_ohlcv_source(tmp_path):
|
| 88 |
-
service = _FakeFinnhub(
|
| 89 |
|
| 90 |
data = await service.fetch_ticker_data("SPY", tmp_path)
|
| 91 |
|
|
@@ -96,52 +80,16 @@ async def test_yahoo_daily_prices_are_primary_ohlcv_source(tmp_path):
|
|
| 96 |
|
| 97 |
|
| 98 |
@pytest.mark.asyncio
|
| 99 |
-
async def
|
| 100 |
-
service = _FakeFinnhub(
|
| 101 |
-
|
| 102 |
-
data = await service.fetch_ticker_data("SPY", tmp_path)
|
| 103 |
-
|
| 104 |
-
assert service.paths[0] == "/stock/candle"
|
| 105 |
-
assert service.used_yfinance is True
|
| 106 |
-
assert data["_OHLCV_provider"] == "finnhub"
|
| 107 |
-
assert data["TIME_SERIES_DAILY_ADJUSTED"]["Time Series (Daily)"]
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
@pytest.mark.asyncio
|
| 111 |
-
async def test_missing_both_daily_price_sources_stays_unavailable(tmp_path):
|
| 112 |
-
service = _FakeFinnhub({"s": "no_data"})
|
| 113 |
|
| 114 |
data = await service.fetch_ticker_data("SPY", tmp_path)
|
| 115 |
|
| 116 |
assert service.used_yfinance is True
|
| 117 |
-
assert "/stock/candle" in service.paths
|
| 118 |
assert "_OHLCV_provider" not in data
|
| 119 |
assert "TIME_SERIES_DAILY_ADJUSTED" not in data
|
| 120 |
-
assert "
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
@pytest.mark.asyncio
|
| 124 |
-
async def test_finnhub_fallback_explains_access_denial_without_exposing_key(tmp_path):
|
| 125 |
-
service = _FakeFinnhub({})
|
| 126 |
-
|
| 127 |
-
async def deny_candle(path, params):
|
| 128 |
-
if path == "/stock/candle":
|
| 129 |
-
request = httpx.Request("GET", "https://finnhub.io/api/v1/stock/candle")
|
| 130 |
-
response = httpx.Response(
|
| 131 |
-
403,
|
| 132 |
-
json={"error": "You don't have access to this resource. token=secret"},
|
| 133 |
-
request=request,
|
| 134 |
-
)
|
| 135 |
-
raise httpx.HTTPStatusError("Forbidden", request=request, response=response)
|
| 136 |
-
return []
|
| 137 |
-
|
| 138 |
-
service._call = deny_candle
|
| 139 |
-
data = await service.fetch_ticker_data("SPY", tmp_path)
|
| 140 |
-
|
| 141 |
-
assert "Finnhub candle access denied (HTTP 403)" in data["_CANDLE_error"]
|
| 142 |
-
assert "don't have access" in data["_CANDLE_error"]
|
| 143 |
-
assert "secret" not in data["_CANDLE_error"]
|
| 144 |
-
assert "TIME_SERIES_DAILY_ADJUSTED" not in data
|
| 145 |
|
| 146 |
|
| 147 |
@pytest.mark.asyncio
|
|
@@ -163,7 +111,7 @@ async def test_provider_errors_redact_credentials_from_logs_and_artifacts(
|
|
| 163 |
tmp_path,
|
| 164 |
caplog,
|
| 165 |
):
|
| 166 |
-
service = _CredentialErrorFinnhub(
|
| 167 |
caplog.set_level(logging.WARNING)
|
| 168 |
|
| 169 |
data = await service.fetch_ticker_data("SPY", tmp_path)
|
|
@@ -184,27 +132,6 @@ def test_app_suppresses_httpx_info_request_logs():
|
|
| 184 |
assert logging.getLogger("httpx").level >= logging.WARNING
|
| 185 |
|
| 186 |
|
| 187 |
-
def test_candle_converter_excludes_nan_price_rows() -> None:
|
| 188 |
-
payload = FinnhubService._candle_to_av({
|
| 189 |
-
"s": "ok",
|
| 190 |
-
"t": [1788048000, 1788134400],
|
| 191 |
-
"o": [650.0, float("nan")],
|
| 192 |
-
"h": [655.0, float("nan")],
|
| 193 |
-
"l": [648.0, float("nan")],
|
| 194 |
-
"c": [654.0, float("nan")],
|
| 195 |
-
"v": [1000000, 42648446],
|
| 196 |
-
})
|
| 197 |
-
|
| 198 |
-
series = payload["Time Series (Daily)"]
|
| 199 |
-
|
| 200 |
-
assert len(series) == 1
|
| 201 |
-
assert next(iter(series.values()))["4. close"] == "654.0"
|
| 202 |
-
assert payload["_data_quality"]["status"] == "provider_partial_data"
|
| 203 |
-
assert payload["_data_quality"]["excluded_rows"][0]["reason"].startswith(
|
| 204 |
-
"invalid price fields"
|
| 205 |
-
)
|
| 206 |
-
|
| 207 |
-
|
| 208 |
def test_datafetcher_summary_reports_excluded_partial_ohlcv_rows() -> None:
|
| 209 |
summary = _build_ticker_summary(
|
| 210 |
"SPY",
|
|
|
|
| 3 |
import logging
|
| 4 |
from typing import Any
|
| 5 |
|
|
|
|
| 6 |
import pytest
|
| 7 |
|
| 8 |
from polyhedra.agents.quant.datafetcher import _build_ticker_summary
|
|
|
|
| 10 |
|
| 11 |
|
| 12 |
class _FakeFinnhub(FinnhubService):
|
| 13 |
+
def __init__(self, yfinance_result: Any = None) -> None:
|
| 14 |
super().__init__()
|
| 15 |
self._api_key = "test-key"
|
|
|
|
| 16 |
self.yfinance_result = yfinance_result
|
| 17 |
self.paths: list[str] = []
|
| 18 |
self.used_yfinance = False
|
| 19 |
|
| 20 |
async def _call(self, path: str, params: dict[str, str]) -> Any:
|
| 21 |
self.paths.append(path)
|
|
|
|
|
|
|
| 22 |
if path == "/company-news":
|
| 23 |
return []
|
| 24 |
if path == "/stock/earnings":
|
|
|
|
| 32 |
|
| 33 |
class _NoKeyFinnhub(_FakeFinnhub):
|
| 34 |
def __init__(self, yfinance_result: Any) -> None:
|
| 35 |
+
super().__init__(yfinance_result=yfinance_result)
|
| 36 |
self._api_key = ""
|
| 37 |
|
| 38 |
async def _call(self, path: str, params: dict[str, str]) -> Any:
|
|
|
|
| 48 |
)
|
| 49 |
|
| 50 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
def _yfinance_result():
|
| 52 |
return (
|
| 53 |
{
|
|
|
|
| 69 |
|
| 70 |
@pytest.mark.asyncio
|
| 71 |
async def test_yahoo_daily_prices_are_primary_ohlcv_source(tmp_path):
|
| 72 |
+
service = _FakeFinnhub(yfinance_result=_yfinance_result())
|
| 73 |
|
| 74 |
data = await service.fetch_ticker_data("SPY", tmp_path)
|
| 75 |
|
|
|
|
| 80 |
|
| 81 |
|
| 82 |
@pytest.mark.asyncio
|
| 83 |
+
async def test_missing_yahoo_daily_prices_stays_unavailable_without_finnhub_candles(tmp_path):
|
| 84 |
+
service = _FakeFinnhub()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
|
| 86 |
data = await service.fetch_ticker_data("SPY", tmp_path)
|
| 87 |
|
| 88 |
assert service.used_yfinance is True
|
| 89 |
+
assert "/stock/candle" not in service.paths
|
| 90 |
assert "_OHLCV_provider" not in data
|
| 91 |
assert "TIME_SERIES_DAILY_ADJUSTED" not in data
|
| 92 |
+
assert data["_CANDLE_error"] == "Yahoo returned no valid daily OHLCV"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
|
| 94 |
|
| 95 |
@pytest.mark.asyncio
|
|
|
|
| 111 |
tmp_path,
|
| 112 |
caplog,
|
| 113 |
):
|
| 114 |
+
service = _CredentialErrorFinnhub(yfinance_result=_yfinance_result())
|
| 115 |
caplog.set_level(logging.WARNING)
|
| 116 |
|
| 117 |
data = await service.fetch_ticker_data("SPY", tmp_path)
|
|
|
|
| 132 |
assert logging.getLogger("httpx").level >= logging.WARNING
|
| 133 |
|
| 134 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 135 |
def test_datafetcher_summary_reports_excluded_partial_ohlcv_rows() -> None:
|
| 136 |
summary = _build_ticker_summary(
|
| 137 |
"SPY",
|
tests/test_provider_smoke.py
CHANGED
|
@@ -15,7 +15,7 @@ async def test_provider_smoke_keeps_each_provider_result(monkeypatch) -> None:
|
|
| 15 |
return {"name": "LLM", "status": "failed", "detail": "connection closed", "elapsed_ms": 9}
|
| 16 |
|
| 17 |
async def finnhub(_ticker):
|
| 18 |
-
return [{"name": "Finnhub
|
| 19 |
|
| 20 |
async def yahoo(_ticker, *, options):
|
| 21 |
assert options is False
|
|
@@ -40,7 +40,7 @@ async def test_provider_smoke_keeps_each_provider_result(monkeypatch) -> None:
|
|
| 40 |
"failed", "ok", "ok", "failed", "ok", "ok", "not_applicable",
|
| 41 |
]
|
| 42 |
assert result["checks"][1]["elapsed_ms"] == 3
|
| 43 |
-
assert "
|
| 44 |
assert result["checks"][2]["elapsed_ms"] == 4
|
| 45 |
assert "HTTP 403" in result["checks"][3]["detail"]
|
| 46 |
assert "Yahoo fallback was not called" in result["checks"][6]["detail"]
|
|
@@ -54,17 +54,15 @@ async def test_finnhub_smoke_reports_each_endpoint_independently(monkeypatch) ->
|
|
| 54 |
async def call(self, path, params):
|
| 55 |
if path == "/quote":
|
| 56 |
return {"c": 100}
|
| 57 |
-
if path == "/stock/candle":
|
| 58 |
-
raise RuntimeError("failed token=secret")
|
| 59 |
if path == "/company-news":
|
| 60 |
-
|
| 61 |
return [{"actual": 1}]
|
| 62 |
|
| 63 |
monkeypatch.setattr(provider_smoke.FinnhubService, "_call", call)
|
| 64 |
|
| 65 |
checks = await provider_smoke._probe_finnhub("SPY")
|
| 66 |
|
| 67 |
-
assert [check["status"] for check in checks] == ["ok", "failed", "
|
| 68 |
assert "secret" not in checks[1]["detail"]
|
| 69 |
assert "token=<redacted>" in checks[1]["detail"]
|
| 70 |
|
|
@@ -89,8 +87,8 @@ async def test_finnhub_smoke_exposes_redacted_403_reason(monkeypatch) -> None:
|
|
| 89 |
monkeypatch.setenv("FINNHUB_API_KEY", "secret")
|
| 90 |
|
| 91 |
async def call(self, path, params):
|
| 92 |
-
if path == "/
|
| 93 |
-
request = httpx.Request("GET", "https://finnhub.io/api/v1/
|
| 94 |
response = httpx.Response(
|
| 95 |
403,
|
| 96 |
json={"error": "You don't have access to this resource. token=secret"},
|
|
@@ -103,9 +101,9 @@ async def test_finnhub_smoke_exposes_redacted_403_reason(monkeypatch) -> None:
|
|
| 103 |
|
| 104 |
checks = await provider_smoke._probe_finnhub("SPY")
|
| 105 |
|
| 106 |
-
assert checks[
|
| 107 |
-
assert "don't have access" in checks[
|
| 108 |
-
assert "secret" not in checks[
|
| 109 |
|
| 110 |
|
| 111 |
@pytest.mark.asyncio
|
|
@@ -139,7 +137,7 @@ async def test_provider_smoke_uses_yahoo_after_marketdata_failure(monkeypatch) -
|
|
| 139 |
return {"name": "LLM", "status": "ok", "detail": "ready", "elapsed_ms": 1}
|
| 140 |
|
| 141 |
async def finnhub(_ticker):
|
| 142 |
-
return [{"name": "Finnhub
|
| 143 |
|
| 144 |
async def yahoo(_ticker, *, options):
|
| 145 |
assert options is False
|
|
@@ -168,12 +166,12 @@ async def test_provider_smoke_uses_yahoo_after_marketdata_failure(monkeypatch) -
|
|
| 168 |
|
| 169 |
|
| 170 |
@pytest.mark.asyncio
|
| 171 |
-
async def
|
| 172 |
async def llm():
|
| 173 |
return {"name": "LLM", "status": "ok", "detail": "ready", "elapsed_ms": 1}
|
| 174 |
|
| 175 |
async def finnhub(_ticker):
|
| 176 |
-
return [{"name": "Finnhub
|
| 177 |
|
| 178 |
async def yahoo(_ticker, *, options):
|
| 179 |
assert options is False
|
|
@@ -193,9 +191,10 @@ async def test_provider_smoke_marks_finnhub_as_daily_price_fallback(monkeypatch)
|
|
| 193 |
|
| 194 |
checks = (await provider_smoke.run_provider_smoke("SPY"))["checks"]
|
| 195 |
|
| 196 |
-
assert checks[1]["status"] == "
|
| 197 |
-
assert "
|
| 198 |
-
assert checks[1]["elapsed_ms"] ==
|
|
|
|
| 199 |
|
| 200 |
|
| 201 |
@pytest.mark.asyncio
|
|
|
|
| 15 |
return {"name": "LLM", "status": "failed", "detail": "connection closed", "elapsed_ms": 9}
|
| 16 |
|
| 17 |
async def finnhub(_ticker):
|
| 18 |
+
return [{"name": "Finnhub quote", "status": "failed", "detail": "HTTP 403", "elapsed_ms": 2}]
|
| 19 |
|
| 20 |
async def yahoo(_ticker, *, options):
|
| 21 |
assert options is False
|
|
|
|
| 40 |
"failed", "ok", "ok", "failed", "ok", "ok", "not_applicable",
|
| 41 |
]
|
| 42 |
assert result["checks"][1]["elapsed_ms"] == 3
|
| 43 |
+
assert "Yahoo daily prices" in result["checks"][1]["detail"]
|
| 44 |
assert result["checks"][2]["elapsed_ms"] == 4
|
| 45 |
assert "HTTP 403" in result["checks"][3]["detail"]
|
| 46 |
assert "Yahoo fallback was not called" in result["checks"][6]["detail"]
|
|
|
|
| 54 |
async def call(self, path, params):
|
| 55 |
if path == "/quote":
|
| 56 |
return {"c": 100}
|
|
|
|
|
|
|
| 57 |
if path == "/company-news":
|
| 58 |
+
raise RuntimeError("failed token=secret")
|
| 59 |
return [{"actual": 1}]
|
| 60 |
|
| 61 |
monkeypatch.setattr(provider_smoke.FinnhubService, "_call", call)
|
| 62 |
|
| 63 |
checks = await provider_smoke._probe_finnhub("SPY")
|
| 64 |
|
| 65 |
+
assert [check["status"] for check in checks] == ["ok", "failed", "ok"]
|
| 66 |
assert "secret" not in checks[1]["detail"]
|
| 67 |
assert "token=<redacted>" in checks[1]["detail"]
|
| 68 |
|
|
|
|
| 87 |
monkeypatch.setenv("FINNHUB_API_KEY", "secret")
|
| 88 |
|
| 89 |
async def call(self, path, params):
|
| 90 |
+
if path == "/quote":
|
| 91 |
+
request = httpx.Request("GET", "https://finnhub.io/api/v1/quote")
|
| 92 |
response = httpx.Response(
|
| 93 |
403,
|
| 94 |
json={"error": "You don't have access to this resource. token=secret"},
|
|
|
|
| 101 |
|
| 102 |
checks = await provider_smoke._probe_finnhub("SPY")
|
| 103 |
|
| 104 |
+
assert checks[0]["status"] == "access_denied"
|
| 105 |
+
assert "don't have access" in checks[0]["detail"]
|
| 106 |
+
assert "secret" not in checks[0]["detail"]
|
| 107 |
|
| 108 |
|
| 109 |
@pytest.mark.asyncio
|
|
|
|
| 137 |
return {"name": "LLM", "status": "ok", "detail": "ready", "elapsed_ms": 1}
|
| 138 |
|
| 139 |
async def finnhub(_ticker):
|
| 140 |
+
return [{"name": "Finnhub quote", "status": "ok", "detail": "quote", "elapsed_ms": 1}]
|
| 141 |
|
| 142 |
async def yahoo(_ticker, *, options):
|
| 143 |
assert options is False
|
|
|
|
| 166 |
|
| 167 |
|
| 168 |
@pytest.mark.asyncio
|
| 169 |
+
async def test_provider_smoke_marks_missing_yahoo_daily_prices_unavailable(monkeypatch) -> None:
|
| 170 |
async def llm():
|
| 171 |
return {"name": "LLM", "status": "ok", "detail": "ready", "elapsed_ms": 1}
|
| 172 |
|
| 173 |
async def finnhub(_ticker):
|
| 174 |
+
return [{"name": "Finnhub quote", "status": "ok", "detail": "quote", "elapsed_ms": 2}]
|
| 175 |
|
| 176 |
async def yahoo(_ticker, *, options):
|
| 177 |
assert options is False
|
|
|
|
| 191 |
|
| 192 |
checks = (await provider_smoke.run_provider_smoke("SPY"))["checks"]
|
| 193 |
|
| 194 |
+
assert checks[1]["status"] == "failed"
|
| 195 |
+
assert "No usable daily prices" in checks[1]["detail"]
|
| 196 |
+
assert checks[1]["elapsed_ms"] == 3
|
| 197 |
+
assert all(check["name"] != "Finnhub candle" for check in checks)
|
| 198 |
|
| 199 |
|
| 200 |
@pytest.mark.asyncio
|