user218030
user218030

Reputation: 117

Multivarate LSTM stock prediction

I'm building a stock prediction using keras. I know how to do a simple one with univariant (such as 'Open'). I want to do with multiple variables such as 'Open, close,High'. The code which processes the data to make it into 3D for feeding the NN is as below for Uni.

X_train = []
y_train = []

for i in range(60, 1260): 
    X_train.append(data_training_scaled[i-60:i, :])
    y_train.append(data_training_scaled[i,:])

X_train, y_train = np.array(X_train), np.array(y_train)
X_train = np.reshape(X_train, (X_train.shape[0], X_train.shape[1], 1))

This code will collect 0-59 days of historical data and predict the 60th day (stored in Y_train). The shape of this array is (1200,60,1) which is 1200 rows of 60 days of historical data counting up. For example row 1 = 0-59 days, row 2 1-60 days etc. to predict days 60 and 61 respectively.

When doing this with multiple variables whats the best way to do it? Does the Open data stay on dimension 1 and the other variables go on dimension 2 and 3 so the shape of the 3D array would be (1200,60,3) for 3 variables?

Upvotes: 0

Views: 603

Answers (2)

ASH
ASH

Reputation: 20342

You want to predict the next day's stock price, right. This code will do it for you.

from pandas_datareader import data as wb
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.pylab import rcParams
from sklearn.preprocessing import MinMaxScaler

#from datetime import datetime, timedelta
#N = 60
#start = (datetime.now() - timedelta(days=N)).date()
#end = datetime.today().strftime('%Y-%m-%d')
#print(start)
#print(end)

start = '2019-02-28'
end = '2020-02-28'

tickers = ['AAPL']

thelen = len(tickers)

price_data = []
for ticker in tickers:
    prices = wb.DataReader(ticker, start = start, end = end, data_source='yahoo')[['Open','Adj Close']]
    price_data.append(prices.assign(ticker=ticker)[['ticker', 'Open', 'Adj Close']])

#names = np.reshape(price_data, (len(price_data), 1))

df = pd.concat(price_data)
df.reset_index(inplace=True)

for col in df.columns: 
    print(col) 

#used for setting the output figure size
rcParams['figure.figsize'] = 20,10
#to normalize the given input data
scaler = MinMaxScaler(feature_range=(0, 1))
#to read input data set (place the file name inside  ' ') as shown below
df.head()

df['Date'] = pd.to_datetime(df.Date,format='%Y-%m-%d')
#df.index = names['Date']
plt.figure(figsize=(16,8))
plt.plot(df['Adj Close'], label='Closing Price')

enter image description here

ntrain = 80
df_train = df.head(int(len(df)*(ntrain/100)))
ntest = -80
df_test = df.tail(int(len(df)*(ntest/100)))


#importing the packages 
from sklearn.preprocessing import MinMaxScaler
from keras.models import Sequential
from keras.layers import Dense, Dropout, LSTM

#dataframe creation
seriesdata = df.sort_index(ascending=True, axis=0)
new_seriesdata = pd.DataFrame(index=range(0,len(df)),columns=['Date','Adj Close'])
length_of_data=len(seriesdata)
for i in range(0,length_of_data):
    new_seriesdata['Date'][i] = seriesdata['Date'][i]
    new_seriesdata['Adj Close'][i] = seriesdata['Adj Close'][i]
#setting the index again
new_seriesdata.index = new_seriesdata.Date
new_seriesdata.drop('Date', axis=1, inplace=True)
#creating train and test sets this comprises the entire data’s present in the dataset
myseriesdataset = new_seriesdata.values
totrain = myseriesdataset[0:255,:]
tovalid = myseriesdataset[255:,:]
#converting dataset into x_train and y_train
scalerdata = MinMaxScaler(feature_range=(0, 1))
scale_data = scalerdata.fit_transform(myseriesdataset)
x_totrain, y_totrain = [], []
length_of_totrain=len(totrain)
for i in range(60,length_of_totrain):
    x_totrain.append(scale_data[i-60:i,0])
    y_totrain.append(scale_data[i,0])
x_totrain, y_totrain = np.array(x_totrain), np.array(y_totrain)
x_totrain = np.reshape(x_totrain, (x_totrain.shape[0],x_totrain.shape[1],1))

#LSTM neural network
lstm_model = Sequential()
lstm_model.add(LSTM(units=50, return_sequences=True, input_shape=(x_totrain.shape[1],1)))
lstm_model.add(LSTM(units=50))
lstm_model.add(Dense(1))
lstm_model.compile(loss='mean_squared_error', optimizer='adadelta')
lstm_model.fit(x_totrain, y_totrain, epochs=3, batch_size=1, verbose=2)
#predicting next data stock price
myinputs = new_seriesdata[len(new_seriesdata) - (len(tovalid)+1) - 60:].values
myinputs = myinputs.reshape(-1,1)
myinputs  = scalerdata.transform(myinputs)
tostore_test_result = []
for i in range(60,myinputs.shape[0]):
    tostore_test_result.append(myinputs[i-60:i,0])
tostore_test_result = np.array(tostore_test_result)
tostore_test_result = np.reshape(tostore_test_result,(tostore_test_result.shape[0],tostore_test_result.shape[1],1))
myclosing_priceresult = lstm_model.predict(tostore_test_result)
myclosing_priceresult = scalerdata.inverse_transform(myclosing_priceresult)



totrain = df_train
tovalid = df_test

#predicting next data stock price
myinputs = new_seriesdata[len(new_seriesdata) - (len(tovalid)+1) - 60:].values

#  Printing the next day’s predicted stock price. 
print(len(tostore_test_result));
print(myclosing_priceresult);

Result:

[[295.27402]]

Model predicted 295 and actual close was 285. Difference is 1%. Not bad!! It's certainly more accurate than that achieved by most portfolio managers, asset managers, hedge fund managers, etc.

enter image description here

Upvotes: 0

TulakHord
TulakHord

Reputation: 432

First step will be to frame the data into a supervised learning problem i.e prediction of time(t) based on previous input data (t-1), (t-2), etc. Once done this data needs to be reshaped in 3-dimension-samples, time-steps, features.

Upvotes: 1

Related Questions