yf176 commited on
Commit
a53b2a8
·
1 Parent(s): 76b8af7

fix: replace Finnhub candle (403) with yfinance for OHLCV data

Browse files

Finnhub free tier returns 403 Forbidden on /stock/candle for ETFs like GLD.
Switch OHLCV source to yfinance (free, no API key) while keeping Finnhub
for news sentiment and earnings which both work on the free tier.

- Add yfinance>=0.2.0 to pyproject.toml dependencies
- FinnhubService._fetch_ohlcv_yfinance: download 90-day daily OHLCV,
return AV-compatible dict (TIME_SERIES_DAILY_ADJUSTED format)
- Run yfinance.download() in thread executor for async safety
- Keep all local SMA/EMA/RSI/MACD indicator computations unchanged

Files changed (2) hide show
  1. pyproject.toml +4 -2
  2. src/polyhedra/services/finnhub.py +34 -19
pyproject.toml CHANGED
@@ -34,8 +34,10 @@ dependencies = [
34
  "aiohttp>=3.9.0",
35
  "redis>=5.0.0",
36
  # v4.0 additions — quant team / paper trading
37
- # Required env vars: FINNHUB_API_KEY (free tier: 60 calls/min — https://finnhub.io/dashboard)
38
- # Optional env vars: POLYHEDRA_QUANT_WATCHLIST (comma-separated tickers, default: AAPL,MSFT,NVDA,GOOGL)
 
 
39
  ]
40
 
41
  [project.optional-dependencies]
 
34
  "aiohttp>=3.9.0",
35
  "redis>=5.0.0",
36
  # v4.0 additions — quant team / paper trading
37
+ # OHLCV: yfinance (free, no key). News/earnings: Finnhub free tier.
38
+ # Required env vars: FINNHUB_API_KEY — https://finnhub.io/dashboard
39
+ # Optional env vars: POLYHEDRA_QUANT_WATCHLIST (comma-separated tickers)
40
+ "yfinance>=0.2.0",
41
  ]
42
 
43
  [project.optional-dependencies]
src/polyhedra/services/finnhub.py CHANGED
@@ -93,32 +93,18 @@ class FinnhubService:
93
  "fetched_at": datetime.now(tz=timezone.utc).isoformat(),
94
  }
95
 
96
- # --- OHLCV candles + locally computed indicators ---
97
- to_ts = int(datetime.now(tz=timezone.utc).timestamp())
98
- from_ts = int((datetime.now(tz=timezone.utc) - timedelta(days=_CANDLE_DAYS)).timestamp())
99
  try:
100
- candle = await self._call("/stock/candle", {
101
- "symbol": ticker,
102
- "resolution": "D",
103
- "from": str(from_ts),
104
- "to": str(to_ts),
105
- })
106
- ohlcv = self._candle_to_av(candle)
107
  combined["TIME_SERIES_DAILY_ADJUSTED"] = ohlcv
108
- daily = ohlcv["Time Series (Daily)"]
109
- sorted_dates = sorted(daily.keys()) # ascending
110
- closes = [float(daily[d]["4. close"]) for d in sorted_dates]
111
  combined["SMA"] = self._compute_sma(closes, sorted_dates, period=20)
112
  combined["EMA"] = self._compute_ema(closes, sorted_dates, period=20)
113
  combined["RSI"] = self._compute_rsi(closes, sorted_dates, period=14)
114
  combined["MACD"] = self._compute_macd(closes, sorted_dates)
115
- except RateLimitExceeded:
116
- combined["_cap_hit"] = True
117
- combined["_cap_at_endpoint"] = "CANDLE"
118
- out_path.write_text(json.dumps(combined, indent=2), encoding="utf-8")
119
- return combined
120
  except Exception as exc:
121
- logger.warning("Finnhub CANDLE for %s failed: %s", ticker, exc)
122
  combined["_CANDLE_error"] = str(exc)
123
 
124
  # --- Company news (last 30 days) ---
@@ -200,6 +186,35 @@ class FinnhubService:
200
  response.raise_for_status()
201
  return response.json()
202
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
203
  # ------------------------------------------------------------------ #
204
  # Format converters (Finnhub → Alpha Vantage-compatible keys)
205
  # ------------------------------------------------------------------ #
 
93
  "fetched_at": datetime.now(tz=timezone.utc).isoformat(),
94
  }
95
 
96
+ # --- OHLCV candles + locally computed indicators (via yfinance, free/no key) ---
 
 
97
  try:
98
+ ohlcv, sorted_dates, closes = await asyncio.get_event_loop().run_in_executor(
99
+ None, self._fetch_ohlcv_yfinance, ticker
100
+ )
 
 
 
 
101
  combined["TIME_SERIES_DAILY_ADJUSTED"] = ohlcv
 
 
 
102
  combined["SMA"] = self._compute_sma(closes, sorted_dates, period=20)
103
  combined["EMA"] = self._compute_ema(closes, sorted_dates, period=20)
104
  combined["RSI"] = self._compute_rsi(closes, sorted_dates, period=14)
105
  combined["MACD"] = self._compute_macd(closes, sorted_dates)
 
 
 
 
 
106
  except Exception as exc:
107
+ logger.warning("yfinance OHLCV for %s failed: %s", ticker, exc)
108
  combined["_CANDLE_error"] = str(exc)
109
 
110
  # --- Company news (last 30 days) ---
 
186
  response.raise_for_status()
187
  return response.json()
188
 
189
+ # ------------------------------------------------------------------ #
190
+ # yfinance OHLCV (free, no API key, runs in thread executor)
191
+ # ------------------------------------------------------------------ #
192
+
193
+ @staticmethod
194
+ def _fetch_ohlcv_yfinance(
195
+ ticker: str,
196
+ ) -> tuple[dict, list[str], list[float]]:
197
+ """Download daily OHLCV via yfinance and return AV-compatible dict."""
198
+ import yfinance as yf
199
+ df = yf.download(ticker, period=f"{_CANDLE_DAYS}d", interval="1d", progress=False, auto_adjust=True)
200
+ if df.empty:
201
+ raise ValueError(f"yfinance returned empty data for {ticker}")
202
+ series: dict[str, dict] = {}
203
+ for ts, row in df.iterrows():
204
+ d = str(ts)[:10] # YYYY-MM-DD
205
+ series[d] = {
206
+ "1. open": str(round(float(row["Open"]), 4)),
207
+ "2. high": str(round(float(row["High"]), 4)),
208
+ "3. low": str(round(float(row["Low"]), 4)),
209
+ "4. close": str(round(float(row["Close"]), 4)),
210
+ "5. adjusted close": str(round(float(row["Close"]), 4)),
211
+ "6. volume": str(int(row["Volume"])),
212
+ }
213
+ ohlcv = {"Time Series (Daily)": series}
214
+ sorted_dates = sorted(series.keys())
215
+ closes = [float(series[d]["4. close"]) for d in sorted_dates]
216
+ return ohlcv, sorted_dates, closes
217
+
218
  # ------------------------------------------------------------------ #
219
  # Format converters (Finnhub → Alpha Vantage-compatible keys)
220
  # ------------------------------------------------------------------ #