mirror of
https://github.com/andrewkdinh/fund-indicators.git
synced 2024-11-22 04:54:22 -08:00
Update StockData.py
Added function to combine all dates and find the average close values for each date
This commit is contained in:
parent
1c9d09aebe
commit
f0d0fccf8b
138
StockData.py
138
StockData.py
@ -193,7 +193,7 @@ class Stock:
|
|||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
'Authorization' : token
|
'Authorization' : token
|
||||||
}
|
}
|
||||||
url = ''.join(('https://api.tiingo.com/tiingo/daily/', self))
|
url = ''.join(('https://api.tiingo.com/tiingo/daily/', self.name))
|
||||||
print("\nSending request to:", url)
|
print("\nSending request to:", url)
|
||||||
requestResponse = requests.get(url, headers=headers)
|
requestResponse = requests.get(url, headers=headers)
|
||||||
#print(requestResponse.json())
|
#print(requestResponse.json())
|
||||||
@ -281,15 +281,15 @@ class Stock:
|
|||||||
date = listOfFirstDates[i]
|
date = listOfFirstDates[i]
|
||||||
if i == 0:
|
if i == 0:
|
||||||
firstDate = date
|
firstDate = date
|
||||||
yearMonthDate = firstDate.split('-')
|
yearMonthDay = firstDate.split('-')
|
||||||
firstYear = yearMonthDate[0]
|
firstYear = yearMonthDay[0]
|
||||||
firstMonth = yearMonthDate[1]
|
firstMonth = yearMonthDay[1]
|
||||||
firstDay = yearMonthDate[2]
|
firstDay = yearMonthDay[2]
|
||||||
else:
|
else:
|
||||||
yearMonthDate = date.split('-')
|
yearMonthDay = date.split('-')
|
||||||
year = yearMonthDate[0]
|
year = yearMonthDay[0]
|
||||||
month = yearMonthDate[1]
|
month = yearMonthDay[1]
|
||||||
day = yearMonthDate[2]
|
day = yearMonthDay[2]
|
||||||
if year < firstYear or (year == firstYear and month < firstMonth) or (year == firstYear and month == firstMonth and day < firstDay):
|
if year < firstYear or (year == firstYear and month < firstMonth) or (year == firstYear and month == firstMonth and day < firstDay):
|
||||||
firstDate = date
|
firstDate = date
|
||||||
firstYear = year
|
firstYear = year
|
||||||
@ -300,15 +300,15 @@ class Stock:
|
|||||||
date = listOfLastDates[i]
|
date = listOfLastDates[i]
|
||||||
if i == 0:
|
if i == 0:
|
||||||
lastDate = date
|
lastDate = date
|
||||||
yearMonthDate = lastDate.split('-')
|
yearMonthDay = lastDate.split('-')
|
||||||
lastYear = yearMonthDate[0]
|
lastYear = yearMonthDay[0]
|
||||||
lastMonth = yearMonthDate[1]
|
lastMonth = yearMonthDay[1]
|
||||||
lastDay = yearMonthDate[2]
|
lastDay = yearMonthDay[2]
|
||||||
else:
|
else:
|
||||||
yearMonthDate = date.split('-')
|
yearMonthDay = date.split('-')
|
||||||
year = yearMonthDate[0]
|
year = yearMonthDay[0]
|
||||||
month = yearMonthDate[1]
|
month = yearMonthDay[1]
|
||||||
day = yearMonthDate[2]
|
day = yearMonthDay[2]
|
||||||
if year > lastYear or (year == lastYear and month > lastMonth) or (year == lastYear and month == lastMonth and day > lastDay):
|
if year > lastYear or (year == lastYear and month > lastMonth) or (year == lastYear and month == lastMonth and day > lastDay):
|
||||||
lastDate = date
|
lastDate = date
|
||||||
lastYear = year
|
lastYear = year
|
||||||
@ -322,17 +322,89 @@ class Stock:
|
|||||||
|
|
||||||
def getFinalDatesAndClose(self):
|
def getFinalDatesAndClose(self):
|
||||||
# finalDates and finalClose will coincide (aka i = 1 will correspond to one another)
|
# finalDates and finalClose will coincide (aka i = 1 will correspond to one another)
|
||||||
finalDatesAndClose = () # Will combine finalDates then finalClose
|
finalDatesAndClose = [] # Will combine finalDates then finalClose
|
||||||
finalDates = []
|
finalDates = []
|
||||||
finalClose = []
|
finalClose = []
|
||||||
#print(self.absFirstLastDates)
|
#print(self.absFirstLastDates)
|
||||||
absFirstDate = self.absFirstLastDates[0]
|
absFirstDate = self.absFirstLastDates[0]
|
||||||
absLastDate = self.absFirstLastDates[1]
|
absLastDate = self.absFirstLastDates[1]
|
||||||
date = absFirstDate
|
date = absFirstDate
|
||||||
#while date != absLastDate or date == absLastDate:
|
|
||||||
|
|
||||||
|
allLists = self.allLists
|
||||||
|
while date != absLastDate: # DOESN'T DO LAST DATE
|
||||||
|
tempListOfClose = []
|
||||||
|
found = False
|
||||||
|
for j in range(0, len(allLists), 1): # Look for date in all lists
|
||||||
|
list1 = allLists[j]
|
||||||
|
listOfDates = list1[2]
|
||||||
|
listOfClose = list1[3]
|
||||||
|
for k in range(0, len(listOfDates), 1):
|
||||||
|
if listOfDates[k] == date:
|
||||||
|
if found == False:
|
||||||
|
finalDates.append(date)
|
||||||
|
found = True
|
||||||
|
#print(listOfDates[k])
|
||||||
|
#print(listOfClose[k])
|
||||||
|
#print(listOfClose)
|
||||||
|
tempListOfClose.append(float(listOfClose[k]))
|
||||||
|
k = len(listOfDates) # Dates don't repeat
|
||||||
|
|
||||||
|
if found == True:
|
||||||
|
sum = 0
|
||||||
|
for r in range(0, len(tempListOfClose), 1):
|
||||||
|
sum = sum + tempListOfClose[r]
|
||||||
|
close = sum/len(tempListOfClose)
|
||||||
|
|
||||||
|
finalClose.append(close)
|
||||||
|
#print(close)
|
||||||
|
|
||||||
|
# Go to the next day
|
||||||
|
yearMonthDay = date.split('-')
|
||||||
|
year = int(yearMonthDay[0])
|
||||||
|
month = int(yearMonthDay[1])
|
||||||
|
day = int(yearMonthDay[2])
|
||||||
|
|
||||||
|
day = day + 1
|
||||||
|
if day == 32 and month == 12: # Next year
|
||||||
|
day = 1
|
||||||
|
month = 2
|
||||||
|
year = year + 1
|
||||||
|
elif day == 32: # Next month
|
||||||
|
month = month + 1
|
||||||
|
day = 1
|
||||||
|
if day < 10:
|
||||||
|
day = ''.join(('0', str(day)))
|
||||||
|
if month < 10:
|
||||||
|
month = ''.join(('0', str(month)))
|
||||||
|
date = ''.join((str(year), '-', str(month), '-', str(day)))
|
||||||
|
#print(date)
|
||||||
|
|
||||||
|
# For last date
|
||||||
|
finalDates.append(date)
|
||||||
|
tempListOfClose = []
|
||||||
|
for j in range(0, len(allLists), 1): # Look for date in all lists
|
||||||
|
list1 = allLists[j]
|
||||||
|
listOfDates = list1[2]
|
||||||
|
listOfClose = list1[3]
|
||||||
|
for k in range(0, len(listOfDates), 1):
|
||||||
|
if listOfDates[k] == date:
|
||||||
|
tempListOfClose.append(float(listOfClose[k]))
|
||||||
|
k = len(listOfDates) # Dates don't repeat
|
||||||
|
sum = 0
|
||||||
|
for r in range(0, len(tempListOfClose), 1):
|
||||||
|
sum = sum + tempListOfClose[r]
|
||||||
|
close = sum/len(tempListOfClose)
|
||||||
|
finalClose.append(close)
|
||||||
|
#print(finalDates)
|
||||||
|
#print(finalClose)
|
||||||
|
|
||||||
|
# Want lists from most recent to oldest, comment this out if you don't want that
|
||||||
|
finalDates = list(reversed(finalDates))
|
||||||
|
finalClose = list(reversed(finalClose))
|
||||||
|
|
||||||
|
finalDatesAndClose.append(finalDates)
|
||||||
|
finalDatesAndClose.append(finalClose)
|
||||||
|
return finalDatesAndClose
|
||||||
|
|
||||||
def main(self):
|
def main(self):
|
||||||
# Makes list with ['firstDate', 'lastDate', [allDates], values]
|
# Makes list with ['firstDate', 'lastDate', [allDates], values]
|
||||||
@ -353,23 +425,25 @@ class Stock:
|
|||||||
listOfFirstLastDates.append((listAV[0], listAV[1]))
|
listOfFirstLastDates.append((listAV[0], listAV[1]))
|
||||||
self.allLists.append(listAV)
|
self.allLists.append(listAV)
|
||||||
|
|
||||||
print("\nTiingo") # COMMENTED OUT FOR NOW B/C LIMITED TO 400 REQUESTS/DAY
|
# COMMENTED OUT FOR NOW B/C LIMITED TO 400 REQUESTS/DAY
|
||||||
#listTiingo = Stock.getTiingo(self)
|
print("\nTiingo")
|
||||||
|
listTiingo = Stock.getTiingo(self)
|
||||||
#print(listTiingo)
|
#print(listTiingo)
|
||||||
#listOfFirstLastDates.append((listTiingo[0], listTiingo[1]))
|
listOfFirstLastDates.append((listTiingo[0], listTiingo[1]))
|
||||||
# self.allLists.append(listTiingo)
|
self.allLists.append(listTiingo)
|
||||||
|
|
||||||
#print(listOfFirstLastDates)
|
|
||||||
absFirstLastDates = Stock.getFirstLastDate(self, listOfFirstLastDates)
|
|
||||||
print("\nThe absolute first date with close values is:", absFirstLastDates[0])
|
|
||||||
print("The absolute last date with close values is:", absFirstLastDates[1])
|
|
||||||
|
|
||||||
self.absFirstLastDates = absFirstLastDates
|
|
||||||
|
|
||||||
finalDatesAndClose = Stock.getFinalDatesAndClose(self) # Returns [List of Dates, List of Corresponding Close Values]
|
|
||||||
#print(finalDatesAndClose)
|
|
||||||
|
|
||||||
#print(self.allLists)
|
#print(self.allLists)
|
||||||
|
#print(listOfFirstLastDates)
|
||||||
|
self.absFirstLastDates = Stock.getFirstLastDate(self, listOfFirstLastDates)
|
||||||
|
print("\nThe absolute first date with close values is:", self.absFirstLastDates[0])
|
||||||
|
print("The absolute last date with close values is:", self.absFirstLastDates[1])
|
||||||
|
|
||||||
|
print("\nCombining dates and finding average close values")
|
||||||
|
self.finalDatesAndClose = Stock.getFinalDatesAndClose(self) # Returns [List of Dates, List of Corresponding Close Values]
|
||||||
|
#print("All dates available:", self.finalDatesAndClose[0])
|
||||||
|
#print("All close values:\n", self.finalDatesAndClose[1])
|
||||||
|
print("Uncomment above line in code to see output")
|
||||||
|
|
||||||
def main(): # For testing purposes
|
def main(): # For testing purposes
|
||||||
stockName = 'aapl'
|
stockName = 'aapl'
|
||||||
|
Loading…
Reference in New Issue
Block a user