<jupyter_start><jupyter_text>Create Forex Single Dataset<jupyter_code>##Dataset on just single ticker to test performances
df = data.DataReader('EUR=X', 'yahoo', datetime(2010,8,1), datetime(2014,8,1))
# df['RSI'] = ta.RSI(df.Close.values,timeperiod=14)
# _,_, macdhist = ta.MACD(df.Close.values, fastperiod=12, slowperiod=26, signalperiod=9)
# df['MACDHist'] = macdhist

##Add the predicted coloumn Y, as the last coloumn can be defined however you think is a good representation of a good decision 
##Clean the rest of the Data Frame
y = []
for i in range(0,len(df)):
    if i >= (len(df)- STRIDE):
        y.append(None)
    else:
        if (REWARD_LAG > 1):
            val = 0
            for n in range(REWARD_LAG):
                val = val + df['Close'][i+n+1]
            val = val / float(REWARD_LAG)
            y.append(val)
        else:
            y.append(df['Close'][i+REWARD_LAG])

    
df['Y_Values'] =np.asarray(y)
df = df.dropna()
#print (df)
sliced_df = df.drop(['Adj Close','Volume'] ,axis=1)
#print (sliced_df)
#(X_train, y_train), (X_test, y_test) = train_test_split(sliced_df)
(X_train, y_train) = train_test_split(sliced_df)
print(X_train[0],y_train[0])
print (X_train.shape,y_train.shape)<jupyter_output>(array([[-0.76244909, -0.75153814, -1.36800657, -1.28695383],
       [-1.28305706, -1.17005084, -1.66649887, -1.50673145],
       [-1.51062822, -1.11159935, -1.57219712, -1.50673145],
       [-1.21992945, -1.04535432, -1.55349264, -1.20668044],
       [-1.30487896, -1.21525333, -1.97044664, -1.31111378],
       [-1.77716703, -1.53400881, -1.85744042, -1.77560832],
       [-1.53322946, -0.85986824, -1.53322946, -1.52855334],
       [-1.33059761,  0.11120592, -1.33059761, -1.3274802 ],
       [ 0.25382756,  0.51179349, -0.17091996,  0.24135791],
       [ 0.31461712,  0.67234026, -0.03998862,  0.30838229],
       [ 0.59362558,  0.75651042,  0.1197788 ,  0.58894946],
       [ 0.38865568,  0.42450593, -0.07895628,  0.3933318 ],
       [ 0.08626661,  0.33176289, -0.11090976,  0.07223825],
       [ 0.18758254,  0.54530568, -0.02206349,  0.18290642],
       [ 0.40736016,  1.09630844,  0.29357458,  0.41203627],
       [ 0.87731017,  1.17580247,  0.79158131,  0.88120694],
       [ 1.2202256 ,  1[...]<jupyter_text>Helper function to scrape Google Intraday data<jupyter_code>def get_google_data(symbol, period, window):
    url_root = 'http://www.google.com/finance/getprices?i='
    url_root += str(period) + '&p=' + str(window)
    url_root += 'd&f=d,o,h,l,c,v&df=cpct&q=' + symbol
    print(url_root)
    response = urllib2.urlopen(url_root)
    data = response.read().split('\n')
    #actual data starts at index = 7
    #first line contains full timestamp,
    #every other line is offset of period from timestamp
    parsed_data = []
    anchor_stamp = ''
    end = len(data)
    for i in range(7, end):
        cdata = data[i].split(',')
        if 'a' in cdata[0]:
            #first one record anchor timestamp
            anchor_stamp = cdata[0].replace('a', '')
            cts = int(anchor_stamp)
        else:
            try:
                coffset = int(cdata[0])
                cts = int(anchor_stamp) + (coffset * period)
                parsed_data.append((dt.datetime.fromtimestamp(float(cts)), float(cdata[1]), float(cdata[2]), float(cdata[3]), float(cdata[4]), float(cdata[5])))
            except:
                pass # for time zone offsets thrown into data
    df = pd.DataFrame(parsed_data)
    df.columns = ['ts', 'Open', 'High', 'Low', 'Close', 'Volume']
    df.index = df.ts
    del df['ts']
    return df<jupyter_output><empty_output><jupyter_text>Create Single Intraday Dataset<jupyter_code>df = get_google_data('AAPL', 900, 150)
#print(df)
plt.plot(df['Close'].values[:])
y = []
for i in range(0,len(df)):
    if i >= (len(df)- REWARD_LAG):
        y.append(None)
    else:
        if (REWARD_LAG > 1):
            val = 0
            for n in range(REWARD_LAG):
                val = val + df['Close'][i+n+1]
            val = val / float(REWARD_LAG)
            y.append(val)
            print('here')

        else:
            y.append(df['Close'][i+REWARD_LAG])

    
df['Y_Values'] =np.asarray(y)
df = df.dropna()
sliced_df = df.drop(['Volume'] ,axis=1)
#print(sliced_df)
(X_train, y_train), (X_test, y_test) = train_test_split_intra(sliced_df)
#print(X_train[0],y_train[0])
print(len(X_train),len(X_test))
#print(X_test[0],y_test[0])<jupyter_output>/home/ai2-jedi/anaconda2/lib/python2.7/site-packages/ipykernel/__main__.py:153: FutureWarning: slice indexers when using iloc should be integers and not floating point
/home/ai2-jedi/anaconda2/lib/python2.7/site-packages/ipykernel/__main__.py:154: FutureWarning: slice indexers when using iloc should be integers and not floating point<jupyter_text>Build your Network Structure<jupyter_code>model_intra = Sequential() 

model_intra.add(LSTM(N_HIDDEN, return_sequences=True, activation='tanh', input_shape=(SEQ_LENGTH, num_attr)))
#model_intra.add(LSTM(N_HIDDEN, return_sequences=True, activation='tanh'))
model_intra.add(LSTM(N_HIDDEN, return_sequences=False, activation='tanh'))

model_intra.add(Dense(1,activation='linear'))
model_intra.compile(loss="mean_squared_error", optimizer='adam')
model_intra_full = Sequential() 

model_intra_full.add(LSTM(N_HIDDEN, return_sequences=True, activation='tanh', input_shape=(SEQ_LENGTH, num_attr)))
#model_intra_full.add(LSTM(N_HIDDEN, return_sequences=True, activation='tanh'))
model_intra_full.add(LSTM(N_HIDDEN, return_sequences=False, activation='tanh'))

model_intra_full.add(Dense(1,activation='linear'))
model_intra_full.compile(loss="mean_squared_error", optimizer='adam')
model_forex = Sequential() 

model_forex.add(LSTM(N_HIDDEN, return_sequences=True, activation='tanh', input_shape=(SEQ_LENGTH, num_attr)))
#model_forex.add(LSTM(N_HIDDEN, return_sequences=True, activation='tanh'))
model_forex.add(LSTM(N_HIDDEN, return_sequences=False, activation='tanh'))

model_forex.add(Dense(1,activation='linear'))
model_forex.compile(loss="mean_squared_error", optimizer='adam')<jupyter_output><empty_output><jupyter_text>Fit the model with your training data and parameters<jupyter_code>print(X_train.shape)
print(y_train.shape)
##Train the standard 2 layer LSTM on one ticker. Train one at a time as each can take awhile
model_intra.fit(X_train, y_train, batch_size=50, nb_epoch=150, validation_split=0.01)

##Train the standard 2 layer LSTM on one ticker. Train one at a time as each can take awhile
#model_forex.fit(X_train, y_train, batch_size=50, nb_epoch=100, validation_split=0.01)

##Train the standard 2 layer LSTM on all 500 tickers in S&P but for only 3 years each
#model_intra_full.fit(X_train_merged, y_train_merged, batch_size=1500, nb_epoch=30, validation_split=0.1)  

##Train the bidirectional 2 layer lstm on one ticker
#bimodel.fit([X_train_merged,X_train_merged], y_train_merged, batch_size=1500, nb_epoch=300, validation_split=0.3)<jupyter_output>Train on 1156 samples, validate on 12 samples
Epoch 1/150
1156/1156 [==============================] - 1s - loss: 1.9575 - val_loss: 0.5494
Epoch 2/150
1156/1156 [==============================] - 1s - loss: 1.4731 - val_loss: 0.4006
Epoch 3/150
1156/1156 [==============================] - 1s - loss: 1.3794 - val_loss: 0.2988
Epoch 4/150
1156/1156 [==============================] - 1s - loss: 1.3115 - val_loss: 0.3101
Epoch 5/150
1156/1156 [==============================] - 1s - loss: 1.2994 - val_loss: 0.2682
Epoch 6/150
1156/1156 [==============================] - 1s - loss: 1.2778 - val_loss: 0.2616
Epoch 7/150
1156/1156 [==============================] - 1s - loss: 1.2881 - val_loss: 0.3635
Epoch 8/150
1156/1156 [==============================] - 1s - loss: 1.2643 - val_loss: 0.3308
Epoch 9/150
1156/1156 [==============================] - 1s - loss: 1.2520 - val_loss: 0.3220
Epoch 10/150
1156/1156 [==============================] - 1s - loss: 1.2615 - val_loss: 0.3088
Epoch 11/150
[...]<jupyter_text>Helper Performance Evaluation Functions<jupyter_code>#Function to normalize the test input then denormalize the result. Calculate the rmse of the predicted values on the test set
def predict(X_test,y_test, myModel):
    predicted = []
    for example in X_test:
        x = copy.copy(example)
        #print (x)
        x_norm, mn, mx = normalize(x)
        toPred = []
        toPred.append(x_norm)
        add = np.array(toPred)
        #Predict for the standard model
        predict_standard = myModel.predict(add)
        pred_st = copy.copy(predict_standard)
        y_real_st = deNormalizeY(pred_st,mn,mx)
        predicted.append(y_real_st[0])

        #Predict for the bidirectional model
#         predict_bidirectional = myModel.predict([add,add])
#         pred_bi = copy.copy(predict_bidirectional)
#         y_real_bi = deNormalizeY(pred_bi,mn,mx)
#         predicted.append(y_real_bi[0])

    return predicted

def rmse(predicted,y_test):
    rmse = (((predicted - y_test) ** 2).mean(axis=0))**(1.0/2.0)
    #print ("The root mean squared error of this test set was " + str(rmse))
    return (rmse)

def predictNorm(X_test,y_test, myModel):
    predicted = []
    for example in X_test:
        x = copy.copy(example)
        x_norm, mean, std = normDist(x)
        toPred = []
        toPred.append(x_norm)
        add = np.array(toPred)
        
        #Predict for the standard model
#         predict_standard = myModel.predict(add)
#         pred_st = copy.copy(predict_standard)
#         y_real_st = deNormDist(pred_st,mean,std)
#         predicted.append(y_real_st[0])

        #Predict for the bidirectional model
        predict_bidirectional = myModel.predict([add,add])
        pred_bi = copy.copy(predict_bidirectional)
        y_real_bi = deNormDist(pred_bi,mean,std)
        predicted.append(y_real_bi[0])

    return predicted

def predict_change(X_test, y_test, myModel):
    predicted = []
    for example in X_test:
        toPred = []
        toPred.append(example)
        add = np.array(toPred)
        
        #Predict for the standard model
        predict_standard = myModel.predict(add)
        predicted.append(predict_standard[0])
        
        #predict_bidirectional = myModel.predict([add,add])
        #predicted.append(predict_bidirectional[0])
    return predicted


def predict_standard(X_test, y_test, myModel):
    predicted = []
    for example in X_test:
        seq_trans = []
        indicators = []
        
        for day in example:
            toAdd = [day[0],day[1],day[2],day[3]]
            #indiToAdd = [day[4],day[5]]
            seq_trans.append(toAdd)
            #indicators.append(indiToAdd)
            
        x_norm, mean, std = normDist(np.array(seq_trans))
        
        backTogetherSequence = []
        for i in range(len(x_norm)):
            toAdd = []
            day = x_norm[i]
            for x in day:
                toAdd.append(x)
            #day = indicators[i]
            #for x in day:
                #toAdd.append(x)
            backTogetherSequence.append(toAdd)
                
            
        toPred = []
        toPred.append(backTogetherSequence)
        add = np.array(toPred)
        
        #Predict for the standard model
        predict_standard = myModel.predict(add)
        pred_bi = copy.copy(predict_standard)
        y_real_bi = deNormDist(pred_bi,mean,std)
        predicted.append(y_real_bi[0])
        
        #Predict for the bidirectional model
#         predict_bidirectional = myModel.predict([add,add])
#         pred_bi = copy.copy(predict_bidirectional)
#         y_real_bi = deNormDist(pred_bi,mean,std)
#         predicted.append(y_real_bi[0])
        
    return predicted

def evaluation(predicted,y_test,THRESH):
        net_profit=0
        num_trades = 0
        for i in range(len(predicted)):
            if i>1:
                predicted_change = ((predicted[i] / y_test[i-1]) - 1)
                #print(predicted_change)
                actual_change = (predicted[i] -  y_test[i])*LOT_SIZE
                if predicted_change >= THRESH:
                    #print("Buy")
                    net_profit = net_profit + actual_change 
                    num_trades = num_trades + 1
                elif predicted_change <= -THRESH:
                    #print("Short")
                    net_profit = net_profit - actual_change
                    num_trades = num_trades + 1
                else:
                    #print("Do Nothing")
                    pass
        net_profits.append(net_profit)
        protits_per_trade.append(net_profit/float(num_trades))
        return net_profit,num_trades
    
##Backtesting evaluation function to see how sum of buying and selling on each time frames performs
def evaluation2(X_test,y_test,predicted_st,thresh):
    count = 0
    shortCount = 0
    longCount = 0
    netShort = 0
    netLong = 0
    loss = 0
    gain = 0
    for i in range(len(y_test)):
        closeOnLastDay = X_test[i][SEQ_LENGTH-1][3]
        percent_change = abs(predicted_st[i]-closeOnLastDay)/closeOnLastDay
        #lot_size = percent_change * 10000
        lot_size = 10000
        if( percent_change >= thresh):
            #print(percent_change,lot_size)
            count = count + 1
            #If greater than a 1% change was predicted in either direction take an action
            if(predicted_st[i]<closeOnLastDay):
                #If predicted is less than close of current day: Short
                shortCount = shortCount +1
                if(y_test[i]>closeOnLastDay):
                    #Case where you were incorrect and the stock went up after you shorted it
                    loss = (y_test[i]-closeOnLastDay)*lot_size
                    netShort = netShort - loss
                else:
                    #Case where the stock correctly goes down after you shorted it
                    gain = (closeOnLastDay-y_test[i])*lot_size
                    netShort = netShort + gain
            else:
                #If predicted is more than close of current day: Long
                longCount = longCount +1
                if(y_test[i]>closeOnLastDay):
                    #Case where the stock went up after you longed it
                    gain = (y_test[i]-closeOnLastDay)*lot_size
                    netLong = netLong + gain
                else:
                    #Case where the stock correctly goes down after you shorted it
                    loss = (closeOnLastDay-y_test[i])*lot_size
                    netLong = netLong - loss


    actionsPercentage = count / float(len(y_test))
    net = netLong + netShort
#     print ('You made ' + str(count) + ' trades')
#     print ('You made ' + str(shortCount) + ' Short trades and made ' + str(netShort))
#     print ('You made ' + str(longCount) + ' Long trades and made ' + str(netLong))
#     print('Decisions were made on ' + str(actionsPercentage*100) + "% of available days")
    print ('If you opened and closed a SCALED share position with these predictions per day your net is ' + str(net))
    return (net)
    
##Graph the predicted versus the actual
def graph(y_test,predicted):
    plt.figure(figsize=(15,15))
    plt.plot(y_test[:50])
    plt.plot(predicted[:50])
    plt.show()<jupyter_output><empty_output><jupyter_text>Run prediction on your test data for 1-Day Prediction<jupyter_code>df_test = data.DataReader('EUR=X', 'yahoo', datetime(2014,8,1), datetime(2015,8,1))
# df_test['RSI'] = ta.RSI(df_test.Close.values,timeperiod=14)
# _,_, macdhist = ta.MACD(df_test.Close.values, fastperiod=12, slowperiod=26, signalperiod=9)
# df_test['MACDHist'] = macdhist

y = []
for i in range(0,len(df_test)):
    if i >= (len(df_test)- STRIDE):
        y.append(None)
    else:
        if (REWARD_LAG > 1):
            val = 0
            for n in range(REWARD_LAG):
                val = val + df_test['Close'][i+n+1]
            val = val / float(REWARD_LAG)
            y.append(val)

        else:
            y.append(df_test['Close'][i+REWARD_LAG])
        
df_test['Y_Values'] =np.asarray(y)
df_test = df_test.dropna()
sliced_df_test = df_test.drop(['Adj Close','Volume'] ,axis=1)
X_test_stock, y_test_stock = _load_data_test(sliced_df_test)
#print(X_test_stock[0],y_test_stock[0])

#_, X_test_stock, _, y_test_stock = train_test_split(X_test_stock, y_test_stock, test_size=0.75)

predicted_st = predict_standard(X_test_stock,y_test_stock, model_forex)
#predicted_st = predictNorm(X_test_stock,y_test_stock, bimodel)

#evaluation(X_test_stock,y_test_stock,predicted_st,0.03)
graph(y_test_stock,predicted_st)

sum_error = 0
sum_error_donothing = 0
for i in range(len(predicted_st)):
    if i>0:
        sum_error = sum_error + abs(predicted_st[i] - y_test_stock[i])
        sum_error_donothing = sum_error_donothing + abs(predicted_st[i] - y_test_stock[i-1])
MAE_lstm = sum_error/len(predicted_st)
MAE_donothing = sum_error_donothing/len(predicted_st)
print("MAE for LSTM is: " + str(MAE_lstm))
print("MAE for doing nothing is: " + str(MAE_donothing))

sum_error = 0
sum_error_donothing = 0
for i in range(len(predicted_st)):
    if i>0:
        sum_error = sum_error + (predicted_st[i] - y_test_stock[i])**2
        sum_error_donothing = sum_error_donothing + (predicted_st[i] - y_test_stock[i-1])**2
        
RMSE_lstm = (sum_error/len(predicted_st))**(1.0/2.0)
RMSE_donothing = (sum_error_donothing/len(predicted_st))**(1.0/2.0)
print("RMSE for LSTM is: " + str(RMSE_lstm))
print("RMSE for doing nothing is: " + str(RMSE_donothing))

net_profits = []
protits_per_trade = []
for i in range(20):
    THRESH = i/1000.0
    #print(THRESH)
    LOT_SIZE = 1000
    net_profit,num_trades = evaluation(predicted_st,y_test_stock,THRESH)
    net_profits.append(net_profit)
    print('Net profit for ' + str(THRESH) + " threshhold is " + str(net_profit[0]) + " making " + str(num_trades) + " trades")
    profits_per_trade = net_profit/float(num_trades)
    protits_per_trade.append(profits_per_trade)<jupyter_output><empty_output><jupyter_text>Evaluation and Results for IntraDay Trading<jupyter_code>def predict_intra(X_test, y_test, myModel):
    print(len(X_test))
    predicted = []
    for example in X_test:
        #Transform the training example into gaussing distribution
        x_norm, mean, std = normDist(np.array(example))
        
        #Add examples to array to predict
        toPred = []
        toPred.append(x_norm)
        add = np.array(toPred)
        
        #Predict these examples
        predict_standard = myModel.predict(add)
        pred = copy.copy(predict_standard)
        y_real = deNormDist(pred,mean,std)
        predicted.append(y_real[0])

    return predicted
        
predicted_intra = predict_intra(X_test,y_test, model_intra)
plt.figure(figsize=(20,20))
plt.plot(y_test) 
plt.plot(predicted_intra)
plt.show()<jupyter_output>108<jupyter_text>MAE and RMSE Evaluation<jupyter_code>sum_error = 0
sum_error_donothing = 0
for i in range(len(predicted_intra)):
    if i>0:
        sum_error = sum_error + abs(predicted_intra[i] - y_test[i])
        sum_error_donothing = sum_error_donothing + abs(predicted_intra[i] - y_test[i-1])
MAE_lstm = sum_error/len(predicted_intra)
MAE_donothing = sum_error_donothing/len(predicted_intra)
print("MAE for LSTM is: " + str(MAE_lstm))
print("MAE for doing nothing is: " + str(MAE_donothing))
sum_error = 0
sum_error_donothing = 0
for i in range(len(predicted_intra)):
    if i>0:
        sum_error = sum_error + (predicted_intra[i] - y_test[i])**2
        sum_error_donothing = sum_error_donothing + (predicted_intra[i] - y_test[i-1])**2
        
RMSE_lstm = (sum_error/len(predicted_intra))**(1.0/2.0)
RMSE_donothing = (sum_error_donothing/len(predicted_intra))**(1.0/2.0)
print("RMSE for LSTM is: " + str(RMSE_lstm))
print("RMSE for doing nothing is: " + str(RMSE_donothing))<jupyter_output>RMSE for LSTM is: [0.15719269057322682]
RMSE for doing nothing is: [0.23207816758496383]<jupyter_text>Policy Function Evaluation<jupyter_code>net_profits = []
protits_per_trade = []
for i in range(50):
    THRESH = i/10000.0
    LOT_SIZE = 100
    net_profit = 0
    num_trades = 0
    for i in range(len(predicted_intra)):
        if i>1:
            predicted_change = ((predicted_intra[i] / y_test[i-1]) - 1)
            #print(predicted_change)
            actual_change = (predicted_intra[i] -  y_test[i])*LOT_SIZE
            if predicted_change >= THRESH:
                #print("Buy")
                net_profit = net_profit + actual_change 
                num_trades = num_trades + 1
            elif predicted_change <= -THRESH:
                #print("Short")
                net_profit = net_profit - actual_change
                num_trades = num_trades + 1
            else:
                #print("Do Nothing")
                pass
    net_profits.append(net_profit)
    protits_per_trade.append(net_profit/float(num_trades))
    print(net_profit,num_trades)

plt.plot(net_profits)
plt.show()
plt.plot(protits_per_trade)
plt.show()<jupyter_output><empty_output><jupyter_text>Other Stuff<jupyter_code>buyTotal = 0
sellTotal = 0
correct = 0
sellCorrect = 0
buyCorrect = 0
for i in range(len(predicted_st)):
    realAnswer = y_test_stock[i][0][0]
    if predicted_st[i][1] > predicted_st[i][0]:
        predicted = 0 #Buy
    else:
        predicted = 1 #Sell
        
    if realAnswer == 0:
        ##This is where the actual answer is Buy:Up:[0,1]:0
        buyTotal = buyTotal + 1
        if predicted == realAnswer:
            buyCorrect = buyCorrect + 1
            correct = correct + 1
    else:
        sellTotal = sellTotal + 1 
        if predicted == realAnswer:
            sellCorrect = sellCorrect + 1
            correct = correct + 1
            
percentCorrect = correct/float(len(predicted_st))
buyPercentCorrect = buyCorrect/float(buyTotal)
sellPercentCorrect = sellCorrect/float(sellTotal)

print (correct,len(predicted_st),percentCorrect)
print(buyCorrect,buyTotal, buyPercentCorrect)
print (sellCorrect,sellTotal,sellPercentCorrect)

print (buyTotal/float(len(predicted_st)))
print (sellTotal/float(len(predicted_st)))
##Creat training set of all stocks and store their DFs in a list
testing_dataframes = []
END = datetime.today().utcnow()
for industry in sector_tickers.items()[:2]:
    tickers = industry[1]
    for stock in tickers:
        try:
            print (stock)
            df_test = data.DataReader(stock, 'yahoo', datetime(2010,8,1), datetime(2013,8,1))
#             df_test['RSI'] = ta.RSI(df_test.Close.values,timeperiod=14)
#             _,_, macdhist = ta.MACD(df_test.Close.values, fastperiod=12, slowperiod=26, signalperiod=9)
#             df_test['MACDHist'] = macdhist
            y = []
            for i in range(0,len(df_test)):
                if i >= (len(df_test)- STRIDE):
                    y.append(None)
                else:
                    if(df['Close'][i+STRIDE] > df['Close'][i] ):
                        y.append([0,1])
                    else:
                        y.append([1,0])

            df_test['Y_Values'] =np.asarray(y)
            df_test = df_test.ix[:-REWARD_LAG]
            df_test = df_test.dropna()
            sliced_df_test = df_test.drop(['Adj Close','Volume'] ,axis=1)
            testing_dataframes.append((stock,sliced_df_test))
        except:
            pass<jupyter_output>MMM
AYI
ALK
ALLE
AAL
AME
APH
BA
CHRW
CAT
CTAS
GLW
CSX
CMI
DHR
DE
DAL
DOV
DNB
ETN
EMR
EXPD
FAST
FDX
FLIR
FLS
FLR
GD
GE
GWW
HON
ITW
IR
IRM
JEC
JBHT
KSU
LLL
LEG
LMT
MAS
NLSN
NSC
NOC
PCAR
PH
PNR
PBI
PWR
RTN
RSG
RHI
ROK
COL
ROP
R
LUV
SRCL
TXT
TYC
UNP
UAL
UPS
URI
UTX
VRSK
WM
XYL
APC
APA
BHI
COG
CHK
CVX
XEC
CPGX
CXO
COP
DVN
DO
EOG
EQT
XOM
FTI
HAL
HP
HES
KMI
MRO
MPC
MUR
NOV
NFX
NBL
OXY
OKE
PSX
PXD
RRC
SLB
SWN
SE
TSO
RIG
VLO
WMB<jupyter_text>Creat Baseline RMSE<jupyter_code>totalCorrect = 0
total = 0
for stock in testing_dataframes[:50]:
    
    X_test_stock, y_test_stock = _load_data_test(stock[1])
    predicted_st = predict_standard(X_test_stock,y_test_stock, model)
    
    buyTotal = 0
    sellTotal = 0
    correct = 0
    sellCorrect = 0
    buyCorrect = 0
    
    for i in range(len(predicted_st)):
        realAnswer = y_test_stock[i][0][0]
        if predicted_st[i][1] > predicted_st[i][0]:
            predicted = 0 #Buy
        else:
            predicted = 1 #Sell

        if realAnswer == 0:
            ##This is where the actual answer is Buy:Up:[0,1]:0
            buyTotal = buyTotal + 1
            if predicted == realAnswer:
                buyCorrect = buyCorrect + 1
                correct = correct + 1
        else:
            sellTotal = sellTotal + 1 
            if predicted == realAnswer:
                sellCorrect = sellCorrect + 1
                correct = correct + 1
            
    percentCorrect = correct/float(len(predicted_st))
    buyPercentCorrect = buyCorrect/float(buyTotal)
    sellPercentCorrect = sellCorrect/float(sellTotal)

    print (correct,len(predicted_st),percentCorrect)
    #print(buyCorrect,buyTotal, buyPercentCorrect)
    #print (sellCorrect,sellTotal,sellPercentCorrect)
    
    totalCorrect = totalCorrect + correct
    total = total + len(predicted_st)

totalPercent = totalCorrect/float(total)
print ("Total percent " + str(totalPercent))<jupyter_output>(355, 730, 0.4863013698630137)
(349, 730, 0.4780821917808219)
(388, 730, 0.5315068493150685)
(361, 730, 0.4945205479452055)
(356, 730, 0.4876712328767123)
(363, 730, 0.49726027397260275)
(370, 730, 0.5068493150684932)
(379, 730, 0.5191780821917809)
(370, 730, 0.5068493150684932)
(377, 730, 0.5164383561643836)
(356, 730, 0.4876712328767123)
(358, 730, 0.4904109589041096)
(377, 730, 0.5164383561643836)
(363, 730, 0.49726027397260275)
(362, 730, 0.4958904109589041)
(357, 730, 0.48904109589041095)
(364, 730, 0.4986301369863014)
(377, 730, 0.5164383561643836)
(375, 730, 0.5136986301369864)
(349, 730, 0.4780821917808219)
(368, 730, 0.5041095890410959)
(359, 730, 0.4917808219178082)
(384, 730, 0.5260273972602739)
(376, 730, 0.5150684931506849)
(364, 730, 0.4986301369863014)
(352, 730, 0.4821917808219178)
(366, 730, 0.5013698630136987)
(389, 730, 0.5328767123287671)
(379, 730, 0.5191780821917809)
(382, 730, 0.5232876712328767)
(378, 730, 0.5178082191780822)
(374, 730, 0.5123287671232877)
(362,[...]<jupyter_text>Precision and F1-Score Evaluation<jupyter_code>#Run your test set through the standard model
predicted_st = predict_change(X_test_stock,y_test_stock, model_3)
print (len(predicted_st))
print (predicted_st)
#Run your test set through the bidirectional model
predicted_bi = predict_change(X_test_merged,y_test_merged, bimodel)
print (len(predicted_bi))
#Count the number of positive and the number of negative calls you got right
totalCorrect = 0
total = 0
buyTotal = 0
sellTotal = 0
correct = 0
sellCorrect = 0
buyCorrect = 0
    
for i in range(len(predicted_st)):
    realAnswer = y_test_stock[i][0][0]
    if predicted_st[i][1] > predicted_st[i][0]:
        predicted = 0 #Buy
    else:
        predicted = 1 #Sell

    if realAnswer == 0:
        ##This is where the actual answer is Buy:Up:[0,1]:0
        buyTotal = buyTotal + 1
        if predicted == realAnswer:
            buyCorrect = buyCorrect + 1
            correct = correct + 1
    else:
        sellTotal = sellTotal + 1 
        if predicted == realAnswer:
            sellCorrect = sellCorrect + 1
            correct = correct + 1
            
percentCorrect = correct/float(len(predicted_st))
buyPercentCorrect = buyCorrect/float(buyTotal)
sellPercentCorrect = sellCorrect/float(sellTotal)

print (correct,len(predicted_st),percentCorrect)
print(buyCorrect,buyTotal, buyPercentCorrect)
print (sellCorrect,sellTotal,sellPercentCorrect)
print(buyTotal/float(len(predicted_st)))
print(sellTotal/float(len(predicted_st)))
from sklearn.metrics import f1_score
##Calculate F1 score
actual = []
result = []
for y in y_test_merged:
    if y[0] == 0:
        actual.append(0)
    else:
        actual.append(1)
for y in predicted_st:
    if y[1] > y[0]:
        result.append(0)
    else:
        result.append(1)
score = f1_score(actual,result,average='weighted',pos_label=1)
print(score)
#Same percentage calculations but with a threshold
THRESH = 0.1

totalCorrect = 0
total = 0
noDecision = 0
buyTotal = 0
sellTotal = 0
correct = 0
sellCorrect = 0
buyCorrect = 0
    
for i in range(len(predicted_st)):
    realAnswer = y_test_merged[i][0]
    if predicted_st[i][1] - THRESH > .5:
        predicted = 0 #Buy
    elif predicted_st[i][0] - THRESH > .5:
        predicted = 1 #Sell
    else:
        predicted = 2 #Pass, do not count towards percentages because you make no decision if .6>x>.4

    if predicted == 2:
        noDecision = noDecision + 1
    else:
        total = total + 1
        if realAnswer == 0:
            ##This is where the actual answer is Buy:Up:[0,1]:0
            buyTotal = buyTotal + 1
            if predicted == realAnswer:
                buyCorrect = buyCorrect + 1
                correct = correct + 1
        else:
            sellTotal = sellTotal + 1 
            if predicted == realAnswer:
                sellCorrect = sellCorrect + 1
                correct = correct + 1
            
percentCorrect = correct/float(total)
buyPercentCorrect = buyCorrect/float(buyTotal)
sellPercentCorrect = sellCorrect/float(sellTotal)

print (correct,total,percentCorrect)
print(buyCorrect,buyTotal, buyPercentCorrect)
print (sellCorrect,sellTotal,sellPercentCorrect)

print("If you just predicted all Up " + str(buyTotal/float(total)))
print("If you just predicted all Down " + str(sellTotal/float(total)))
thresholds = []
totalAcc = []
positiveAcc = []
negativeAcc = []
##Graph this graph of the threshold vs accuracy
for i in range(10):
    thresh = i/100.0
    totalCorrect = 0
    total = 0
    noDecision = 0
    buyTotal = 0
    sellTotal = 0
    correct = 0
    sellCorrect = 0
    buyCorrect = 0
    for i in range(len(predicted_st)):
        realAnswer = y_test_merged[i][0]
        if predicted_st[i][1] - thresh > .5:
            predicted = 0 #Buy
        elif predicted_st[i][0] - thresh > .5:
            predicted = 1 #Sell
        else:
            predicted = 2 #Pass, do not count towards percentages because you make no decision if .6>x>.4

        if predicted == 2:
            noDecision = noDecision + 1
        else:
            total = total + 1
            if realAnswer == 0:
                ##This is where the actual answer is Buy:Up:[0,1]:0
                buyTotal = buyTotal + 1
                if predicted == realAnswer:
                    buyCorrect = buyCorrect + 1
                    correct = correct + 1
            else:
                sellTotal = sellTotal + 1 
                if predicted == realAnswer:
                    sellCorrect = sellCorrect + 1
                    correct = correct + 1

    percentCorrect = correct/float(total)
    buyPercentCorrect = buyCorrect/float(buyTotal)
    sellPercentCorrect = sellCorrect/float(sellTotal)
    thresholds.append(thresh)
    totalAcc.append(percentCorrect)
    positiveAcc.append(buyPercentCorrect)
    negativeAcc.append(sellPercentCorrect)
plt.plot(totalAcc)
plt.show()
plt.plot(positiveAcc)
plt.show()
plt.plot(negativeAcc)
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12380952380952381, 'fraction_numerical': 0.06612153278819946, 'mean_word_length': 2.9232607875204426, 'pattern_counts': {'":': 0, '<': 49, '<?xml version=': 0, '>': 77, 'https://': 0, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '13', 'dir': 'jupyter-structured-clean-dedup', 'id': '362075', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 12427, 'n_tokens_neox': 10849, 'n_words': 2341, 'path': 'Stock_Price_Prediction_With_RNNs/stock_prediction_keras_FINAL.ipynb', 'repo_name': 'mikimaus78/ml_monorepo', 'size': '31185'}