-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathfinnhub.py
More file actions
404 lines (342 loc) · 14.4 KB
/
finnhub.py
File metadata and controls
404 lines (342 loc) · 14.4 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
"""Finnhub API service for live market data."""
import asyncio
import logging
from datetime import datetime, timedelta
from typing import Optional
import httpx
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy import select
from ..models import StockQuote, CompanyProfile
logger = logging.getLogger(__name__)
class FinnhubService:
"""Service for fetching live market data from Finnhub API."""
BASE_URL = "https://finnhub.io/api/v1"
CACHE_DURATION_SECONDS = 60 # Cache quotes for 1 minute
def __init__(self, api_key: str):
self.api_key = api_key
self.client = httpx.AsyncClient(timeout=10.0)
async def close(self):
"""Close HTTP client."""
await self.client.aclose()
async def get_quote(self, symbol: str) -> Optional[dict]:
"""
Fetch real-time quote for a symbol.
Returns dict with: c (current), h (high), l (low), o (open),
pc (previous close), d (change), dp (percent change)
"""
# Use mock data if API key is test_key or empty
if self.api_key in ("test_key", "", None):
return self._get_mock_quote(symbol)
try:
url = f"{self.BASE_URL}/quote"
params = {"symbol": symbol, "token": self.api_key}
response = await self.client.get(url, params=params)
response.raise_for_status()
data = response.json()
# Validate response has data
if data.get("c") == 0:
logger.warning(f"No quote data for {symbol}, using mock")
return self._get_mock_quote(symbol)
return data
except httpx.HTTPError as e:
logger.error(f"Finnhub API error for {symbol}: {e}, using mock")
return self._get_mock_quote(symbol)
except Exception as e:
logger.error(f"Unexpected error fetching quote for {symbol}: {e}, using mock")
return self._get_mock_quote(symbol)
def _get_mock_quote(self, symbol: str) -> dict:
"""Generate mock quote data for testing."""
import random
# Base prices for common symbols
base_prices = {
"AAPL": 178.50,
"MSFT": 405.30,
"GOOGL": 142.80,
"AMZN": 175.20,
"TSLA": 238.40,
"META": 520.60,
"NVDA": 875.30,
"AMD": 195.80,
}
base_price = base_prices.get(symbol, 100.00)
# Add some randomness
variation = random.uniform(-0.05, 0.05)
current = round(base_price * (1 + variation), 2)
prev_close = round(base_price, 2)
change = round(current - prev_close, 2)
percent_change = round((change / prev_close) * 100, 2)
return {
"c": current,
"h": round(current * 1.02, 2),
"l": round(current * 0.98, 2),
"o": round(prev_close * 1.001, 2),
"pc": prev_close,
"d": change,
"dp": percent_change
}
async def get_company_profile(self, symbol: str) -> Optional[dict]:
"""
Fetch company profile information.
Returns dict with: name, country, currency, exchange, ipo,
marketCapitalization, phone, shareOutstanding, ticker, weburl,
logo, finnhubIndustry
"""
# Use mock data if API key is test_key or empty
if self.api_key in ("test_key", "", None):
return self._get_mock_profile(symbol)
try:
url = f"{self.BASE_URL}/stock/profile2"
params = {"symbol": symbol, "token": self.api_key}
response = await self.client.get(url, params=params)
response.raise_for_status()
data = response.json()
# Check if valid response
if not data or not data.get("name"):
logger.warning(f"No profile data for {symbol}, using mock")
return self._get_mock_profile(symbol)
return data
except httpx.HTTPError as e:
logger.error(f"Finnhub API error for profile {symbol}: {e}, using mock")
return self._get_mock_profile(symbol)
except Exception as e:
logger.error(f"Unexpected error fetching profile for {symbol}: {e}, using mock")
return self._get_mock_profile(symbol)
def _get_mock_profile(self, symbol: str) -> dict:
"""Generate mock company profile data for testing."""
companies = {
"AAPL": {
"name": "Apple Inc.",
"country": "US",
"currency": "USD",
"exchange": "NASDAQ",
"finnhubIndustry": "Technology",
"ipo": "1980-12-12",
"marketCapitalization": 2800000,
"logo": "https://static2.finnhub.io/file/publicdatany/finnhubimage/stock_logo/AAPL.png",
"weburl": "https://www.apple.com/"
},
"MSFT": {
"name": "Microsoft Corporation",
"country": "US",
"currency": "USD",
"exchange": "NASDAQ",
"finnhubIndustry": "Technology",
"marketCapitalization": 3000000,
"logo": "https://static2.finnhub.io/file/publicdatany/finnhubimage/stock_logo/MSFT.svg",
"weburl": "https://www.microsoft.com/"
},
"GOOGL": {
"name": "Alphabet Inc.",
"country": "US",
"currency": "USD",
"exchange": "NASDAQ",
"finnhubIndustry": "Technology",
"marketCapitalization": 1800000,
"logo": "https://static2.finnhub.io/file/publicdatany/finnhubimage/stock_logo/GOOGL.png",
"weburl": "https://www.google.com/"
},
}
# Return company data if available, otherwise generic
return companies.get(symbol, {
"name": f"{symbol} Corporation",
"country": "US",
"currency": "USD",
"exchange": "NYSE",
"finnhubIndustry": "Technology",
"marketCapitalization": 50000,
"weburl": f"https://www.{symbol.lower()}.com/"
})
async def update_cached_quote(
self,
db: AsyncSession,
symbol: str,
force: bool = False
) -> Optional[StockQuote]:
"""
Update cached quote in database.
Args:
db: Database session
symbol: Stock symbol
force: Force update even if cache is fresh
Returns:
Updated StockQuote or None
"""
# Check cache first
if not force:
result = await db.execute(
select(StockQuote).where(StockQuote.symbol == symbol)
)
cached = result.scalar_one_or_none()
if cached:
age = datetime.utcnow() - cached.updated_at
if age.total_seconds() < self.CACHE_DURATION_SECONDS:
logger.debug(f"Using cached quote for {symbol}")
return cached
# Fetch fresh data
quote_data = await self.get_quote(symbol)
if not quote_data:
return None
# Update or create
result = await db.execute(
select(StockQuote).where(StockQuote.symbol == symbol)
)
stock_quote = result.scalar_one_or_none()
if stock_quote:
stock_quote.current_price = quote_data["c"]
stock_quote.change = quote_data["d"]
stock_quote.percent_change = quote_data["dp"]
stock_quote.high = quote_data["h"]
stock_quote.low = quote_data["l"]
stock_quote.open = quote_data["o"]
stock_quote.previous_close = quote_data["pc"]
stock_quote.updated_at = datetime.utcnow()
else:
stock_quote = StockQuote(
symbol=symbol,
current_price=quote_data["c"],
change=quote_data["d"],
percent_change=quote_data["dp"],
high=quote_data["h"],
low=quote_data["l"],
open=quote_data["o"],
previous_close=quote_data["pc"]
)
db.add(stock_quote)
await db.commit()
await db.refresh(stock_quote)
logger.info(f"Updated quote for {symbol}: ${quote_data['c']}")
return stock_quote
async def update_company_profile(
self,
db: AsyncSession,
symbol: str
) -> Optional[CompanyProfile]:
"""
Update company profile in database.
Args:
db: Database session
symbol: Stock symbol
Returns:
Updated CompanyProfile or None
"""
# Check if already exists and is recent (cache for 24 hours)
result = await db.execute(
select(CompanyProfile).where(CompanyProfile.symbol == symbol)
)
profile = result.scalar_one_or_none()
if profile:
age = datetime.utcnow() - profile.updated_at
if age < timedelta(hours=24):
logger.debug(f"Using cached profile for {symbol}")
return profile
# Fetch fresh data
profile_data = await self.get_company_profile(symbol)
if not profile_data:
return None
if profile:
# Update existing
profile.name = profile_data.get("name", "")
profile.country = profile_data.get("country")
profile.currency = profile_data.get("currency")
profile.exchange = profile_data.get("exchange")
profile.industry = profile_data.get("finnhubIndustry")
profile.market_cap = profile_data.get("marketCapitalization")
profile.logo = profile_data.get("logo")
profile.phone = profile_data.get("phone")
profile.weburl = profile_data.get("weburl")
profile.finnhub_industry = profile_data.get("finnhubIndustry")
# Parse IPO date
ipo_str = profile_data.get("ipo")
if ipo_str:
try:
profile.ipo = datetime.strptime(ipo_str, "%Y-%m-%d").date()
except:
pass
profile.updated_at = datetime.utcnow()
else:
# Create new
ipo_date = None
ipo_str = profile_data.get("ipo")
if ipo_str:
try:
ipo_date = datetime.strptime(ipo_str, "%Y-%m-%d").date()
except:
pass
profile = CompanyProfile(
symbol=symbol,
name=profile_data.get("name", ""),
country=profile_data.get("country"),
currency=profile_data.get("currency"),
exchange=profile_data.get("exchange"),
industry=profile_data.get("finnhubIndustry"),
market_cap=profile_data.get("marketCapitalization"),
ipo=ipo_date,
logo=profile_data.get("logo"),
phone=profile_data.get("phone"),
weburl=profile_data.get("weburl"),
finnhub_industry=profile_data.get("finnhubIndustry")
)
db.add(profile)
await db.commit()
await db.refresh(profile)
logger.info(f"Updated profile for {symbol}: {profile.name}")
return profile
async def batch_update_quotes(
self,
db: AsyncSession,
symbols: list[str]
) -> dict[str, Optional[StockQuote]]:
"""
Update multiple quotes efficiently with rate limiting.
Args:
db: Database session
symbols: List of stock symbols
Returns:
Dict mapping symbol to StockQuote
"""
results = {}
for symbol in symbols:
quote = await self.update_cached_quote(db, symbol)
results[symbol] = quote
# Rate limiting: 30 API calls/second max
await asyncio.sleep(0.04)
return results
async def search_symbols(self, query: str) -> list[dict]:
"""
Search for stock symbols.
Args:
query: Search query
Returns:
List of symbol results
"""
# Use mock data if test_key
if self.api_key in ("test_key", "", None):
return self._mock_search(query)
try:
url = f"{self.BASE_URL}/search"
params = {"q": query, "token": self.api_key}
response = await self.client.get(url, params=params)
response.raise_for_status()
data = response.json()
return data.get("result", [])
except Exception as e:
logger.error(f"Error searching symbols: {e}")
return self._mock_search(query)
def _mock_search(self, query: str) -> list[dict]:
"""Mock symbol search for testing."""
symbols = [
{"description": "Apple Inc.", "displaySymbol": "AAPL", "symbol": "AAPL", "type": "Common Stock"},
{"description": "Microsoft Corporation", "displaySymbol": "MSFT", "symbol": "MSFT", "type": "Common Stock"},
{"description": "Alphabet Inc.", "displaySymbol": "GOOGL", "symbol": "GOOGL", "type": "Common Stock"},
{"description": "Amazon.com Inc.", "displaySymbol": "AMZN", "symbol": "AMZN", "type": "Common Stock"},
{"description": "Tesla Inc.", "displaySymbol": "TSLA", "symbol": "TSLA", "type": "Common Stock"},
{"description": "Meta Platforms Inc.", "displaySymbol": "META", "symbol": "META", "type": "Common Stock"},
{"description": "NVIDIA Corporation", "displaySymbol": "NVDA", "symbol": "NVDA", "type": "Common Stock"},
{"description": "Advanced Micro Devices Inc.", "displaySymbol": "AMD", "symbol": "AMD", "type": "Common Stock"},
]
# Filter by query
query_lower = query.lower()
return [
s for s in symbols
if query_lower in s["symbol"].lower() or query_lower in s["description"].lower()
]