def DownloadAndGraphStocks(tickerList: list): for ticker in tickerList: prices = PricingData(ticker) print('Loading ' + ticker) if prices.LoadHistory(True): print('Calcualting stats ' + ticker) prices.NormalizePrices() prices.CalculateStats() prices.PredictPrices(2, 15) prices.NormalizePrices() prices.SaveStatsToFile(True) psnap = prices.GetCurrentPriceSnapshot() titleStatistics = ' 5/15 dev: ' + str( round(psnap.fiveDayDeviation * 100, 2)) + '/' + str( round(psnap.fifteenDayDeviation * 100, 2)) + '% ' + str( psnap.low) + '/' + str( psnap.nextDayTarget) + '/' + str( psnap.high) + ' ' + str( psnap.snapshotDate)[:10] print('Graphing ' + ticker + ' ' + str(psnap.snapshotDate)[:10]) for days in [90, 180, 365, 2190, 4380]: prices.GraphData(None, days, ticker + '_days' + str(days) + ' ' + titleStatistics, (days < 1000), True, str(days).rjust(4, '0') + 'd', trimHistoricalPredictions=False)
def DownloadAndGraphStocks(tickerList: list): for ticker in tickerList: prices = PricingData(ticker) print('Loading ' + ticker) if prices.LoadHistory(requestedEndDate=GetTodaysDate()): print('Calcualting stats ' + ticker) prices.NormalizePrices() prices.CalculateStats() prices.PredictPrices(2, 15) prices.NormalizePrices() #prices.SaveStatsToFile(includePredictions=True, verbose=True) psnap = prices.GetCurrentPriceSnapshot() titleStatistics = ' 5/15 dev: ' + str( round(psnap.fiveDayDeviation * 100, 2)) + '/' + str( round(psnap.fifteenDayDeviation * 100, 2)) + '% ' + str( psnap.low) + '/' + str( psnap.nextDayTarget) + '/' + str( psnap.high) + ' ' + str( psnap.snapShotDate)[:10] print('Graphing ' + ticker + ' ' + str(psnap.snapShotDate)[:10]) for days in [90, 180, 365, 2190, 4380]: prices.GraphData(endDate=None, daysToGraph=days, graphTitle=ticker + '_days' + str(days) + ' ' + titleStatistics, includePredictions=(days < 1000), saveToFile=True, fileNameSuffix=str(days).rjust(4, '0') + 'd', trimHistoricalPredictions=False)
def DownloadAndSaveStocksWithStats(tickerList: list): for ticker in tickerList: prices = PricingData(ticker) print('Loading ' + ticker) if prices.LoadHistory(requestedEndDate=GetTodaysDate()): print('Calcualting stats ' + ticker) prices.CalculateStats() prices.SaveStatsToFile(includePredictions=False, verbose=True)
def OpportunityFinder(tickerList: list): outputFolder = 'data/dailypicks/' summaryFile = '_summary.txt' overBoughtList = [] oversoldList = [] highDeviationList = [] for root, dirs, files in os.walk(outputFolder): for f in files: if f.endswith('.txt') or f.endswith('.png'): os.unlink(os.path.join(root, f)) for ticker in tickerList: prices = PricingData(ticker) print('Checking ' + ticker) if prices.LoadHistory(True): prices.CalculateStats() psnap = prices.GetCurrentPriceSnapshot() titleStatistics = ' 5/15 dev: ' + str( round(psnap.fiveDayDeviation * 100, 2)) + '/' + str( round(psnap.fifteenDayDeviation * 100, 2)) + '% ' + str( psnap.low) + '/' + str( psnap.nextDayTarget) + '/' + str( psnap.high) + str(snapshotDate) if psnap.low > psnap.channelHigh: overBoughtList.append(ticker) if psnap.high < psnap.channelLow: oversoldList.append(ticker) prices.GraphData(None, 60, ticker + ' 60d ' + titleStatistics, False, True, '60d', outputFolder) if psnap.fiveDayDeviation > .0275: highDeviationList.append(ticker) prices.GraphData(None, 60, ticker + ' 60d ' + titleStatistics, False, True, '60d', outputFolder) print('Over bought:') print(overBoughtList) print('Over sold:') print(oversoldList) print('High deviation:') print(highDeviationList) f = open(outputFolder + summaryFile, 'w') f.write('Over bought:\n') for t in overBoughtList: f.write(t + '\n') f.write('\nOver sold:\n') for t in oversoldList: f.write(t + '\n') f.write('\nHigh deviation:\n') for t in highDeviationList: f.write(t + '\n') f.close()
def OpportunityFinder(tickerList: list): outputFolder = 'data/dailypicks/' summaryFile = '_DailyPicks.csv' candidates = pd.DataFrame(columns=list([ 'Ticker', 'hp2Year', 'hp1Year', 'hp6mo', 'hp3mo', 'hp2mo', 'hp1mo', 'currentPrice', 'channelHigh', 'channelLow', 'shortEMA', 'longEMA', '2yearPriceChange', '1yearPriceChange', '6moPriceChange', '3moPriceChange', '2moPriceChange', '1moPriceChange', 'dailyGain', 'monthlyGain', 'monthlyLossStd', 'Comments' ])) candidates.set_index(['Ticker'], inplace=True) for root, dirs, files in os.walk(outputFolder): for f in files: if f.endswith('.png'): os.unlink(os.path.join(root, f)) for ticker in tickerList: prices = PricingData(ticker) currentDate = GetTodaysDate() print('Checking ' + ticker) if prices.LoadHistory(requestedEndDate=currentDate): prices.CalculateStats() psnap = prices.GetPriceSnapshot(AddDays(currentDate, -730)) hp2Year = psnap.fiveDayAverage psnap = prices.GetPriceSnapshot(AddDays(currentDate, -365)) hp1Year = psnap.fiveDayAverage psnap = prices.GetPriceSnapshot(AddDays(currentDate, -180)) hp6mo = psnap.fiveDayAverage psnap = prices.GetPriceSnapshot(AddDays(currentDate, -90)) hp3mo = psnap.fiveDayAverage psnap = prices.GetPriceSnapshot(AddDays(currentDate, -60)) hp2mo = psnap.fiveDayAverage psnap = prices.GetPriceSnapshot(AddDays(currentDate, -30)) hp1mo = psnap.fiveDayAverage psnap = prices.GetCurrentPriceSnapshot() currentPrice = psnap.twoDayAverage Comments = '' if psnap.low > psnap.channelHigh: Comments += 'OverBought; ' if psnap.high < psnap.channelLow: Comments += 'OverSold; ' if psnap.fiveDayDeviation > .0275: Comments += 'HighDeviation; ' if Comments != '': titleStatistics = ' 5/15 dev: ' + str( round(psnap.fiveDayDeviation * 100, 2)) + '/' + str( round(psnap.fifteenDayDeviation * 100, 2)) + '% ' + str(psnap.low) + '/' + str( psnap.nextDayTarget) + '/' + str( psnap.high) + str(psnap.snapShotDate) prices.GraphData(None, 60, ticker + ' 60d ' + titleStatistics, False, True, '60d', outputFolder) if (currentPrice > 0 and hp2Year > 0 and hp1Year > 0 and hp6mo > 0 and hp2mo > 0 and hp1mo > 0): #values were loaded candidates.loc[ticker] = [ hp2Year, hp1Year, hp6mo, hp3mo, hp2mo, hp1mo, currentPrice, psnap.channelHigh, psnap.channelLow, psnap.shortEMA, psnap.longEMA, (currentPrice / hp2Year) - 1, (currentPrice / hp1Year) - 1, (currentPrice / hp6mo) - 1, (currentPrice / hp3mo) - 1, (currentPrice / hp2mo) - 1, (currentPrice / hp1mo) - 1, psnap.dailyGain, psnap.monthlyGain, psnap.monthlyLossStd, Comments ] else: print(ticker, currentPrice, hp2Year, hp1Year, hp6mo, hp2mo, hp1mo) print(candidates) candidates.to_csv(outputFolder + summaryFile)
def TrainTickerRaw(ticker: str = '^SPX', UseLSTM: bool = True, prediction_target_days: int = 5, epochs: int = 500, usePercentages: bool = False, hidden_layer_size: int = 512, dropout: bool = True, dropout_rate: float = 0.01, learning_rate: float = 2e-5): plot = PlotHelper() prices = PricingData(ticker) print('Loading ' + ticker) if prices.LoadHistory(True): prices.TrimToDateRange('1/1/2000', '3/1/2018') if usePercentages: prices.ConvertToPercentages( ) #Percentages don't work well I suspect because small errors have a huge impact when you revert back to the original prices and they roll forward else: prices.NormalizePrices() prices.CalculateStats() model = StockPredictionNN(baseModelName=ticker, UseLSTM=UseLSTM) if UseLSTM: window_size = 1 modelDescription = ticker + '_LSTM' modelDescription += '_epochs' + str(epochs) + '_histwin' + str( window_size) + '_daysforward' + str(prediction_target_days) if usePercentages: modelDescription += '_percentages' FieldList = ['Average'] model.LoadSource(sourceDF=prices.GetPriceHistory(), FieldList=FieldList, window_size=window_size) model.LoadTarget(targetDF=None, prediction_target_days=prediction_target_days) model.MakeBatches(batch_size=128, train_test_split=.93) model.BuildModel(layer_count=1, hidden_layer_size=hidden_layer_size, dropout=dropout, dropout_rate=dropout_rate, learning_rate=learning_rate) model.DisplayModel() model.Train(epochs=epochs) model.Predict(True) model.Save() #model.DisplayDataSample() else: #CNN window_size = 16 * prediction_target_days modelDescription = ticker + '_CNN' modelDescription += '_epochs' + str(epochs) + '_histwin' + str( window_size) + '_daysforward' + str(prediction_target_days) if usePercentages: modelDescription += '_percentages' #FieldList = None FieldList = ['High', 'Low', 'Open', 'Close'] model.LoadSource(sourceDF=prices.GetPriceHistory(), FieldList=FieldList, window_size=window_size) model.LoadTarget(targetDF=None, prediction_target_days=prediction_target_days) model.MakeBatches(batch_size=64, train_test_split=.93) model.BuildModel(layer_count=1, hidden_layer_size=hidden_layer_size, dropout=dropout, dropout_rate=dropout_rate, learning_rate=learning_rate) model.DisplayModel() model.Train(epochs=epochs) model.Predict(True) model.Save() if usePercentages: predDF = model.GetTrainingResults(True, True) predDF = predDF.loc[:, ['Average', 'Average_Predicted']] print('Unraveling percentages..') predDF['Average_Predicted'].fillna(0, inplace=True) predDF.iloc[0] = prices.CTPFactor['Average'] for i in range(1, predDF.shape[0]): predDF.iloc[i] = (1 + predDF.iloc[i]) * predDF.iloc[i - 1] print(predDF) predDF['PercentageDeviation'] = abs( (predDF['Average'] - predDF['Average_Predicted']) / predDF['Average']) predDF.to_csv(dataFolder + modelDescription + '.csv') plot.PlotDataFrame(predDF[['Average', 'Average_Predicted']], modelDescription, 'Date', 'Price', True, dataFolder + modelDescription) plot.PlotDataFrameDateRange( predDF[['Average', 'Average_Predicted']], None, 160, modelDescription + '_last160ays', 'Date', 'Price', dataFolder + modelDescription + '_last160Days') plot.PlotDataFrameDateRange( predDF[['Average', 'Average_Predicted']], None, 500, modelDescription + '_last500Days', 'Date', 'Price', dataFolder + modelDescription + '_last500Days') else: model.PredictionResultsSave(modelDescription, True, True) model.PredictionResultsPlot(modelDescription, True, False)