-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetch_biggestDailyMovers.py
More file actions
50 lines (37 loc) · 1.13 KB
/
fetch_biggestDailyMovers.py
File metadata and controls
50 lines (37 loc) · 1.13 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
import requests
import csv
from datetime import date
today = date.today()
today = str(today)
def getMovers(td_consumer_key):
movers_url = "https://api.tdameritrade.com/v1/marketdata/{index}/movers"
big_movers = {}
movers_symbols = []
fields = ['Desciption',
'Symbol',
'Direction',
'Change ( % )']
indexes = ['$DJI','$SPX.X','$COMPX']
for i in indexes:
# Fetch data
endpoint = movers_url.format(index = i)
data = requests.get(url= endpoint,
params={'apikey' : td_consumer_key})
# Jsonify()
data = data.json()
# Parse JSON -> Dictionary
for x in data:
change = x['change'] * 100
temp = (x['description'],
x['symbol'],
x['direction'],
f'{change :0.3f}')
movers_symbols.append(x['symbol'])
big_movers.update({movers_symbols[-1] : temp})
# Dictionary -> CSV
with open(f'./{today}/{today[5:]}\'s big moves.csv', 'w') as f:
writer = csv.writer(f)
writer.writerow(fields)
for x in big_movers:
writer.writerow(big_movers[x])
return movers_symbols