Site icon R-bloggers

Stock Market Predictions with Artificial Neural Networks

[This article was first published on Frank Portman, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Many of my research and personal interests lie in the realm of Machine Learning for several reasons. To me, it is the perfect blend of mathematics, statistics, and computer science. Also, it is extremely pervasive in today’s society. Everything from improved web-search to self-driving cars can be attributed to developments in Machine Learning.

For one of my computational finance classes, I attempted to implement a Machine Learning algorithm in order to predict stock prices, namely S&P 500 Adjusted Close prices. In order to do this, I turned to Artificial Neural Networks (ANN) for a plethora of reasons. ANNs have been known to work well for computationally intensive problems where a user may not have a clear hypothesis of how the inputs should interact. As such, ANNs excel at picking up hidden patterns within the data so well that they often overfit!

Keeping this in mind, I experimented with a technique known as a ‘sliding window’. Rather than train the model with years of S&P 500 data, I created an ANN that would train over the past 30 days (t-30, …, t) to predict the close price at t+1. A 30 day sliding window seemed to make a good fit. It wasn’t so wide as to not capture the current market atmosphere, but also it wasn’t narrow enough to be hypersensitive to recent erratic movements.

< !--more-->

Then, I had to decide on the input variables I was going to use. Many stock market models are pure time-series autoregressive functions, but the benefit of ANNs is that we can use them as a more traditional Machine Learning technique, we several inputs (and not only previous prices). This is helpful, because there exist an extremely large number of technical indicators which should uncover some significance in the market. I defined several of my own inputs that I thought would be significant predictors such as:

In addition, I did some research and introduced several other technical indicators. Since there were so many, I fit a Random Forest model in order to reduce the dimensionality of the problem. After taking only the most important variables, I was left with:

Finally, in order to obtain a stable model, I had to scale the inputs so that all variables lied in the range of [-1, 1]. This is done to avoid oversaturating the individual neurons in the ANN with outliers. There are several ways to do this, and I just created a simple function called ‘myScale’ in order to get the job done.

< notextile>
1
2
3
4
5
myScale <- function(x) {

  (x - min(x)) / (max(x) - min(x))

}

I won’t be sharing the final model, but I did create a simulation of my methodology for the 2013 trading year.

< notextile>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
trainPlot <- function(x) {

  g <- ggplot() + geom_path(aes(x = date, y = Adj.Close), colour = I("blue"),
                            data = sp.2013)

  g <- g + geom_vline(xintercept = as.numeric(sp.2013$date[x]), colour="green",
                      linetype = "longdash")

  g <- g + geom_vline(xintercept = as.numeric(sp.2013$date[x + 29]),
                      colour="green", linetype = "longdash")

  g <- g + annotate("text", x = sp.2013$date[x + 15], y = 1575,
                    label = "TRAIN", col = "blue")

  g <- g + geom_path(aes(x = date, y = Adj.Close), data = pred[31:(x + 30), ],
                     colour = I("red"))

  g <- g + theme_bw()

}

plotit <- function(x) print(trainPlot(x))

library(animation)
ani.options(ani.width = 900, ani.height = 650)

saveGIF(for (i in 1:40) plotit(i), convert = "gm convert",
        cmd.fun = shell, clean = T, outdir = getwd())

The animation demonstrates the mechanics of a Sliding Window model. We see that the ‘Training’ Window keeps moving up, regardless of whether the prediction was accurate or not. The RMSE of the model for the 2013 dates is 1.2115 which is extremely small considering the prices of the S&P 500 are over 1000. We know from the RMSE, and from the animation that the fit tends to be good. When the predicted values are slightly off, we can still see that the ANN usually correctly predicts the direction of the S&P 500 for that day. Perhaps in the future I will revisit this problem as a classification instead due to the results we’re seeing.

The beauty of my result is that although this is a naive implementation of a sophisticated model, we can see that the results are really good. I can spend many weeks tuning the model and playing around with the inputs to get a better fit.

To leave a comment for the author, please follow the link and comment on their blog: Frank Portman.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.