Next Day Stock Price Prediction using Neural Networks from Yahoo Finance API

Akhil Kasare
3 min readMar 13, 2021
Photo by Markus Winkler on Unsplash

Note : This blog is not a investing advice it is literally not an investing advice in any way you shouldn’t use this model to predict stock prices because it uses LSTM networks. It's going to use last n days and it's going to predict one day into the future it's not going to project into the far future 50/60 days it's just going to predict the next day based on the 60 days. So please don't make use this to make investment decisions its just more about learning Machine Learning, Neural Networks & Python.

To begin with the project we need to import all the necessary libraries

After importing all the necessary libraries then we need to load the data. But before that we need to specify the company name in which we are interested in to do that you need to use the ticker symbol of that company here I have used Bitcoin. In this project we are going to make predictions for Bitcoin

Now we need to import the data of the company from what time to what time you need the company data like you have to specify starting and ending data

After importing the data we need to load the data of the company & then we need to pass the website from where we want the data & the starting and the ending date. For reading the data we are going to use the DataReader library.

Now the next step is to prepare the data. After preparing the data we need scale down the data between 0 and 1. So in order to scale our data we will use MinMaxScaler. The reason of scaling down the data is because the lowest stock price for example can be 10$ and the highest can be 620$ so we need to scale it down. This is how we will scale down our data.

In the above code snippet after defining the MinMaxScaler we are not going to transform the entire DataFrame we are only interested in the ‘Closing’ price. We are not going to predict the opening price or high price we are going to predict the price after markets have been closed i.e we are going to predict the closing price.

Now after scaling the data we are going to define how many days we want to look into the past to predict the next day or how many days do I want to base my predictions. Here we are going to consider for 90 days.

After considering the prediction data for 90 days we going to prepare the training data. To prepare the training data we need to create two empty list i.e x_train and y_train and you need to append the data inside the lists.

We our training data now we need to convert our training data into a numpy array and we need to reshape it.

Everything is ready now actually we can proceed towards model building. In this project we are using LSTM you can experiment with other algorithms.

In the last layer we have used only one neuron the reason behind using only a single neuron is that we are only going to predict the price of the stock.

Hurray! we are done with model building part now we need to check the accuracy of how our data is fitting the model for that we need to create test data and we make sure that this data our model has never seen before.

Our test data is ready now we need to make need to make prediction on our test data.

Everything is set we have made predictions from out test dataset now tha main challenging task is to make prediction for the next day. So let’s do it

Hurray ! you have made the stock price prediction for Bitcoin for the next day.

You can find the complete code on GitHub : https://github.com/akhiilkasare/Stock-Price-Prediction-using-LSTM

If you like it dont forget to give a clap and star the repo on github for more machine learning and deep learning projects.

Thank You !

--

--