20_Stock Price Prediction Using Machine Learning
20_Stock Price Prediction Using Machine Learning
Learning
of
Bachelor of Technology
in
Computer Science & Engineering
Name of the students Name of
Supervisor:
Sumit Raj Sikarwar (1905110100115) Er. Jayati Krishna
Goswami
Anurag Sharma (1905110100027) Assistant Professor
To the best of our knowledge and belief, this is the original work and
has not been submitted for any other degree elsewhere.
Date :
Place:
Signatures:
The matter presented in this project report in full or part, has not been
submitted by me for the award of any other degree elsewhere and is free
from plagiarism.
Name of Candidates:
Anurag Sharma
(1905110100027)
ACKNOWLEDGEMENTS
We are thankful to and would like to pay our special regards to G.L
BAJAJ GROUP OF INSTITUTIONS for providing required resources for
this work. I wish to express my sincere appreciation to my supervisors,
their assistance was a milestone in completion this project. Prof Mr.
Jayati Krishna Goswami persistently guided me in planning of work and
Prof. Mr. Ramakant Baghel intelligently solved my queries. Without their
support, a significant portion of project would not be possible in
constrained time.
We are also thankful to and so must also appreciate our families and
friends for helping us survive all the stress throughout the year. To our
parents, for supporting us both on and off the water. We would like to
thank you all mentioned and other people who have helped us directly or
indirectly for pushing us farther than we thought we could.
effectively predict the stock market price, which can provide useful
insights for traders and investors about the future trends and conditions of
the market. Some of the traditional methods that have been used for this
purpose are artificial neural network, which is a technique that can find
hidden patterns and classify the data that is used for forecasting the stock
market.
However, this project proposes a different method that does not depend
on fitting the data to a specific model, but rather identifies the latent
predictions and measure the performance using root mean square error as
a metric.
LIST OF FIGURES
FIG. NO
TITLE PAGE NO
3.1 Proposed Work Flow 11
3.2
LSTM Architecture 12
3.3
CNN Architecture 14
4.1
Stock Dataset Information 20
4.2
Read Dataset 20
4.3
Graph of Closing-Price history 21
4.4
Data-scaling 21
4.5.
Long Short Term Memory Summary 22
4.6
CNN Summary 22
4.7
Training Process 23
4.8
Predicted Close Price 23
4.9
Plot for Real vs Predicted value for GOOGLE using 24
Long Short Term Memory (LSTM)
4.10
Plot for Real vs Predicted value for GOOGLE using 25
CNN
4.11 Plot for Real vs Predicted value for INFY using 25
LSTM
4.16 Plot for Real vs Predicted value for TESLA using CNN 27
4.17
Plot for Real vs Predicted value for TWITTER using LSTM 27
4.18
Plot for Real vs Predicted value for TWITTER using CNN 27
LIST OF ABBREVIATIONS
Over the years, various forecasting models have been devised and
successfully implemented in the realm of market price prediction.
Generally, this classification could be done as :
1. Time-series analysis
2. Fundamental analysis
3. Technical analysis
Time-Series Analysis
Time series analysis offers several advantages for examining trends, patterns, cycles,
or periods within the available data. This analysis is particularly valuable for
identifying early indications of bullish or bearish trends in the stock market, aiding in
making wise investment decisions.
Fundamental Analysis
Fundamental analysis is built on the belief that society requires capital for
progress, and if a company performs well, it should be rewarded with
additional capital, leading to an increase in stock price. This type of
analysis is commonly employed by fund managers because it relies on
sensible, objective evaluation using publicly available data, such as
financial statements.
Technical Analysis
Technical analysis, with its gaze keenly trained on the ephemeral realms
of short-term trading pursuits, finds its veritable haven within the dynamic
domains of commodities and forex markets. It is within these arenas that
traders fervently fixate upon the capricious fluctuations of price, seeking
to seize fleeting opportunities. And in this endeavor, they adhere to a set
of cardinal principles that govern the sagacious minds of technical
analysts. Firstly, they ardently uphold the belief that all salient
information pertaining to a company is already encapsulated within the
ethereal realm of its stock price. Secondly, they keenly observe the
rhythmic undulations of price movements, recognizing the cadence of
trends that courses through the market's veins.
And lastly, technical analysts place their faith in the unyielding tenet that
the annals of history are a prism through which price patterns reverberate,
echoing the symphony of market psychology and human behavior.
1.3Objectives
Stock market prediction involves the endeavor to forecast the future value
of a company's stock or other financial investments traded on the stock
exchange. Successfully estimating the upcoming price of a stock can yield
significant profits, enabling investors to make wise investment decisions
and generate substantial returns. By employing various analysis
techniques and methodologies, investors strive to anticipate market trends
and make informed choices that lead to profitable outcomes. Effective
stock market prediction plays a crucial role in guiding investment
strategies and maximizing potential profits.
1.4 Motivation
The results of the study demonstrate that the LSTM algorithm yielded
positive outcomes, showcasing higher accuracy in predicting stock prices.
By leveraging the capabilities of LSTM-based machine learning, the
study aimed to improve the precision of forecasting the stock prices of a
company. The findings suggest that employing LSTM in the prediction
process led to more reliable and accurate stock price forecasts compared
to other methods used in the study, such as regression.
Furthermore, the results indicate that the model performs better in short-
term prediction rather than long-term prediction based on the findings of
this study. This implies that the model's accuracy is higher when
predicting stock price trends over a shorter time horizon.
To train the model, NSE data from the internet is used, and the input is
organized and grouped based on user configuration. The RNN-based
architecture proves to be highly effective in forecasting stock prices, as it
adjusts the configuration accordingly. Additionally, the model
incorporates the backpropagation mechanism to handle data collection
and grouping, ensuring that the data remains separate and avoids any
mixing.
Overall, the study highlights the effectiveness of using LSTM and RNN
models for stock price prediction, leveraging various factors and
strategies to improve accuracy. The model's ability to capture detailed
features and adapt to different configurations contributes to its success in
forecasting stock prices.
Stock Market Prediction Using
Machine Learning Techniques:
Publication Year: 2016
Author: Mehak Usmani, Syed Hasan Adil, Kamran Raza, Syed Sad Azhar Ali
Journal Name: 2016 IEEE
Summary: The primary objective of this study is to forecast the market
performance of the Karachi Stock Exchange (KSE) based on day closing
using machine learning algorithms. The model predicts whether the
market performance will be positive or negative, utilizing a range of input
attributes. These attributes include oil rates, gold and silver rates, interest
rates, foreign exchange (FEX) rates, news, and social media feed.
The study identifies that the most influential feature in predicting the
market performance was the oil rate attribute. The findings validate that
machine learning techniques have the capability to predict the stock
market's performance. The Multi-Layer Perceptron algorithm achieved a
70% accuracy in correctly predicting market performance.
The experimental results demonstrate that the model achieves a high level
of accuracy when using multi-feature input variables, with an accuracy
rate of 0.033. This indicates that the model's predictions closely align with
the actual stock values and meet the desired forecasting objectives.
On the other hand, when using univariate feature input, the model exhibits
a higher predicted squared absolute error of 0.155. This suggests that the
model's performance is inferior compared to the multi-feature variable
input scenario. The use of multiple input features allows the model to
capture a broader range of information and patterns, resulting in improved
accuracy in stock price prediction.
The goal is to enhance the accuracy and stability of stock price prediction
by employing the RNN LSTM architecture. The paper conducts
experiments using ten sets of historical stock data and achieves an average
error rate of 3.449 Root Mean Square Error (RMSE).
The case study relies on data from Standard & Poor's (S&P500) and
NASDAQ. The researchers develop a stock prediction model that
leverages LSTM and compares it with other models for performance
evaluation. The main discovery of the case study is that their forecasting
system provides a more accurate prediction of the stock closing price for
the next day compared to the comparison models.
To demonstrate the utility of the system, the researchers test five different
models: Moving Average (MA), Exponential Moving Average (EMA),
Support Vector Machine (SVM), and LSTM. The target of prediction is
the closing value of the stock for the next day.
The results of the study suggest that deep learning algorithms outperform
MLR in terms of prediction accuracy. The SVM algorithm also shows
promising results. By leveraging sentiment analysis and selecting the most
effective prediction algorithm, the study enhances the accuracy of stock
price forecasting.
The paper outlines a time series analysis method that combines information
theory and Long Short-Term Memory (LSTM) to model the dynamics of stock
prices. The integration of transfer entropy, which quantifies the information
flow between relevant variables, enhances the prediction accuracy of the LSTM
model. The results show a high correlation between the modelled and real stock
prices, although there may be slight differences in terms of Mean Absolute
Error (MAE) and Root Mean Square Error (RMSE).
It's important to note that while machine learning can offer powerful
prediction capabilities, it is still essential to carefully consider the quality
and relevance of the input data, as well as the appropriate preprocessing
and feature selection techniques.
The proposed system for stock price prediction in this paper utilizes
machine learning techniques, specifically the LSTM algorithm, to
estimate the future value of stocks. LSTM is chosen as it has shown to
perform well in capturing the behavioral changes in stock prices over a
given period.
The dataset used for analysis is obtained from Yahoo Finance and consists
of approximately 900,000 records of stock prices and relevant data. The
data includes information such as date, symbol, open price, close price,
low price, high price, and volume. Only data for a single company is
considered in this study. The data is preprocessed by transforming it into a
data frame using the Pandas library in Python.
The study concludes that LSTM is the best model for the proposed
methodology, indicating its ability to identify patterns and relationships
within the data. It acknowledges that the stock market may not always
follow a regular pattern or cycle, and the duration and existence of trends
can vary based on companies and sectors. By analyzing trends and cycles
using techniques like LSTM, investors can potentially obtain higher
profits by leveraging current information and analyzing various factors.
Various attributes related to stock prices, such as open price, high price,
low price, close price, and adjusted close price, are extracted from a large
dataset. These attributes serve as input features for training the models.
To preprocess the data, techniques like normalization and one-hot
encoding are applied. Normalization is used to scale the numerical
features within a certain range, while one-hot encoding is employed to
convert categorical variables into binary vectors. The preprocessed data is
then divided into two sets: a training set and a testing set. The ratio of the
split is 80:20, meaning 80% of the data is used for training the models,
and the remaining 20% is reserved for evaluating their performance.
Three different approaches are used to train the models: LSTM, CNN, and
a combination of LSTM and CNN (LSTMCNN). These models are
trained using the training set, and their parameters are adjusted to
minimize the prediction errors.
To evaluate the performance of the models, the root mean square error
(RMSE) is calculated. RMSE measures the difference between the
predicted values and the actual values, giving an indication of how well
the models are able to estimate the stock prices.
LSTM-Architecture
Structure Of LSTM :
LSTM (Long Short-Term Memory) consists of a chain-like structure that
includes four neural networks and specialized memory cells called cells.
One of the key features of LSTM is the presence of memory cells, which
make decisions regarding storing, reading, writing, and forgetting
information.
A memory cell in LSTM is equipped with three main gates:
oInput gate: Controls the flow of new information into the memory cell.
oForget gate: Regulates which information remains stored in the memory
cell.
oOutput gate: Determines how the information in the memory cell is used
to compute the output.
These gates enable LSTM to effectively process and retain important
information over longer sequences, allowing it to capture and utilize long-
term dependencies in the data.
LSTM Has Several Major Applications :
Handwriting-generation
Image Captioning
Language Modelling
Machine Translation
Question-Answering Chatbot
Working of CNN model :
CNN Architecture
A Convolutional Neural Network (CNN) model consists of several key layers that
collectively enable effective image processing and feature extraction. These layers
include:
2. MAX Pooling: MAX pooling is a downsampling operation that reduces the spatial
dimensions of the feature map. It selects the maximum value within a local region,
thereby preserving the most salient features while reducing computational
complexity and enhancing translation invariance.
4. Flatten: The flatten layer reshapes the multidimensional feature map into a one-
dimensional vector. It converts the spatial structure into a sequential format,
preparing the data to be fed into a fully connected layer.
5. Dense: The dense layer, also known as a fully connected layer, is responsible for
learning complex relationships between the extracted features. Each neuron in this
layer is connected to every neuron in the previous layer, and the connections are
weighted. The dense layer performs linear operations followed by non-linear
activation functions.
In summary, the layers in a CNN model work in tandem to extract features, reduce
spatial dimensions, regularize the network, capture complex relationships, and
introduce non-linearity, ultimately enabling the model to learn and make predictions
based on input images.
Chapter Four - Dataset, Implementation and
Result
The dataset utilized in this study encompasses a wide range of historical stock data
obtained from the National Stock Exchange (NSE). It provides a comprehensive and
detailed view of the daily performance and behavior of various stocks across
different sectors. The dataset covers multiple sectors, including Banking, Pharma,
Petroleum, Software, and Textiles, allowing for a diverse analysis of the stock
market.
For each stock, the dataset captures essential attributes that are crucial in
understanding its performance. These attributes include the opening price, which
represents the initial trading price of the stock at the beginning of the trading day.
The highest price denotes the maximum price at which the stock was traded during
the day, while the lowest price signifies the minimum price observed for the stock
during the same period.
The closing price indicates the final trading price of the stock at the end of the
trading day, providing valuable information about the overall market sentiment and
investor activity. The adjusted closing price takes into account any corporate actions
or events that might affect the stock's value, such as stock splits or dividends.
In addition to price data, the dataset also includes the trading volume of stocks,
which reflects the total number of shares traded for a particular stock during a given
period. This volume information is crucial in understanding market liquidity and the
level of investor participation in buying or selling a particular stock.
4.2.1 PYTHON
The choice of Python as the programming language for this project was made
for several compelling reasons. Firstly, Python boasts a large and active
community of developers, making it easy to find solutions and resolve any
challenges encountered during the project. The availability of resources like
Stack Overflow ensures that assistance is readily available for troubleshooting
and problem-solving.
4.2.4 TENSORFLOW
With Jupyter Notebook, users can combine narrative text, live code,
visualizations, and mathematical equations in a single document.
This makes it a versatile tool for data scientists, researchers, and
analysts to explore and communicate their findings effectively.
Step 1: Dataset-Analysis
Read Dataset
Post performing data analysis, We had read the dataset and it showed the
dataset information table starting from the tail. There are 4276 data are
available in each companies dataset.
Step 3: Graph of Closing-Price history :
Step 4: Pre-processing :-
In the training process, a total of 1548 samples are used for training the
model, while 464 samples are set aside for validation. The data is
processed in batches, with a batch size of 1, meaning that each iteration
of the training algorithm uses a single sample to update the model's
parameters. The training process is performed for one epoch, which
means that the entire training dataset is passed through the model once.
This allows the model to learn from the training data and make
adjustments to its parameters based on the observed outcomes. The
validation samples are used to evaluate the performance of the model
during training and provide feedback on its generalization capabilities.
Step 8: Predicted Result
Predicted Closing-Price
Stock Name
LSTM CNN
GOOGLE 22.5409 8.1499
INFOSYS LTD. 19.4190 16.2015
T-MOBILE 5.4396 6.6478
US Inc.
TESLA 4.7152 3.7276
TWITTER 1.3909 2.5984
Accuracy
Step 9: Predicted Graph
Plot for Real vs Predicted value for T-MOBILE US Inc. using CNN
Plot for Real vs Predicted value for TESLA using LSTM
Fig 4.18: Plot for Real vs Predicted value for TWITTER using
CNN
Chapter Five - Conclusion and Future
Work
In the report, we compare the LSTM model and the CNN model for
predicting the future value of stocks using data from NSE listed
companies. The proposed method demonstrates its ability to identify
interrelationships within the data by utilizing specific information at a
given moment for prediction. While LSTM and CNN models are
commonly used in other time-dependent data analysis, they may not
always capture the rapid changes that occur in the stock market.
To enhance accuracy, future work may involve training the models with
more diverse and detailed data. Additionally, exploring the use of other
algorithms and incorporating them into a hybrid model could lead to
further improvements.
Bibliography & References :