<jupyter_start><jupyter_text>**IMPORT LIBRARY**<jupyter_code>import numpy as np
import pandas as pd
import math
import sklearn
import sklearn.preprocessing
import datetime
import seaborn as sns


import numpy as np
import matplotlib.pyplot as plt
import matplotlib.pyplot as plt2
import pandas as pd
from pandas import datetime
import math, time
import itertools
from sklearn import preprocessing
import datetime
from sklearn.metrics import mean_squared_error
from math import sqrt
from keras.models import Sequential
from keras.layers.core import Dense, Dropout, Activation
from keras.layers.recurrent import LSTM
from keras.models import load_model
import keras
import pandas_datareader.data as web
import h5py<jupyter_output><empty_output><jupyter_text>**IMPORT DATA**<jupyter_code>from google.colab import files

uploaded = files.upload()
forex_df = pd.read_csv("Data.csv", index_col = 0)
forex_df.dropna(inplace=True)
forex_df.isnull().sum()
forex_df.head()
forex_df.describe()<jupyter_output><empty_output><jupyter_text>**Correlation Plot**<jupyter_code>sns.heatmap(forex_df.corr(),annot= True)<jupyter_output><empty_output><jupyter_text>**Dropping China's Foreign Exchange column** **As  DEXCHUS has a corr value of 0.26 we are dropping that column**<jupyter_code>forex_df.drop('DEXCHUS', axis=1,inplace=True)

forex_df.head()
forex_df.info()<jupyter_output><class 'pandas.core.frame.DataFrame'>
Index: 577 entries, 5/20/2016 to 9/7/2018
Data columns (total 4 columns):
DEXSZUS    577 non-null float64
DEXMXUS    577 non-null float64
DEXBZUS    577 non-null float64
DEXINUS    577 non-null float64
dtypes: float64(4)
memory usage: 22.5+ KB<jupyter_text>**No NULL values in forex_df dataset** **Since the range of the columns are at  different scales we are NORMALIZING the data**  Normalization<jupyter_code>def normalize_data(df):
    min_max_scaler = sklearn.preprocessing.MinMaxScaler()
    df['DEXINUS'] = min_max_scaler.fit_transform(df.DEXINUS.values.reshape(-1,1))
    df['DEXMXUS'] = min_max_scaler.fit_transform(df.DEXMXUS.values.reshape(-1,1))
    df['DEXBZUS'] = min_max_scaler.fit_transform(df.DEXBZUS.values.reshape(-1,1))
    df['DEXSZUS'] = min_max_scaler.fit_transform(df.DEXSZUS.values.reshape(-1,1))
    return df
  
forex_df_norm = normalize_data(forex_df)
forex_df_norm.head()<jupyter_output><empty_output><jupyter_text>**Ploting  Normalized Forex Market**<jupyter_code>forex_df_norm[0:].plot()
seq_len = 22
d = 0.2
shape = [4, seq_len, 1] # feature, window, output
neurons = [128, 128, 32, 1]
epochs = 100<jupyter_output><empty_output><jupyter_text>**Train Test Split** **Converting  into arrays for  input to the neural network****<jupyter_code>def load_data(forex, seq_len):
    amount_of_features = len(forex.columns)
    data = forex.as_matrix() 
    sequence_length = seq_len + 1 # index starting from 0
    result = []
    
    for index in range(len(data) - sequence_length): # maxmimum date = lastmost date - sequence length
        result.append(data[index: index + sequence_length]) # index : index + 22days
    
    result = np.array(result)
    row = round(0.9 * result.shape[0]) # 90% split
    m
    train = result[:int(row), :] # 90% date
    X_train = train[:, :-1] # all data until day 
    y_train = train[:, -1][:,-1] # day m + 1 for DEXINUS

    
    X_test = result[int(row):, :-1] # all data until day
    y_test = result[int(row):, -1][:,-1] # day m + 1 for DEXINUS

    X_train = np.reshape(X_train, (X_train.shape[0], X_train.shape[1], amount_of_features))#
    X_test = np.reshape(X_test, (X_test.shape[0], X_test.shape[1], amount_of_features))  

    return [X_train, y_train, X_test, y_test]<jupyter_output><empty_output><jupyter_text>**We are incorporating 4 features(forex makets to predict India's FOREX)**<jupyter_code>X_train, y_train, X_test, y_test = load_data(forex_df_norm , seq_len)
print(X_train.shape[0])#expected number of observations to read each batch
print(X_train.shape[1])# number of time steps
print(X_train.shape[2])#number of features.
print(X_test.shape[0])#expected number of observations to read each batch
print(X_test.shape[1])# number of time steps
print(X_test.shape[2])#number of features.<jupyter_output>499
22
4
55
22
4<jupyter_text>**MODEL**<jupyter_code>def build_model2(layers, neurons, d):
    model = Sequential()
    
    model.add(LSTM(neurons[0], input_shape=(layers[1], layers[0]), return_sequences=True))
    model.add(Dropout(d))
        
    model.add(LSTM(neurons[1], input_shape=(layers[1], layers[0]), return_sequences=False))
    model.add(Dropout(d))
        
    model.add(Dense(neurons[2],kernel_initializer="uniform",activation='relu'))        
    model.add(Dense(neurons[3],kernel_initializer="uniform",activation='linear'))
    # model = load_model('my_LSTM_stock_model1000.h5')
    # adam = keras.optimizers.Adam(decay=0.2)
    model.compile(loss='mse',optimizer='adam', metrics=['accuracy'])
    model.summary()
    return model
model = build_model2(shape, neurons, d)
# layers = [4, 22, 1]
model.fit(
    X_train,
    y_train,
    batch_size=24,
    epochs=100,
    validation_split=0.1,
    verbose=1)
def model_score(model, X_train, y_train, X_test, y_test):
    trainScore = model.evaluate(X_train, y_train, verbose=0)
    print('Train Score: %.5f MSE (%.2f RMSE)' % (trainScore[0], math.sqrt(trainScore[0])))

    testScore = model.evaluate(X_test, y_test, verbose=0)
    print('Test Score: %.5f MSE (%.2f RMSE)' % (testScore[0], math.sqrt(testScore[0])))
    return trainScore[0], testScore[0]
model_score(model, X_train, y_train, X_test, y_test)

def denormalize(forex_df_norm, normalized_value): 
    forex_df_norm = forex_df_norm['DEXINUS'].values.reshape(-1,1)
    normalized_value = normalized_value.reshape(-1,1)
    
    #return df.shape, p.shape
    min_max_scaler = preprocessing.MinMaxScaler()
    a = min_max_scaler.fit_transform(forex_df_norm)
    new = min_max_scaler.inverse_transform(normalized_value)
    return new

newp = denormalize(forex_df_norm, p)
newy_test = denormalize(forex_df_norm, y_test)<jupyter_output><empty_output><jupyter_text>**Plot**<jupyter_code>import matplotlib.pyplot as plt2

plt2.plot(newp,color='red', label='Prediction')
plt2.plot(newy_test,color='blue', label='Actual')
plt2.legend(loc='best')
plt2.show()<jupyter_output><empty_output><jupyter_text>**END**<jupyter_code>def percentage_difference(model, X_test, y_test):
    percentage_diff=[]

    p = model.predict(X_test)
    for u in range(len(y_test)): # for each data index in test data
        pr = p[u][0] # pr = prediction on day u

        percentage_diff.append((pr-y_test[u]/pr)*100)
    return p  
p = percentage_difference(model, X_test, y_test)

def denormalize(forex_df_norm, normalized_value):    
    stock_name = stock_name['DEXINUS'].values.reshape(-1,1)
    normalized_value = normalized_value.reshape(-1,1)
    
    #return df.shape, p.shape
    min_max_scaler = preprocessing.MinMaxScaler()
    a = min_max_scaler.fit_transform(df)
    new = min_max_scaler.inverse_transform(normalized_value)
    return new
def plot_result(stock_name, normalized_value_p, normalized_value_y_test):
    newp = denormalize(stock_name, normalized_value_p)
    newy_test = denormalize(stock_name, normalized_value_y_test)
    plt2.plot(newp, color='red', label='Prediction')
    plt2.plot(newy_test,color='blue', label='Actual')
    plt2.legend(loc='best')
    plt2.title('The test result for {}'.format(forex_df_norm))
    plt2.xlabel('Days')
    plt2.ylabel('Adjusted Close')
    plt2.show()
plot_result(stock_name, p, y_test)

model.compile(loss='mean_squared_error', optimizer=Adam(lr = 0.0005) , metrics = ['mean_squared_error'])

history = model.fit(X_train, y_train, epochs=100 , batch_size = 25 , 
            validation_data = (X_test,y_test))

import matplotlib.pyplot as plt

plt.plot(history.history['mean_squared_error'])
plt.plot(history.history['val_mean_squared_error'])
plt.title('model mean squared error')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()
# summarize history for loss
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('model loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()
def model_score(model, X_train, y_train, X_test, y_test):
    trainScore = model.evaluate(X_train, y_train, verbose=0)
    print('Train Score: %.5f MSE (%.2f RMSE)' % (trainScore[0], math.sqrt(trainScore[0])))
    testScore = model.evaluate(X_test, y_test, verbose=0)
    print('Test Score: %.5f MSE (%.2f RMSE)' % (testScore[0], math.sqrt(testScore[0])))
    return trainScore[0], testScore[0]

model_score(model, X_train, y_train, X_test, y_test)
pred = model.predict(X_test)
pred = MinMaxScaler.inverse_transform(pred)
pred[:10]


def denormalize(forex_df_norm, normalized_value): 
    forex_df_norm = forex_df_norm['DEXINUS'].values.reshape(-1,1)
    normalized_value = normalized_value.reshape(-1,1)
    
    #return df.shape, p.shape
    min_max_scaler = preprocessing.MinMaxScaler()
    a = min_max_scaler.fit_transform(forex_df_norm)
    new = min_max_scaler.inverse_transform(normalized_value)
    return new

newp = denormalize(forex_df_norm, p)
newy_test = denormalize(forex_df_norm, y_test)
def percentage_difference(model, X_test, y_test):
    percentage_diff=[]

    p = model.predict(X_test)
    for u in range(len(y_test)): # for each data index in test data
        pr = p[u][0] # pr = prediction on day u

        percentage_diff.append((pr-y_test[u]/pr)*100)
    return p
pred = percentage_difference(model, X_test, y_test)

pred = model.predict(testX)
pred = scaler.inverse_transform(pred)
pred[:10]
model = build_model2(shape, neurons, d)
# layers = [4, 22, 1]
model.fit(
    X_train,
    y_train,
    batch_size=24,
    epochs=epochs,
    validation_split=0.1,
    verbose=1)
def percentage_difference(model, X_test, y_test):
    percentage_diff=[]

    p = model.predict(X_test)
    for u in range(len(y_test)): # for each data index in test data
        pr = p[u][0] # pr = prediction on day u

        percentage_diff.append((pr-y_test[u]/pr)*100)
    return p
  
p = percentage_difference(model, X_test, y_test)
forex_df_norm
def denormalize(forex_df_norm, normalized_value): 
    stock_name = stock_name['DEXINUS'].values.reshape(-1,1)
    normalized_value = normalized_value.reshape(-1,1)
    

    #return df.shape, p.shape
    min_max_scaler = preprocessing.MinMaxScaler()
    a = min_max_scaler.fit_transform(stock_name)
    new = min_max_scaler.inverse_transform(normalized_value)
    return new
def plot_result(stock_name, normalized_value_p, normalized_value_y_test):
    newp = denormalize(stock_name, normalized_value_p)
    newy_test = denormalize(stock_name, normalized_value_y_test)
    plt2.plot(newp, color='red', label='Prediction')
    plt2.plot(newy_test,color='blue', label='Actual')
    plt2.legend(loc='best')
    plt2.title('The test result for {}'.format(forex_df_norm))
    plt2.xlabel('Days')
    plt2.ylabel('DEXINUS')
    plt2.show()
def model_score(model, X_train, y_train, X_test, y_test):
    trainScore = model.evaluate(X_train, y_train, verbose=0)
    print('Train Score: %.5f MSE (%.2f RMSE)' % (trainScore[0], math.sqrt(trainScore[0])))

    testScore = model.evaluate(X_test, y_test, verbose=0)
    print('Test Score: %.5f MSE (%.2f RMSE)' % (testScore[0], math.sqrt(testScore[0])))
    return trainScore[0], testScore[0]


model_score(model, X_train, y_train, X_test, y_test)
import matplotlib.pyplot as plt2

plt2.plot(newp,color='red', label='Prediction')
plt2.plot(newy_test,color='blue', label='Actual')
plt2.legend(loc='best')
plt2.show()<jupyter_output><empty_output><jupyter_text>**Saving Model**<jupyter_code>model.save('LSTM_forex_prediction-20170429.h5')<jupyter_output><empty_output><jupyter_text>**Hyperparameter Optimization**<jupyter_code>seq_len = 22
shape = [4, seq_len, 1] # feature, window, output
neurons = [128, 128, 32, 1]
epochs = 300
def quick_measure(forex_df_norm, seq_len, d, shape, neurons, epochs):
    df = forex_df_norm
    X_train, y_train, X_test, y_test = load_data(df, seq_len)
    model = build_model2(shape, neurons, d)
    model.fit(X_train, y_train, batch_size=24, epochs=epochs, validation_split=0.1, verbose=1)
    # model.save('LSTM_Forex_prediction-20170429.h5')
    trainScore, testScore = model_score(model, X_train, y_train, X_test, y_test)
    return trainScore, testScore
dlist = [0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8]
neurons_LSTM = [32, 64, 128, 256, 512, 1024, 2048]
dropout_result = {}

for d in dlist:    
    trainScore, testScore = quick_measure(forex_df_norm, seq_len, d, shape, neurons, epochs)
    dropout_result[d] = testScore
def percentage_difference(model, X_test, y_test):
    percentage_diff=[]

    p = model.predict(X_test)
    for u in range(len(y_test)): # for each data index in test data
        pr = p[u][0] # pr = prediction on day u

        percentage_diff.append((pr-y_test[u]/pr)*100)
    return p
  
def model_score(model, X_train, y_train, X_test, y_test):
    trainScore = model.evaluate(X_train, y_train, verbose=0)
    print('Train Score: %.5f MSE (%.2f RMSE)' % (trainScore[0], math.sqrt(trainScore[0])))

    testScore = model.evaluate(X_test, y_test, verbose=0)
    print('Test Score: %.5f MSE (%.2f RMSE)' % (testScore[0], math.sqrt(testScore[0])))
    return trainScore[0], testScore[0]


model_score(model, X_train, y_train, X_test, y_test)
lists = sorted(seq_len_result.items())
x,y = zip(*lists)
plt.plot(x,y)
plt.title('Finding the best hyperparameter')
plt.xlabel('Days')
plt.ylabel('Mean Square Error')
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1344999267828379, 'fraction_numerical': 0.025626006735978913, 'mean_word_length': 5.172164482602802, 'pattern_counts': {'":': 0, '<': 52, '<?xml version=': 0, '>': 52, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '95538', 'license': 'MIT', 'n_tokens_mistral': 5330, 'n_tokens_neox': 5049, 'n_words': 1004, 'path': 'Algorithmic-Trading-in-the-FOREX-Market-master/Models/TensorFlow_LSTM_Keras.ipynb', 'repo_name': 'vkumar24/Forx_Algo_Trading', 'size': '13658'}