-
Notifications
You must be signed in to change notification settings - Fork 103
/
client.py
361 lines (277 loc) · 11 KB
/
client.py
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
import json
import requests
from finnhub.exceptions import FinnhubAPIException
from finnhub.exceptions import FinnhubRequestException
class Client:
API_URL = "https://finnhub.io/api/v1"
DEFAULT_TIMEOUT = 10
def __init__(self, api_key):
self._session = self._init_session(api_key)
@staticmethod
def _init_session(api_key):
session = requests.session()
session.headers.update({"Accept": "application/json",
"User-Agent": "finnhub/python"})
session.params["token"] = api_key
return session
def close(self):
self._session.close()
def __enter__(self):
return self
def __exit__(self, *exc):
self.close()
def _request(self, method, path, **kwargs):
uri = "{}/{}".format(self.API_URL, path)
kwargs["timeout"] = kwargs.get("timeout", self.DEFAULT_TIMEOUT)
kwargs["params"] = self._format_params(kwargs.get("params", {}))
response = getattr(self._session, method)(uri, **kwargs)
return self._handle_response(response)
@staticmethod
def _handle_response(response):
if not response.ok:
raise FinnhubAPIException(response)
try:
content_type = response.headers.get('Content-Type', '')
if 'application/json' in content_type:
return response.json()
if 'text/csv' in content_type:
return response.text
if 'text/plain' in content_type:
return response.text
raise FinnhubRequestException("Invalid Response: {}".format(response.text))
except ValueError:
raise FinnhubRequestException("Invalid Response: {}".format(response.text))
@staticmethod
def _merge_two_dicts(first, second):
result = first.copy()
result.update(second)
return result
@staticmethod
def _format_params(params):
return {k: json.dumps(v) if isinstance(v, bool) else v for k, v in params.items()}
def _get(self, path, **kwargs):
return self._request("get", path, **kwargs)
@property
def api_key(self):
return self._session.params.get("token")
@api_key.setter
def api_key(self, token):
self._session.params["token"] = token
def covid19(self):
return self._get("/covid19/us")
def company_profile(self, **params):
return self._get("/stock/profile", params=params)
def company_profile2(self, **params):
return self._get("/stock/profile2", params=params)
def aggregate_indicator(self, symbol, resolution):
return self._get("/scan/technical-indicator", params={
"symbol": symbol,
"resolution": resolution,
})
def crypto_exchanges(self):
return self._get("/crypto/exchange")
def forex_exchanges(self):
return self._get("/forex/exchange")
def press_releases(self, symbol, _from=None, to=None):
return self._get("/press-releases", params={
"symbol": symbol,
"from": _from,
"to": to
})
def company_executive(self, symbol):
return self._get("/stock/executive", params={"symbol": symbol})
def stock_dividends(self, symbol, _from=None, to=None):
return self._get("/stock/dividend", params={
"symbol": symbol,
"from": _from,
"to": to
})
def stock_symbols(self, exchange):
return self._get("/stock/symbol", params={"exchange": exchange})
def recommendation_trends(self, symbol):
return self._get("/stock/recommendation", params={"symbol": symbol})
def price_target(self, symbol):
return self._get("/stock/price-target", params={"symbol": symbol})
def upgrade_downgrade(self, **params):
return self._get("/stock/upgrade-downgrade", params=params)
def option_chain(self, **params):
return self._get("/stock/option-chain", params=params)
def company_peers(self, symbol):
return self._get("/stock/peers", params={"symbol": symbol})
def company_basic_financials(self, symbol, metric):
return self._get("/stock/metric", params={
"symbol": symbol,
"metric": metric
})
def financials(self, symbol, statement, freq):
return self._get("/stock/financials", params={
"symbol": symbol,
"statement": statement,
"freq": freq
})
def financials_reported(self, **params):
return self._get("/stock/financials-reported", params=params)
def fund_ownership(self, symbol, limit=None):
return self._get("/stock/fund-ownership", params={
"symbol": symbol,
"limit": limit
})
def company_earnings(self, symbol, limit=None):
return self._get("/stock/earnings", params={
"symbol": symbol,
"limit": limit
})
def company_revenue_estimates(self, symbol, freq=None):
return self._get("/stock/revenue-estimate", params={
"symbol": symbol,
"freq": freq
})
def company_eps_estimates(self, symbol, freq=None):
return self._get("/stock/eps-estimate", params={
"symbol": symbol,
"freq": freq
})
def exchange(self):
return self._get("/stock/exchange")
def filings(self, **params):
return self._get("/stock/filings", params=params)
def stock_symbol(self, **params):
return self._get("/stock/symbol", params=params)
def quote(self, symbol):
return self._get("/quote", params={
"symbol": symbol
})
def transcripts(self, _id):
return self._get("/stock/transcripts", params={"id": _id})
def transcripts_list(self, symbol):
return self._get("/stock/transcripts/list", params={"symbol": symbol})
def sim_index(self, **params):
return self._get("/stock/similarity-index", params=params)
def stock_candles(self, symbol, resolution, _from, to, **kwargs):
params = self._merge_two_dicts({
"symbol": symbol,
"resolution": resolution,
"from": _from,
"to": to
}, kwargs)
return self._get("/stock/candle", params=params)
def stock_tick(self, symbol, date, limit, skip, _format='json', **kwargs):
params = self._merge_two_dicts({
"symbol": symbol,
"date": date,
"limit": limit,
"skip": skip,
"format": _format
}, kwargs)
return self._get("/stock/tick", params=params)
def forex_rates(self, **params):
return self._get("/forex/rates", params=params)
def forex_symbols(self, exchange):
return self._get("/forex/symbol", params={
"exchange": exchange
})
def forex_candles(self, symbol, resolution, _from, to, _format='json'):
return self._get("/forex/candle", params={
"symbol": symbol,
"resolution": resolution,
"from": _from,
"to": to,
"format": _format
})
def crypto_symbols(self, exchange):
return self._get("/crypto/symbol", params={"exchange": exchange})
def crypto_candles(self, symbol, resolution, _from, to, _format='json'):
return self._get("/crypto/candle", params={
"symbol": symbol,
"resolution": resolution,
"from": _from,
"to": to,
"format": _format
})
def pattern_recognition(self, symbol, resolution):
return self._get("/scan/pattern", params={
"symbol": symbol,
"resolution": resolution
})
def support_resistance(self, symbol, resolution):
return self._get("/scan/support-resistance", params={
"symbol": symbol,
"resolution": resolution
})
def technical_indicator(self, symbol, resolution, _from, to, indicator, indicator_fields=None):
indicator_fields = indicator_fields or {}
params = self._merge_two_dicts({
"symbol": symbol,
"resolution": resolution,
"from": _from,
"to": to,
"indicator": indicator
}, indicator_fields)
return self._get("/indicator", params=params)
def stock_splits(self, symbol, _from, to):
return self._get("/stock/split", params={
"symbol": symbol,
"from": _from,
"to": to
})
def general_news(self, category, min_id=0):
return self._get("/news", params={
"category": category,
"minId": min_id
})
def company_news(self, symbol, _from, to):
return self._get("/company-news", params={
"symbol": symbol,
"from": _from,
"to": to
})
def news_sentiment(self, symbol):
return self._get("/news-sentiment", params={
"symbol": symbol
})
def ownership(self, symbol, limit=None):
return self._get("/stock/ownership", params={
"symbol": symbol,
"limit": limit
})
def country(self):
return self._get("/country")
def merger_country(self):
return self._get("/merger/country")
def merger(self, **params):
return self._get("/merger", params=params)
def economic_code(self):
return self._get("/economic/code")
def economic_data(self, code):
return self._get("/economic", params={"code": code})
def calendar_economic(self):
return self._get("/calendar/economic")
def earnings_calendar(self, **params):
return self._get("/calendar/earnings", params=params)
def ipo_calendar(self, _from, to):
return self._get("/calendar/ipo", params={
"from": _from,
"to": to
})
def indices_const(self, **params):
return self._get("/index/constituents", params=params)
def indices_hist_const(self, **params):
return self._get("/index/historical-constituents", params=params)
def etfs_profile(self, symbol):
return self._get("/etf/profile", params={"symbol": symbol})
def etfs_holdings(self, symbol):
return self._get("/etf/holdings", params={"symbol": symbol})
def etfs_ind_exp(self, symbol):
return self._get("/etf/holdings", params={"symbol": symbol})
def etfs_country_exp(self, symbol):
return self._get("/etf/country", params={"symbol": symbol})
def international_filings(self, symbol="", country=""):
return self._get("/stock/international-filings", params={"symbol": symbol, "country": country})
def sec_sentiment_analysis(self, access_number):
return self._get("/stock/filings-sentiment", params={"accessNumber": access_number})
def sec_similarity_index(self, symbol="", cik="", freq="annual"):
return self._get("/stock/similarity-index", params={"symbol": symbol, "cik": cik, "freq": freq})
def last_bid_ask(self, symbol):
return self._get("/stock/bidask", params={"symbol": symbol})
def fda_calendar(self):
return self._get("/fda-advisory-committee-calendar")