-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_curr.py
53 lines (33 loc) · 1.05 KB
/
get_curr.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
import pandas as pd
import requests
from datetime import date
from pandas.io.json import json_normalize
today = date.today()
def construct_url(ticker, token, startDate=today):
first = "https://api.tiingo.com/tiingo/daily/" + str(ticker.lower())
third = "/prices?startDate=" + str(startDate)
last = "&token=" + str(token)
url = first + third + last
return url
print(construct_url('AAPL', tiingo_api))
def get_price(ticker, token):
try:
headers = {
'Content-Type': 'application/json'
}
requestResponse = requests.get(
construct_url(ticker, token), headers=headers)
req = requestResponse.json()
df_json = json_normalize(req)
price = df_json['open'][0]
return price
except:
print(f"Cannot return ticker {ticker}")
df = pd.read_csv('Stocks_raw_data.csv', header=0)
df_out_list = []
tickers = list(df['SYMBOL'].unique())
tickers.pop(0)
print(tickers)
for ticker in tickers:
price = get_price(ticker, tiingo_api)
print(price, ticker)