mirror of
https://github.com/andrewkdinh/fund-indicators.git
synced 2024-11-21 18:54:17 -08:00
Update StockData.py
Added Tiingo API
This commit is contained in:
parent
2837ea736f
commit
57c9741052
124
StockData.py
124
StockData.py
@ -30,30 +30,38 @@ class Stock:
|
||||
self.dates = newDates
|
||||
|
||||
def getDates(self): # returns beginning and end dates available
|
||||
print("Getting available dates for", self, "...\n")
|
||||
print("Getting available dates for", self, "...")
|
||||
# Gets first and last possible dates from each source
|
||||
# Also gets all dates from each source
|
||||
|
||||
# IEX
|
||||
print("\nDates from IEX...")
|
||||
firstLastDatesIEX = Stock.getDatesIEX(self, 'firstLast')
|
||||
print("\nFirst and last dates:", firstLastDatesIEX)
|
||||
print("First and last dates:", firstLastDatesIEX)
|
||||
print("Adding dates available to datesIEX")
|
||||
datesIEX = Stock.getDatesIEX(self, 'all')
|
||||
#print("All dates (recent first):", datesIEX, "\n")
|
||||
#print("All dates (recent first):", datesIEX, "\n") # Uncomment line to view output
|
||||
|
||||
# Alpha Vantage
|
||||
print("\nDates from Alpha Vantage...")
|
||||
firstLastDatesAV = Stock.getDatesAV(self, 'firstLast')
|
||||
print("\nFirst and last dates:", firstLastDatesAV)
|
||||
print("First and last dates:", firstLastDatesAV)
|
||||
print("Adding dates available to datesAV")
|
||||
datesAV = Stock.getDatesAV(self, 'all')
|
||||
#print("All dates (recent first):", datesAV, "\n")
|
||||
#print("All dates (recent first):", datesAV, "\n") # Uncomment line to view output
|
||||
|
||||
#
|
||||
# Tiingo
|
||||
print("\nDates from Tiingo...")
|
||||
firstLastDatesTiingo = Stock.getDatesTiingo(self, 'firstLast')
|
||||
print("First and last dates:", firstLastDatesTiingo)
|
||||
print("Adding dates available to datesTiingo")
|
||||
datesTiingo = Stock.getDatesTiingo(self, 'all')
|
||||
#print("All dates (recent first):", datesTiingo, "\n") # Uncomment line to view output
|
||||
|
||||
def getDatesIEX(self, which):
|
||||
url = ''.join(('https://api.iextrading.com/1.0/stock/', self, '/chart/5y'))
|
||||
#link = "https://api.iextrading.com/1.0/stock/spy/chart/5y"
|
||||
print("URL:", url)
|
||||
#print("URL:", url)
|
||||
f = requests.get(url)
|
||||
json_data = f.text
|
||||
loaded_json = json.loads(json_data)
|
||||
@ -71,7 +79,7 @@ class Stock:
|
||||
finalDate = lastLine['date']
|
||||
#print("Final date:", finalDate)
|
||||
dates.append((firstDate, finalDate))
|
||||
else:
|
||||
elif which == 'all':
|
||||
# for i in range(0, len(loaded_json), 1): # If you want to do oldest first
|
||||
for i in range(len(loaded_json)-1, 0, -1):
|
||||
line = loaded_json[i]
|
||||
@ -82,7 +90,7 @@ class Stock:
|
||||
def getDatesAV(self, which):
|
||||
url = ''.join(('https://www.alphavantage.co/query?function=TIME_SERIES_MONTHLY&symbol=', self, '&apikey=', apiAV))
|
||||
# https://www.alphavantage.co/query?function=TIME_SERIES_MONTHLY&symbol=MSFT&apikey=demo
|
||||
print("URL:", url)
|
||||
#print("URL:", url)
|
||||
f = requests.get(url)
|
||||
json_data = f.text
|
||||
loaded_json = json.loads(json_data)
|
||||
@ -102,35 +110,83 @@ class Stock:
|
||||
#print("firstDate:", firstDate)
|
||||
#print("lastDate:", lastDate)
|
||||
dates.append((firstDate, lastDate))
|
||||
else:
|
||||
elif which == 'all':
|
||||
dates = listOfDates
|
||||
return dates
|
||||
'''
|
||||
# Find finalDate (comes first)
|
||||
print("Length:", len(loaded_json))
|
||||
lastLine = loaded_json[-1] # Returns last value of the list (Equivalent to len(loaded_json)-1)
|
||||
print("lastLine:", lastLine)
|
||||
finalDate = lastLine['date']
|
||||
print("Final date:", finalDate)
|
||||
dates = []
|
||||
dates.append((firstDate, finalDate))
|
||||
'''
|
||||
'''
|
||||
def printDates(self):
|
||||
print("Getting data from IEX...")
|
||||
url = ''.join(('https://api.iextrading.com/1.0/stock/', self.name, '/chart/5y'))
|
||||
#link = "https://api.iextrading.com/1.0/stock/spy/chart/5y"
|
||||
print("URL:", url)
|
||||
f = requests.get(url)
|
||||
#print(f.text)
|
||||
json_data = f.text
|
||||
loaded_json = json.loads(json_data)
|
||||
|
||||
print("Printing from IEX...")
|
||||
for i in range (0,len(loaded_json),1):
|
||||
a = loaded_json[i]
|
||||
print(a['date'])
|
||||
def getDatesTiingo(self, which):
|
||||
'''
|
||||
headers = {
|
||||
'Content-Type': 'application/json',
|
||||
'Authorization' : 'Token <TOKEN>'
|
||||
}
|
||||
requestResponse = requests.get("https://api.tiingo.com/api/test/",
|
||||
headers=headers)
|
||||
print(requestResponse.json())
|
||||
|
||||
#OR we can use the token directly in the url
|
||||
|
||||
headers = {
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
requestResponse = requests.get("https://api.tiingo.com/api/test?token=<TOKEN>",
|
||||
headers=headers)
|
||||
print(requestResponse.json())
|
||||
'''
|
||||
token = ''.join(('Token ', apiTiingo))
|
||||
headers = {
|
||||
'Content-Type': 'application/json',
|
||||
'Authorization' : token
|
||||
}
|
||||
url = ''.join(('https://api.tiingo.com/tiingo/daily/', self))
|
||||
requestResponse = requests.get(url, headers=headers)
|
||||
#print(requestResponse.json())
|
||||
loaded_json = requestResponse.json()
|
||||
#print(loaded_json)
|
||||
#print(len(loaded_json))
|
||||
'''
|
||||
list1 = list(loaded_json)
|
||||
for i in range (0, len(list1), 1):
|
||||
if list1[i] == 'startDate':
|
||||
startNum = i
|
||||
elif list1[i] == 'endDate':
|
||||
endNum = i
|
||||
print(list1[startNum])
|
||||
print(list1[endNum])
|
||||
'''
|
||||
firstDate = loaded_json['startDate']
|
||||
finalDate = loaded_json['endDate']
|
||||
#print(firstDate)
|
||||
#print(finalDate)
|
||||
dates = []
|
||||
if which == 'firstLast':
|
||||
#print("URL:", url)
|
||||
dates.append((firstDate, finalDate))
|
||||
elif which == 'all':
|
||||
url2 = ''.join((url, '/prices?startDate=', firstDate, '&endDate=', finalDate))
|
||||
# https://api.tiingo.com/tiingo/daily/<ticker>/prices?startDate=2012-1-1&endDate=2016-1-1
|
||||
#print("Second URL:", url2)
|
||||
requestResponse2 = requests.get(url2, headers=headers)
|
||||
loaded_json2 = requestResponse2.json()
|
||||
#print(loaded_json2)
|
||||
#print(len(loaded_json2))
|
||||
'''
|
||||
print(loaded_json2[0])
|
||||
temp = loaded_json2[0]
|
||||
temp2 = temp['date']
|
||||
temp3 = temp2.split('T00:00:00.000Z')
|
||||
print(temp2)
|
||||
print(temp3)
|
||||
print(temp3[0])
|
||||
print(temp3[1])
|
||||
'''
|
||||
for i in range(len(loaded_json2)-1, 0, -1):
|
||||
line = loaded_json2[i]
|
||||
dateWithTime = line['date']
|
||||
temp = dateWithTime.split('T00:00:00.000Z')
|
||||
date = temp[0]
|
||||
dates.append(date)
|
||||
return dates
|
||||
|
||||
def main():
|
||||
#if __name__ == '__main__':
|
||||
|
Loading…
Reference in New Issue
Block a user