Check list of dates in StockReturn.py

This commit is contained in:
Andrew Dinh 2019-01-17 08:50:19 -08:00
parent eb44066b5f
commit 61e0cd0d91
3 changed files with 56 additions and 26 deletions

View File

@ -4,9 +4,18 @@ class Functions:
return min(items, key=lambda x: abs(x - pivot))
def stringToDate(date):
from datetime import datetime
#datetime_object = datetime.strptime('Jun 1 2005 1:33PM', '%b %d %Y %I:%M%p')
datetime_object = datetime.strptime(date, '%Y-%m-%d').date()
return(datetime_object)
'''
dateSplit = date.split('-')
year = int(dateSplit[0])
month = int(dateSplit[1])
day = int(dateSplit[2])
datetime_object = datetime.date(year, month, day)
'''
return datetime_object
def main():
exit()

View File

@ -29,8 +29,7 @@ Daily Requests = 20,000
Symbol Requests = 500
'''
import requests, json, socket
import importlib.util, sys # To check whether a package is installed
import requests, json
from datetime import datetime
class StockData:
@ -446,8 +445,8 @@ class StockData:
finalClose = finalDatesAndClose[1]
finalDates = []
from Functions import Functions
for i in range(0, len(finalDatesStrings), 1):
from Functions import Functions
temp = Functions.stringToDate(finalDatesStrings[i])
finalDates.append(temp)
#print(finalDates)
@ -457,6 +456,7 @@ class StockData:
return(finalDatesAndClose2)
def is_connected():
import socket # To check internet connection
try:
# connect to the host -- tells us if the host is actually
# reachable
@ -468,6 +468,7 @@ class StockData:
return False
def main(self):
import importlib.util, sys # To check whether a package is installed
packages = ['requests']
for i in range(0, len(packages), 1):

View File

@ -36,35 +36,51 @@ class Return:
firstLastDates.append(lastDate)
return firstLastDates
def getFirstLastDates2(self, stock):
finalDatesAndClose = StockData.returnFinalDatesAndClose(stock)
finalDatesAndClose2 = StockData.returnFinalDatesAndClose2(stock)
firstDate = self.firstLastDates[0]
lastDate = self.firstLastDates[1]
finalDates = finalDatesAndClose[0]
firstDateExists = False
lastDateExists = False
for i in range(0, len(finalDates), 1):
if finalDates[i] == str(firstDate):
firstDateExists = True
elif finalDates[i] == lastDate:
lastDateExists = True
i = len(finalDates)
if firstDateExists == False:
print("Could not find first date. Changing first date to closest date")
tempDate = Functions.stringToDate(firstDate) # Change to datetime
print('Original first date: ', tempDate)
#tempDate = datetime.date(2014,1,17)
newFirstDate = Functions.getNearest(finalDatesAndClose2[0], tempDate)
print('New first date: ', newFirstDate)
firstDate = str(newFirstDate)
if lastDateExists == False:
print("Could not find final date. Changing final date to closest date")
tempDate2 = Functions.stringToDate(lastDate) # Change to datetime
print('Original final date: ', tempDate2)
#tempDate2 = datetime.date(2014,1,17)
newLastDate = Functions.getNearest(finalDatesAndClose2[0], tempDate2)
print('New final date: ', newLastDate)
lastDate = str(newLastDate)
firstLastDates = []
firstLastDates.append(firstDate)
firstLastDates.append(lastDate)
return firstLastDates
def getUnadjustedReturn(self, stock):
finalDatesAndClose = StockData.returnFinalDatesAndClose(stock)
finalDatesAndClose2 = StockData.returnFinalDatesAndClose2(stock)
firstDate = self.firstLastDates[0]
lastDate = self.firstLastDates[1]
finalDates = finalDatesAndClose[0]
finalClose = finalDatesAndClose[1]
firstClose = 0
for i in range(0, len(finalDates), 1):
if finalDates[i] == firstDate:
firstClose = finalClose[i]
elif finalDates[i] == lastDate:
lastClose = finalClose[i]
i = len(finalDates)
if firstClose == 0:
print("Could not find first date. Changing first date to closest date")
temp = Functions.stringToDate(firstDate) # Change to datetime
print('Original first date: ', temp)
newFirstDate = Functions.getNearest(finalDatesAndClose2[0], temp)
print('New first date: ', newFirstDate)
for i in range(0, len(finalDates), 1):
if finalDates[i] == str(newFirstDate):
firstClose = finalClose[i]
print(firstClose)
print(lastClose)
# def getBeta(self, timeFrame):
@ -86,6 +102,10 @@ class Return:
self.firstLastDates = Return.getFirstLastDates(self, stock)
print('Dates: ', self.firstLastDates)
print('\nMaking sure dates are within list...')
self.firstLastDates = Return.getFirstLastDates2(self, stock)
print('New dates: ', self.firstLastDates)
print('\nGetting unadjusted return')
Return.getUnadjustedReturn(self, stock)