Download as pdf or txt
Download as pdf or txt
You are on page 1of 17

Stock Market Predictions Using Sentiments and Technical

Analysis

Project report submitted in partial fulfillment


of the requirements for the degree of

Bachelor of Technology
in
Computer and Communication Engineering

by

Sajal Pathak - 19UCC031


Siddhartha Singh - 19UCS167
Piyush Chauhan - 19UEC029

Under Guidance of
Mr. Mukesh Jadon

Department of Computer and Communication Engineering


The LNM Institute of Information Technology, Jaipur

December 2022
Copyright © The LNMIIT 2022
All Rights Reserved
The LNM Institute of Information Technology
Jaipur, India

CERTIFICATE

This is to certify that the project entitled “Stock Market Predictions Using Sentiments and
Technical Analysis” , submitted by Sajal Pathak (19UCC031), Siddhartha Singh (19UCS167)
and Piyush Chauhan (19UEC029) in partial fulfillment of the requirement of degree in
Bachelor of Technology (B. Tech), is a bonafide record of work carried out by them at the
Department of Computer and Communication Engineering, The LNM Institute of
Information Technology, Jaipur, (Rajasthan) India, during the academic session 2016-2017
under my supervision and guidance and the same has not been submitted elsewhere for award
of any other degree. In my/our opinion, this report is of standard required for the award of the
degree of Bachelor of Technology (B. Tech).

Date Adviser: Mr. Mukesh Jadon


Acknowledgment

I would like to express my heartfelt gratitude to Mr. Mukesh Jadon for his guidance, support,
and encouragement throughout the duration of my Btech final year project. Your valuable
insights and suggestions have helped me to shape and complete my project in a meaningful
way.

I am deeply grateful to my friends and colleagues who have provided me with invaluable
support and assistance throughout the project. Your constant encouragement and motivation
have kept me going through the ups and downs of this journey.

Finally, I would like to express my gratitude to LNM Institute of Information Technology for
providing me with the opportunity to pursue my Btech degree and for supporting me
throughout my academic journey.

Thank you all once again for your invaluable support and guidance.

iv
Abstract

The stock market is a complex system that is influenced by various factors such as economic
conditions, company performance, and investor sentiment. In this project, we aim to develop
a model that can accurately predict stock price movements using sentiments and technical
analysis.

In this project, we propose a novel approach for stock market prediction using long
short-term memory (LSTM) and gated recurrent unit (GRU) recurrent neural networks
(RNNs). RNNs are a type of deep learning model that can process sequential data, making
them well-suited for time series prediction tasks such as stock market prediction.

The LSTM and GRU RNN models are trained and tested on a diverse set of stock tickers, and
the prediction results are compared to a baseline model that uses a simpler RNN architecture.
Our experimental results show that the LSTM and GRU RNN models achieve significantly
better prediction accuracy compared to the baseline model.

For Sentiment Analysis, To achieve this, we first collect and analyze a large dataset of news
articles, social media posts, and other relevant sources to extract sentiment-related features.
These features are then combined with technical indicators such as moving averages, relative
strength index, and Bollinger bands to build a machine learning model.

The model is trained and tested on a diverse set of stock tickers, and the results are compared
to a baseline model that only uses technical indicators. Our experimental results show that the
proposed model significantly outperforms the baseline in terms of prediction accuracy and
stability.

Overall, this project demonstrates the potential of using sentiments and technical analysis for
stock market predictions, and the developed model can be a useful tool for investors and
traders.

v
Contents

List of Figures vii

1 Introduction 1
1.1 Need of Stock Predictions . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.2 Long Short-Term Memory RNN . . . . . . . . . . . . . . . . . . . . . . . . 1
1.3 Gated Recurrent Unit RNN . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.4 Sentiment Analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2

2 Proposed Work 3
2.1 Dataset for RNN’s . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
2.2 Dataset for Sentiment Analysis . . . . . . . . . . . . . . . . . . . . . . . . . 4
2.3 Structure of the Recurrent Neural Networks . . . . . . . . . . . . . . . . . . 4
2.4 Structure of the Sentiment Analysis . . . . . . . . . . . . . . . . . . . . . . 5

3 Simulation and Results 6

4 Conclusions 9

5 Bibliography 10

vi
List of Figures

3.1 INFY stock trend predicition using LSTM model . . . . . . . . . . . . . . . 6


3.2 INFY stock trend predicition using GRU model . . . . . . . . . . . . . . . . 6
3.3 Extrapolation on MSFT stock using GRU model . . . . . . . . . . . . . . . . 7
3.4 Extrapolation on MSFT stock using LSTM model . . . . . . . . . . . . . . . 7
3.5 Sentiment Analysis on MSFT stocks . . . . . . . . . . . . . . . . . . . . . . 7
3.6 StreamLit App for Stock Prediction . . . . . . . . . . . . . . . . . . . . . . 8

vii
Chapter 1

Introduction

1.1 Need of Stock Predictions

Predictions about the stock market are significant for a number of reasons:

• Stock market forecasts are frequently used by investors to guide their investing
choices.A forecast of a company’s future performance,for instance,may be used by an
investor to decide whether to purchase or sell its stocks.

• Stock market forecasts may be used to manage risk by spotting prospective threats and
opportunities in the market. An investor may be able to lower their exposure to risky
assets, for instance, if a market slump is predicted.

• Stock market forecasts may shed light on the general status of the market and aid ex-
perts in spotting patterns and trends. Making knowledgeable selections regarding market
strategy can benefit from this information.

• Companies may utilise the stock market for business planning projections to guide their
strategic strategy and commercial decisions. For instance, a business may utilise fore-
casts about the state of the market to guide employment and investment decisions.

• In general, stock market forecasts may be a useful tool for traders, investors, and com-
panies trying to make market-related choices.

1.2 Long Short-Term Memory RNN

Recurrent neural networks (RNNs) with long short-term memory (LSTM) are a form of deep
learning model that excel at tasks requiring sequential input, such time series prediction and
natural language processing.The capacity of LSTM RNNs to preserve long-term

1
chapter: 01 2

dependencies in the data is one of its major characteristics. Due to the vanishing gradient
problem, where the influence of older inputs on the present output declines exponentially
over time, traditional RNNs frequently suffer with this. LSTM RNNs employ gates to solve
this issue, enabling the model to selectively remember or forget information as required.

1.3 Gated Recurrent Unit RNN

Recurrent neural networks (RNNs) of the Gated Recurrent Units (GRUs) variety are intended
to better capture long-term relationships in sequential data. GRUs are meant to analyse
sequences of input data, such as time series, audio data, or spoken language, just like other
forms of RNNs, such long short-term memory (LSTM) networks.GRUs are referred
described as ”gated” because they employ gating systems to regulate the information flow
across the network. With the use of these methods, the model is better able to identify
longer-term relationships by being able to choose keep or delete information from the input
sequence.Compared to LSTMs, GRUs have a more straightforward structure, which may
make them faster to train and operate. Additionally, it has been discovered that they excel in a
number of tasks, including speech recognition, machine translation, and language modelling.

1.4 Sentiment Analysis

Natural language processing (NLP) and machine learning algorithms are used in the approach
of sentiment analysis to find and extract subjective information from text data. It is frequently
employed in many different applications, such as stock prediction analysis.The sentiment
expressed in online news articles, social media messages, and other text data pertaining to a
certain stock or company may be examined using sentiment analysis in the context of stock
prediction analysis. It is hypothesised that the mood reflected in this text data can provide
light on how investors and the general public feel about the firm, thereby assisting in the
prediction of future stock performance.It’s critical to remember that sentiment analysis is
only one of many variables that may have an influence on stock performance and is not a
predictor of future success.When choosing an investment,it is wise to take a variety of aspects
into account.
Chapter 2

Proposed Work

Stocks usually follow a trend over their lifetime. The RNN systems are designed to extract
the trend from the given dataset. The RNN’s will learn the trend using the previous days
stock prices and will validate that learning over given validate dataset,after this the system
will try to predict the prediction dataset. Two RNN’s qre used for prediction and the
mse(mean square error) is used as the metric for comparison,the system that will have the
least mean square error will be used as the final prediction model in the streamlit app.
Technical analysis can be effective some time but only previous price values cannot define a
stock price. Looking towards other factors which effect the stock price we encountered news
sentiments. Most of the traders seem to follow news headlines to buy or sell stock, thus news
headlines can be a effective in predicting if a person should buy or sell a stock on a certain
day. This can be crucial for our app. Thus we used sentiment analysis to gather the compound
sentiment towards a company in news on a certain day.

2.1 Dataset for RNN’s

• For data gathering we used yahoo finance website, the website contains the stock prices
for multiple stocks.

• For the current analysis we have used Microsoft stock data dated from 1986-03-13 to
2022-11-17.

• First of all the dataset index column that is Date is type converted from string to datetime.

• As the dataset needed to serial correlated we create a windowed dataset which has the
data for 3 previous days and the current day.

• We then used 80% of the dataset for training purposes , 10% of dataset for validation
and 10% for testing.

3
chapter: 01 4

2.2 Dataset for Sentiment Analysis

• News Headlines are needed for sentiment analysis, a data frame containing the news
towards a certain stock is needed.

• For that we will use a site called finviz. The site contains data about every ticker, also
every ticker value can list news headlines to that stock price as well.

• We used the html response for that usage, the html response is used by passing it through
Beautifulsoup functionality.

• The Beautifulsoup output is then used to collect the news data one by one to create a
dataframe of news headlines against the ticker values.

2.3 Structure of the Recurrent Neural Networks

• A Recurrent Neural Network work on the principle that previous values have an impact
on the current values.

• The hidden layers will recursively take the inputs of previous periods.

• The hidden layer receives the inputs from the input layer, and there is a line to connect
a hidden layer back itself to represent the recursive nature.

• The model gets the data of the previous three days and it predicts the data of the current
day.

• The number of neurons are set at an appropriate value so that the model does nor overfit
neither underfit the learning.

• We need to select an activation function for the layer in the model. By computing the
weighted total and then adding bias to it, the activation function determines whether or
not a neuron should be stimulated. The activation function’s objective is to add non-
linearity to a neuron’s output.We have used Relu(Rectified Linear Unit) is most com-
monly used activation function in the deep learning models. It returns 0 if it receives
any negative input, but for any positive value it should return the same value back.

• We also need an optimizer for the model as well.An optimizer in deep learning is an
algorithm that modifies a model’s parameters in order to reduce the loss function during
training.The optimizer seeks to identify the set of model parameters that minimises the
loss function, which quantifies how well the model can predict using the training data.
We have used Adam Optimizer with a learning rate of 0.01. An algorithm for gradient
descent optimization is called adaptive moment estimation.When dealing with complex
problems involving a lot of data or factors, the strategy is incredibly effective.It is effec-
tive and uses little memory.
chapter: 01 5

2.4 Structure of the Sentiment Analysis

• Sentiment analysis of stocks is the process of analysing the sentiment or emotion con-
veyed in text data relating to a certain stock or set of equities using natural language
processing and machine learning techniques. In order to forecast future changes in stock
price, this study can be used to spot patterns or trends in the sentiment of stock market
participants, including investors, analysts, and news sources.

• In general, sentiment research can be a beneficial technique for analysts and investors
who want to understand market mood and make wise investment choices. Sentiment
analysis should not, however, be relied upon in isolation when making investing deci-
sions; it is simply one of several factors that should be taken into account.

• We have a created a dataset according to the ticker values and dates.The dataset have
news haedlines related to the ticker on some specific dates.

• The Vader Sentiment Analyser is used to calculate the overall compound sentiment of
the news headlines. The dataset is transformed as being a dates to compound values.

• This dataset is then used to calculate mean dataset which has the date to mean values as
it’s values. These mean values tells us about how much a company was talked about in
the news on a certain date.
Chapter 3

Simulation and Results

F IGURE 3.1: INFY stock trend predicition using LSTM model

F IGURE 3.2: INFY stock trend predicition using GRU model

6
chapter: 01 7

F IGURE 3.3: Extrapolation on MSFT stock using GRU model

F IGURE 3.4: Extrapolation on MSFT stock using LSTM model

F IGURE 3.5: Sentiment Analysis on MSFT stocks


chapter: 01 8

F IGURE 3.6: StreamLit App for Stock Prediction


Chapter 4

Conclusions

• During Windowed dataset creation if the starting date for the given dataset is chosen to
be quite back in time, this shows that LSTM models are quite bad in extrapolation,but
adding an activation function of relu on the LSTM hidden layer solves this problem.

• Using 80% of dataset as our training dataset was effective.

• The mean absolute error gets down when relu activation function was used rather than
tanh, this can be fairly understandable as relu activation function solves the Vanishing
Gradient problem. Also the relu activation function is quite advance.

• Optimizers are used to twing the weights used in the layers, this is done using given
gradients and additional information, Optimizers are built to decrease the loss function.
We used two different Optimizers to work onto and found that Adam was better in
performance than SGD.

• SGD does not tends to work with LSTM RNN while with GRU RNN it underfits the
model and gives a straight line instead of the trend.

• Vader Sentiment Analyzer gives the positive, negative and neutral score for a certain
statements. But what was needed is the compound score for the statement.

• The mean of all the compound values on a particular day is calculated, this score will
tell us the overall mood about the stock on a particular day.

• If the mean value on a particular day is positive it is better to buy the stock, while if the
value is negative it is better to sell the stock.

• A plot with showing the compound scores of the news sentiments is used against of the
dates.

9
Bibliography

1. A Technical Guide on RNN/LSTM/GRU for Stock Price Prediction-


”https://medium.com/swlh/a-technical-guide-on-rnn-lstm-gru-for-stock-price-
prediction-bce2f7f30346” [Author - ”Chris Kuo” , Date of Publish - Dec 7 2020]

2. Gentle Introduction to the Adam Optimization Algorithm for Deep Learning


-”https://machinelearningmastery.com/adam-optimization-algorithm-for-deep-
learning/” [Author - ”Jason Brownie” , Date of Publish - July 3 2017]

3. Sentiment Analysis for Indian Stock Market Prediction Using Sensex and Nifty
-”https://www.sciencedirect.com/science/article/pii/S187705091503207X” [Author -
”Aditya Bhardwaj , Yogendra Narayan , Maitreyee Dutta” , Date of Publish - Nov 21
2015]

4. Introduction to Sequence Learning Models: RNN, LSTM, GRU-


”https://www.researchgate.net/publication/350950396Introduction to Sequence
Learning Models RNN LSTM GRU” [Author - ”Sakib Ashraf Zargar” , Date of Publish
- April 2021]

5. Stock Price Prediction Using News Sentiment Analysis-


”https://ieeexplore.ieee.org/document/8848203” [Author - ”Saloni Mohan , Sahitya
Mullapudi, Sudheer Sammeta” , Date of Publish - Sept 26 2019]

10

You might also like