Can deep learning be used for trading?
Stock price prediction using LSTM – Long Short-Term Memory.
Deep learning is a type of machine learning and artificial intelligence (AI) that imitates the way humans gain certain types of knowledge. Deep learning is an important element of data science, which includes statistics and predictive modeling. Some examples of deep learning models are:
Artificial Neural Networks
Recurrent Neural Networks
Convolutional Neural Networks
The Long short-term memory (LSTM) is an artificial neural network used in the fields of artificial intelligence and deep learning.
Unlike standard feedforward neural networks, LSTM has feedback connections. Such a recurrent neural network can process not only single data points (such as images), but also entire sequences of data (such as speech or video). For example, LSTM is applicable to tasks such as unsegmented, connected handwriting recognition, speech recognition, machine translation, robot control, video games, and healthcare. LSTM has become the most cited neural network of the 20th century.
LSTM networks are well-suited to classifying, processing, and making predictions based on time series data. Since there can be lags of unknown duration between important events in a time series. LSTMs were developed to deal with the vanishing gradient problem that can be encountered when training traditional RNNs. Relative insensitivity to gap length is an advantage of LSTM over RNNs, hidden Markov models and other sequence learning methods in numerous applications.
The following is the step by step working of our LSTM model:
Loading the data:
We used the Yahoo Finance library of python to fetch ‘MSFT’ stock’s adjusted close price. Why use Adjusted Close and not Close: While closing price merely refers to the cost of shares at the end of the day, the adjusted closing price considers other factors like dividends, stock splits, and new stock offerings. Since the adjusted closing price begins where the closing price ends. Essentially, a more accurate measure of stocks’ value.
Train and Test Split:
We did a train test split of 80%, i.e., our training data (used for training the model). 80% of the total data set and the remaining 20% is the testing data.
Here 2 operations happen:
Scaling : When running such deep learning algorithms, it really helps if the data used is of similar scale. So, we use MinMaxScaler to scale all the data into similar scale (0 to 1)
Timesteps : We create a dataset (using def create_dataset(dataset, timestep=1)) to predict the stock price today, using previous n – days of stock prices (timesteps = n)
Moreover, we fit the LSTM on the dataset we preprocessed in the previous step. Due to our available resources we limited to the number of epochs to 5. Also, we use the ADAM optimizer in our model. Adam, an optimization algorithm useful instead of the classical stochastic gradient descent procedure. To update network weights iterative based in training data.
Finally, we use our fitted model to predict stock price of ‘MSFT’.
Conclusion: Our model produced great results. The RMSE for training data is approximately 0.0022 and for the testing data is 0.045.
Use to create a dataset which would essentially use previous 50 days of adj close prices to predict next prices.