How To Predict The Weekly Candle Using Neural Network?

In the last post, I showed you how you can make 200 pips per trade with a small stop loss of 10 pips. The main idea is to keep the risk small while at the same time try to catch a big move. The practical problem that we face is how do we know the move will be more than 200 pips or not. In order to know the size of the move we will try to predict the weekly candle and see if the range of the weekly candle is more than 200 pips or not. Just keep this in mind that prediction can never be 100% accurate. We just need a prediction that is 70% accurate. On the weekly timeframe most of the economic news release don’t have much impact. So we don’t have to bother about the economic news releases like NFP Report, GDP q/q, CPI m/m etc. Market already knows the impact of these reports on the weekly timeframe.

GBPUSD Weekly Candle Prediction

You should have R and RStudio installed on your computer. R and RStudio are powerful data analysis software that is available freely and have been developed by academics from different parts of the world. We will develop the neural network model on R. In the model, I have used the lagged prices as I believe that this it the best inputs for the neural network. We are using a recurrent neural network that is known as Elman Neural Network. This is a recurrent neural network that are difficult to model  but thanks to R we can do the modelling now with ease. R will take around 30 minutes to do the calculations, train the model and then make the predictions. Below is a screenshot of an Elman Network!

Elman Neural Network

Neural networks are very good at modelling non-linear time series. As you can see the predicted values and the actual values are pretty close. For example the predicted closing price is 1.45503 while the actual close was 1.45197 which is pretty close. In the same manner the predicted high price is 1.47683 while the actual high made during the week was 1.47247 which is also close. Similarly actual low price is 1.43861 while the predicted low is 1.44209. So the predicted and actual prices are close with difference of around 30-40 pips. These were the prediction for the weekly candle of GBPUSD. As said above we are not looking for very accurate predictions. What we are looking for the predicted values to be close with a difference not greater than 20-40 pips around 70-80% of the time. Now let’s do the calculations for GBPUSD next week high, low and close!

> # Import the csv file
> quotes <- read.csv("E:/MarketData/GBPUSD10080.csv", header=FALSE)
> 
> #number of rows in the dataframe
> x <-nrow(quotes)
> 
> # load quantmod package
> library(quantmod)
> 
> #convert the data frame into an xts object
> quotes <- as.ts(quotes)
> 
> 
> #convert time series into a zoo object
> quotes1 <- as.zoo(quotes)
> 
> 
> # lag the data
> x1 <- lag(quotes1, k=-1, na.pad=TRUE)
> x2 <- lag(quotes1, k=-2, na.pad=TRUE)
> x3 <- lag(quotes1, k=-3, na.pad=TRUE)
> x4 <- lag(quotes1, k=-4, na.pad=TRUE)
> x5 <- lag(quotes1, k=-5, na.pad=TRUE)
> 
> 
> #
> #Neural Network for the Close Price Time Series
> #
> 
> 
> # combine all the above matrices into one matrix having close prices
> CQuotes <- cbind (x1[ ,6], x2[ ,6], x3[ ,6], x4[ ,6], x5[ ,6],
+                   
+                   quotes1[,6])
> 
> #scale the data
> CQuotes <- scale(CQuotes, center=T, scale=T)
> 
> # create data for training
> inputs_trainC  <- CQuotes[100:x, 1:5]
> outputs_trainC <- CQuotes[100:x, 6]
> 
> # load RSNNS package
> library(RSNNS)
> 
> #build the Elman Neural Network
> modelC <- elman (inputs_trainC, outputs_trainC, 
+                  size =c(20,20) , 
+                  learnFuncParams =c(0.1) , 
+                  maxit =20000)
> 
> #create testing data
> inputs_testC <- CQuotes[x+1, 1:5]
> 
> #make predictions
> predC <- predict(modelC , inputs_testC)
> 
> CQuotes[x+1,6] <- predC[1]
> 
> 
> library(DMwR)
> predC1 <- unscale(CQuotes[x+1, 6], CQuotes)
> 
> #show predicted close price
> predC1
            
1042 1.44977
> 
> 
> 
> #
> #Neural Network for the High Price Time Series
> #
> 
> 
> # combine all the above matrices into one matrix having high prices
> HQuotes <- cbind (x1[ ,4], x2[ ,4], x3[ ,4], x4[ ,4], x5[ ,4],
+                   
+                   quotes1[,4])
> 
> #scale the data
> HQuotes <- scale(HQuotes, center=T, scale=T)
> 
> # create data for training
> inputs_trainH  <- HQuotes[100:x, 1:5]
> outputs_trainH <- HQuotes[100:x, 6]
> 
> 
> #build the Elman Neural Network
> modelH <- elman (inputs_trainH, outputs_trainH, 
+                  size =c(20,20) , 
+                  learnFuncParams =c(0.1) , 
+                  maxit =20000)
> 
> #create testing data
> inputs_testH <- HQuotes[x+1, 1:5]
> 
> #make predictions
> predH <- predict(modelH , inputs_testH)
> 
> HQuotes[x+1,6] <- predH[1]
> 
> #unscale the predicted high price
> 
> predH1 <- unscale(HQuotes[x+1, 6], HQuotes)
> 
> #show predicted close price
> predH1
             
1042 1.471346
> 
> 
> #
> #Neural Network for the Low Price Time Series
> #
> 
> 
> # combine all the above matrices into one matrix having low prices
> LQuotes <- cbind (x1[ ,5], x2[ ,5], x3[ ,5], x4[ ,5], x5[ ,5],
+                   
+                   quotes1[,5])
> 
> #scale the data
> LQuotes <- scale(LQuotes, center=T, scale=T)
> 
> # create data for training
> inputs_trainL  <- LQuotes[100:x, 1:5]
> outputs_trainL <- LQuotes[100:x, 6]
> 
> 
> #build the Elman Neural Network
> modelL <- elman (inputs_trainL, outputs_trainL, 
+                  size =c(20,20) , 
+                  learnFuncParams =c(0.1) , 
+                  maxit =20000)
> 
> #create testing data
> inputs_testL <- LQuotes[x+1, 1:5]
> 
> #make predictions
> predL <- predict(modelL , inputs_testL)
> 
> LQuotes[x+1,6] <- predL[1]
> 
> #unscale the predicted low price
> 
> predL1 <- unscale(LQuotes[x+1, 6],LQuotes)
> 
> #show predicted low price
> predL1
             
1042 1.442809
> 
> #show predicted high, Low and close
> 
> predH1
             
1042 1.471346
> predL1
             
1042 1.442809
> predC1
            
1042 1.44977

Above are the predicted values of High, Low and Close. I will trade with these values. Just keep this in mind that each currency pair will need optimization of inputs. These 5 lagged prices work good for GBPUSD. You should also read my previous post on how you can easily make 1000 pips per month with a low risk  of 20 pips. Now these predicted values of weekly high, low and close give you a rough idea of where the support and resistance will be. These are not exact levels as pointed out in the beginning of this post. Think of them as bands. These levels give you a rough idea of the weekly range. I will combine these levels with my candlestick trading system. If you have read my previous post I have explained my candlestick trading system. I use the candlesticks for generating buy/sell signals. These levels will help me in fixing the profit target with a high level of confidence.

Problems With Neural Network Training And Testing

There is a problem when you use neural networks. Neural networks are black boxes that are used to approximate the function that describes the relationship between the input and output values. Now most of the time this is what happens: Neural Networks use Back Propagation (BP) algorithm to minimize the cost function. The weights that minimize the cost function are then used to predict the future values. Most of the time neural network will start with a random input and use gradient descent to find the global minima that minimizes the cost function. Instead of finding the global minima it gets stuck at some local minima. So every time neural network model will give you a different result which is a signal that the model is finding it difficult to obtain convergence. This is precisely what happened in the above model. On next run, the model gave a different prediction.

Finding a good model is a time consuming process. What we are interested is in an out of sample prediction that is good. Good means above 80% accuracy if we want to use the predictions of a neural network in actual live trading.  We need the prediction to be close to the actual value which we don’t know. But we can check for this is by dividing the data into training and testing. Training data is used to train the neural network. Once the neural network gets trained, we use the testing data that has not been used in training to make predictions. We check the prediction with the actual and see how many of the output values have been correctly predicted and match with the actual. It will difficult for the model to make a prediction that is exactly equal to the actual. But the prediction can be pretty close. We can develop a criteria that can be used to see how close the predictions were with the actual.

In the above example we have used the neural network for regression. We can also develop a classification model that just tells us buy/sell. This is what we will do in the future post and see which model gives better performance in actual trading.

0 Comments