Esempio n. 1
0
def ANNCaller(stockSymbol, isDaily=True):
    dataTrain = readJson(stockSymbol, 10, isDaily)
    # print(dataTrain)
    myNN = NN(11, 5, 1)
    myNN.loadWeights(stockSymbol)

    result = []
    resultJson = {}
    resultJson[stockSymbol] = {}

    prediction = [0] * 4
    for i in range(4):
        # print(dataTrain[i])
        prediction[i] = round(myNN.runNN(dataTrain[i][::-1])[0], 2)
    # print(dataTrain)
    stdev = round(statistics.stdev(dataTrain[3]), 2)
    averageErrorPath = ''
    if isDaily:
        averageErrorPath = path.abspath(
            path.join(
                path.abspath(sys.argv[0]),
                "..")) + '\\dataset\\daily\\prediction_averageErr_ANN.json'
    else:
        averageErrorPath = path.abspath(
            path.join(
                path.abspath(sys.argv[0]),
                "..")) + '\\dataset\\intraday\\prediction_averageErr_ANNA.json'

    with open(averageErrorPath, 'r') as f:
        averageError = json.load(f)[stockSymbol]

    # print(prediction)

    return (prediction[0], prediction[1], prediction[2], prediction[3],
            averageError, stdev)
def bayesianCaller(stockSymbol, isDaily=True):
    n = 20
    m = 6
    date = 0
    # fileArray=['NFLX.csv', 'NVDA.csv', 'TESL.csv', 'WB.csv']
    # dataSource=readCSV(stockSymbol,dataType,date,n)
    dataTrain = readJson(stockSymbol, n, isDaily)

    # readCSV(stockSymbol,dataType,date,n)
    # if date<0 or date+n>=len(dataSource):
    # return [-2,-2,-2]
    # print(dataTrain)
    if date + n > len(dataTrain[0]):
        return ([-2, -2, -2, -2], -2, -2)
    # dataTrain=dataSource[date:date+n][::-1]
    # for n in [6,10,16,20]:
    # print('n='+str(n))
    # for m in [5,6,7,8]:
    # print('n='+str(n)+', m='+str(m))
    # result=[]
    # avgRelErr=0
    # for stockSymbol in fileArray:
    # for pointStart in [1,101,201]:
    prediction = [0] * 4
    for i in range(4):
        # print(dataTrain[i])
        prediction[i] = round(bayesian(dataTrain[i], m, n), 2)
    # print(dataTrain)
    stdev = round(statistics.stdev(dataTrain[3]), 2)
    averageErrorPath = ''
    if isDaily:
        #ctypes.windll.user32.MessageBoxW(0, path.abspath(path.join(path.abspath(sys.argv[0]), "..")), "Your title", 1)
        averageErrorPath = path.abspath(
            path.join(path.abspath(sys.argv[0]), "..")
        ) + '\\dataset\\daily\\prediction_averageErr_bayesian.json'
    else:
        #ctypes.windll.user32.MessageBoxW(0, path.abspath(path.join(path.abspath(sys.argv[0]), "..")), "Your title", 1)
        averageErrorPath = path.abspath(
            path.join(path.abspath(sys.argv[0]), "..")
        ) + '\\dataset\\intraday\\prediction_averageErr_bayesian.json'

    with open(averageErrorPath, 'r') as f:
        averageError = json.load(f)[stockSymbol]

    # print(prediction)

    return (prediction[0], prediction[1], prediction[2], prediction[3],
            averageError, stdev)
Esempio n. 3
0
def callerANN(stockSymbol, isDaily=True):
    dataSource = readJson(stockSymbol, 61, isDaily)
    # print(dataSource)
    global stock
    stock = stockSymbol

    result = []
    # prediction=[0]*4
    # print(dataSource[3][10:])
    dataTrain = dataFormatter(dataSource[3][10:][::-1], 10, 5, 51)[::-1]
    dateArray = []
    for num in [10, 20, 30, 40, 50]:
        dateArray.append(dataSource[4][num])
    # print(dateArray)

    # print(dataTrain)

    myNN = NN(11, 5, 1)
    # myNN.initWeightsRand()
    myNN.loadWeights()
    for inputData, outputData in dataTrain:
        predictionItem = myNN.runNN(inputData)
        print(inputData, outputData, predictionItem)
def bayesianHistory(stockSymbol, dateScope, gap, isDaily=True):
    m = 6
    n = gap
    dataSource = readJson(stockSymbol, -1, isDaily)[3:5]  #price close only
    # print(dataSource)
    # readCSV(stockSymbol,dataType,date,n)
    # if date<0 or date+n>=len(dataSource):
    # return [-2,-2,-2]
    if dateScope >= len(dataSource[0]):
        return -1
    errorSum = 0
    result = {}
    prediction = bayesian(dataSource[0][9::-1], m, n)
    result['nextDay'] = {}
    result['nextDay']['prediction'] = prediction
    for k in range(gap, dateScope - gap, gap):
        dataTrain = dataSource[0][k:k + gap][::-1]
        trueValue = dataSource[0][k - 1]
        date = dataSource[1][k - 1]
        prediction = bayesian(dataTrain, m, n)
        error = abs((prediction - trueValue) / trueValue * 100)
        result[date] = {}
        result[date]['prediction'] = prediction
        result[date]['trueValue'] = trueValue
        result[date]['relativeError'] = error
        errorSum += error
    # for n in [6,10,16,20]:
    # print('n='+str(n))
    # for m in [5,6,7,8]:
    # print('n='+str(n)+', m='+str(m))
    # result=[]
    # avgRelErr=0
    # for stockSymbol in fileArray:
    # for pointStart in [1,101,201]:
    # prediction=[0]*4
    # for i in range(4):
    #     prediction[i] = round(bayesian(dataTrain[i],m,n),2)
    # # print(dataTrain)
    # stdev=round(statistics.stdev(dataTrain[3]),2)
    # # print(prediction)
    # return(prediction,-1,stdev)
    # print (result)
    averageError = errorSum / len(result)
    # print(averageError)
    curvefile = ''
    avgFile = ''

    if isDaily:
        curvefile = 'dataset/daily/prediction_' + stockSymbol + '_bayesian.json'
        avgFile = 'dataset/daily/prediction_averageErr_bayesian.json'
    else:
        curvefile = 'dataset/intraday/prediction_' + stockSymbol + '_bayesian.json'
        avgFile = 'dataset/intraday/prediction_averageErr_bayesian.json'
    # print(result)
    with open(curvefile, "w") as f:
        json.dump(result, f)

    with open(avgFile, "r") as f:
        # errStr=json.load(f)
        errDict = json.load(f)
        errDict[stockSymbol] = averageError
    with open(avgFile, "w") as f:
        json.dump(errDict, f)
Esempio n. 5
0
def testANN(stockSymbol, isDaily=True):
    dataSource = readJson(stockSymbol, 250, isDaily)
    # print(dataSource)
    global stock
    stock = stockSymbol
    myNN = NN(11, 5, 1)
    # myNN.initWeightsRand()
    myNN.loadWeights(stockSymbol)

    result = []
    resultJson = {}
    resultJson[stockSymbol] = {}

    # prediction=[0]*4
    # print(dataSource[3][10:])
    dataTrain = dataFormatter(dataSource[3][9:][::-1], 10, 5, 241)[::-1]
    # print(len(dataTrain))
    dateArray = []
    for num in range(10, 241, 10):
        dateArray.append(dataSource[4][num])

    errorSum = 0
    result = {}
    # print(dataSource[3][:10][::-1])
    prediction = myNN.runNN(dataSource[3][:10][::-1])[0]
    # print(prediction)
    result['nextDay'] = {}
    result['nextDay']['prediction'] = prediction
    for i in range(24):
        dataItem = dataTrain[i]
        # print(dataTrain)
        date = dateArray[i]
        # dataTrain=dataTrain[0]
        trueValue = dataItem[1][0]
        # print(dataTrain[0])
        prediction = myNN.runNN(dataItem[0])[0]
        error = abs((prediction - trueValue) / trueValue * 100)
        result[date] = {}
        result[date]['prediction'] = prediction
        result[date]['trueValue'] = trueValue
        result[date]['relativeError'] = error
        errorSum += error
    # for n in [6,10,16,20]:
    # print('n='+str(n))
    # for m in [5,6,7,8]:
    # print('n='+str(n)+', m='+str(m))
    # result=[]
    # avgRelErr=0
    # for stockSymbol in fileArray:
    # for pointStart in [1,101,201]:
    # prediction=[0]*4
    # for i in range(4):
    #     prediction[i] = round(bayesian(dataTrain[i],m,n),2)
    # # print(dataTrain)
    # stdev=round(statistics.stdev(dataTrain[3]),2)
    # # print(prediction)
    # return(prediction,-1,stdev)
    # print (result)
    averageError = errorSum / len(result)
    # print(averageError)
    curvefile = ''
    avgFile = ''

    if isDaily:
        curvefile = path.abspath(
            path.join(path.abspath(sys.argv[0]), "..")
        ) + '\\dataset\\daily\\prediction_' + stockSymbol + '_ANN.json'
        avgFile = path.abspath(path.join(
            path.abspath(sys.argv[0]),
            "..")) + '\\dataset\\daily\\prediction_averageErr_ANN.json'
    else:
        curvefile = path.abspath(
            path.join(path.abspath(sys.argv[0]), "..")
        ) + '\\dataset\\intraday\\prediction_' + stockSymbol + '_ANN.json'
        avgFile = path.abspath(path.join(
            path.abspath(sys.argv[0]),
            "..")) + '\\dataset\\intraday\\prediction_averageErr_ANN.json'
    # print(result)
    with open(curvefile, "w") as f:
        json.dump(result, f)

    with open(avgFile, "r") as f:
        # errStr=json.load(f)
        errDict = json.load(f)
        errDict[stockSymbol] = averageError
    with open(avgFile, "w") as f:
        json.dump(errDict, f)

    # print(dateArray)
    # print(dataTrain)
    for inputData, outputData in dataTrain:
        predictionItem = myNN.runNN(inputData)
        print(inputData, outputData, predictionItem)