0% found this document useful (0 votes)
26 views

Stock Prediction Using Machine Learning

The document discusses using machine learning techniques to predict stock prices. It reviews previous works that used artificial neural networks, support vector machines, genetic algorithms and recurrent neural networks to predict stock market trends. The paper then describes collecting stock price data over 10 years from various companies and using a long short-term memory neural network model to analyze the data and predict future stock prices.

Uploaded by

Mazen
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

Stock Prediction Using Machine Learning

The document discusses using machine learning techniques to predict stock prices. It reviews previous works that used artificial neural networks, support vector machines, genetic algorithms and recurrent neural networks to predict stock market trends. The paper then describes collecting stock price data over 10 years from various companies and using a long short-term memory neural network model to analyze the data and predict future stock prices.

Uploaded by

Mazen
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

WSEAS TRANSACTIONS on COMPUTER RESEARCH

DOI: 10.37394/232018.2021.9.17 Shubha Singh, Sreedevi Gutta, Ahmad Hadaegh

Stock Prediction Using Machine Learning


SHUBHA SINGH, SREEDEVI GUTTA, AHMAD HADAEGH
Department of Computer Science and Information System
California State University San Marcos
333 Twin Oak valley Rd. San Marcos CA, 92009, USA

Abstract: The Trend of stock price prediction is becoming more popular than ever. Share market is difficult to
predict due to its volatile nature. There are no rules to follow to predict what will happen with the stock in the
future. To predict accurately is a huge challenge since the market trend always keep changing depending on
many factors. The objective is to apply machine learning techniques to predict stocks and maximize the profit.
In this work, we have shown that with the help of artificial intelligence and machine learning, the process of
prediction can be improved. While doing the literature review, we realized that the most effective machine
learning tool for this research include: Artificial Neural Network (ANN), Support Vector Machine (SVM), and
Genetic Algorithms (GA). All categories have common and unique findings and limitations. We collected data
for about 10 years and used Long Short-Term Memory (LSTM) Neural Network-based machine learning
models to analyze and predict the stock price. The Recurrent Neural Network (RNN) is useful to preserve the
time-series features for improving profits. The financial data High and Close are used as input for the model.

Key-Words: - Date Mining, Machine learning, Stock Prediction, Date Cleaning, Data Prediction, Recurrent
Neural Network, Data Normalization, Support Vector Machine (SVM), Genetic Algorithms
Received: June 27, 2021. Revised: October 25, 2021. Accepted: November 28, 2021. Published: December 16, 2021.

1. Introduction solve the multilayered complicated patterns and


Stock market is dynamic, unpredictable due to come up with a good prediction.
nature of the volatile market. Predicting any stock
value accurately is a huge challenge as there are so The main purpose of this research is to predict
many factors to consider such as news, sentiments, future price of a particular stock. In this research I
economy, financial reports and much more. The collected stock price data from Yahoo Finance to
strategies for investment in stock market is very feed the data to machine learning algorithm model.
complex and depends on tons of data. Profit always
comes with the risk of losses. To minimize the risk 2. Related Work
of losing money and maximize the profit the In the work of Kim and Han [3], the researchers
techniques to predict stock value is highly useful. have used ANN and GA to predict future stock
There are two main approaches that are being used price. The data authors have used is from Korea
for predicting stock values. In the first method stock price index (KOSPI). The sample data they
which is Traditional Time Series method the have collected from KOSPI was about 10 years
prediction is based on the historical data of that ranging from January 1989 to December 1998. They
particular stock. In this method the stock’s closing applied some required optimization and techniques
price, opening price volume etc. has been used. The to prepare the data to be used. They used GA to
second method, that is qualitative, the prediction is optimize ANN. There were 12 hidden layers that
based on factors like company profile, news articles, were not adjustable. Also, the author only focused
economy, social media, market sentiments etc. on two factors in optimization while he agrees that
GA has great potential for optimization. Similar
For stock market the size of the data is quite work is also presented by Qiu and Song [6] where
huge and random so we need models that are they are predicting the movement of Japanese stock
efficient and can deal with the complexity of this by using ANN and GA. They named this algorithm
huge amount of data. The stocks data are GA-ANN model since it was a mixture of both.
complicated and difficult to understand due to the
hidden patterns. Machine learning techniques have Hassan and Nath applied Hidden Markov [7]
potential to deal with the complexity and dig to Model (HMM) for predicting the stock prices for

E-ISSN: 2415-1521 152 Volume 9, 2021


WSEAS TRANSACTIONS on COMPUTER RESEARCH
DOI: 10.37394/232018.2021.9.17 Shubha Singh, Sreedevi Gutta, Ahmad Hadaegh

four different Airlines. One of best thing in their 3. Methodology


research paper was that the approach they took do
not need any expert to build the model. The problem 3.1 Data Collection and Preparation
with that research is that they used very less data for As shown in Figure 1, the data for three companies
evaluation and the data was related to a specific from different sectors for around 10 years from date
industry so this may not fetch a good result in 1/1/2012 to 10/10/2021 has been collected from
predicting. The authors have used data of around Yahoo Finance. The dataset includes the data for
two years only which is very less for machine to Tech company, Banking sector, and a Food service
understand and predict the trend. to understand movement of stock prices in different
sectors. The data contains information about the
Lee in [6] used SVM to predict the stock market stock such as High, Low, Open, Close, Adjacent
trend. The author collected data from NASDAQ close, and Volume. From table 1, I have selected the
from Taiwan Economic Journal. Lee used the Close column to train and test my model.
method supported sequential forward search (SSFS)
for the feature selection. The author also created
some procedure that will adjust parameter that have
different values. The structure that they used was
very clear for the feature selection of the model.

Thomas Fischer and Christopher Krauss in [8]


used long short-term memory (LSTM) to predict the
stock market trend. They collected dataset from
S&P 500 index for Thomson Reuters from
December 1989 to September 2015. After collecting
the data, they converted the list into binary matrix.
For optimization they used RMSprop. They used the
latest technique to perform the task, but they did not Table 1: Collected Data
have any background knowledge in financial
domain. In this paper the author did not mention
how they train the model with long-time The data has been divided into training and
testing segment. Training set is the subsection of the
dependencies.
original data that is divided to train the model and
test data is the subsection of the original data that is
divided to test the model by comparing with the
actual value to evaluate the accuracy of the model. I
have divided them into 80:20 ratio where 80% of
the data has been used for training purpose and 20%
of data is used for testing. The data has been scaled
to normalize the data between a range so that it is
easier for the algorithm to learn patterns from the
data. The given range for the data is from 0 to 1.

After scaling the data, I created the training


dataset where timestamp is 60. It is the batch size of
the data that is processed by the model for each
iteration for input and output. So, in the first
Figure 1: System Architecture iteration, it will use first 60 data as input and 61st as
output. In the next iteration it will use from 2nd to
61st as input and 62nd as output and so on. In the
M. Roondiwala, H. Patel and S. Varma. in [10] next step, I build the LSTM model.
has used RNN-LSTM model on NIFTY-50 stocks.
They collected data for 5 years and RMSE to find Epoch is the number of the passes of the entire
out the error rate. The window size they are using to training dataset the model completes. Here I am
predict the price movement is of 21 days. using epoch number 100 so the model will go
through each dataset 100 times.

E-ISSN: 2415-1521 153 Volume 9, 2021


WSEAS TRANSACTIONS on COMPUTER RESEARCH
DOI: 10.37394/232018.2021.9.17 Shubha Singh, Sreedevi Gutta, Ahmad Hadaegh

3.2 RNN 4. Proposed System


LSTM is a special type of Recurrent Neural As represented getting the historical data from
Network (RNN). This uses data from previous market is mandatory step. Then we will have to
records and based on that it predicts the future. RNN extract the feature, which is required for data
can remember our previous input in their memory analysis, then divide it as testing and training data,
when a huge dataset is given. training the algorithm to predict the price and the
final step it to visualize the data. Figure 4 represents
the Architecture of the proposed system.

• Step 1: Data Collection.


• Step 2: Data Preprocessing after getting the
historic data from the market for a particular
Figure 2: An unrolled RNN share and read the close price
(Aditi Mittal Oct 12, 2019 • Step 3: Divide the data
Understanding RNN and LSTM) • Step 4: Do a feature scaling on the data so that
the data values will range between 0 and 1.
Figure 2 shows how unrolled RNN works and • Step 5: Building the RNN (Recurrent neural
Figure 3 shows how Rolled RNN works. Here if we network) for data set and Initialize the RNN by
unloop the data, we can see that the first output is using sequential regressor.
based on the first input and second output is based • Step 6: Building the LSTM model
on the first and second then the third output is based • Step 7: Train the model by providing batch size
on first, second and third and so on. RNN can be and epochs.
thought as multiple copies of the same neural • Step 8: Making the predictions and visualizing
network where each passing a message to the the results using plotting techniques.
successor.

The problem with RNN is that it is long term


dependent, so if we take a large set of RNN data
then there could be two types of problem.

Figure 3: Rolled RNN

3.2.1 Vanishing Gradient


As we know Neural networks works on
Backpropagation which means when we get the Figure 4: Proposed system
output, we compared it with actual output and if
there is any error either large or small error it will 4.1 Long Short-Term Memory (LSTM)
update the weight of the neural network. To overcome from this problem, we come up with
another version of RNN that is Long Short-Term
If the error is less than 1, it will be multiplied by Memory (LSTM). This is a special kind of RNN
learning rate and while reaching to the last cell it that is capable of learning long-term dependencies.
will be very less that is almost zero. That is why it is This is specially designed to avoid long-term
called vanishing gradient. dependency problems and memorize information for
long period of time. LSTM can process entire
3.2.2 Exploding Gradient sequence of datasets and uses a supervised learning
When the algorithm assigns a high importance process.
to the weights, it will explode while reaching to
the end cell

E-ISSN: 2415-1521 154 Volume 9, 2021


WSEAS TRANSACTIONS on COMPUTER RESEARCH
DOI: 10.37394/232018.2021.9.17 Shubha Singh, Sreedevi Gutta, Ahmad Hadaegh

In this stage the data we collected is going to fed to


the model and trained for prediction.

Figure 5: LSTM

Graph 1: Apple Stock Prediction

Figure 6: LSTM memory cell

In Figure 5 we can see a single cell is updated


with controlling gates which controls the previous
memory as well as the input. This is how they
handle vanishing and exploding gradients. Figure 6
shows clear picture of how an input gate an output
gate and a forget gate are working. The cell
remembers values and the gates regulate the flow of
information.

4.2 Building the LSTM model: Graph 2: Bank of America Stock Prediction
To build the LSTM model we will need to import
Keras library and packages. Keras is basically tenser
flow high-level API for building and training
models. Here we are going to import some libraries.

Sequential – It is basically linear stack of layers


through which we can create a sequential layer by
passing the list by it.
Dense - It is the regular deeply connected neural
network layer. It is most commonly used layer to
change the dimension of the output. It represents a
metrics vector multiplication so the value which are
trainable parameter get updated during back
propagation.

Next, we are going to initialize RNN. For time Graph 3: McDonald's Stock Prediction
series problems like this we are going to use
regression model. The first step in this is to read the
This LSTM model is composed of a sequential
data which is a sequential data, and we are going to
input layer followed by three LSTM layers and a
assign this to regressor.
dense layer. In the model we are also using dropout.
The dropout is a regularization technique for
After this we are going to move to the next step
reducing overfitting, so it drops out units in neural
that is the most important stage training the model.
networks.

E-ISSN: 2415-1521 155 Volume 9, 2021


WSEAS TRANSACTIONS on COMPUTER RESEARCH
DOI: 10.37394/232018.2021.9.17 Shubha Singh, Sreedevi Gutta, Ahmad Hadaegh

After this we are going to compile the model. there are many factors and problems that should be
Here we are using optimizer. Optimizers are considered before making a prediction.
methods used to change the attributes of the neural
network such as weights and learning rate in order
to reduce the losses. The type of optimizer used can
greatly affect how fast the algorithm converges to
the minimum value. Here we are using Adam
optimizer. The Adam optimizer combines the perk
of two other optimizers ADAgrad and RMSprop.

Next step is to train my model where I have


given the epoch as 100. Epoch is a term used to
indicate how many times the algorithm has
completed. Then I have created testing data from
scaled values, converted the data to a NumPy array
and reshaped the data.

The root mean squared error (RMSE) is used to


evaluate the model. It is a good measure of how
accurate the model predicts or response. The lower
value of RMSE indicates the good prediction.
Table 2a: Apple
5. Results and Evaluation
After training the result of the prediction we need to To understand this better, I have also generated
show the result. The result could be shown in many values in table format. This is easier to read and
ways but the most effective way to show the result understand for any specific values. In the table, we
in a concise view is through a Graph. In Graph we can easily figure out what the actual value of the
can show the comparison between the result of both specified date is and what the model is predicting
actual values and prediction value using the data for the same date. (see tables 2a, 2b, and 2c)
that were collected and processed.

In these graphs (Graphs 1, 2, and 3), the blue line


(Train) is showing the data used in training, the red
line (Val) is the actual value of the data and orange
line (Prediction) is the graph for predicted data. We
can see actual and predicted values are going in
similar direction. In all the graphs we see that both
lines are very close to each other.

Any model cannot predict stock value accurately


for many reasons such as if there is any positive or
negative news come from that company the stock
prices will have an impact. Furthermore, if some
sudden changes in market happens, we cannot
predict that beforehand. There could be multiple
reasons the stock price can fluctuate aberrant. In this
project, I am trying to feed the pattern of the stock
price for a particular company and based on that I
am creating a model that will be trained with the
historical data and based on the pattern it will apply
an algorithm and will predict the future value. Thus, Table 2b: Bank of America
we cannot be completely sure in predicting the stock
price. The analysis shows that to predict the value of
such kind of activity is not a straight task because

E-ISSN: 2415-1521 156 Volume 9, 2021


WSEAS TRANSACTIONS on COMPUTER RESEARCH
DOI: 10.37394/232018.2021.9.17 Shubha Singh, Sreedevi Gutta, Ahmad Hadaegh

accurate. I have collected data for 10 years to train


my model so the accuracy label of my model will
increase. We can see in the Table 3 how size of data
impact models accuracy.
Hassan and Nath in [7] applied Hidden Markov
Model (HMM) to predict the stock price but the
problem is that they have chosen and collected data
only for one sector that is Airline and they collected
data for four different company, but all are from
same sector. To see if our model is working well it
is good to use data from diverse sectors. In my work
I have used data from different sectors such as Tech
company, Bank sector and Food services. The data
Hassan and Nath collected is only for 2 years that is
not sufficient for a model to train with good
accuracy.
In the work of Kim and Han [3], they have used
ANN to predict future stock price. The problem
Table 2c: McDonald's with the ANN is that in this each node that is hidden
is simply a node with a single activation function.
Whereas in my work I have used LSTM, and in this
each node is a memory cell that can store contextual
information. LSTM can remember specific function
for longer time, and this helps the model to work
better.
In the work of Lee in [6] used SVM to predict
Table 3: RMSE for different years data the stock market trend. The problem with SVM is
that it is not suitable for huge data set and for
predicting stock price we need tons of data.
ARIMA Linear SVM LSTM Sometime SVM will underperform if the number of
Regression (RMSE) features for each data point exceeds the number of
(RMSE) (RMSE) (RMSE)
training data samples, whereas LSTM performs
better in these situations.
72.64 3.22 1.58 0.43
Table 4: RMSE Comparison with other works
7. Conclusion
In conclusion we can say that the popularity of stock
6. Comparison: market trading is growing rapidly which is
In table 4 we can see the difference of RMSE encouraging researchers to find out new methods for
between these models. This ARIMA RMSE result I the prediction using new techniques.
have taken from DEVELOPERS CORNER [11]. The forecasting techniques is not only helpful to
The ARIMA model got a RMSE of 72.64 that is not researchers, but it also helps investors or any person
good prediction whereas LSTM model got RMSE of dealing with the stock market. To help predict stock
0.43 which is very good as compared to the other indices or forecasting model with good accuracy is
model. There could be multiple reasons for this required.
some of which can be the ARIMA model is not
working good because the author is using only about Overall, we conclude that Stock prediction using
five years of data to train the model. The Linear Machine learning can increase the prediction
Regression and SVM RMSE I have taken from accuracy. Through historical data we can find the
Vaishnavi Gururaj, Shriya V R and Dr. Ashwini K pattern of the stock and how the stocks movement
[12] work. We can clearly see here that LSTM is has been over years and learn through them. A
giving the best results among these models. model can learn the trends with huge amount of data
For good prediction huge data is necessary in provided and train themselves to predict the
[10] M. Roondiwala, H. Patel and S. Varma have movement of the stock price. However, it is also
collected data only for 5 years if they would have important to understand the parameters that
collected more data, the prediction rate will be more

E-ISSN: 2415-1521 157 Volume 9, 2021


WSEAS TRANSACTIONS on COMPUTER RESEARCH
DOI: 10.37394/232018.2021.9.17 Shubha Singh, Sreedevi Gutta, Ahmad Hadaegh

influence the prediction in order to develop an [8] Thomas Fischer and Christopher Krauss long
efficient product with accuracy of results. We can short-term memory networks for financial
conclude that historical data can be used as powerful market predictions Volume 270, Issue 2, 16
tool that allows us to forecast the movement more October 2018, Pages 654-669
accurately. However, our method might not generate [9] Ming-ChiLee. Using support vector machine
the accurate result in this case for the number of with a hybrid feature selection method to the
reasons mentioned such as any negative or positive stock trend prediction. Volume 36, Issue
news or sudden change in market etc. Machine 8, October 2009, Pages 10896-10904
learning is the best way to recognize patterns and [10] M. Roondiwala, H. Patel and S. Varma,
predict results rather than apply the analysis "Predicting stock prices using LSTM,"
manually on large sets of data. There are some International Journal of Science and Research
drawbacks that need to consider while applying the (IJSR), vol. 6, no. 4, pp. 1754-1756, · April
algorithm to predict the result. Nobody should 2017
blindly rely on these models and invest since there [11] https://analyticsindiamag.com/comparing-
are number of reasons that the prediction can go in arima-model-and-lstm-rnn-model-in-time-
other direction. series-forecasting/
[12] Vaishnavi Gururaj, Shriya V R and Dr.
8. Future Work: Ashwini K, Stock Market Prediction using
The model used in my project has shown very Linear Regression and Support Vector
promising result. From the various results shown Machines Volume 14, Number 8 2019
through graphs and tables in the research can [13] Colah's blog August 27, 2015 Understanding
confirm that my model is capable of tracing the LSTM Networks
evolution of closing prices. For our future work we [14] Hongxiang Fan, Mingliang Jiang, Ligang Xu,
will try to find the best sets for about data length Hua Zhu, Junxiang Cheng,and Jiahu Jiang,
and number of training epochs that better suit our Comparison of Long Short Term Memory
assets and maximize our prediction accuracy. Networks and the Hydrological Modelling
Runoff Simulation
Reference:
[1] Adil Moghar and Mhamed Hamicheb. Stock Author Contributions:
Market Prediction Using LSTM Recurrent Shubha Singh: Graduate Student doing her thesis.
Neural Network, Procedia Computer Science. Her role was doing the research and implementation
Volume 170, 2020, Pages 1168-1173 of this paper. This paper was part of her master
[2] Pramod and Mallikarjuna Shastry.Stock Price thesis.
Prediction Using LSTM. January 2021. Test Sreedevi Gutta: Advisor of Shubha. Dr. Gutta was
Engineering and Management 83 (May-June the advisor of Shubha and provided valuable
2020):5246-5251. comments every week mainly on sections 3 and 4.
[3] Kyoung-jaeKim and IngooHan. prediction of Ahmad Hadaegh: “co-advisor”. Dr. Hadaegh also
stock price index. Volume 19, Issue 2, August directed Shubha in her work providing valuable
2000, Pages 125-132 feedback mainly on sections 4 and 5.
[4] Raghav Nandakumar1, Uttamraj K R2, Vishal
R3, Y V Lokeswari4. Stock Price Prediction Creative Commons Attribution License 4.0
Using Long Short-Term Memory. Volume: 05
Issue: 03 |Mar-2018. International Research
(Attribution 4.0 International, CC BY 4.0)
Journal of Engineering and Technology This article is published under the terms of the Creative
(IRJET) Commons Attribution License 4.0
[5] Shipra Saxena. Introduction to Long Short- https://creativecommons.org/licenses/by/4.0/deed.en_US
Term Memory. March 16, 2021
[6] Mingyue Qiu and Yu Song. Predicting the
Direction of Stock Market Index Movement
Using an Optimized Artificial Neural Network
Model May 19, 2016
[7] M.R. Hassan and B. Nath. Stock market
forecasting using hidden Markov model. 23
January 2006

E-ISSN: 2415-1521 158 Volume 9, 2021

You might also like