Predicting/Guessing Stock Price Trend with Recurrent Neural Network using PyTorch

oF CouRsE NoT

The Purpose of This Article

Surprisingly though, I learned a lot during this doomed-to-fail process. Not that I am currently making easy money in the stock market. Rather, now I understand why some common tutorials you found online may appear to work but will NOT work and what it might actually take to do good stock prediction. The sole reason that I am writing this article is to share my most up-to-date understanding with those of you who are also interested in stock prediction with machine learning. I will attach my Python code to the end. Of course, nothing I said in this article should be taken as financial advice.

Recurrent Neural Network


First Possible Mistake: Close vs Adj Close

It is tempting to just use closing price in the “Close” column to represent the stock price on a particular day as was done in some tutorials that I found online. However, this does not account for the “technical” price actions caused by dividends, stock-splitting, and etc. For example, AAPL split on a 4-for-1 basis on August 28, 2020. For us human beings, it is easy to see the implication on the stock price. But for the machine learning program, it is much more challenging to understand the impact of this rare event. We might as well feed the machine with better data from the start. In fact, the adjusted closing price (“Adj Close”) considers all these technical price actions, which we will use in our code. Let’s plot the “Adj Close” column then.

Second Possible Mistake: Normalization over All Data

Another mistake that I saw people often make is not to normalize or wrongly normalize the data. The reason we need to normalize the data is as follows: in reality we care more about the relative prediction error, but when the machine learning algorithm is optimizing the hyperparameters, it is often minimizing the total error.

Also, I found that people sometimes simply apply a min-max normalization to the total data. This is NOT appropriate because it creates some additional artificial correlation between prices from different dates (your potential test set and validation set). If you have tried one of those tutorials before, you would see the downfall of this as soon as you change the date range of your training data.

Our general idea here is to use the past 19 days’ data to predict the 20th day’s price. For every 20-day window, the first 19 datapoints will be the “x” and the 20th datapoint will be the “y”. RNN will help us find the correlation between “x” and “y”. We introduce a local normalization scheme for each 20-day group. We can use the min and max of the first half points in the group to apply min-max normalization to the whole 20 points (the resulting values are not necessarily between 0 and 1). This is to reduce correlation introduced by the normalization process. The realization of this can be found in my code. Also we will do the usual training-test splitting. I put the part of my code where I do my normalization and preprocessing below


During the training, I did 100 epochs and the important thing to plot here is the comparison between the model results and the real data in the training set and the training loss as a function of epochs.

Training results

It appears that the model performed pretty well within the training set and the number of epochs also seemed large enough. Next we can try our luck in the test set. Note that our model only provides the predicted price in the normalized sense. We need to write a small function to reverse the process to generate the real price.

Now is supposedly the moment of truth. How does our stock prediction model work on the data that it has not seen.

So far, it looks great!

Ready to Make Easy Money? Not really…

Third Possible Mistake: One-Day Prediction vs Several-Day Prediction

Our test set here contains many days and if you naively look at the final plot, you may think we can have pretty good predictions very deep into the future. This is NOT true. Whether for the training or the test, we always use the first 19 days of REAL prices as the model input, so we are only predicting one day into the future. I also tried to both train and test the model to predict 10 days into the future, the relative error was usually >20%, so not useful at all.

Fourth Possible Mistake: 5% Error is Good Enough?

Our one-day prediction was actually not that bad, often around 5% off the real price. Unfortunately, as traders know, 5% is considered to be a pretty big single-day price move especially for mega-cap companies. The info provided by the current RNN model, although quite accurate already, is still too noisy for you to comfortably make profits in the stock market. I would like to point out though, if one can predict the price with 5% error one or two weeks into the future, by selling credit spreads, there will be easy money to be made. But unfortunately, with our model, once you try to look further into the future, the error grows recklessly.

Concluding Marks

So here is the trade-off. It is more likely that you can use machine learning to capture the momentum of price actions on a short time scale. But you need to have high accuracy and make trades very frequently to make meaningful money. Many of you probably knew what HFT is already. Alternatively, if you build a model that accounts for news mentions and provide an okay prediction, say a few days into the future, you can also be rich pretty soon. Both are hard problems to solve. As you might have expected from the very beginning, making money is hard.

Before I forgot, here is the link to the code I used and I learned a lot from this article written by Rodolfo Saldanha.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store