LSTM, 1 property, 1 stock on normalized data

This code is the result of some work I have done on LSTM.

Purpose: predict the next day price with the use of a LSTM neural network and just one property.

How:

  • I have normalized the data,
  • I have used just 1 parameter as input (close price),
  • I have built the code using basic functions to module the basis.

Code sections

I have divided the code in Jupyter sections, so it’s easy to run.

  • 1.- Load libraries common data and functions
  • 2.- Preparing the data for LSTM
  • 3.- Split Train/Test dataset & dimension the data vectors
  • 4.- Creating the LSTM model
  • 5.- Build prediction on test dataset
  • 6.- Predict the stock price for tomorrow
  • 10.- Analysis: Generate and save data to analyze results
  • 20.- Generate Next day result

The training of the code takes between 45-60 minutes on Google Collab, so I save the model and I use it to predict the following days for testing purposes (it takes 5 minutes).

Once you have generated the models, to predict the following day you just have to run section #1 and section #6.

The configuration I have used is this one:

Results

Despite the accuracy is over 98%, the results are very poor. The True Positives and True Negatives ratio is low (61.97%) and that makes the manual back-test I did very poor.

Last 5 days forecast for Microsoft (MSFT)

To calculate the results, I do it in 2 steps. First, I extract the data (predictions and real data) into a CSV executing the section “10.- Analysis: Generate and save data to analyze results”. Then I run another Jupyter notebook that enables me to calculate the results.

In this case on Microsoft the table is:

Next step?

My next step is to turn this code from a single parameter to a multiple parameters, and see how it works.

The code

You can find the code here:

Leave a Comment