top of page

Recurrent Neural Network to Predict Stock Prices

This is a brief presentation of my analysis, click here for my full iPython notebook.

Overview

We can use a neural network to use 5 years of stock data from Yahoo Finance to predict our current year's stock prices. This study is based on a paper from Stanford University.

Introduction

Recurrent Neural Networks are excellent to use along with time series analysis to predict stock prices. What is time series analysis? Time series analysis comprises methods for analyzing time series data in order to extract meaningful statistics and other characteristics of the data. Time series forecasting is the use of a model to predict future values based on previously observed values.

An example is this. Would today affect the stock prices of tomorrow? Would last week affect the stock prices of tomorrow? How about last month? Last year? Seasons or fiscal quarters? Decades? Although stock advisers may have different opinions, recurrent neural networks use every single case and find the best method to predict.

Problem: Client wants to know when to invest to get the largest return in 2017.

Data: 5 years of Tesla stock prices. (2012-2017)

Solution: Use recurrent neural networks to predict Tesla stock prices in 2017 using data from 2012-2016.

Methods

To teach our machine how to use neural networks to make predictions, we are going to use deep learning from TensorFlow. Deep learning is a field of machine learning that uses algorithms inspired by how neurons function in the human brain. TensorFlow is a machine learning framework that Google created to design, build, and train deep learning models. The name, "TensorFlow", is derived from how neural networks perform on multidimensional data arrays or tensors. It's a flow or tensors, just like how the human brain has a flow of neurons!

We will also use Keras, which runs on top of TensorFlow. Keras was developed with a focus on enabling fast experimentation.

Data

We will be using stock prices from Tesla Motors (TSLA). Up-to-date data can be taken from Yahoo Finance. We type "Tesla" in the search bar then go to the "Historical Data" tab. Then we download the relevant time frame for this study. In our case, we download from years 2012 to 2016 to train our data. Finally, we download all data in 2017 to test our model performance. (2 sets, 2012-2016 for the training set, 2017 for the test set.)

Preparing our Data

We first load our CSV file from Yahoo Finance into a data frame.

Then, we get our relevant feature. We are only interested in opening prices since we are most likely going to buy as soon as the stock is open for trading. We do this by getting rid of all other columns.

Finally, we make a 2 column data frame of: Today, Tomorrow. We are most interested in how stock prices rise and fall and what better way to do it on a daily basis?

Building the Recurrent Neural Network

This section is a bit technical so feel free to skip this if you are just here to see the performance. We start by importing our Keras package. We use a regression instead of a classification method since we are predicting trends, not classes. Then we use a sigmoid function with 4 layers to predict probability. Finally, we use a final dense layer to predict stock price. Our batch size will be 32 and we will go over our set with 200 epochs.

Making Predictions and Visualizing the Results

All right! When our neural network is finished with training, we test its accuracy with real 2017 data. Keep in mind that our neural network only has stock prices until 2017. It's only going to predict the 2017 data.

The predicted prices in blue are extremely close to the actual prices reflected in this year (Up until September, the month of writing!) Even better, when our model does make its slight mistakes, it under predicts the stock prices which is even better for investors to count on.

Conclusion

The thing amazing about a recurrent neural network is the fact that it takes advantage of time series analysis to predict stock prices. We are so focused on days, weeks, months, or even seasons to make our big stock purchases. Each investor has their reasons on when to buy or sell. Recurrent neural networks use all of those biases to make the best possible prediction with great accuracy.

Reusability

Want to reuse my code to predict your own stock prices?

  1. Fork my iPython notebook from GitHub.

  2. Get your CSV data from Yahoo Finance. (Replace "TSLA" with your own stock ticker)

  3. Install Anaconda for Python, TensorFlow for Deep Learning, and Keras to support Tensorflow.

  4. Edit my notebook by replacing my Tesla data with your own stock and run!

Featured Posts
Recent Posts
Archive
Search By Tags
No tags yet.
bottom of page