Stock Price Prediction using Machine Learning

Harkishan Singh
4 min readDec 19, 2020

Stock market is a medium where the financial and business activities around the companies are funded by the retail investors. It is a well known fact that if we can predict the forthcoming price of a stock, anyone can make a handsome profit. You may have noticed that people call the stock market “Satta bazar” in India or they refer to it as a gambling. Many people resist investing in the stock market in India by giving an argument that they might lose their money. The motive of the project is to get a better insight of stock prices and to make good predictions of the future prices so that it can benefit people and at the same time making more people aware of the fact that investing is not gambling. The problem of predicting future stock price can be tackled by ML to some extent but it cannot guarantee 100% results as the stock market is not only driven by previously existing patterns but also from many other factors. Some of the factors which drive stock price are: 1. Company’s financial performance for the year. 2. Company’s fundamentals : debt, equity, number of shareholders, book value of the share etc. 3. Trend of the stock and also the stock market: Uptrend, downtrend, sideways market etc. 4. Good/bad news for the company. 5. Management of the company. There can be even more reasons for stock price volatility but these are the main ones. The problem lies in predicting the values of stock from the past trend behaviour stock.

Data Collection

Data is collected using Yahoo Finance API and we have worked on Google stock price. We can make API call for Google stock price history, we get Google stock price history as following data frame (which then can be stored as pandas data frame).

When we put a request to get the historical data of what we get from the API is : opening, closing, high, low price and volume of the stock on that particular day. Opening price is the price at which the stock opened that day, similar is the closing price. High price is the highest price at which the stock was traded, equivalent is the low price. Volume is the number of shares which changed hands on that particular day.

Preparing Data

We work up open price of the stock and predict the future value of the same. Lets say we have the following stock prices at some consecutive days:

We make a window size (win)= 2, which would slide through the stock price data and we can make out training data and testing data which we can feed into the model. Based on above data and win = 2 (I have used win = 100 in project, here just for illustration purpose I am taking win = 2), the prepared data will be:

X = [[47,40], [40,46], [46,42], [42,41] ] and Y = [46, 42, 41, 44]

This data is then scaled between [0,1] as the model is sensitive to scaling. We split the data into 80:20 for training and testing.

Model

The model architecture contain 3 LSTM, 3 dropout and 1 dense layers. The output layer contains a single node representing the predicted stock price of the next day.

Results

The error calculated is Mean Squared Error (MSE). For, window size of 100, the MSE = 0.0028 on the scaler data. Below is the predicted stock trend :

Predicted Stock trend

The blue line is the actual stock trend of Google, whereas the orange line is the predicted stock trend. It is very much clear that orange line is very strongly following the original stock price trend.

Group — 56

I (Harkishan Singh : 2017233) am the only member of group 56 and all work is done individually by myself. Work includes collection of data, processing the data, building the model, training the model and finally predicting the stock price trend from the trained model.

References

--

--