Reduced logging

This commit is contained in:
Andrew Dinh 2019-04-21 15:59:16 -07:00
parent 457379c3ba
commit 67faa403eb
2 changed files with 45 additions and 27 deletions

View File

@ -35,12 +35,13 @@ python main.py
``` ```
- Common mutual funds are listed in `stocks.txt` - Common mutual funds are listed in `stocks.txt`
- Configure and rename `config.example.json` to `config.json` if you would like to skip beginning questions (only for advanced users) - Configure and rename `config.example.json` to `config.json` if you would like to skip some beginning questions (only for advanced users)
## Planned Features ## Planned Features
- Graphical user interface (GUI) - Graphical user interface (GUI)
- Multithreading/asynchronous requests - Multithreading/asynchronous requests
- Option to change amount to log (DEBUG, INFO, ERRORS)
## Contributing ## Contributing

69
main.py
View File

@ -83,6 +83,7 @@ class Stock:
removeOutliers = True removeOutliers = True
sourceList = ['Yahoo', 'Alpha Vantage', 'IEX', 'Tiingo'] sourceList = ['Yahoo', 'Alpha Vantage', 'IEX', 'Tiingo']
plotIndicatorRegression = False plotIndicatorRegression = False
timePlotIndicatorRegression = 5 # seconds
config = 'N/A' config = 'N/A'
# BENCHMARK VALUES # BENCHMARK VALUES
@ -169,7 +170,7 @@ class Stock:
values.append(value) values.append(value)
listIEX.append(values) listIEX.append(values)
print(len(listIEX[0]), 'dates and', len(listIEX[1]), "close values") # print(len(listIEX[0]), 'dates and', len(listIEX[1]), "close values")
return listIEX return listIEX
def AV(self): def AV(self):
@ -204,7 +205,7 @@ class Stock:
values.append(float(value)) values.append(float(value))
# listAV.append(values) # listAV.append(values)
listAV.append(list(reversed(values))) listAV.append(list(reversed(values)))
print(len(listAV[0]), 'dates and', len(listAV[1]), "close values") # print(len(listAV[0]), 'dates and', len(listAV[1]), "close values")
return listAV return listAV
@ -258,8 +259,7 @@ class Stock:
# Used loop from finding dates # Used loop from finding dates
listTiingo.append(values) listTiingo.append(values)
print(len(listTiingo[0]), 'dates and', # print(len(listTiingo[0]), 'dates and', len(listTiingo[1]), "close values")
len(listTiingo[1]), "close values")
return listTiingo return listTiingo
def Yahoo(self): def Yahoo(self):
@ -304,8 +304,7 @@ class Stock:
else: else:
break break
print(len(listYahoo[0]), 'dates and', # print(len(listYahoo[0]), 'dates and', len(listYahoo[1]), "close values")
len(listYahoo[1]), "close values")
return listYahoo return listYahoo
def datesAndClose(self): def datesAndClose(self):
@ -354,7 +353,7 @@ class Stock:
return datesAndCloseList return datesAndCloseList
def datesAndCloseFitTimeFrame(self): def datesAndCloseFitTimeFrame(self):
print('\nShortening list to fit time frame') # print('\nShortening list to fit time frame')
# Have to do this because if I just make dates = self.allDates & closeValues = self.allCloseValues, then deleting from dates & closeValues also deletes it from self.allDates & self.allCloseValues (I'm not sure why) # Have to do this because if I just make dates = self.allDates & closeValues = self.allCloseValues, then deleting from dates & closeValues also deletes it from self.allDates & self.allCloseValues (I'm not sure why)
dates = [] dates = []
closeValues = [] closeValues = []
@ -367,8 +366,7 @@ class Stock:
# print(self.timeFrame, ' months ago: ', firstDate, sep='') # print(self.timeFrame, ' months ago: ', firstDate, sep='')
closestDate = Functions.getNearest(dates, firstDate) closestDate = Functions.getNearest(dates, firstDate)
if closestDate != firstDate: if closestDate != firstDate:
print('Closest date available for ' + # print('Closest date available to ' + str(self.timeFrame) + ' months ago: ' + str(closestDate))
str(self.name) + ':' + ' ' + str(closestDate))
firstDate = closestDate firstDate = closestDate
else: else:
print(self.name, 'has a close value for', firstDate) print(self.name, 'has a close value for', firstDate)
@ -780,10 +778,6 @@ class Stock:
elif Stock.indicator == 'Market Capitalization': elif Stock.indicator == 'Market Capitalization':
indicatorValue = str( indicatorValue = str(
input(Stock.indicator + ' of ' + self.name + ': ')) input(Stock.indicator + ' of ' + self.name + ': '))
else:
cprint(
'Something is wrong. Indicator was not found. Ending program', 'white', 'on_red')
exit()
if Functions.strintIsFloat(indicatorValue) is True: if Functions.strintIsFloat(indicatorValue) is True:
indicatorValueFound = True indicatorValueFound = True
@ -1250,7 +1244,7 @@ def returnMain(benchmark, listOfStocks):
listOfStocks[i].name + ' from list of stocks', 'yellow') listOfStocks[i].name + ' from list of stocks', 'yellow')
del listOfStocks[i] del listOfStocks[i]
if len(listOfStocks) == 0: if len(listOfStocks) == 0:
print('No stocks fit time frame. Ending program') # print('No stocks fit time frame. Ending program')
cprint('No stocks fit time frame. Ending program', cprint('No stocks fit time frame. Ending program',
'white', 'on_red') 'white', 'on_red')
exit() exit()
@ -1281,7 +1275,7 @@ def returnMain(benchmark, listOfStocks):
str(len(listOfStocks)), 'green') str(len(listOfStocks)), 'green')
if len(listOfStocks) < 2: if len(listOfStocks) < 2:
# print('Cannot proceed to the next step. Exiting program') # print('Cannot proceed to the next step. Exiting program')
cprint('Cannot proceed to the next step. Exiting program', cprint('Unable to proceed. Exiting program',
'white', 'on_red') 'white', 'on_red')
exit() exit()
@ -1400,8 +1394,9 @@ def plot_regression_line(x, y, b, i):
plt.ylabel(listOfReturnStrings[i]) plt.ylabel(listOfReturnStrings[i])
# function to show plot # function to show plot
t = Stock.timePlotIndicatorRegression
plt.show(block=False) plt.show(block=False)
for i in range(3, 0, -1): for i in range(t, 0, -1):
if i == 1: if i == 1:
sys.stdout.write('Keeping plot open for ' + sys.stdout.write('Keeping plot open for ' +
str(i) + ' second \r') str(i) + ' second \r')
@ -1453,19 +1448,23 @@ def indicatorMain(listOfStocks):
listOfStocks[i]) listOfStocks[i])
except: except:
print('Error retrieving indicator data') print('Error retrieving indicator data')
listOfStocks[i].indicatorValue = 'N/A' print('\nWould you like to enter a ' + str(Stock.indicator) + ' value for ' + str(listOfStocks[i].name) + '?')
print('') r = Functions.trueOrFalse()
if r is True:
listOfStocks[i].indicatorValue = 'Remove'
else:
listOfStocks[i].indicatorValue = 'N/A'
if listOfStocks[i].indicatorValue == 'N/A': if listOfStocks[i].indicatorValue == 'N/A':
listOfStocks[i].indicatorValue = Stock.indicatorManual( listOfStocks[i].indicatorValue = Stock.indicatorManual(
listOfStocks[i]) listOfStocks[i])
elif listOfStocks[i].indicatorValue == 'Stock': elif listOfStocks[i].indicatorValue == 'Stock' or listOfStocks[i].indicatorValue == 'Remove':
cprint('Removing ' + cprint('Removing ' +
listOfStocks[i].name + ' from list of stocks', 'yellow') listOfStocks[i].name + ' from list of stocks', 'yellow')
del listOfStocks[i] del listOfStocks[i]
if len(listOfStocks) < 2: if len(listOfStocks) < 2:
# print('Not able to go to the next step. Ending program') # print('Not able to go to the next step. Ending program')
cprint('Not able to go to the next step. Ending program', cprint('Unable to proceed. Ending program',
'white', 'on_red') 'white', 'on_red')
exit() exit()
else: else:
@ -1473,6 +1472,7 @@ def indicatorMain(listOfStocks):
listOfStocks[i].indicatorValue) listOfStocks[i].indicatorValue)
listOfStocksIndicatorValues.append(listOfStocks[i].indicatorValue) listOfStocksIndicatorValues.append(listOfStocks[i].indicatorValue)
i = i + 1 i = i + 1
print('')
# Remove outliers # Remove outliers
if Stock.removeOutliers is True: if Stock.removeOutliers is True:
@ -1543,7 +1543,7 @@ def indicatorMain(listOfStocks):
print('\n', end='') print('\n', end='')
for i in range(0, len(Stock.indicatorCorrelation), 1): for i in range(0, len(Stock.indicatorCorrelation), 1):
formula = ''.join( formula = ''.join(
('y = ', str(round(float(Stock.indicatorRegression[i][0]), 2)), 'x + ', str(round(float(Stock.indicatorRegression[i][1]), 2)))) ('f(x) = ', str(round(float(Stock.indicatorRegression[i][0]), 2)), 'x + ', str(round(float(Stock.indicatorRegression[i][1]), 2))))
print('Linear regression equation for ' + Stock.indicator.lower() + ' and ' + print('Linear regression equation for ' + Stock.indicator.lower() + ' and ' +
listOfReturnStrings[i].lower() + ': ' + formula) listOfReturnStrings[i].lower() + ': ' + formula)
@ -1574,15 +1574,32 @@ def plotIndicatorRegression():
print( print(
'matplotlib is not installed. \nIf you would like to install' + 'matplotlib is not installed. \nIf you would like to install' +
' it (and have a display), run `pip install matplotlib`') ' it (and have a display), run `pip install matplotlib`')
return False Stock.plotIndicatorRegression = False
else: else:
print('\nWould you like to plot indicator linear regression ' print('\nWould you like to plot indicator linear regression '
'results?') 'results?')
plotLinear = Functions.trueOrFalse() plotLinear = Functions.trueOrFalse()
if plotLinear is True: if plotLinear is True:
return True Stock.plotIndicatorRegression = True
else: else:
return False Stock.plotIndicatorRegression = False
else:
Stock.plotIndicatorRegression = False
# Ask for how long
if Stock.plotIndicatorRegression is True:
timeFound = False
print('')
while timeFound is False:
x = str(input('How long would you like to keep the graph up (seconds)? '))
if Functions.stringIsInt(x) is True:
if int(x) > 0:
Stock.timePlotIndicatorRegression = int(x)
timeFound = True
else:
print('Please choose a number greater than zero')
else:
print('Please choose an integer')
def main(): def main():
@ -1640,8 +1657,8 @@ def main():
Stock.removeOutliers = outlierChoice() Stock.removeOutliers = outlierChoice()
# Check if matplotlib is installed and if so, ask user if # Check if matplotlib is installed and if so, ask user if
# they want to plot # they want to plot and for how long
Stock.plotIndicatorRegression = plotIndicatorRegression() plotIndicatorRegression()
else: else:
if Stock.config['Check Packages'] is not False: if Stock.config['Check Packages'] is not False: