Wow. How the world has evolved. In the year 1885, Karl Benz had invented the very first gas-powered car. This is where the evolution of cars started. Now we have gotten cars that run through electricity rather than gas, not to mention they are also self-driving cars. This car is the one and only Tesla. We have now accomplished this through the power of Artificial Intelligence.
Tesla has grown greatly as a company and its stocks have been growing. Well, through the power of AI we are able to predict their future stocks to know how well the company is going to do. In this article, I will be going through the code in which I created a Machine Learning Model that predicts the price of Tesla stock in the month of January 2020.
What Are We Trying To Do
Let’s start by knowing what we want to do in our model. 2020 has been a great year for Elon Musk’s Tesla company. What we want to do is to be able to predict the stocks for Tesla beforehand. In the model, I will predict the stocks using a neural network for the year 2020 and will compare them to the actual stocks.
Now I will explain the model. The model that I used takes in historical prices and volume (number of shares traded that day) as input. The purpose of this is to see if there are any patterns that occur in the movement of the stock. This type of analysis is called technical analysis in finance formally defined as “the discipline which evaluates investments in price trends and patterns seen on charts.” Technical analysts believe that there are insights in previous movements of stocks to help predict their future movements. To predict January’s prices I used Tesla's historical price data from its IPO (back in 2010) to December 2019 as training data. The model would use these data points to find the patterns and then apply them to the test data which is the January prices. Here are the datasets I used.
Now let’s get on to the code.
The first thing I did here is import the dataset that we talked about earlier and that is Tesla’s historical price data. I have imported this into the model as a CSV file for it to be compatible.
The next thing we have to do is normalize the data. What this means is that you are adjusting the values to fit on a certain scale, in this case, the scale would be 0–1. As you can see here, I used the MinMaxScaler to complete this action.
LSTM and RNN
For this next part of the code, you need to know a little bit about LSTM and RNN, also known as long short-term memory networks and recurrent neural networks. LSTM is the type of neural network we have used in this model. An LSTM model is a type of RNN. The only thing you need to know is that the features of LSTM and RNN help predict more technical things and find patterns easily. I won’t go into the technical depth of RNNs or LSTMs you just have to understand the key concept that LSTMs are good at storing long-term dependencies which is good for stock market predictions.
For an LTSM there is something called timestamps. A timestamp is basically the date at which the stock is at that price. In our training data set, we have over 2000 timestamps (one for each day) each with an open, high price, low price, close, and volume traded values. We are only concerned with the open price and the volume traded numbers.
As you can see here our model will essentially look at the past 60 timestamps (past 60 days) open price and predict a price for the next day.
Creating the ANN
Now we have gotten to the stage of building the RNN. Building the RNN is made simple because we are using a library called Keras which is a very high-level ML framework. We are using a Keras class called Sequential, as well as layers from Keras: the LSTM, Dropout, and Dense layer.
Adding the Layers
A neural network is built up of 3 main layers. An input layer, a hidden layer, and an output layer. However, there can be multiple hidden layers. Now we have to add the layers to our model.
I started by putting in 4 layers. The image below is an example of the first layer out of the 4.
Then I added an output layer followed by compiling all the layers together to complete the RNN.
Training and Testing
I used this line of code to fit the model into the training set
At first, I ran the model with 10 epochs which means that I trained it 10 times. The more you train it, the better its accuracy. When I did with 10 epochs I got a final loss of 0.04 on the training data. And this is what I got compared to the actual stocks in January.
Not so great huh?
So then I decided to run it with 100 epochs. And this was the result.
That is much better. If we did even more epochs its accuracy could reach to almost perfect. So as you can see, this model works.
I have uploaded the full code to my GitHub and have also created a youtube video going through the code in further detail.
Link to full code : https://github.com/myshagilani/PredictingTeslaStocks/blob/main/script.py
Link to Youtube video : https://www.youtube.com/watch?v=QXT8yfeJHB8