Пример #1
0
def Main(args):
    if (len(args) != 3 and len(args) != 4):
        PrintUsage()
        return
    #Test if file exists
    try:
        open(args[0])
    except Exception as e:
        print('Error opening file: ' + args[0])
        print(str(e))
        PrintUsage()
        return
    #Test validity of start date string
    try:
        datetime.strptime(args[1], '%Y-%m-%d').timestamp()
    except Exception as e:
        print(e)
        print('Error parsing date: ' + args[1])
        PrintUsage()
        return
    #Test validity of end date string
    try:
        datetime.strptime(args[2], '%Y-%m-%d').timestamp()
    except Exception as e:
        print('Error parsing date: ' + args[2])
        PrintUsage()
        return
    #Test validity of final optional argument
    if (len(args) == 4):
        predPrd = args[3].upper()
        if (predPrd == 'D'):
            predPrd = 'daily'
        elif (predPrd == 'W'):
            predPrd = 'weekly'
        elif (predPrd == 'M'):
            predPrd = 'monthly'
        else:
            PrintUsage()
            return
    else:
        predPrd = 'daily'
    #Everything looks okay; proceed with program
    #Grab the data frame
    D = ParseData(args[0])
    #The number of previous days of data used
    #when making a prediction
    numPastDays = 16
    PlotData(D)
    #Number of neurons in the input layer
    i = numPastDays * 7 + 1
    #Number of neurons in the output layer
    o = D.shape[1] - 1
    #Number of neurons in the hidden layers
    h = int((i + o) / 2)
    #The list of layer sizes
    #layers = [('F', h), ('AF', 'tanh'), ('F', h), ('AF', 'tanh'), ('F', o)]
    #R = ANNR([i], layers, maxIter = 1000, tol = 0.01, reg = 0.001, verbose = True)
    R = KNeighborsRegressor(n_neighbors=5)
    sp = StockPredictor(R, nPastDays=numPastDays)
    #Learn the dataset and then display performance statistics
    sp.Learn(D)
    sp.TestPerformance()
    #Perform prediction for a specified date range
    P = sp.PredictDate(args[1], args[2], predPrd)
    #Keep track of number of predicted results for plot
    n = P.shape[0]
    #Append the predicted results to the actual results
    D = P.append(D)
    #Predicted results are the first n rows
    PlotData(D, range(n + 1))
    return (P, n)
Пример #2
0
def Main(args):
    if (len(args) != 3 and len(args) != 4):
        PrintUsage()
        return

    #Obtain CSV from Internet
    print(
        '\n#############   Downloading Historical Data from Internet   #############'
    )
    file = args[0] + '.csv'
    str2 = 'http://real-chart.finance.yahoo.com/table.csv?s=' + args[
        0] + '.NS&d=02&e=16&f=2017&g=d&a=0&b=1&c=1996&ignore=.csv'
    f = open(file, 'wb')
    f.write(requests.get(str2).content)
    f.close()
    print('\n#############   Download Complete   ##############')
    #Test if file exists
    try:
        open(file)
    except Exception as e:
        print('Error opening file: ' + file)
        print(str(e))
        PrintUsage()
        return
    #Test validity of start date string
    try:
        datetime.strptime(args[1], '%Y-%m-%d').timestamp()
    except Exception as e:
        print('Error parsing date: ' + args[1])
        PrintUsage()
        return
    #Test validity of end date string
    try:
        datetime.strptime(args[2], '%Y-%m-%d').timestamp()
    except Exception as e:
        print('Error parsing date: ' + args[2])
        PrintUsage()
        return
    #Test validity of final optional argument
    if (len(args) == 4):
        predPrd = args[3].upper()
        if (predPrd == 'D'):
            predPrd = 'daily'
        elif (predPrd == 'W'):
            predPrd = 'weekly'
        elif (predPrd == 'M'):
            predPrd = 'monthly'
        else:
            PrintUsage()
            return
    else:
        predPrd = 'daily'
    print('\n#############   Processing on Data   ##############')
    #Everything looks okay; proceed with program
    D = ParseData(file)
    #The number of previous days of data used
    #when making a prediction
    numPastDays = 20
    PlotData(D, args[0])
    #Number of neurons in the input layer
    i = numPastDays * 7 + 1
    #Number of neurons in the output layer
    o = D.shape[1] - 1
    #Number of neurons in the hidden layers
    h = int((i + o) / 2)
    #The list of layer sizes
    layers = [i, h, h, o]
    #Type of Regressor Used
    #R = RandomForestRegressor(n_estimators = 5)
    R = MLPR(layers, maxItr=1000, tol=0.40, reg=0.001, verbose=True)
    sp = StockPredictor(R, nPastDays=numPastDays)
    #Learn the dataset and then display performance statistics
    sp.Learn(D)
    #sp.TestPerformance()
    #Perform prediction for a specified date range
    P, R = sp.PredictDate(args[1], args[2], predPrd)
    print('\n#############   Predection is on the way   ##############')
    #Keep track of number of predicted results for plot
    n = P.shape[0]
    #Append the predicted results to the actual results
    D = P.append(D)
    #Predicted results are the first n rows
    PlotData(D, args[0], range(n + 1))

    return (R, n)
Пример #3
0
def main():
    predictor = StockPredictor()

    predictor.train(period="Year")
    print("Year prediction:", predictor.predict(2020))
    predictor.plot_prediction(period="Year", numeric_value=2020)

    predictor.train(period="Month")
    print("Month prediction:", predictor.predict(9))
    predictor.plot_prediction(period="Month", numeric_value=9)

    predictor.train(period="Day")
    print("Day prediction:", predictor.predict(20))
    predictor.plot_prediction(period="Day", numeric_value=20)
Пример #4
0
def Main(args):
    if(len(args) != 3 and len(args) != 4):
        PrintUsage()
        return
    #Test if file exists
    try:
        open(args[0])
    except Exception as e:
        print('Error opening file: ' + args[0])
        print(str(e))
        PrintUsage()
        return
    #Test validity of start date string
    try:
        datetime.strptime(args[1], '%Y-%m-%d').timestamp()
    except Exception as e:
        print('Error parsing date: ' + args[1])
        PrintUsage()
        return
    #Test validity of end date string
    try:
        datetime.strptime(args[2], '%Y-%m-%d').timestamp()
    except Exception as e:
        print('Error parsing date: ' + args[2])
        PrintUsage()
        return    
    #Test validity of final optional argument
    if(len(args) == 4):
        predPrd = args[3].upper()
        if(predPrd == 'D'):
            predPrd = 'daily'
        elif(predPrd == 'W'):
            predPrd = 'weekly'
        elif(predPrd == 'M'):
            predPrd = 'monthly'
        else:
            PrintUsage()
            return
    else:
        predPrd = 'daily'
    #Everything looks okay; proceed with program
    #Grab the data frame
    D = ParseData(args[0])
    #The number of previous days of data used
    #when making a prediction
    numPastDays = 15
    #PlotData(D)
    #Number of neurons in the input layer
    i = numPastDays * 7 + 1
    #Number of neurons in the output layer
    o = D.shape[1] - 1
    #Number of neurons in the hidden layers
    h = int((i + o) / 2)
    #The list of layer sizes
    layers = [i, h, h, h, h,h,h, o]
    R = MLPR(layers, maxItr = 100000, tol = 0.01, reg = 0.001, verbose = True, batchSize=200)
    #R = KNeighborsRegressor(n_neighbors = 7)
    sp = StockPredictor(R, nPastDays = numPastDays)


    #Learn the dataset and then display performance statistics
    size=len(D)
    trainingData =DataFrame.reset_index(D[5:size], drop=True)
    print(trainingData)
    sp.Learn(trainingData)
    sp.TestPerformance()
    #Perform prediction for a specified date range
    P= sp.PredictDate(args[1], args[2], predPrd)
    #Keep track of number of predicted results for plot
    print("done")
    print(P)
    n = P.shape[0]
    print("nnnnn")
    print(n)
    #Append the predicted results to the actual results
    #D = P.append(D)
    #Predicted results are the first n rows
    PlotTestData(P, D[0:5])
    return (P, n)