forked from JerBouma/FinanceDatabase
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson_picker.py
539 lines (493 loc) · 23.1 KB
/
json_picker.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
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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
import requests
import json
def select_cryptocurrencies(cryptocurrency=None,
base_url="https://raw.githubusercontent.com/JerBouma/FinanceDatabase/master/"
"Database/Cryptocurrencies",
use_local_location=False, all_cryptocurrencies_json="_Cryptocurrencies"):
"""
Description
----
Returns all cryptocurrencies when no input is given and has the option to give
a specific set of symbols for the cryptocurrency you provide.
Input
----
cryptocurrency (string, default is None)
If filled, gives all data for a specific cryptocurrency.
base_url (string, default is GitHub location)
The possibility to enter your own location if desired.
use_local_location (string, default False)
The possibility to select a local location (i.e. based on Windows path)
all_cryptocurrencies_json (string, default is _Cryptocurrencies)
Alter the name of the all cryptocurrencies json if desired.
Output
----
json_data (dictionary)
Returns a dictionary with a selection or all data based on the input.
"""
if cryptocurrency:
json_file = f"{base_url}/{cryptocurrency}.json"
if use_local_location:
with open(json_file) as json_local:
json_data = json.load(json_local)
else:
try:
request = requests.get(json_file)
json_data = json.loads(request.text)
except json.decoder.JSONDecodeError:
raise ValueError(f"Not able to find any data for {cryptocurrency}.")
else:
json_file = f"{base_url}/{all_cryptocurrencies_json}.json"
if use_local_location:
with open(json_file) as json_local:
json_data = json.load(json_local)
else:
try:
request = requests.get(json_file)
json_data = json.loads(request.text)
except json.decoder.JSONDecodeError:
raise ValueError("Not able to find any data.")
return json_data
def select_currencies(currency=None,
base_url="https://raw.githubusercontent.com/JerBouma/FinanceDatabase/"
"master/Database/Currencies",
use_local_location=False, all_currencies_json="_Currencies"):
"""
Description
----
Returns all currencies when no input is given and has the option to give
a specific set of symbols for the currency you provide.
Input
----
currency (string, default is None)
If filled, gives all data for a specific currency.
base_url (string, default is GitHub location)
The possibility to enter your own location if desired.
use_local_location (string, default False)
The possibility to select a local location (i.e. based on Windows path)
all_currencies_json (string, default is _Currencies)
Alter the name of the all currencies json if desired.
Output
----
json_data (dictionary)
Returns a dictionary with a selection or all data based on the input.
"""
if currency:
json_file = f"{base_url}/{currency}.json"
if use_local_location:
with open(json_file) as json_local:
json_data = json.load(json_local)
else:
try:
request = requests.get(json_file)
json_data = json.loads(request.text)
except json.decoder.JSONDecodeError:
raise ValueError(f"Not able to find any data for {currency}.")
else:
json_file = f"{base_url}/{all_currencies_json}.json"
if use_local_location:
with open(json_file) as json_local:
json_data = json.load(json_local)
else:
try:
request = requests.get(json_file)
json_data = json.loads(request.text)
except json.decoder.JSONDecodeError:
raise ValueError("Not able to find any data.")
return json_data
def select_etfs(category=None, exclude_exchanges=True,
base_url="https://raw.githubusercontent.com/JerBouma/FinanceDatabase/"
"master/Database/ETFs",
use_local_location=False, all_etfs_json="_ETFs"):
"""
Description
----
Returns all ETFs when no input is given and has the option to give
a specific set of symbols for the category you provide.
Input
----
category (string, default is None)
If filled, gives all data for a specific category.
exclude_exchanges (boolean, default is True):
Whether you want to exclude exchanges from the search. If False,
you will receive multiple times i.e. Vanguard S&P 500 from different exchanges.
base_url (string, default is GitHub location)
The possibility to enter your own location if desired.
use_local_location (string, default False)
The possibility to select a local location (i.e. based on Windows path)
all_etfs_json (string, default is _ETFs)
Alter the name of the all etfs json if desired.
Output
----
json_data (dictionary)
Returns a dictionary with a selection or all data based on the input.
"""
if category:
if use_local_location:
json_file = f"{base_url}/{category}.json"
with open(json_file) as json_local:
json_data = json.load(json_local)
else:
try:
category = category.replace('%', '%25').replace(' ', '%20')
json_file = f"{base_url}/{category}.json"
request = requests.get(json_file)
json_data = json.loads(request.text)
except json.decoder.JSONDecodeError:
raise ValueError(f"Not able to find any data for {category}.")
else:
json_file = f"{base_url}/{all_etfs_json}.json"
if use_local_location:
with open(json_file) as json_local:
json_data = json.load(json_local)
else:
try:
request = requests.get(json_file)
json_data = json.loads(request.text)
except json.decoder.JSONDecodeError:
raise ValueError("Not able to find any data.")
if exclude_exchanges:
for etf in json_data.copy():
if '.' in etf:
del json_data[etf]
if not len(json_data):
raise ValueError("Because exclude_exchanges is set to True, all available data for "
f"this combination ({category}) is removed. Set this parameter to False to obtain data.")
return json_data
def select_equities(country=None, sector=None, industry=None, exclude_exchanges=True,
base_url="https://raw.githubusercontent.com/JerBouma/FinanceDatabase/"
"master/Database/Equities",
use_local_location=False):
"""
Description
----
Returns all equities when no input is given and has the option to give
a specific set of symbols for the country, sector and/or industry provided.
The data depends on the combination of inputs. For example Country + Sector
gives all symbols for a specific sector in a specific country.
Input
----
country (string, default is None)
If filled, gives all data for a specific country.
sector (string, default is None)
If filled, gives all data for a specific sector.
industry (string, default is None)
If filled, gives all data for a specific industry.
exclude_exchanges (boolean, default is True):
Whether you want to exclude exchanges from the search. If False,
you will receive multiple times i.e. Tesla from different exchanges.
base_url (string, default is GitHub location)
The possibility to enter your own location if desired.
use_local_location (string, default False)
The possibility to select a local location (i.e. based on Windows path)
Output
----
json_data (dictionary)
Returns a dictionary with a selection or all data based on the input.
"""
if country and sector and industry:
if use_local_location:
json_file = f"{base_url}/Countries/{country}/{sector}/{industry}.json"
with open(json_file) as json_local:
json_data = json.load(json_local)
else:
try:
country = country.replace('%', '%25').replace(' ', '%20')
sector = sector.replace('%', '%25').replace(' ', '%20')
industry = industry.replace('%', '%25').replace(' ', '%20')
json_file = f"{base_url}/Countries/{country}/{sector}/{industry}.json"
request = requests.get(json_file)
json_data = json.loads(request.text)
except json.decoder.JSONDecodeError:
raise ValueError(f"Not able to find any data with the combination of Country ({country}), "
f"Sector ({sector}) and Industry ({industry}).")
elif country and sector:
if use_local_location:
json_file = f"{base_url}/Countries/{country}/{sector}/_{sector}.json"
with open(json_file) as json_local:
json_data = json.load(json_local)
else:
try:
country = country.replace('%', '%25').replace(' ', '%20')
sector = sector.replace('%', '%25').replace(' ', '%20')
json_file = f"{base_url}/Countries/{country}/{sector}/_{sector}.json"
request = requests.get(json_file)
json_data = json.loads(request.text)
except json.decoder.JSONDecodeError:
raise ValueError(f"Not able to find any data with the combination of Country ({country}) "
f"and Sector ({sector})")
elif sector and industry:
if use_local_location:
json_file = f"{base_url}/Sectors/{sector}/{industry}.json"
with open(json_file) as json_local:
json_data = json.load(json_local)
else:
try:
sector = sector.replace('%', '%25').replace(' ', '%20')
industry = industry.replace('%', '%25').replace(' ', '%20')
json_file = f"{base_url}/Sectors/{sector}/{industry}.json"
request = requests.get(json_file)
json_data = json.loads(request.text)
except json.decoder.JSONDecodeError:
raise ValueError("Not able to find any data with the combination of, "
f"Sector ({sector}) and Industry ({industry}).")
elif country and industry:
if use_local_location:
json_file = f"{base_url}/Countries/{country}/Industries/{industry}.json"
with open(json_file) as json_local:
json_data = json.load(json_local)
else:
try:
country = country.replace('%', '%25').replace(' ', '%20')
industry = industry.replace('%', '%25').replace(' ', '%20')
json_file = f"{base_url}/Countries/{country}/Industries/{industry}.json"
request = requests.get(json_file)
json_data = json.loads(request.text)
except json.decoder.JSONDecodeError:
raise ValueError("Not able to find any data with the combination of, "
f"Country ({country}) and Industry ({industry}).")
elif country:
if use_local_location:
json_file = f"{base_url}/Countries/{country}/{country}.json"
with open(json_file) as json_local:
json_data = json.load(json_local)
else:
try:
country = country.replace('%', '%25').replace(' ', '%20')
json_file = f"{base_url}/Countries/{country}/{country}.json"
request = requests.get(json_file)
json_data = json.loads(request.text)
except json.decoder.JSONDecodeError:
raise ValueError(f"Not able to find any data for {country}.")
elif sector:
if use_local_location:
json_file = f"{base_url}/Sectors/{sector}/_{sector}.json"
with open(json_file) as json_local:
json_data = json.load(json_local)
else:
try:
sector = sector.replace('%', '%25').replace(' ', '%20')
json_file = f"{base_url}/Sectors/{sector}/_{sector}.json"
request = requests.get(json_file)
json_data = json.loads(request.text)
except json.decoder.JSONDecodeError:
raise ValueError(f"Not able to find any data for {sector}.")
elif industry:
if use_local_location:
json_file = f"{base_url}/Industries/{industry}.json"
with open(json_file) as json_local:
json_data = json.load(json_local)
else:
try:
industry = industry.replace('%', '%25').replace(' ', '%20')
json_file = f"{base_url}/Industries/{industry}.json"
request = requests.get(json_file)
json_data = json.loads(request.text)
except json.decoder.JSONDecodeError:
raise ValueError(f"Not able to find any data for {industry}.")
else:
if use_local_location:
json_data = {}
json_file_part_one = f"{base_url}/Equities Part 1.json"
with open(json_file_part_one) as json_local:
json_data_part_one = json.load(json_local)
json_data.update(json_data_part_one)
json_file_part_two = f"{base_url}/Equities Part 2.json"
with open(json_file_part_two) as json_local:
json_data_part_two = json.load(json_local)
json_data.update(json_data_part_two)
else:
try:
json_data = {}
json_file_part_one = f"{base_url}/Equities Part 1.json"
request = requests.get(json_file_part_one)
json_data_segment = json.loads(request.text)
json_data.update(json_data_segment)
json_file_part_two = f"{base_url}/Equities Part 2.json"
request = requests.get(json_file_part_two)
json_data_segment = json.loads(request.text)
json_data.update(json_data_segment)
except json.decoder.JSONDecodeError:
raise ValueError("Not able to find any data.")
if exclude_exchanges:
for company in json_data.copy():
if '.' in company:
del json_data[company]
if not len(json_data):
raise ValueError("Because exclude_exchanges is set to True, all available data for "
f"this combination ({country}, {sector} and {industry}) is removed. "
f"Set this parameter to False to obtain data.")
return json_data
def select_funds(category=None, exclude_exchanges=True,
base_url="https://raw.githubusercontent.com/JerBouma/FinanceDatabase/"
"master/Database/Funds",
use_local_location=False, all_funds_json="_Funds"):
"""
Description
----
Returns all funds when no input is given and has the option to give
a specific set of symbols for the category you provide.
Input
----
category (string, default is None)
If filled, gives all data for a specific category.
exclude_exchanges (boolean, default is True):
Whether you want to exclude exchanges from the search. If False,
you will receive multiple times i.e. AAEUX from different exchanges.
base_url (string, default is GitHub location)
The possibility to enter your own location if desired.
use_local_location (string, default False)
The possibility to select a local location (i.e. based on Windows path)
all_funds_json (string, default is _Funds)
Alter the name of the all funds json if desired.
Output
----
json_data (dictionary)
Returns a dictionary with a selection or all data based on the input.
"""
if category:
if use_local_location:
json_file = f"{base_url}/{category}.json"
with open(json_file) as json_local:
json_data = json.load(json_local)
else:
try:
category = category.replace('%', '%25').replace(' ', '%20')
json_file = f"{base_url}/{category}.json"
request = requests.get(json_file)
json_data = json.loads(request.text)
except json.decoder.JSONDecodeError:
raise ValueError(f"Not able to find any data for {category}.")
else:
json_file = f"{base_url}/{all_funds_json}.json"
if use_local_location:
with open(json_file) as json_local:
json_data = json.load(json_local)
else:
try:
request = requests.get(json_file)
json_data = json.loads(request.text)
except json.decoder.JSONDecodeError:
raise ValueError("Not able to find any data.")
if exclude_exchanges:
for fund in json_data.copy():
if '.' in fund:
del json_data[fund]
if not len(json_data):
raise ValueError("Because exclude_exchanges is set to True, all available data for "
f"this combination ({category}) is removed. Set this parameter to False to "
f"obtain data.")
return json_data
def select_indices(market=None, exclude_exchanges=True,
base_url="https://raw.githubusercontent.com/JerBouma/FinanceDatabase/"
"master/Database/Indices",
use_local_location=False, all_indices_json="_Indices"):
"""
Description
----
Returns all indices when no input is given and has the option to give
a specific set of symbols for the market you provide.
Input
----
market (string, default is None)
If filled, gives all data for a specific market.
exclude_exchanges (boolean, default is True):
Whether you want to exclude exchanges from the search. If False,
you will receive multiple times i.e. ^GSPC from different exchanges.
base_url (string, default is GitHub location)
The possibility to enter your own location if desired.
use_local_location (string, default False)
The possibility to select a local location (i.e. based on Windows path)
all_indices_json (string, default is _Indices)
Alter the name of the all indices json if desired.
Output
----
json_data (dictionary)
Returns a dictionary with a selection or all data based on the input.
"""
if market:
json_file = f"{base_url}/{market}.json"
if use_local_location:
with open(json_file) as json_local:
json_data = json.load(json_local)
else:
try:
request = requests.get(json_file)
json_data = json.loads(request.text)
except json.decoder.JSONDecodeError:
raise ValueError(f"Not able to find any data for {market}.")
else:
json_file = f"{base_url}/{all_indices_json}.json"
if use_local_location:
with open(json_file) as json_local:
json_data = json.load(json_local)
else:
try:
request = requests.get(json_file)
json_data = json.loads(request.text)
except json.decoder.JSONDecodeError:
raise ValueError("Not able to find any data.")
if exclude_exchanges:
for index in json_data.copy():
if '.' in index:
del json_data[index]
if not len(json_data):
raise ValueError("Because exclude_exchanges is set to True, all available data for "
f"this combination ({market}) is removed. Set this parameter to False to obtain data.")
return json_data
def select_moneymarkets(market=None, exclude_exchanges=True,
base_url="https://raw.githubusercontent.com/JerBouma/FinanceDatabase/"
"master/Database/Moneymarkets",
use_local_location=False, all_moneymarkets_json="_Moneymarkets"):
"""
Description
----
Returns all moneymarkets when no input is given and has the option to give
a specific set of symbols for the market you provide.
Input
----
market (string, default is None)
If filled, gives all data for a specific market.
exclude_exchanges (boolean, default is True):
Whether you want to exclude exchanges from the search. If False,
you will receive multiple times i.e. SOND from different exchanges.
base_url (string, default is GitHub location)
The possibility to enter your own location if desired.
use_local_location (string, default False)
The possibility to select a local location (i.e. based on Windows path)
all_moneymarkets_json (string, default is _Moneymarkets)
Alter the name of the all moneymarkets json if desired.
Output
----
json_data (dictionary)
Returns a dictionary with a selection or all data based on the input.
"""
if market:
json_file = f"{base_url}/{market}.json"
if use_local_location:
with open(json_file) as json_local:
json_data = json.load(json_local)
else:
try:
request = requests.get(json_file)
json_data = json.loads(request.text)
except json.decoder.JSONDecodeError:
raise ValueError(f"Not able to find any data for {market}.")
else:
json_file = f"{base_url}/{all_moneymarkets_json}.json"
if use_local_location:
with open(json_file) as json_local:
json_data = json.load(json_local)
else:
try:
request = requests.get(json_file)
json_data = json.loads(request.text)
except json.decoder.JSONDecodeError:
raise ValueError("Not able to find any data.")
if exclude_exchanges:
for moneymarket in json_data.copy():
if '.' in moneymarket:
del json_data[moneymarket]
if not len(json_data):
raise ValueError("Because exclude_exchanges is set to True, all available data for "
f"this combination ({market}) is removed. Set this parameter to False to obtain data.")
return json_data