Stock Price prediction using ML

Nandish Bhayani
5 min readOct 29, 2021

--

Overview

Predicting how the stock market will perform is one of the most difficult things to do. There are so many factors involved in the prediction — physical factors vs. physhological, rational and irrational behaviour, etc. All these aspects combine to make share prices volatile and very difficult to predict with a high degree of accuracy.

using machine learning we can predict stock more accurately and precisely.In this project we have worked with historical data about the stock prices of a publicly listed company. We have implement a mix of machine learning algorithms to predict the future stock price of company, starting with simple algorithms like averaging and linear regression, and then move on to advanced techniques like LSTM.

Analysis

There are mainly two types of analysis used in stock price prediction.

1.FUNDAMENTAL ANALYSIS.

  • Fundamental Analysis involves analyzing the company’s future profitability on the basis of its current business environment and financial performance.

The fundamental analysis is dependent mainly to the climate in the business. The factors mostly playing role in the business climate are monetary and fiscal policy, employment and demand and business and demand cycles. Then the next step is the fundamental market direction which depend on the trend of the company.

2. TECHNICAL ANALYSIS:

  • Technical Analysis, on the other hand, includes reading the charts and using statistical figures to identify the trends in the stock market.

In this project our focus will be on the technical analysis part.

Implementation:

Our implementation will be on following contents:

1. Data sets

2. Data normalization

3. Training neural network

4. Testing LSTM

1. Data sets:

Data-sets is collected from https://www.quandl.com/data/NSE and this data is used for the prediction of future stock prices. First of all for prediction imports different data sets in ‘.csv’ format which is to be test.

2. Data normalization:

• Feature scaling: whenever using a neural network, data-sets must have to be normalize or scale between 0 and one such can be achieve using sklearnlibrary.

• Creating data structure and reshape: In a time series problems, values are predicted at interval time T .This model is going to predict the opening stock price of the data based on prices for the past 60 days(60 time step(T) and 1 output).Hence data-set splits into train and label sets. Train set have past 60 values and La- bel have corresponding that 61th value which is to be predicted and becomes input for next time-step. Similarly time steps are created for whole data-sets. Then data-sets are to be reshape into array as they are in form of lists.

3. Training neural network :

• Building the model: Next step is to build LSTM model by adding layers and dropout layers.where in this model five layers with input layer and output layer(dense layer with one unit)each having no of units and dropout are implemented.Dropout layer is added to avoid over-fitting. Above can be achieved using keras library.

• Compile: Further implemented LSTM model is to be compile and train ,but before training on training data-sets compilation is triggered with parameterfor loss function and optimizer.

• Train: Now next step is to train LSTM model that was build so far by giving parameters train set,label set,number of epochs.Training of model is achieved using Fit() method.

EPOCHS: Means when an entire data-set is passed both forward and backward through the neural net- work only once is called one epoch.Each epoch car- ried out minimum loss in loss function.

4. Testing LSTM:

• Prediction: Now after completion of training, LSTM network is ready for test by making predictions on given test data-sets.As we did with our training data- set similarly test data-sets are also to be shaped to right format. Prediction is achieved using pre- dict(method) and corresponding graph can be plotted using matplot library.

• Error calculation: At last predicted values and real testing values are compared and then error is cal- culated.So RMSE(root mean square error) is used for calculate error and percentage error which is re- quired for the accuracy of our model.

RESULTS:

Our project describes the use of three companies historical data as dataset to train and test SVM model which are Facebook, Reliance, Torrent power. The results are in form of graph which are given figures. If the datasets are too big historical data of any companies then some time it may happen that model follows trend and gives prediction result but some important political decision or any other event happened and stock prices affected by these events. And in majority cases it happens that stock market changes because of this global or national problem. To overcome this problem in model we have decided that we are only going to use 30 days of data to train our model and then predict next days values. And in many cases this model is works very accurately. Here for this SVM model accuracy is in form of graphs. Which we can see in these companies data graphs. Three different kernel functions are used to predict the stock prices. In fig.10 which is graph for Facebook dataset, RBF kernel is best fit and the pre- diction value is also nearer to actual values. In fig.11 which is graph for Torrent-Power Data the polynomial kernel is best fit. In fig.12 which is graph for Reliance also polynomial kernel is best fit. The graphs shows that the actual data points are overlapped by predicted value points.

USING SVM:

NOW FOR LSTM PREDICTED VALUES WILL BE

DESIGNED AS

This is how we predict stock prices using a machine-learning model.

--

--