fund-indicators/StockReturn.py

191 lines
7.1 KiB
Python
Raw Normal View History

2019-01-16 08:54:06 -08:00
# ExpenseRatio.py
# Andrew Dinh
# Python 3.6.7
# Description:
'''
Calculates return for each stock from the lists from ExpenseRatio.py
2019-01-30 08:18:34 -08:00
listOfReturn = [Unadjusted Return, Sharpe Ratio, Sortino Ratio, Treynor Ratio, Jensen's Alpha]
2019-01-16 08:54:06 -08:00
'''
from StockData import StockData
import datetime
from Functions import Functions
class Return:
def __init__(self, newListOfReturn = [], newTimeFrame = [], newBeta = 0, newStandardDeviation = 0, newNegativeStandardDeviation = 0, newMarketReturn = 0, newSize = 0, newSizeOfNeg = 0, newFirstLastDates = [], newAllLists = [], newAbsFirstLastDates = ''):
self.listOfReturn = newListOfReturn
2019-01-30 08:18:34 -08:00
self.timeFrame = newTimeFrame # [years, months (30 days)]
2019-01-16 08:54:06 -08:00
self.beta = newBeta
self.standardDeviation = newStandardDeviation
self.negativeStandardDeviation = newNegativeStandardDeviation
self.marketReturn = newMarketReturn
self.size = newSize
self.sizeOfNeg = newSizeOfNeg
self.firstLastDates = newFirstLastDates
2019-01-30 08:18:34 -08:00
def returnTimeFrame(self):
return self.timeFrame
def setTimeFrame(self, newTimeFrame):
self.timeFrame = newTimeFrame
2019-01-16 08:54:06 -08:00
def getFirstLastDates(self, stock):
firstLastDates = []
timeFrame = self.timeFrame
firstDate = datetime.datetime.now() - datetime.timedelta(days=timeFrame[0]*365)
firstDate = firstDate - datetime.timedelta(days=timeFrame[1]*30)
firstDate = ''.join((str(firstDate.year),'-', str(firstDate.month), '-', str(firstDate.day)))
lastDate = StockData.returnAbsFirstLastDates(stock)[1]
#print(lastDate)
firstLastDates.append(firstDate)
firstLastDates.append(lastDate)
return firstLastDates
2019-01-17 08:50:19 -08:00
def getFirstLastDates2(self, stock):
2019-01-16 08:54:06 -08:00
finalDatesAndClose = StockData.returnFinalDatesAndClose(stock)
finalDatesAndClose2 = StockData.returnFinalDatesAndClose2(stock)
firstDate = self.firstLastDates[0]
lastDate = self.firstLastDates[1]
finalDates = finalDatesAndClose[0]
2019-01-17 08:50:19 -08:00
firstDateExists = False
lastDateExists = False
2019-01-16 08:54:06 -08:00
for i in range(0, len(finalDates), 1):
2019-01-17 08:50:19 -08:00
if finalDates[i] == str(firstDate):
firstDateExists = True
2019-01-16 08:54:06 -08:00
elif finalDates[i] == lastDate:
2019-01-17 08:50:19 -08:00
lastDateExists = True
2019-01-16 08:54:06 -08:00
i = len(finalDates)
2019-01-17 08:50:19 -08:00
if firstDateExists == False:
2019-01-16 08:54:06 -08:00
print("Could not find first date. Changing first date to closest date")
2019-01-17 08:50:19 -08:00
tempDate = Functions.stringToDate(firstDate) # Change to datetime
2019-01-17 16:40:42 -08:00
print('Original first date:', tempDate)
2019-01-17 08:50:19 -08:00
#tempDate = datetime.date(2014,1,17)
newFirstDate = Functions.getNearest(finalDatesAndClose2[0], tempDate)
2019-01-17 16:40:42 -08:00
print('New first date:', newFirstDate)
2019-01-17 08:50:19 -08:00
firstDate = str(newFirstDate)
2019-01-16 08:54:06 -08:00
2019-01-17 08:50:19 -08:00
if lastDateExists == False:
print("Could not find final date. Changing final date to closest date")
tempDate2 = Functions.stringToDate(lastDate) # Change to datetime
2019-01-17 16:40:42 -08:00
print('Original final date:', tempDate2)
2019-01-17 08:50:19 -08:00
#tempDate2 = datetime.date(2014,1,17)
newLastDate = Functions.getNearest(finalDatesAndClose2[0], tempDate2)
2019-01-17 16:40:42 -08:00
print('New final date:', newLastDate)
2019-01-17 08:50:19 -08:00
lastDate = str(newLastDate)
2019-01-16 08:54:06 -08:00
2019-01-17 08:50:19 -08:00
firstLastDates = []
firstLastDates.append(firstDate)
firstLastDates.append(lastDate)
return firstLastDates
def getUnadjustedReturn(self, stock):
finalDatesAndClose = StockData.returnFinalDatesAndClose(stock)
firstDate = self.firstLastDates[0]
lastDate = self.firstLastDates[1]
finalDates = finalDatesAndClose[0]
2019-01-17 16:40:42 -08:00
finalClose = finalDatesAndClose[1]
for i in range(0, len(finalDates), 1):
if finalDates[i] == str(firstDate):
firstClose = finalClose[i]
elif finalDates[i] == lastDate:
lastClose = finalClose[i]
i = len(finalDates)
print('Close values:', firstClose, '...', lastClose)
2019-01-30 08:18:34 -08:00
fullUnadjustedReturn = float(lastClose/firstClose)
unadjustedReturn = fullUnadjustedReturn**(1/(self.timeFrame[0]+(self.timeFrame[1])*.1))
2019-01-17 16:40:42 -08:00
return unadjustedReturn
2019-01-16 08:54:06 -08:00
2019-01-30 08:18:34 -08:00
def getBeta(self):
# Can be calculated with correlation
import numpy as np
finalDatesAndClose = StockData.returnFinalDatesAndClose(stock)
firstDate = self.firstLastDates[0]
lastDate = self.firstLastDates[1]
finalDates = finalDatesAndClose[0]
finalClose = finalDatesAndClose[1]
for i in range(0, len(finalDates), 1):
if finalDates[i] == str(firstDate):
firstClose = finalClose[i]
55ggbh
#list1 =
list2 = [1,2,4,1]
print(numpy.corrcoef(list1, list2)[0, 1])
2019-01-16 08:54:06 -08:00
# def getStandardDeviation(self, timeFrame):
2019-01-30 08:18:34 -08:00
def mainBenchmark(self, stock):
print('Beginning StockReturn.py')
2019-01-16 08:54:06 -08:00
# Find date to start from and last date
self.timeFrame = []
2019-01-17 16:40:42 -08:00
self.listOfReturn = []
2019-01-16 08:54:06 -08:00
print("\nPlease enter a time frame in years: ", end='')
#timeFrameYear = int(input())
timeFrameYear = 5
2019-01-22 10:49:58 -08:00
print(timeFrameYear)
2019-01-16 08:54:06 -08:00
self.timeFrame.append(timeFrameYear)
print("Please enter a time frame in months (30 days): ", end='')
#timeFrameMonth = int(input())
timeFrameMonth = 0
2019-01-22 10:49:58 -08:00
print(timeFrameMonth)
2019-01-16 08:54:06 -08:00
self.timeFrame.append(timeFrameMonth)
#print(self.timeFrame)
self.firstLastDates = Return.getFirstLastDates(self, stock)
print('Dates: ', self.firstLastDates)
2019-01-17 08:50:19 -08:00
print('\nMaking sure dates are within list...')
self.firstLastDates = Return.getFirstLastDates2(self, stock)
print('New dates: ', self.firstLastDates)
2019-01-16 08:54:06 -08:00
print('\nGetting unadjusted return')
2019-01-17 16:40:42 -08:00
unadjustedReturn = Return.getUnadjustedReturn(self, stock)
self.listOfReturn.append(unadjustedReturn)
2019-01-30 08:18:34 -08:00
print('Average annual return for the past', self.timeFrame[0], 'years and', self.timeFrame[1], 'months: ', end='')
print((self.listOfReturn[0]-1)*100, '%', sep='')
def main(self, stock):
print('Beginning StockReturn.py')
# Find date to start from and last date
self.listOfReturn = []
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')
unadjustedReturn = Return.getUnadjustedReturn(self, stock)
self.listOfReturn.append(unadjustedReturn)
print('Average annual return for the past', self.timeFrame[0], 'years and', self.timeFrame[1], 'months: ', end='')
print((self.listOfReturn[0]-1)*100, '%', sep='')
#print('\nGetting beta')
#beta = Return.getBeta(self, stock)
2019-01-16 08:54:06 -08:00
def main():
stockName = 'spy'
stock1 = StockData(stockName)
print("Finding available dates and close values for", stock1.name)
StockData.main(stock1)
stock1Return = Return()
2019-01-30 08:18:34 -08:00
Return.setTimeFrame(stock1Return, [5, 0])
2019-01-16 08:54:06 -08:00
Return.main(stock1Return, stock1)
if __name__ == "__main__":
main()