This commit is contained in:
Andrew Dinh 2018-12-18 11:53:14 -08:00
parent e8720e3655
commit 49cbc8300e
3 changed files with 76 additions and 76 deletions

3
.gitignore vendored
View File

@ -1,2 +1,5 @@
__pycache__/StockData.cpython-37.pyc __pycache__/StockData.cpython-37.pyc
__pycache__/
*.pyc

View File

@ -16,85 +16,83 @@ apiBarchart = 'a17fab99a1c21cd6f847e2f82b592838'
apiTiingo = '2e72b53f2ab4f5f4724c5c1e4d5d4ac0af3f7ca8' apiTiingo = '2e72b53f2ab4f5f4724c5c1e4d5d4ac0af3f7ca8'
import requests, json import requests, json
'''
def install(package):
if hasattr(pip, 'main'):
pip.main(['install', package])
else:
pip._internal.main(['install', package])
'''
class Stock:
def __init__(self, newStock = '', newfirstLastDates = [], newDates = []):
self.name = newStock
self.firstLastDates = newfirstLastDates
self.dates = newDates
def getDates(self): # returns beginning and end dates available class Stock:
print("Getting available dates for", self, "...") def __init__(self, newName = '', newfirstLastDates = [], newAbsFirstLastDates = [], newDates = [], newListIEX = [], newListAV = [], newListTiingo = []):
# Gets first and last possible dates from each source self.name = newName # Name of stock
# Also gets all dates from each source 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.dates = newDates # All available dates
# List from each source containing: ['firstDate', 'lastDate', allDates, values]
self.listIEX = newListIEX # Dates available from IEX
self.listAV = newListAV # Dates available from AV
self.listTiingo = newListTiingo # Dates available from Tiingo
def main(self):
# Makes list with [firstDate, lastDate, allDates, values]
# IEX # IEX
print("\nDates from IEX...") print("\nIEX")
firstLastDatesIEX = Stock.getDatesIEX(self, 'firstLast') listIEX = Stock.getIEX(self)
print("First and last dates:", firstLastDatesIEX)
print("Adding dates available to datesIEX")
datesIEX = Stock.getDatesIEX(self, 'all')
#print("All dates (recent first):", datesIEX, "\n") # Uncomment line to view output
# Alpha Vantage def set(self, newName, newfirstLastDates, newAbsFirstLastDates, newDates, newListIEX, newListAV, newListTiingo):
print("\nDates from Alpha Vantage...") self.name = newName # Name of stock
firstLastDatesAV = Stock.getDatesAV(self, 'firstLast') self.firstLastDates = newfirstLastDates # Dates that at least 2 sources have (or should it be all?) - Maybe let user decide
print("First and last dates:", firstLastDatesAV) self.absFirstLastDates = newAbsFirstLastDates # Absolute first and last dates from all sources
print("Adding dates available to datesAV") self.dates = newDates # All available dates
datesAV = Stock.getDatesAV(self, 'all')
#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') # ADD BACK WHEN DONE
#print("All dates (recent first):", datesTiingo, "\n") # Uncomment line to view output
'''
listOfFirstLastDates = []
listOfFirstLastDates.append(firstLastDatesIEX)
listOfFirstLastDates.append(firstLastDatesAV)
#listOfFirstLastDates.append(firstLastDatesTiingo)
#print(listOfFirstLastDates)
# Find the min and max date
firstLastDates = Stock.getFirstLastDate(self, listOfFirstLastDates)
print("\nAbsolute first and last date:", firstLastDates)
def getDatesIEX(self, which): # List from each source containing: ['firstDate', 'lastDate', allDates, values]
self.listIEX = newListIEX # Dates available from IEX
self.listAV = newListAV # Dates available from AV
self.listTiingo = newListTiingo # Dates available from Tiingo
def setFirstLastDates(newFirstLastDates):
self.firstLastDates = newFirstLastDates
def setAbsFirstLastDates(newAbsFirstLastDates):
self.absFirstLastDates = newAbsFirstLastDates
def setDates(newDates):
self.dates = newDates
def setListIEX(newListIEX):
self.listIEX = newListIEX
def setListAV(newListAV):
self.listAV = newListAV
def setListTiingo(newListTiingo):
self.listTiingo = newListTiingo
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("URL:", 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)
dates = [] listIEX = []
if which == 'firstLast':
# Find firstDate (comes first) # Adding (firstDate, lastDate) to list
firstLine = loaded_json[0] # Find firstDate (comes first)
#print("firstLine:", firstLine) firstLine = loaded_json[0]
firstDate = firstLine['date'] #print("firstLine:", firstLine)
#print("firstDate:",firstDate) firstDate = firstLine['date']
# Find finalDate (comes last) #print("firstDate:",firstDate)
#print("Length:", len(loaded_json)) # Find lastDate (comes last)
lastLine = loaded_json[-1] # Returns last value of the list (Equivalent to len(loaded_json)-1) #print("Length:", len(loaded_json))
lastLine = loaded_json[-1] # Returns last value of the list (Equivalent to len(loaded_json)-1)
#print("lastLine:", lastLine) #print("lastLine:", lastLine)
finalDate = lastLine['date'] lastDate = lastLine['date']
#print("Final date:", finalDate) #print("last date:", lastDate)
dates.append((firstDate, finalDate)) listIEX.append(firstDate)
elif which == 'all': listIEX.append(lastDate)
# 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): allDates = []
line = loaded_json[i] # for i in range(0, len(loaded_json), 1): # If you want to do oldest first
date = line['date'] for i in range(len(loaded_json)-1, 0, -1):
dates.append(date) line = loaded_json[i]
return dates date = line['date']
allDates.append(date)
listIEX.append(allDates)
#print(listIEX)
return listIEX
def getDatesAV(self, which): def getDatesAV(self, which):
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))
@ -164,15 +162,15 @@ class Stock:
print(list1[endNum]) print(list1[endNum])
''' '''
firstDate = loaded_json['startDate'] firstDate = loaded_json['startDate']
finalDate = loaded_json['endDate'] lastDate = loaded_json['endDate']
#print(firstDate) #print(firstDate)
#print(finalDate) #print(lastDate)
dates = [] dates = []
if which == 'firstLast': if which == 'firstLast':
#print("URL:", url) #print("URL:", url)
dates.append((firstDate, finalDate)) dates.append((firstDate, lastDate))
elif which == 'all': elif which == 'all':
url2 = ''.join((url, '/prices?startDate=', firstDate, '&endDate=', finalDate)) url2 = ''.join((url, '/prices?startDate=', firstDate, '&endDate=', lastDate))
# https://api.tiingo.com/tiingo/daily/<ticker>/prices?startDate=2012-1-1&endDate=2016-1-1 # https://api.tiingo.com/tiingo/daily/<ticker>/prices?startDate=2012-1-1&endDate=2016-1-1
#print("Second URL:", url2) #print("Second URL:", url2)
requestResponse2 = requests.get(url2, headers=headers) requestResponse2 = requests.get(url2, headers=headers)
@ -256,13 +254,12 @@ class Stock:
return firstLastDates return firstLastDates
def main(): def main():
#if __name__ == '__main__':
# install('requests')
stock = 'spy' stock = 'spy'
spy = Stock(stock) spy = Stock(stock)
#print(spy.name)
Stock.getDates(stock) Stock.set('spy', 2,2,2,2,2,2,2)
Stock.main(stock)
#Stock.printDates(spy) #Stock.printDates(spy)
if __name__ == "__main__": if __name__ == "__main__":

Binary file not shown.