-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetch_currentStockQuote.py
More file actions
43 lines (39 loc) · 1.28 KB
/
fetch_currentStockQuote.py
File metadata and controls
43 lines (39 loc) · 1.28 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
import requests
from datetime import datetime
import csv
from datetime import date
today = date.today()
today = str(today)
def getQuotes(tickers,td_consumer_key):
quotes_url = 'https://api.tdameritrade.com/v1/marketdata/{stock_ticker}/quotes?'
# Data needed:
data_dictionary = {}
# Data points:
fields = ['description',
'Open Price',
'Bid Price',
'Ask Price',
'Net % Change (decimal)',
'Market % change (decimal)']
# Fetch each ticker's quote:
for stock in tickers:
endpoint = quotes_url.format(stock_ticker = stock)
quotes = requests.get(url=endpoint,
params={'apikey' : td_consumer_key})
# Jsonify()
data = quotes.json()
# Build Dictionary
temp = (data[stock]['description'],
data[stock]['openPrice'],
data[stock]['bidPrice'],
data[stock]['askPrice'],
data[stock]['netPercentChangeInDouble'],
data[stock]['markPercentChangeInDouble'])
data_dictionary.update({data[stock]['description']:temp})
# Dictionary -> CSV
with open(f'./{today}/{today[5:]}\'s_quotes.csv', 'w+') as f:
writer = csv.writer(f)
writer.writerow(fields)
for x in data_dictionary:
writer.writerow(data_dictionary[x])
return 0