[go: up one dir, main page]

0% found this document useful (0 votes)
42 views29 pages

STOCK PREDICTION USING LSTM - With Testing

The document discusses using deep learning techniques to predict stock prices and recommend buying or selling stocks based on a user's investment amount and company choice. It covers literature on the topic, proposed chapter structure, and mentions extracting features from stock market data for preprocessing.

Uploaded by

Shruthi B
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views29 pages

STOCK PREDICTION USING LSTM - With Testing

The document discusses using deep learning techniques to predict stock prices and recommend buying or selling stocks based on a user's investment amount and company choice. It covers literature on the topic, proposed chapter structure, and mentions extracting features from stock market data for preprocessing.

Uploaded by

Shruthi B
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

STOCK PREDICTION USING LSTM

Project Report
Submitted By
BHAARATHI NATARAJAN (2021115019)
NAREN KARTHICK (2021115069)
SHRUTHI B (2021115106)

In partial fulfillment for the award of the degree


of
BACHELOR OF TECHNOLOGY
in
INFORMATION TECHNOLOGY

DEPARTMENT OF INFORMATION SCIENCE AND


TECHNOLOGY
COLLEGE OF ENGINEERING GUINDY
ANNA UNIVERSITY, CHENNAI 600025
SEPTEMBER 2023
TABLE OF CONTENTS

ChapteN Title Page


o.

1. Acknowledgement

2. Abstract

3. Introduction

4. Literature Review

5. Chapters

6. List of Figures

7. System Architecture and Design

8. Problem Statement

9. Solution Strategy
10. Methodology

11. Data Exploration and Preprocessing

12. Algorithms and Techniques

13. Evaluation Metrics

14. Exploratory Visualization

15. Statistical Calculation

16. Implementation

17. Testing and Deployment

18. Python Packages Used

19. Conclusion

20. References
ACKNOWLEDGMENT
We would like to express our sincere gratitude to Dr. G Geetha
(Associate Professor, DIST), Ms. G Mahalakshmi (Teaching
Fellow, DIST), and Ms. Sangeetha (Research Scholar, DIST),
who formed our discussion panel with regards to this project for
their guidance and support which was invaluable throughout the
course of the project.
We would like to Appreciate and Applaud their efforts taken by
Dr. S Swamynathan (HOD, DIST) to ensure that the program
‘Socially Relevant Project’ runs smoothly and in a timely
manner, and for his efforts in fostering an environment
conducive to learning and exploration. We are grateful for the
enriching knowledge achieved from our professors and peers
alike in due course of this project.
We extend our heartfelt thanks to the Department of Information
Science and Technology for their commitment to academic
excellence, and experiential learning that provided us with a
solid foundation for this undertaking.
ABSTRACT
This project aims to use Deep Learning methodologies in the
financial sector, more specifically the stock market in order to
predict the possible profits on investment and to recommend
whether for a particular time duration whether the stock can be
bought or sold, based on the amount of money that is liable for
investment.

Among students there is a general lack of awareness regarding


the scope of investing and the importance of knowledge of the
stock market. The learning curve for the purpose is quite gradual
as the process of doing so may take years. Amateur decisions
often lead to massive losses on this regard, as can be seen by
many indian investors in the current day scenario. It is of great
importance to generate a secondary income as a student in order
to sustain in the present economic scenario, where youth
unemployment rates have skyrocketed. To accurately predict the
current state of the market, and make data driven decisions, this
project aims to equip its users with the necessary tools to ensure
that their decision is backed by mathematical evidence.

INTRODUCTION
Economics and Finance are fields that have always been
relevant to any form of growth, from the perspective of an
individual to the perspective of entire nations. Finance and
economics go hand in hand, the current state of the economy
affecting finances, and the current state of finances affecting the
economy. They are not mutually exclusive. In order to
understand the financial market, one must be aware of various
phenomena that affect it. It is no surprise that volatility is an
inherent feature of the market, and it is affected by so many
factors that it may take years to fully grasp a proper
understanding of when the market will grow, and when it will
fall short.

An important aspect of the financial market (apart from


traditional investment methodologies such as mutual funds and
fixed deposits), is the stock market. Stocks, or equity, is
essentially shares of ownership of a company. If a person was to
buy a stock from say company ‘X’ for the price ‘Y’, then he or
she owns some ‘Z’ percentage of the company. Now, if the
person were to decide that he or she is going to sell the stock,
then a profit of some ‘M’ will be obtained based on the current
value of the stock of company ‘X’. This stock value depends on
how well the company is performing, the current market status,
and various other features. Some of the more important market
terminologies have been discussed here as follows:
1. OLPC parameters: The open, high, low, and closing
features of a stock discuss the opening price of the stock
when the market day begins, highest price of the stock
during the day, lowest price during the day and the final
price of the stock at the end of the day. Adjusted closing
price is a more accurate measure of closing price. These
parameters convey how well a stock performs, and can be
used to make predictions on how a stock will perform in
the future.
2. Moving Average: This is a technical analysis tool that
allows traders to identify trends, momentum and the level
of resistance.
Note: Trends and seasonality are inherent features of stock
market data as they fall under the category of time series
data
3. Return Price: The price at which a stock of a company can
be sold for currently.

The stock market involves buying and selling these shares to


make some profit. However, it is not always profitable, as the
state of the market is highly unpredictable. A stock that was
performing well, may not continue to do so, despite historic
evidence. As a result, losses may be incurred. It is this loss that
prevents a person from investment in the stock market.

On the world wide web, there are many resources such as


‘Money Control’ that serve the purpose of displaying the current
market status, along with providing recommendations on what
stock to invest in. However, investment is normally
recommended over a very long duration of time. This is because,
if one were to invest only for a few weeks, the amount of profit
gained on the capital is so low, that it makes no difference at all.
However, there is a recurrent trading strategy known as high
frequency trading wherein experts frequently buy and sell shares
on a daily basis. Although from the perspective of a smaller time
frame they appear to lose money, on a much larger time frame
they actually gain a huge margin.

Taking into account the lack of awareness of such strategies,


improper or misinformation on stocks being spread, and the
overall uneasiness surrounding the stock market, not many
people are actively involved in investments. Many times, the
people who are investing, are doing so in an extremely
uneducated fashion, that it almost seems as though they are
gambling.
In order to prevent such adhocism and misinformation regarding
stocks, and to educate the general population (with a particular
bias to students as they serve as the major demographic of this
project), this deep learning project renders assistance towards
gaining knowledge on top performing companies on the basis of
recently updated census data, predicting the performance of a
chosen company on the basis of their moving average and
deploying a deep learning model that recommends whether a
user should buy a stock, or sell it on the basis of what amount
the user wishes to invest, and what company the user wishes to
invest in.

LITERATURE SURVEY
With the increasing availability of stock market data on the
world wide web, various machine learning and deep learning
models have been researched and explored for the use of stock
prediction. Various algorithms such as Random Forest
Prediction, Bayesian Belief Networks, Support Vector Machines
have been employed for this purpose.

Research similar to predicting the closing prices of stocks using


a multi-layered perceptron and support vector machines have
been conducted on the basis of data scraped from the web. [1]
(Jingyi Shen and M. Omar Shafiq 2020).

Some of the more commonly used datasets for the purpose of


stock prediction have been taken from Kaggle, as was done by
Kifayat Ullah and Mohammed Qasim [2], in their paper on
Google Stock Prediction using Comprehensive Neural
Networks.

As opposed to supervised and unsupervised learning,


reinforcement learning techniques were used to predict purely
the return price of stocks as opposed to the adjacent closing
price, as it may convey a more cohesive information to a user on
the amount they may gain on investment. [3] (Alamir Labib
Awad 2023)

CHAPTERS
Chapter Title Page
No. No.

1. System Architecture and Design

2. Problem Statement

3. Solution Strategy

4. Methodology

5. Data Exploration

6. Preprocessing

7. Evaluation Metrics

8. Exploratory Visualization

9. Statistical Calculation

10. Implementation

11. Deployment and Testing

12. Python Packages utilized


LIST OF FIGURES
Fig Figure Title Figure Plot
No.
1. Historical view of the closing
price of stocks, with the time
plotted on the X - axis and
stock prices plotted on the Y-
axis.

2. Volume of Sales view, plotted


by extracting the volume
feature in the dataset against
time.

3. Plot of the moving average of


various companies, over
differing durations of time
marked with three different
colors.

4. Extracting daily return


information by calculating
percent change of Closing
prices and plotting the values
against time for each
company.
5. Normal distribution /
histogram of daily return
prices to visually represent
the frequency of occurrence
of individual daily return
values for sampled data of
each company
6. Pair Plot to show the
correlation of daily returns of
two companies to see their
relationship.
(This example shows one
company with respect to
itself)

7. Pair plot of Microsoft against


Google in terms of daily
return
8. Pair plot that helps view
linear correlations between
the companies that have been
chosen, along with the normal
distributions of daily returns

9. Kernel Density Plots of daily


returns of one company with
respect to another company,
along with normal
distributions and scatter plots

10. Plotting the correlation of


Return prices and Closing
prices of various companies.

11. Plot representing the risks of


investing in a company (risk
analysis)
12. Representing the Actual
Closing prices vs. the Closing
Prices predicted by the LSTM
model.

SYSTEM ARCHITECTURE AND DESIGN


The machine learning approach is often static, and requires the
entire process to be repeated in order to account for data that is
constantly changing. For an application or model that must
constantly pump out different results for different variations in
data, machine learning can often result in inefficient processing,
and high response times. As a result, this project chooses the
Deep Learning route by deploying a Long Short Term Memory
model and training it constantly on available data using the ‘K
Fold Cross Validation’ technique. Details for the same will be
referred to in chapter 4.

The relevant system architecture is given as follows:


PROBLEM STATEMENT
The increasing volatility in the global economy has driven the
population to investments as means for secondary income and
long term sustenance. People who fail to adapt will face severe
financial difficulties due to several reasons. In order to prevent
the same, learning about the stock market is very essential. This
project aims to equip its users with the ability to read and
understand various stock features, and make an informed
decision on whether to buy or sell a stock on the basis of the
amount they are willing to invest.

SOLUTION STRATEGY
This project utilizes the Deep Learning network - Long Short
Term Memory network, which is a recurrent neural network
trained to analyze sequential data.

Visualizations of the existing data is presented to the users,


highlighting the features for the same. Elaborate graphs that
have been plotted on the data are also displayed, which serves as
a means for understanding the data and what lies in it.

Using yahoo finance data, and existing census data, a list of


companies whose stocks are prevalent have been selected, and
these companies will be presented to the user. For each of these
companies, an LSTM model has been trained which will predict
the closing price of the stock for a particular date (which the
user may define), and recommend whether to buy or sell the
share. The special feature of this model is its nature in predicting
both short term and long term closing prices which has been
leveraged in this project. Evidence for the selection of
companies is provided by means of census data visualization,
and evaluation of the model is done for every prediction as a test
of its accuracy.
METHODOLOGY
The data collected from various sources is organized into data
objects with consistent features and values associated with those
features. This data pertains to the stock features High, Low,
Open, Close, Adjusted Close and Volume of each company that
is selected on the basis of census data.

As opposed to just choosing companies from the census data,


yahoo finance has been referred to get accurate values for
various features of the stocks pertaining to each top performing
company.

Once the data has been collected and ordered (while ensuring
consistency and limited missing values), detailed visualization
has been done for each feature, by looking at its change over a
period of time. This includes various scatter plots, histograms
and pairplots that have been constructed, and will be explained
in section a.

The data is now transformed or normalized to fit into a uniform


range of [0 - 1], in order to improve the efficiency of the neural
network.
This data is now split into a training set and a testing set by
means of K fold cross validation, wherein the entire data is split
into K partitions or folds. Out of these K folds, one is reserved
for the purpose of testing the model. This procedure is to ensure
that the entirety of the available data is used to train the model,
and as a result can greatly help in improving the bias-variance
tradeoff, or the accuracy of prediction of the model.

Once the data has been successfully split, a Long Short Term
model is built. The model is built with an input layer consisting
of 128 LSTM instances, a single hidden layer consisting of 64
LSTM instances and an output layer. The optimizer for this
neural network is chosen to be the standard adam optimizer, and
the loss function is set to mean squared error.

Once the model is built, it is trained on the scaled training set,


and tested on the scaled testing set. For a neural network, its
performance can be tested by ‘Mean Absolute Percent Error
(MAPE), ‘Mean Absolute Error’, ‘R2 Score’ and ‘Root Mean
Squared Error’.

Once the percent error has been found to be below the standard
threshold for each metric, the model is ready for deployment and
testing.
In the forthcoming sections, each phase of the model
deployment cycle has been explained in detail.
a. Data Exploration and Cleaning:
This process is necessary to ensure that the accuracy of the
prediction is good, and Preprocessing the data correctly
ensures that the model can run with a lower probability of
facing errors.
Data is first collected from various libraries that use APIs to
extract real world data. These libraries are updated on a daily
basis, thus ensuring the goodness of data.
This data is then preprocessed to check for null or missing
values, which is quite rare as stock market data is often
collected from reputed resources that log their values in an
orderly fashion. Before this data is normalized and sent to
train the model, some essential visualizations must be done
in order to understand the data and what truly lies in it.

For the given stock market data, the features have been
highlighted as - Open, High, Low, Close, Volume and
Adjusted Close. Each of these attributes for every company
can be visualized by plotting them against a given period of
time as follows:
Fig 1: Representing the attribute ‘Close’ for the companies
Apple, Google, Microsoft and Amazon. This provides
information on the trends and seasonality seen in the closing
prices of the stock. From the graph we can infer that for most
of these companies, there is a negative trend, i.e, there is an
overall decrease in the closing prices. These may be due to
several economic and political factors.

The above figure can be constructed for several attributes,


however closing price happens to be the most essential in
determining whether an investment will lead to a net positive
or negative gain.
Fig 3: Plot of the moving average of the stocks for companies
Apple, Google, Microsoft and amazon. The moving is plotted for
a period of 10 days, 20 days and 50 days, and it smooths out
small fluctuations in the trend to eliminate noise in the data.
Noise can be very risky for the purpose of prediction.

While the above graphs convey attribute specific information for


individual companies, the attributes for the stocks of various
companies can be compared in order to infer whether one
company’s growth or fall affects another company. This is an
important inference as it conveys which group of companies
show a similar trend. Investing in those groups or avoiding those
groups may be greatly beneficial.
Fig 9: Normal distributions for various companies have been
illustrated, which plots the frequency of occurrence of a
particular value corresponding to the attribute ‘Daily Return’
that is computed by taking percent change of adjacent closing
price. Kernel Density Plot has also been utilized for the purpose
of bivariate analysis, and a scatter plot has been constructed to
showcase the linear relationship or growth between these
companies. This indicates that for companies X, Y in the list
Apple, Google, Microsoft and Amazon, when the stock prices of
X increase, the stock prices of Y also increase.
The margin of increase of the price can be conveyed by plotting
a heat map annotated with the correlation values corresponding
to the closing prices of the various companies (Illustr. Fig 10).
This quantifies the magnitude of increase of one stock with
respect to another.

Fig 10. Heatplot for Closing Price and Daily Returns for the
four standard companies.
b. Algorithm and Techniques:
For the purpose of prediction, LSTM (a recurrent neural network
that contains various memory cells to store subsequent outputs
to enhance the prediction and recall capabilities of the model)
has been employed.
The advantage of using LSTM as opposed to traditional machine
learning models is the performance enhancement and the ease of
use in terms of constantly updated data.
The model is trained and tested on the existing dataset of various
companies, (categorized into training and testing sets by means
of K fold validation) continuously over a period of time, so that
it learns the trends present in the stock prices, and the various
closing prices at various points in time.
After training the model, it is then tested on one portion or fold
of the existing data. The predictions obtained are plotted against
the actual values for the given dates, as can be seen in Fig 12.

Fig 12: Plot of predicted vs actual values of the Closing prices


of the stocks for Apple. The blue section indicates the training
data, the red section indicates actual values of the stock for the
year 2022, and the yellow section indicates the predicted values
for the year 2022. This is used as evidentiary support to assist
the user in making an informed decision regarding investments.

c. Evaluation Metrics:
The correctness and accuracy of model prediction is calculated
using four metrics. Normally in the case of neural networks, F1
scores, precision and recall would be employed, however as this
project does not deal with the task of segregation, those metrics
will produce erroneous results, and thus have been omitted.
Mean Absolute Percent Error: This is a statistical metric that
assesses the accuracy of a forecasting method. This is relevant
as the LSTM model forecasts the closing prices of a company’s
stock.
Mean Absolute Error: This metric measures the error between
paired observations expressing the same phenomenon.
R2 Score: It is the coefficient of determination, which is a
statistical measure to figure out how close the forecasted values
are to the fitted regression line.

IMPLEMENTATION
The entire project has been implemented in Python with the
infamous Machine Learning Libraries:
1. Numpy
2. Pandas
3. Matplotlib
4. Seaborn
5. Scikit-learn
6. TensorFlow
The data necessary for stock prediction was obtained from:
1. pandas.data_reader( )
2. YFinance
The pandas data reader library allows stock data access from
yahoo finance, and has special methods to preprocess and format
this data. The yfinance library includes all of the stock data from
yahoo finance that is updated on a daily basis.
The LSTM model was implemented using TensorFlow.keras,
which is lightweight and runs quicker than a tensor flow
instance on local machines. The model contained 4 layers with
one input layer, two hidden layers and one output layer. The
optimiser was defined as ‘adam’ and the loss function was set to
mean squared error. Both of these metrics are standard for any
neural network, although they can be modified. This project
chooses to retain the aforementioned.
The input layer contains 128 instances of the LSTM class,
whereas the first hidden layer contains 64 instances of the same.
The second hidden layer and the output layer contain 25 and 1
instances of the Dense class respectively.
The input for the project is the time duration for investment and
the amount that the user wishes to invest. The output of the
model is the approximate closing price of the stock at that time
period, and whether the stock can be bought or sold (pertaining
to one company). This is also supported by illustrating the
moving average of the stock for that time period.

A prototype for the project is given as follows:


DEPLOYMENT AND TESTING
In order for ease of access for the user, the deep learning model
has been deployed in a simple flask based web application that
includes an interface for users to select various companies and
view various graphs pertaining to those companies. Clear cut
indications, and explanations for occurrence of data has also
been explained in the web application to increase the
accessibility to the users, and to achieve to overall motive of the
project.

Detailed Testing Report:


Data Collection and Missing values test
Constant data updation test
Generating readable graphs that meet the visualization
standards for an application
Proper data validation and consistency checks
Testing for nominal bias variance trade off with proper
cross validation
Performance test
Prediction test
Testing the model on CLI - Guide / Exper
Testing the model on CLI - generic user / Fellow Student
Testing the model on Web App - Guide / Expert
Testing the model on Web App - Generic user / Fellow
Student

These testing methodologies are checked for every phase of the


project, and done continuously as the project progresses in order
to ensure that the functional and nonfunctional requirements are
met
The functional requirements include input validation and
correctness of prediction. The non functional requirements
include User interface for the web application and presentation
of various information to the user.

CONCLUSION
Thus, the stock prices for a given period of time, regardless of
whether the duration is long or short, can be predicted accurately
using a deep learning reinforcement approach for prediction.
This prediction is also supported by various plots and statistical
metrics that strongly indicate to the user whether the investment
can be made or not. The user also has the leverage to enter the
necessary amount that he or she wishes to invest in a stock.

Stock prediction is an essential tool that many investors utilize


to check whether their expertise opinion corresponds to a
mathematical output that is supported via statistics. Apart from
mature investors, amateurs as well as people who have never
invested before can utilize these prediction strategies to decide
whether or not they can proceed with an investment. This
project also educates its users on the various features of the
stock and trends that are seen in shares of every company, and
the correlation between various companies in the stock market.
REFERENCES
1. https://link.springer.com/article/10.1007/s10619-020-07291
-1 - Testing the performance of a Long Short Term Memory
network
2. https://www.mdpi.com/2571-5577/6/6/106 - Stock Market
Prediction using Reinforcement Deep Learning
3.

You might also like