A Machine Learning Model for Stock Price Prediction

Tejashree Nawale
8 min readMar 6, 2022

stock market prediction and forecasting using stacked LSTM (machine learning)

Photo by Yiorgos Ntrahas on Unsplash

In this article, I am going to walk you through how we can train a model that will help us predict the closing stock price of co-operation (Apple Inc.). Stock market prediction is an act of trying to determine the future value of company stock or other financial instrument traded on a financial exchange. stock price is affected by the news about the company and other factors. Broadly, stock market analysis is divided into two parts that is Fundamental Analysis and Technical Analysis. But our focus will be on the technical analysis part. It includes reading the charts and using statistical figures to identify the trends in the stock market.

Potential Business Problem:

Apple is one of the biggest tech giants. Its sales figures also benefit from the release of innovative products or services. Prediction of stock price index movement is regarded as a challenging task of financial time series prediction due to volatile and non-linear nature. An accurate prediction of stock price movement may yield profits for investors. Here, we used a Long Short Term Memory Network (LSTM) for building our model to predict the stock prices of Apple. With the introduction of Machine Learning and programmed methods of prediction have proved to be more efficient in predicting stock prices.

Why solve this problem?

Companies and equity traders to predict future revenues and changes in the economic and business sectors.

We’ll be using a dataset from Kaggle and for this particular project, I have used the data for ‘Apple’. This dataset contains information about apple stock. The columns in the given dataset are as follows: date, open, high, low, last, close, etc. The data consists of records of roughly 14 features.

Long Short Term Memory (LSTM)

LSTMs are widely used for sequence prediction problems and have proven to be extremely effective. LTSMs are a type of Recurrent Neural Network architecture used in the field of deep learning for learning long-term dependencies. It can not only process single data points such as images but also commonly used for processing and predicting time-series data.

LSTMs work in a three-step process. LSTM has three gates:

  • The input gate: The input gate adds information to the cell state
  • The forget gate: It removes the information that is no longer required by the model
  • The output gate: Output Gate at LSTM selects the information to be shown as output
  1. Exploratory Data Analysis

In Data Analysis we will try to find out missing values in the dataset, All the Numerical variables and Distribution of the numerical variables, Categorical Variables, Outliers, Relationship between an independent and dependent feature.

I will start by importing all the necessary libraries that we need for this task and import the dataset as shown below.

The first thing that we can do when tackling a data science problem is getting an understanding of the dataset that you are working with. Key observations and trends in the data were noted down .For this you can use df.info,df.head()etc.

  • The columns Open and Close represent the starting and final price at which the stock is traded on a particular day. The profit or loss calculation is usually determined by the closing price of a stock for the day, hence we will consider the close column as our target variable.
  • High and Low represent the maximum and minimum of the share for the day.
  • all 1257 entries present for each and every features which means there is no missing value in our datasets. data is present till 22nd may 2020.

In this project we are using Tiingo. Tiingo is a financial data platform that makes high quality financial tools available to all. Tiingo has a REST and Real-Time Data API. Free registration is required to get an API key.

TIINGO NOTE: Once you create an account, your account with be assigned an authentication token. This token is used in place of your username & password throughout the API, so keep it safe like you would your password.

Feature Engineering

After cleaning the data, we can visualize data and better understand the relationships between different variables. There are many more visualizations that you can do to learn more about your dataset, like scatterplots, histograms, boxplots, etc.

so here we can see how our plotted stock close price looks like from 2015 to 2020

We are using LSTM mode for prediction. LSTM are sensitive to the scale of data so we have to transform values between 0 to 1 by using MinMax Scalar. After executing the step you get values as shown in Out[5] .

Model Training:

In this situation, we split the data into training set and test set, then fit candidate models on the training set, evaluate and select them on the test set. we are taking 0.60 i.e. 60% total length of data as our training size and remaining 40% would be our test size. considering this, Train dataset(754) and Test dataset(503) is formed.

In timeseries data ,next step always dependent on previous step of data foe that we consider timesteps. Suppose we have to compute next day output, for that ‘how many previous day’ we need to be dependent on is decided by timesteps .we are just converting data into independent and dependent feature based on this timesteps. For instance, to predict the 51st price, this function creates input vectors of 50 data points prior and uses the 51st price as the outcome value. Similarly, for the next record we have to just shift one position to the right.

Shape gives us features present in x_train and y_train. i.e. 653 records 100 timesteps. Before implementing any LSTMs we need to reshape X_train into a 3 dimensions, in the form of number of samples, number of timesteps, and number of features. our X_train dataset is in 2 dimensional form ,so that we are giving (X_train.shape[1],1) as input to are lstm.

Later compiled it with ‘mean squared error .Optimizer we used is ‘adam’. optimizer is used to improve upon lost function and loss function is used to measure, how well the model did on training set.

Same steps we need to apply on test sets.

Now, predict the X_train and X_test dataset and get the values. epoch is the number of iteration when entire dataset is passed forward and backword trough a neural network.

We have already scaled the data, now we need to do reverse scaling to find out RMSE performance matrix. For that we used scalrar.inverse_ trasform. here 183.10943.. is the RMSE performance matrix for the training dataset.so we could get good output considering this value because difference between two RMSE values seems less as shown below. which means LSTM model working properly.

Be careful about making generalizations to other stocks, because, unlike other stationary time series, stock market data is less-to-none seasonal and more chaotic. Just checking the RMSE does not help us in understanding how the model performed. Let’s visualize this to get a more intuitive understanding. So here is a plot of the predicted values along with the actual values.

As in the graph below, test data predicted output is in green color ,red color is the complete dataset and yellow color is the training dataset. predicted values and actual values shows closed graph connection which means model performing well.

Next 10 days output we are predicting, for that we are taking 101 to 111 records. Red color curve shows us next 10 days data based on previous 100 timesteps. we have taken previous 100 data to do the future prediction of 10 days. adding tis output in list i.e. lst_output .

To see the complete output we need to combine df1(lst_output) in df3 output and you could see that next 10 days prediction. We can improve this accuracy by bi-directional LSTMs. You can also predict for next 50 days or 100 days and try to see how graph looks like.

The LSTM model can be tuned for various parameters such as changing the number of LSTM layers, adding dropout value or increasing the number of epochs. But are the predictions from LSTM enough to identify whether the stock price will increase or decrease? Certainly not! you can try out different machine learning algorithms. You can also improve this model by trying figure out hyperparameters that are most effective.

Conclusion

In this post, you learned about LSTMs ,how to perform stock price prediction using machine learning. Time series forecasting is a very intriguing field to work with, it's a great learning exercise .

Thank you for taking time to read .Full code can be found in my Github repo here (master branch), If you have any questions, feel free to connect with me in the comments section below.

--

--