mirror of
https://github.com/andrewkdinh/fund-indicators.git
synced 2024-11-24 09:14:18 -08:00
Update StockData.py
This commit is contained in:
parent
2c7fe01b65
commit
1c9d09aebe
73
StockData.py
73
StockData.py
@ -1,10 +1,7 @@
|
|||||||
# StockData.py
|
# StockData.py
|
||||||
# Andrew Dinh
|
# Andrew Dinh
|
||||||
# Python 3.6.1
|
# Python 3.6.1
|
||||||
# Description:
|
# Description: Returns all available dates and prices for each stock requested.
|
||||||
'''
|
|
||||||
Returns all available dates and prices for each stock requested.
|
|
||||||
'''
|
|
||||||
|
|
||||||
# Alpha Vantage API Key: O42ICUV58EIZZQMU
|
# Alpha Vantage API Key: O42ICUV58EIZZQMU
|
||||||
# Barchart API Key: a17fab99a1c21cd6f847e2f82b592838
|
# Barchart API Key: a17fab99a1c21cd6f847e2f82b592838
|
||||||
@ -19,11 +16,12 @@ import requests, json
|
|||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
|
||||||
class Stock:
|
class Stock:
|
||||||
def __init__(self, newName = '', newfirstLastDates = [], newAbsFirstLastDates = [], newDates = [], newListIEX = [], newListAV = [], newListTiingo = []):
|
|
||||||
|
def __init__(self, newName = '', newfirstLastDates = [], newAbsFirstLastDates = [], newfinalDatesAndClose = [], newAllLists = []):
|
||||||
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
|
||||||
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.finalDatesAndClose = newfinalDatesAndClose # All available dates
|
||||||
'''
|
'''
|
||||||
Format:
|
Format:
|
||||||
# List from each source containing: [firstDate, lastDate, allDates, values, timeFrame]
|
# List from each source containing: [firstDate, lastDate, allDates, values, timeFrame]
|
||||||
@ -32,10 +30,8 @@ class Stock:
|
|||||||
values (close) = ['164.6307', 164.6307]
|
values (close) = ['164.6307', 164.6307]
|
||||||
timeFrame = [days, weeks, years]
|
timeFrame = [days, weeks, years]
|
||||||
'''
|
'''
|
||||||
self.listIEX = newListIEX # Dates available from IEX
|
self.allLists = newAllLists
|
||||||
self.listAV = newListAV # Dates available from AV
|
'''
|
||||||
self.listTiingo = newListTiingo # Dates available from Tiingo
|
|
||||||
|
|
||||||
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
|
||||||
@ -59,9 +55,10 @@ class Stock:
|
|||||||
self.listAV = newListAV
|
self.listAV = newListAV
|
||||||
def setListTiingo(newListTiingo):
|
def setListTiingo(newListTiingo):
|
||||||
self.listTiingo = newListTiingo
|
self.listTiingo = newListTiingo
|
||||||
|
'''
|
||||||
|
|
||||||
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.name, '/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("\nSending request to:", url)
|
print("\nSending request to:", url)
|
||||||
f = requests.get(url)
|
f = requests.get(url)
|
||||||
@ -77,7 +74,6 @@ class Stock:
|
|||||||
firstDate = firstLine['date']
|
firstDate = firstLine['date']
|
||||||
#print("firstDate:",firstDate)
|
#print("firstDate:",firstDate)
|
||||||
# Find lastDate (comes last)
|
# Find lastDate (comes last)
|
||||||
#print("Length:", len(loaded_json))
|
|
||||||
lastLine = loaded_json[-1] # Returns last value of the list (Equivalent to len(loaded_json)-1)
|
lastLine = loaded_json[-1] # Returns last value of the list (Equivalent to len(loaded_json)-1)
|
||||||
#print("lastLine:", lastLine)
|
#print("lastLine:", lastLine)
|
||||||
lastDate = lastLine['date']
|
lastDate = lastLine['date']
|
||||||
@ -126,7 +122,7 @@ class Stock:
|
|||||||
|
|
||||||
def getAV(self):
|
def getAV(self):
|
||||||
listAV = []
|
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.name, '&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("\nSending request to:", url)
|
print("\nSending request to:", url)
|
||||||
f = requests.get(url)
|
f = requests.get(url)
|
||||||
@ -203,7 +199,6 @@ class Stock:
|
|||||||
#print(requestResponse.json())
|
#print(requestResponse.json())
|
||||||
loaded_json = requestResponse.json()
|
loaded_json = requestResponse.json()
|
||||||
#print(loaded_json)
|
#print(loaded_json)
|
||||||
#print(len(loaded_json))
|
|
||||||
'''
|
'''
|
||||||
list1 = list(loaded_json)
|
list1 = list(loaded_json)
|
||||||
for i in range (0, len(list1), 1):
|
for i in range (0, len(list1), 1):
|
||||||
@ -325,6 +320,20 @@ class Stock:
|
|||||||
absFirstLastDates.append(lastDate)
|
absFirstLastDates.append(lastDate)
|
||||||
return absFirstLastDates
|
return absFirstLastDates
|
||||||
|
|
||||||
|
def getFinalDatesAndClose(self):
|
||||||
|
# finalDates and finalClose will coincide (aka i = 1 will correspond to one another)
|
||||||
|
finalDatesAndClose = () # Will combine finalDates then finalClose
|
||||||
|
finalDates = []
|
||||||
|
finalClose = []
|
||||||
|
#print(self.absFirstLastDates)
|
||||||
|
absFirstDate = self.absFirstLastDates[0]
|
||||||
|
absLastDate = self.absFirstLastDates[1]
|
||||||
|
date = absFirstDate
|
||||||
|
#while date != absLastDate or date == absLastDate:
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def main(self):
|
def main(self):
|
||||||
# Makes list with ['firstDate', 'lastDate', [allDates], values]
|
# Makes list with ['firstDate', 'lastDate', [allDates], values]
|
||||||
|
|
||||||
@ -335,37 +344,45 @@ class Stock:
|
|||||||
listIEX = Stock.getIEX(self)
|
listIEX = Stock.getIEX(self)
|
||||||
#print(listIEX)
|
#print(listIEX)
|
||||||
listOfFirstLastDates.append((listIEX[0], listIEX[1]))
|
listOfFirstLastDates.append((listIEX[0], listIEX[1]))
|
||||||
|
self.allLists.append(listIEX)
|
||||||
|
|
||||||
# Alpha Vantage
|
# Alpha Vantage
|
||||||
print("\nAlpha Vantage (AV)")
|
print("\nAlpha Vantage (AV)")
|
||||||
listAV = Stock.getAV(self)
|
listAV = Stock.getAV(self)
|
||||||
#print(listAV)
|
#print(listAV)
|
||||||
listOfFirstLastDates.append((listAV[0], listAV[1]))
|
listOfFirstLastDates.append((listAV[0], listAV[1]))
|
||||||
|
self.allLists.append(listAV)
|
||||||
|
|
||||||
print("\nTiingo") # COMMENTED OUT FOR NOW B/C LIMITED TO 400 REQUESTS/DAY
|
print("\nTiingo") # COMMENTED OUT FOR NOW B/C LIMITED TO 400 REQUESTS/DAY
|
||||||
#listTiingo = Stock.getTiingo(self)
|
#listTiingo = Stock.getTiingo(self)
|
||||||
#print(listTiingo)
|
#print(listTiingo)
|
||||||
#listOfFirstLastDates.append((listTiingo[0], listTiingo[1]))
|
#listOfFirstLastDates.append((listTiingo[0], listTiingo[1]))
|
||||||
|
# self.allLists.append(listTiingo)
|
||||||
|
|
||||||
#print(listOfFirstLastDates)
|
#print(listOfFirstLastDates)
|
||||||
absFirstLastDates = Stock.getFirstLastDate(self, listOfFirstLastDates)
|
absFirstLastDates = Stock.getFirstLastDate(self, listOfFirstLastDates)
|
||||||
print("\nThe absolute first date was:", absFirstLastDates[0])
|
print("\nThe absolute first date with close values is:", absFirstLastDates[0])
|
||||||
print("The absolute last date was:", absFirstLastDates[1])
|
print("The absolute last date with close values is:", 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
|
self.absFirstLastDates = absFirstLastDates
|
||||||
'''
|
|
||||||
|
|
||||||
def main():
|
finalDatesAndClose = Stock.getFinalDatesAndClose(self) # Returns [List of Dates, List of Corresponding Close Values]
|
||||||
stock = 'spy'
|
#print(finalDatesAndClose)
|
||||||
spy = Stock(stock)
|
|
||||||
Stock.main(stock)
|
#print(self.allLists)
|
||||||
#Stock.printDates(spy)
|
|
||||||
|
def main(): # For testing purposes
|
||||||
|
stockName = 'aapl'
|
||||||
|
stock1 = Stock(stockName)
|
||||||
|
print("Finding available dates and close values for", stock1.name)
|
||||||
|
Stock.main(stock1)
|
||||||
|
|
||||||
|
def imported(): # When this file has been imported
|
||||||
|
# Add stuff here
|
||||||
|
return
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
|
||||||
|
else:
|
||||||
|
imported()
|
Loading…
Reference in New Issue
Block a user