-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSectorAnalysis
More file actions
executable file
·38 lines (28 loc) · 1.03 KB
/
SectorAnalysis
File metadata and controls
executable file
·38 lines (28 loc) · 1.03 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
#!/usr/bin/python3
import json
import requests
APIKEY = '' # go to https://www.alphavantage.co/support/ for your API KEY
SECTION_ORDER = ['5 Day', '1 Month', '3 Month', 'Year-to-Date']
def GetSectorPerformances():
url_fmt = 'https://www.alphavantage.co/query?function=SECTOR&apikey={}'
url = url_fmt.format(APIKEY)
response = requests.get(url)
return response.json()
def main():
perf = GetSectorPerformances()
for section in SECTION_ORDER:
keys = [key for key in perf.keys() if section.lower() in key.lower()]
if keys:
data = [(float(v.rstrip('%')), k) for k,v in perf[keys[0]].items()]
data.sort(reverse=True)
print('+' + '='*58 + '+')
print('| ' + (section + ' Performance').center(56) + ' |')
print('+' + '-'*58 + '+')
width = max([len(s) for _,s in data])
for percent,sector in data:
info = sector.rjust(width) + ': %.2f%%' % percent
print('|', info, '|'.rjust(57-len(info)))
print('+' + '-'*58 + '+')
print()
if __name__ == '__main__':
main()