mirror of
https://github.com/andrewkdinh/fund-indicators.git
synced 2024-11-21 18:04:19 -08:00
Update StockData.py
This commit is contained in:
parent
1a01d44379
commit
78a20fe11d
267
StockData.py
267
StockData.py
@ -13,9 +13,10 @@ Returns all available dates and prices for each stock requested.
|
|||||||
|
|
||||||
apiAV = 'O42ICUV58EIZZQMU'
|
apiAV = 'O42ICUV58EIZZQMU'
|
||||||
apiBarchart = 'a17fab99a1c21cd6f847e2f82b592838'
|
apiBarchart = 'a17fab99a1c21cd6f847e2f82b592838'
|
||||||
apiTiingo = '2e72b53f2ab4f5f4724c5c1e4d5d4ac0af3f7ca8'
|
apiTiingo = '2e72b53f2ab4f5f4724c5c1e4d5d4ac0af3f7ca8' # Limited to 400 requests/day
|
||||||
|
|
||||||
import requests, json
|
import requests, json
|
||||||
|
from datetime import datetime
|
||||||
|
|
||||||
class Stock:
|
class Stock:
|
||||||
def __init__(self, newName = '', newfirstLastDates = [], newAbsFirstLastDates = [], newDates = [], newListIEX = [], newListAV = [], newListTiingo = []):
|
def __init__(self, newName = '', newfirstLastDates = [], newAbsFirstLastDates = [], newDates = [], newListIEX = [], newListAV = [], newListTiingo = []):
|
||||||
@ -23,19 +24,18 @@ class Stock:
|
|||||||
self.firstLastDates = newfirstLastDates # Dates that at least 2 sources have (or should it be all?) - Maybe let user decide
|
self.firstLastDates = newfirstLastDates # Dates that at least 2 sources have (or should it be all?) - Maybe let user decide
|
||||||
self.absFirstLastDates = newAbsFirstLastDates # Absolute first and last dates from all sources
|
self.absFirstLastDates = newAbsFirstLastDates # Absolute first and last dates from all sources
|
||||||
self.dates = newDates # All available dates
|
self.dates = newDates # All available dates
|
||||||
|
'''
|
||||||
# List from each source containing: ['firstDate', 'lastDate', allDates, values]
|
Format:
|
||||||
|
# List from each source containing: [firstDate, lastDate, allDates, values, timeFrame]
|
||||||
|
# firstDate & lastDate = '2018-12-18' (year-month-date)
|
||||||
|
allDates = ['2018-12-17', '2018-12-14'] (year-month-date)
|
||||||
|
values (close) = ['164.6307', 164.6307]
|
||||||
|
timeFrame = [days, weeks, years]
|
||||||
|
'''
|
||||||
self.listIEX = newListIEX # Dates available from IEX
|
self.listIEX = newListIEX # Dates available from IEX
|
||||||
self.listAV = newListAV # Dates available from AV
|
self.listAV = newListAV # Dates available from AV
|
||||||
self.listTiingo = newListTiingo # Dates available from Tiingo
|
self.listTiingo = newListTiingo # Dates available from Tiingo
|
||||||
|
|
||||||
def main(self):
|
|
||||||
# Makes list with [firstDate, lastDate, allDates, values]
|
|
||||||
|
|
||||||
# IEX
|
|
||||||
print("\nIEX")
|
|
||||||
listIEX = Stock.getIEX(self)
|
|
||||||
|
|
||||||
def set(self, newName, newfirstLastDates, newAbsFirstLastDates, newDates, newListIEX, newListAV, newListTiingo):
|
def set(self, newName, newfirstLastDates, newAbsFirstLastDates, newDates, newListIEX, newListAV, newListTiingo):
|
||||||
self.name = newName # Name of stock
|
self.name = newName # Name of stock
|
||||||
self.firstLastDates = newfirstLastDates # Dates that at least 2 sources have (or should it be all?) - Maybe let user decide
|
self.firstLastDates = newfirstLastDates # Dates that at least 2 sources have (or should it be all?) - Maybe let user decide
|
||||||
@ -63,13 +63,14 @@ class Stock:
|
|||||||
def getIEX(self):
|
def getIEX(self):
|
||||||
url = ''.join(('https://api.iextrading.com/1.0/stock/', self, '/chart/5y'))
|
url = ''.join(('https://api.iextrading.com/1.0/stock/', self, '/chart/5y'))
|
||||||
#link = "https://api.iextrading.com/1.0/stock/spy/chart/5y"
|
#link = "https://api.iextrading.com/1.0/stock/spy/chart/5y"
|
||||||
#print("URL:", url)
|
print("\nSending request to:", url)
|
||||||
f = requests.get(url)
|
f = requests.get(url)
|
||||||
json_data = f.text
|
json_data = f.text
|
||||||
loaded_json = json.loads(json_data)
|
loaded_json = json.loads(json_data)
|
||||||
listIEX = []
|
listIEX = []
|
||||||
|
|
||||||
# Adding (firstDate, lastDate) to list
|
print("\nFinding first and last date")
|
||||||
|
# Adding (firstDate, lastDate) to listIEX
|
||||||
# Find firstDate (comes first)
|
# Find firstDate (comes first)
|
||||||
firstLine = loaded_json[0]
|
firstLine = loaded_json[0]
|
||||||
#print("firstLine:", firstLine)
|
#print("firstLine:", firstLine)
|
||||||
@ -83,56 +84,107 @@ class Stock:
|
|||||||
#print("last date:", lastDate)
|
#print("last date:", lastDate)
|
||||||
listIEX.append(firstDate)
|
listIEX.append(firstDate)
|
||||||
listIEX.append(lastDate)
|
listIEX.append(lastDate)
|
||||||
|
print(listIEX[0], ',', listIEX[1])
|
||||||
|
|
||||||
|
print("\nFinding all dates given")
|
||||||
allDates = []
|
allDates = []
|
||||||
# for i in range(0, len(loaded_json), 1): # If you want to do oldest first
|
# 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):
|
for i in range(len(loaded_json)-1, -1, -1):
|
||||||
line = loaded_json[i]
|
line = loaded_json[i]
|
||||||
date = line['date']
|
date = line['date']
|
||||||
allDates.append(date)
|
allDates.append(date)
|
||||||
listIEX.append(allDates)
|
listIEX.append(allDates)
|
||||||
#print(listIEX)
|
#print(listIEX[1])
|
||||||
|
print("Uncomment above line in code to see output")
|
||||||
|
|
||||||
|
print("\nFinding close values for each date")
|
||||||
|
values = []
|
||||||
|
# for i in range(0, len(loaded_json), 1): # If you want to do oldest first
|
||||||
|
for i in range(len(loaded_json)-1, -1, -1):
|
||||||
|
line = loaded_json[i]
|
||||||
|
value = line['close']
|
||||||
|
values.append(value)
|
||||||
|
listIEX.append(values)
|
||||||
|
#print(listIEX[3])
|
||||||
|
print("Uncomment above line in code to see output")
|
||||||
|
|
||||||
|
print("\nFinding time frame given [days, weeks, years]")
|
||||||
|
timeFrame = []
|
||||||
|
d1 = datetime.strptime(firstDate, "%Y-%m-%d")
|
||||||
|
d2 = datetime.strptime(lastDate, "%Y-%m-%d")
|
||||||
|
timeFrameDays = abs((d2 - d1).days)
|
||||||
|
#print(timeFrameDays)
|
||||||
|
timeFrameYears = float(timeFrameDays / 365)
|
||||||
|
timeFrameWeeks = float(timeFrameDays / 7)
|
||||||
|
timeFrame.append(timeFrameDays)
|
||||||
|
timeFrame.append(timeFrameWeeks)
|
||||||
|
timeFrame.append(timeFrameYears)
|
||||||
|
listIEX.append(timeFrame)
|
||||||
|
print(listIEX[4])
|
||||||
|
|
||||||
return listIEX
|
return listIEX
|
||||||
|
|
||||||
def getDatesAV(self, which):
|
def getAV(self):
|
||||||
|
listAV = []
|
||||||
url = ''.join(('https://www.alphavantage.co/query?function=TIME_SERIES_MONTHLY&symbol=', self, '&apikey=', apiAV))
|
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
|
# https://www.alphavantage.co/query?function=TIME_SERIES_MONTHLY&symbol=MSFT&apikey=demo
|
||||||
#print("URL:", url)
|
print("\nSending request to:", url)
|
||||||
f = requests.get(url)
|
f = requests.get(url)
|
||||||
json_data = f.text
|
json_data = f.text
|
||||||
loaded_json = json.loads(json_data)
|
loaded_json = json.loads(json_data)
|
||||||
#print(loaded_json['Monthly Time Series'])
|
#print(loaded_json['Monthly Time Series'])
|
||||||
monthlyTimeSeries = loaded_json['Monthly Time Series']
|
monthlyTimeSeries = loaded_json['Monthly Time Series']
|
||||||
#print(monthlyTimeSeries)
|
#print(monthlyTimeSeries)
|
||||||
#print(len(monthlyTimeSeries))
|
|
||||||
#length = len(monthlyTimeSeries)
|
|
||||||
#print(monthlyTimeSeries['2018-12-17'])
|
|
||||||
#print(type(monthlyTimeSeries))
|
|
||||||
listOfDates = list(monthlyTimeSeries)
|
listOfDates = list(monthlyTimeSeries)
|
||||||
#print(listOfDates)
|
#print(listOfDates)
|
||||||
dates = []
|
|
||||||
if which == 'firstLast':
|
|
||||||
firstDate = listOfDates[-1]
|
|
||||||
lastDate = listOfDates[0]
|
|
||||||
#print("firstDate:", firstDate)
|
|
||||||
#print("lastDate:", lastDate)
|
|
||||||
dates.append((firstDate, lastDate))
|
|
||||||
elif which == 'all':
|
|
||||||
dates = listOfDates
|
|
||||||
return dates
|
|
||||||
|
|
||||||
def getDatesTiingo(self, which):
|
firstDate = listOfDates[-1]
|
||||||
|
lastDate = listOfDates[0]
|
||||||
|
#print("firstDate:", firstDate)
|
||||||
|
#print("lastDate:", lastDate)
|
||||||
|
listAV.append(firstDate)
|
||||||
|
listAV.append(lastDate)
|
||||||
|
listAV.append(listOfDates)
|
||||||
|
|
||||||
|
print("\nFinding first and last date")
|
||||||
|
print(listAV[0], ',', listAV[1])
|
||||||
|
print("\nFinding all dates given")
|
||||||
|
#print(listAV[2])
|
||||||
|
print("Uncomment above line in code to see output")
|
||||||
|
|
||||||
|
print("\nFinding close values for each date")
|
||||||
|
values = []
|
||||||
|
for i in range(0, len(listOfDates), 1):
|
||||||
|
temp = listOfDates[i]
|
||||||
|
loaded_json2 = monthlyTimeSeries[temp]
|
||||||
|
value = loaded_json2['4. close']
|
||||||
|
values.append(value)
|
||||||
|
listAV.append(values)
|
||||||
|
#print(listOfDates[0])
|
||||||
|
#i = listOfDates[0]
|
||||||
|
#print(monthlyTimeSeries[i])
|
||||||
|
#print(listAV[3])
|
||||||
|
print("Uncomment above line in code to see output")
|
||||||
|
|
||||||
|
print("\nFinding time frame given [days, weeks, years]")
|
||||||
|
timeFrame = []
|
||||||
|
d1 = datetime.strptime(firstDate, "%Y-%m-%d")
|
||||||
|
d2 = datetime.strptime(lastDate, "%Y-%m-%d")
|
||||||
|
timeFrameDays = abs((d2 - d1).days)
|
||||||
|
#print(timeFrameDays)
|
||||||
|
timeFrameYears = float(timeFrameDays / 365)
|
||||||
|
timeFrameWeeks = float(timeFrameDays / 7)
|
||||||
|
timeFrame.append(timeFrameDays)
|
||||||
|
timeFrame.append(timeFrameWeeks)
|
||||||
|
timeFrame.append(timeFrameYears)
|
||||||
|
listAV.append(timeFrame)
|
||||||
|
print(listAV[4])
|
||||||
|
|
||||||
|
return listAV
|
||||||
|
|
||||||
|
def getTiingo(self):
|
||||||
'''
|
'''
|
||||||
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
|
#OR we can use the token directly in the url
|
||||||
|
|
||||||
headers = {
|
headers = {
|
||||||
'Content-Type': 'application/json'
|
'Content-Type': 'application/json'
|
||||||
}
|
}
|
||||||
@ -146,6 +198,7 @@ class Stock:
|
|||||||
'Authorization' : token
|
'Authorization' : token
|
||||||
}
|
}
|
||||||
url = ''.join(('https://api.tiingo.com/tiingo/daily/', self))
|
url = ''.join(('https://api.tiingo.com/tiingo/daily/', self))
|
||||||
|
print("\nSending request to:", url)
|
||||||
requestResponse = requests.get(url, headers=headers)
|
requestResponse = requests.get(url, headers=headers)
|
||||||
#print(requestResponse.json())
|
#print(requestResponse.json())
|
||||||
loaded_json = requestResponse.json()
|
loaded_json = requestResponse.json()
|
||||||
@ -161,52 +214,70 @@ class Stock:
|
|||||||
print(list1[startNum])
|
print(list1[startNum])
|
||||||
print(list1[endNum])
|
print(list1[endNum])
|
||||||
'''
|
'''
|
||||||
|
listTiingo = []
|
||||||
|
|
||||||
|
print("\nFinding first and last date")
|
||||||
firstDate = loaded_json['startDate']
|
firstDate = loaded_json['startDate']
|
||||||
lastDate = loaded_json['endDate']
|
lastDate = loaded_json['endDate']
|
||||||
#print(firstDate)
|
#print(firstDate)
|
||||||
#print(lastDate)
|
#print(lastDate)
|
||||||
|
listTiingo.append(firstDate)
|
||||||
|
listTiingo.append(lastDate)
|
||||||
|
print(listTiingo[0], ',', listTiingo[1])
|
||||||
|
|
||||||
|
print("\nFinding all dates given")
|
||||||
dates = []
|
dates = []
|
||||||
if which == 'firstLast':
|
values = [] # Used loop for finding values
|
||||||
#print("URL:", url)
|
url2 = ''.join((url, '/prices?startDate=', firstDate, '&endDate=', lastDate))
|
||||||
dates.append((firstDate, lastDate))
|
# https://api.tiingo.com/tiingo/daily/<ticker>/prices?startDate=2012-1-1&endDate=2016-1-1
|
||||||
elif which == 'all':
|
print("\nSending request to:", url2)
|
||||||
url2 = ''.join((url, '/prices?startDate=', firstDate, '&endDate=', lastDate))
|
requestResponse2 = requests.get(url2, headers=headers)
|
||||||
# https://api.tiingo.com/tiingo/daily/<ticker>/prices?startDate=2012-1-1&endDate=2016-1-1
|
loaded_json2 = requestResponse2.json()
|
||||||
#print("Second URL:", url2)
|
#print(loaded_json2)
|
||||||
requestResponse2 = requests.get(url2, headers=headers)
|
#print(len(loaded_json2))
|
||||||
loaded_json2 = requestResponse2.json()
|
for i in range(len(loaded_json2)-1, -1, -1):
|
||||||
#print(loaded_json2)
|
line = loaded_json2[i]
|
||||||
#print(len(loaded_json2))
|
dateWithTime = line['date']
|
||||||
'''
|
temp = dateWithTime.split('T00:00:00.000Z')
|
||||||
print(loaded_json2[0])
|
date = temp[0]
|
||||||
temp = loaded_json2[0]
|
dates.append(date)
|
||||||
temp2 = temp['date']
|
|
||||||
temp3 = temp2.split('T00:00:00.000Z')
|
value = line['close']
|
||||||
print(temp2)
|
values.append(value)
|
||||||
print(temp3)
|
listTiingo.append(dates)
|
||||||
print(temp3[0])
|
#print(listTiingo[2])
|
||||||
print(temp3[1])
|
print("Uncomment above line in code to see output")
|
||||||
'''
|
|
||||||
for i in range(len(loaded_json2)-1, 0, -1):
|
print("Finding close values for each date")
|
||||||
line = loaded_json2[i]
|
# Used loop from finding dates
|
||||||
dateWithTime = line['date']
|
listTiingo.append(values)
|
||||||
temp = dateWithTime.split('T00:00:00.000Z')
|
#print(listTiingo[3])
|
||||||
date = temp[0]
|
print("Uncomment above line in code to see output")
|
||||||
dates.append(date)
|
|
||||||
return dates
|
print("Finding time frame given [days, weeks, years]")
|
||||||
|
timeFrame = []
|
||||||
|
d1 = datetime.strptime(firstDate, "%Y-%m-%d")
|
||||||
|
d2 = datetime.strptime(lastDate, "%Y-%m-%d")
|
||||||
|
timeFrameDays = abs((d2 - d1).days)
|
||||||
|
#print(timeFrameDays)
|
||||||
|
timeFrameYears = float(timeFrameDays / 365)
|
||||||
|
timeFrameWeeks = float(timeFrameDays / 7)
|
||||||
|
timeFrame.append(timeFrameDays)
|
||||||
|
timeFrame.append(timeFrameWeeks)
|
||||||
|
timeFrame.append(timeFrameYears)
|
||||||
|
listTiingo.append(timeFrame)
|
||||||
|
print(listTiingo[4])
|
||||||
|
|
||||||
|
return listTiingo
|
||||||
|
|
||||||
def getFirstLastDate(self, listOfFirstLastDates):
|
def getFirstLastDate(self, listOfFirstLastDates):
|
||||||
listOfFirstDates = []
|
listOfFirstDates = []
|
||||||
listOfLastDates = []
|
listOfLastDates = []
|
||||||
#print(len(listOfFirstLastDates))
|
#print(len(listOfFirstLastDates))
|
||||||
for i in range (0, len(listOfFirstLastDates), 1):
|
for i in range (0, len(listOfFirstLastDates), 1):
|
||||||
temp = listOfFirstLastDates[i]
|
firstLastDates = listOfFirstLastDates[i]
|
||||||
datesTemp = temp[0]
|
firstDate = firstLastDates[0]
|
||||||
#print(datesTemp)
|
lastDate = firstLastDates[1]
|
||||||
firstDate = datesTemp[0]
|
|
||||||
#print(firstDate)
|
|
||||||
lastDate = datesTemp[1]
|
|
||||||
#print(lastDate)
|
|
||||||
listOfFirstDates.append(firstDate)
|
listOfFirstDates.append(firstDate)
|
||||||
listOfLastDates.append(lastDate)
|
listOfLastDates.append(lastDate)
|
||||||
#print(listOfFirstDates)
|
#print(listOfFirstDates)
|
||||||
@ -249,16 +320,50 @@ class Stock:
|
|||||||
lastMonth = month
|
lastMonth = month
|
||||||
lastDay = day
|
lastDay = day
|
||||||
#print(lastDate)
|
#print(lastDate)
|
||||||
firstLastDates = []
|
absFirstLastDates = []
|
||||||
firstLastDates.append((firstDate, lastDate))
|
absFirstLastDates.append(firstDate)
|
||||||
return firstLastDates
|
absFirstLastDates.append(lastDate)
|
||||||
|
return absFirstLastDates
|
||||||
|
|
||||||
|
def main(self):
|
||||||
|
# Makes list with ['firstDate', 'lastDate', [allDates], values]
|
||||||
|
|
||||||
|
listOfFirstLastDates = []
|
||||||
|
|
||||||
|
# IEX
|
||||||
|
print("\nIEX")
|
||||||
|
listIEX = Stock.getIEX(self)
|
||||||
|
#print(listIEX)
|
||||||
|
listOfFirstLastDates.append((listIEX[0], listIEX[1]))
|
||||||
|
|
||||||
|
# Alpha Vantage
|
||||||
|
print("\nAlpha Vantage (AV)")
|
||||||
|
listAV = Stock.getAV(self)
|
||||||
|
#print(listAV)
|
||||||
|
listOfFirstLastDates.append((listAV[0], listAV[1]))
|
||||||
|
|
||||||
|
print("\nTiingo") # COMMENTED OUT FOR NOW B/C LIMITED TO 400 REQUESTS/DAY
|
||||||
|
#listTiingo = Stock.getTiingo(self)
|
||||||
|
#print(listTiingo)
|
||||||
|
#listOfFirstLastDates.append((listTiingo[0], listTiingo[1]))
|
||||||
|
|
||||||
|
#print(listOfFirstLastDates)
|
||||||
|
absFirstLastDates = Stock.getFirstLastDate(self, listOfFirstLastDates)
|
||||||
|
print("\nThe absolute first date was:", absFirstLastDates[0])
|
||||||
|
print("The absolute last date was:", absFirstLastDates[1])
|
||||||
|
|
||||||
|
'''
|
||||||
|
FIGURE OUT HOW TO MAKE EACH OF THESE LISTS AN ATTRIBUTE OF CLASS STOCK
|
||||||
|
self.listIEX = listIEX
|
||||||
|
self.listAV = listAV
|
||||||
|
self.listTiingo = listTiingo
|
||||||
|
|
||||||
|
self.absFirstLastDates = absFirstLastDates
|
||||||
|
'''
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
stock = 'spy'
|
stock = 'spy'
|
||||||
spy = Stock(stock)
|
spy = Stock(stock)
|
||||||
|
|
||||||
Stock.set('spy', 2,2,2,2,2,2,2)
|
|
||||||
|
|
||||||
Stock.main(stock)
|
Stock.main(stock)
|
||||||
#Stock.printDates(spy)
|
#Stock.printDates(spy)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user