Time Series Stock Prediction with LSTM
Get Rich Quick With Deep Learning?
Computational advances have made deep learning more popular than ever. One specific area of deep learning is recurrent neural networks (RNN). These special artificial neural networks have a ‘memory’ system to help them store the information of previous inputs, unlike a traditional feed forward neural network. One of the most famous recurrent neural networks is called long short-term memory (LSTM).
Since LSTMs have a memory cell, they specifically work well with time series data. These models are great because they can easily process an entire sequence of data and use the memory cell, which makes the network able to effectively associate memories with the current input. Luckily for us, the stock market provides us with tons of time series data.
In this project I will be feeding the model with a set of sequences that will help predict a given price using time steps. I will be using time steps of 60, meaning in order to predict the price of a given day we need to know the prices 60 days before. My plan is to use an LSTM model to make stock predictions and get rich quick.
Lastly, before I make stock market predictions, it’s important to understand technical analysis vs fundamental analysis. Technical analysis looks at the price movement of a security and uses this data to attempt to predict future price movements. Fundamental analysis instead looks at the economic and financial factors that influence a business. While it is best to combine your knowledge of these two, in this tutorial I will only be using time series forecasting for technical analysis.
Importing Dependencies & Data
Here we are going to download all the necessary dependencies and utilize pandas datareader to get data on $QQQ. This initial data will be used for training our model. I will use more recent data to make our predictions. Below is what our dataset looks like.
Visualizing Our Data
Before I start preprocessing the data, I decided to do some basic visualizations. This includes line graphs of closing prices, volume traded, and moving averages.
Preprocessing The Data
I transformed the close column to a numerical type and dropped any NAN values we might have. I then kept only the “Close” column.
For best results it is recommended to rescale your data into values between 0 and 1. To do this I used MinMaxScaler from sklearn.
Now it is time to prepare the data for training. First, I need to prepare our input sequences with 60 timesteps (X_train), along with the respective labels (y_train). Next, we will be adding another axis for the batch size, since the input for an LSTM network is a 3D tensor (seq_len, timesteps, batch_size).
Building The Model
As you can see above, our model contains 4 layers of LSTM network all followed by a dropout layer for regularization purposes and at the top we will be adding a final Dense layer. All of this is compiled using an adam optimizer and a mse as a loss function. Then we are going to train our data using 20 epochs and a batch size of 32 .
Testing The Model
To test our model, I utilized pandas datareader again to obtain more recent data. I then applied the exact same data preprocessing steps that we did before on the training data. I could then obtain the label (y_test) and the testing input (inputClosing) that has been scaled and transformed to the input shape of our model.
Making A Prediction
While not perfect, above you can see the model has successfully predicted the bullish and bearish trends.
Final Thoughts
Utilizing this deep learning technique isn’t a get-rich quick solution. While this is great for technical analysis, there are other forms of analysis which play a big part in price prediction. Nevertheless, using this in conjunction with other forms of analysis could give you great insights into future stock prices.