├── README.md ├── RELIANCE.NS.csv └── RNN_LSTM_GRU.py /README.md: -------------------------------------------------------------------------------- 1 | # AlgorithmicTrading-MachineLearning 2 | Deep Learning - Neural network (RNN, LSTM & GRU) 3 | -------------------------------------------------------------------------------- /RNN_LSTM_GRU.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on Fri Oct 19 18:42:41 2018 4 | 5 | @author: Umesh 6 | """ 7 | #import all libraries 8 | import numpy as np 9 | import pandas as pd 10 | import math 11 | import sklearn 12 | import sklearn.preprocessing 13 | import datetime 14 | import os 15 | import matplotlib.pyplot as plt 16 | import tensorflow as tf 17 | 18 | # import dataset 19 | dataset = pd.read_csv('E:\EPAT\RELIANCE.NS.csv', index_col = 0) 20 | df_stock = dataset.copy() 21 | df_stock = df_stock.dropna() 22 | df_stock = df_stock[['Open', 'High', 'Low', 'Close']] 23 | 24 | # data scaling (normalizing) 25 | def normalize_data(df): 26 | min_max_scaler = sklearn.preprocessing.MinMaxScaler() 27 | df['Open'] = min_max_scaler.fit_transform(df.Open.values.reshape(-1,1)) 28 | df['High'] = min_max_scaler.fit_transform(df.High.values.reshape(-1,1)) 29 | df['Low'] = min_max_scaler.fit_transform(df.Low.values.reshape(-1,1)) 30 | df['Close'] = min_max_scaler.fit_transform(df['Close'].values.reshape(-1,1)) 31 | return df 32 | df_stock_norm = df_stock.copy() 33 | df_stock_norm = normalize_data(df_stock_norm) 34 | 35 | # Splitting the dataset into Train, Valid & test data 36 | valid_set_size_percentage = 10 37 | test_set_size_percentage = 10 38 | seq_len = 20 # taken sequence length as 20 39 | def load_data(stock, seq_len): 40 | data_raw = stock.as_matrix() 41 | data = [] 42 | for index in range(len(data_raw) - seq_len): 43 | data.append(data_raw[index: index + seq_len]) 44 | data = np.array(data); 45 | valid_set_size = int(np.round(valid_set_size_percentage/100*data.shape[0])); 46 | test_set_size = int(np.round(test_set_size_percentage/100*data.shape[0])); 47 | train_set_size = data.shape[0] - (valid_set_size + test_set_size); 48 | x_train = data[:train_set_size,:-1,:] 49 | y_train = data[:train_set_size,-1,:] 50 | x_valid = data[train_set_size:train_set_size+valid_set_size,:-1,:] 51 | y_valid = data[train_set_size:train_set_size+valid_set_size,-1,:] 52 | x_test = data[train_set_size+valid_set_size:,:-1,:] 53 | y_test = data[train_set_size+valid_set_size:,-1,:] 54 | return [x_train, y_train, x_valid, y_valid, x_test, y_test] 55 | 56 | x_train, y_train, x_valid, y_valid, x_test, y_test = load_data(df_stock_norm, seq_len) 57 | print('x_train.shape = ',x_train.shape) 58 | print('y_train.shape = ', y_train.shape) 59 | print('x_valid.shape = ',x_valid.shape) 60 | print('y_valid.shape = ', y_valid.shape) 61 | print('x_test.shape = ', x_test.shape) 62 | print('y_test.shape = ',y_test.shape) 63 | 64 | 65 | """Building the Model""" 66 | 67 | # parameters & Placeholders 68 | n_steps = seq_len-1 69 | n_inputs = 4 70 | n_neurons = 200 71 | n_outputs = 4 72 | n_layers = 2 73 | learning_rate = 0.001 74 | batch_size = 50 75 | n_epochs = 100 76 | train_set_size = x_train.shape[0] 77 | test_set_size = x_test.shape[0] 78 | tf.reset_default_graph() 79 | X = tf.placeholder(tf.float32, [None, n_steps, n_inputs]) 80 | y = tf.placeholder(tf.float32, [None, n_outputs]) 81 | 82 | # function to get the next batch 83 | index_in_epoch = 0; 84 | perm_array = np.arange(x_train.shape[0]) 85 | np.random.shuffle(perm_array) 86 | 87 | def get_next_batch(batch_size): 88 | global index_in_epoch, x_train, perm_array 89 | start = index_in_epoch 90 | index_in_epoch += batch_size 91 | if index_in_epoch > x_train.shape[0]: 92 | np.random.shuffle(perm_array) # shuffle permutation array 93 | start = 0 # start next epoch 94 | index_in_epoch = batch_size 95 | end = index_in_epoch 96 | return x_train[perm_array[start:end]], y_train[perm_array[start:end]] 97 | 98 | #RNN 99 | layers = [tf.contrib.rnn.BasicRNNCell(num_units=n_neurons, activation=tf.nn.elu) 100 | for layer in range(n_layers)] 101 | # LSTM 102 | #layers = [tf.contrib.rnn.BasicLSTMCell(num_units=n_neurons, activation=tf.nn.elu) 103 | # for layer in range(n_layers)] 104 | 105 | #LSTM with peephole connections 106 | #layers = [tf.contrib.rnn.LSTMCell(num_units=n_neurons, 107 | # activation=tf.nn.leaky_relu, use_peepholes = True) 108 | # for layer in range(n_layers)] 109 | 110 | #GRU 111 | #layers = [tf.contrib.rnn.GRUCell(num_units=n_neurons, activation=tf.nn.leaky_relu) 112 | # for layer in range(n_layers)] 113 | 114 | multi_layer_cell = tf.contrib.rnn.MultiRNNCell(layers) 115 | rnn_outputs, states = tf.nn.dynamic_rnn(multi_layer_cell, X, dtype=tf.float32) 116 | stacked_rnn_outputs = tf.reshape(rnn_outputs, [-1, n_neurons]) 117 | stacked_outputs = tf.layers.dense(stacked_rnn_outputs, n_outputs) 118 | outputs = tf.reshape(stacked_outputs, [-1, n_steps, n_outputs]) 119 | outputs = outputs[:,n_steps-1,:] # keep only last output of sequence 120 | 121 | # Cost function 122 | loss = tf.reduce_mean(tf.square(outputs - y)) 123 | 124 | #optimizer 125 | optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate) 126 | training_op = optimizer.minimize(loss) 127 | 128 | # Fitting the model 129 | with tf.Session() as sess: 130 | sess.run(tf.global_variables_initializer()) 131 | for iteration in range(int(n_epochs*train_set_size/batch_size)): 132 | x_batch, y_batch = get_next_batch(batch_size) # fetch the next training batch 133 | sess.run(training_op, feed_dict={X: x_batch, y: y_batch}) 134 | if iteration % int(5*train_set_size/batch_size) == 0: 135 | mse_train = loss.eval(feed_dict={X: x_train, y: y_train}) 136 | mse_valid = loss.eval(feed_dict={X: x_valid, y: y_valid}) 137 | print('%.2f epochs: MSE train/valid = %.6f/%.6f'%( 138 | iteration*batch_size/train_set_size, mse_train, mse_valid)) 139 | # Predictions 140 | y_test_pred = sess.run(outputs, feed_dict={X: x_test}) 141 | 142 | #checking prediction output nos 143 | y_test_pred.shape 144 | 145 | # ploting the graph 146 | comp = pd.DataFrame({'Column1':y_test[:,3],'Column2':y_test_pred[:,3]}) 147 | plt.figure(figsize=(10,5)) 148 | plt.plot(comp['Column1'], color='blue', label='Target') 149 | plt.plot(comp['Column2'], color='black', label='Prediction') 150 | plt.legend() 151 | plt.show() 152 | --------------------------------------------------------------------------------