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)
示例#2
0
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 PlotPrediction(ticker: str = '^SPX',
                   predictionMethod: int = 0,
                   daysToGraph: int = 60,
                   daysForward: int = 5,
                   learnhingEpochs: int = 500):
    print('Plotting predictions for ' + ticker)
    prices = PricingData(ticker)
    if prices.LoadHistory(True):
        prices.NormalizePrices()
        prices.PredictPrices(predictionMethod, daysForward, learnhingEpochs)
        prices.NormalizePrices()
        prices.GraphData(None, daysToGraph,
                         ticker + ' ' + str(daysToGraph) + 'days', True, True,
                         str(daysToGraph) + 'days')
        prices.SaveStatsToFile(True)
示例#4
0
def TestPredictionModels(ticker: str = '^SPX',
                         numberOfLearningPasses: int = 300):
    #Simple procedure to test different prediction methods 4,20,60 days in the future
    plot = PlotHelper()
    prices = PricingData(ticker)
    if prices.LoadHistory():
        prices.TrimToDateRange('1/1/2000', '3/1/2018')
        print('Loading ' + ticker)
        for daysForward in [4, 20, 60]:
            for predictionMethod in range(0, 5):
                modelDescription = ticker + '_method' + str(
                    predictionMethod) + '_epochs' + str(
                        numberOfLearningPasses) + '_daysforward' + str(
                            daysForward)
                print('Predicting ' + str(daysForward) +
                      ' days using method ' + modelDescription)
                prices.PredictPrices(predictionMethod, daysForward,
                                     numberOfLearningPasses)
                predDF = prices.pricePredictions.copy()
                predDF = predDF.join(prices.GetPriceHistory())
                predDF['PercentageDeviation'] = abs(
                    (predDF['Average'] - predDF['estAverage']) /
                    predDF['Average'])
                averageDeviation = predDF['PercentageDeviation'].tail(
                    round(predDF.shape[0] / 4)).mean(
                    )  #Average of the last 25% to account for training.
                print('Average deviation: ', averageDeviation * 100, '%')
                predDF.to_csv(dataFolder + modelDescription + '.csv')
                plot.PlotDataFrame(predDF[['estAverage', 'Average']],
                                   modelDescription, 'Date', 'Price', True,
                                   dataFolder + modelDescription)
                plot.PlotDataFrameDateRange(
                    predDF[['Average', 'estAverage']], None, 160,
                    modelDescription + '_last160ays', 'Date', 'Price',
                    dataFolder + modelDescription + '_last160Days')
                plot.PlotDataFrameDateRange(
                    predDF[['Average', 'estAverage']], None, 500,
                    modelDescription + '_last500Days', 'Date', 'Price',
                    dataFolder + modelDescription + '_last500Days')