-
Notifications
You must be signed in to change notification settings - Fork 0
/
tiingoconnect.py
73 lines (58 loc) · 2.09 KB
/
tiingoconnect.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
import os
import requests
import pandas as pd
from tiingo import TiingoClient
yourtoken = os.getenv("TIINGO_API_KEY")
config = {}
config['session'] = True
config['api_key'] = yourtoken
client = TiingoClient(config)
def DataReader(ticker, d_start, d_end):
# We are expecting DateTime type for input dates !! (just like original Pandas DataReader)
d_end = d_end.strftime("%Y-%m-%d")
d_start = d_start.strftime("%Y-%m-%d")
# print d_start
# print d_end
# headers = {
# 'Content-Type': 'application/json',
# 'Authorization' : 'Token ' + yourtoken
# }
# url = "https://api.tiingo.com/tiingo/daily/" + ticker + "/prices?startDate=" + d_start + "&endDate="+ d_end
# print(f"url: {url}")
# requestResponse = requests.get(url,headers=headers)
# print(type(requestResponse))
# print(requestResponse)
# json_result = requestResponse.json()
# print(f"json_result type: {type(json_result)}")
# df = pd.DataFrame.from_records(json_result)
print("Getting Data from Tiingo")
df = client.get_dataframe(ticker, startDate=d_start, endDate=d_end)
print(df.head())
# Check for existance of Adj Close column
# If not, check for existance of Close column
# If not -> throw error
# If no Adj Close, but Close, copy Close to Adj close
if not "adjClose" in df.columns:
if "close" in df.columns:
df["adjClose"] = df["close"]
else:
print("Error: No Close information")
return
# Convert ISO date format to Pandas DateTime
#df["date"] = pd.to_datetime(df["date"])
# Align Column Names to previous DataReader names
print(df.columns)
df = df.rename(
columns={
"date": "Date",
"open": "Open",
"adjClose": "Adj Close",
"volume": "Volume",
"high": "High",
"low": "Low",
"close": "Close",
}
)
a = df.set_index(["Date"])
print(a)
return a