Autoregressive Model For Intraday Trading On M1, M5, M15 & M30

In this post we are going to discuss an autoregressive model for intraday trading. Developing models for high frequency intraday trading is not easy. You should always keep this in mind that no model can be 100% accurate. Price prediction is a difficult art. Even central banks have difficulty in predicting how price is going to react to their policy stimulus. Take the example of Reserve Bank of New Zealand. RBNZ thought that by lowering interest rate by 20 basis point it will weaken New Zealand Dollar. But instead the exact opposite happened. Instead of sending NZDUSD down the exact reverse happened. Lowering interest rates by 20 basis points instead drove NZDUSD higher as market thought RBNZ didn’t lower the interest rates much. What this means NZD appreciated. Now before making the announcement for lowering interest rate by 20 basis points, RBNZ must have run simulation models to see how the market would react. Only after getting satisfactory results from their simulation models, they must have made the announcement. But the market react totally differently.

Financial media is constantly reporting now a days that central banks are losing their ability to control their currencies. Take the case of FED. FED cannot control USD as it could a few decades back. Why? Because there are more USD outside USA than inside meaning foreigners are having more dollars as compared to Americans. This makes things complicated for FED and it has to run more and more sophisticated models to predict how things are going to react. Now as traders we are also interested in predicting price. This is how we make a living. If we can predict price on M1, M5, M15 and M30 timeframes, we can use to that prediction to make more and more pips. Did you watch Trading and Portfolio Management Institute South African documentary?

In this post we are going to develop an autoregressive model for predicting closing price on M1, M5, M15, M30. You should have R installed on your computer. This is what I want to do. I will download M1 csv data. Read it in R and run an autoregession algorithm to make prediction for the next 30 minutes. If the predictions are good we can develop an indicator and use the predictions to trade 15 and 30 minute binary options. However if predictions are not good, we will look into why the model is not making good predictions. Before you continue you should read this post on how to convert intraday price data into xts time series.

What Is An Autoregressive Process?

Autoregressive process means we try to regress the present time series value with older values using either the ordinary least square algorithm or the maximum likelihood estimate algorithm. We use an autoregressive process when we believe the past values can be used to predict the future values. Once we have the relationship of the present value with the past values we use that relationship to predict the future values. Now this is something important for you to understand. Autoregressive coefficients that we calculate keep on changing with the new time series values meaning the coefficients are dynamic in nature and it would be a better idea to built a Kalman Filter so that these values are constantly updated as new price values arrive. We will do that in the next post. For now if you don’t know what an autoregressive process is, you can watch the video below that explains what is an autoregressive process.

Below is the R code for our autoregressive model for intraday trading.

> ## predict the next 30 candles
> 
> 
> # Import the csv file
> quotes <- read.csv("E:/MarketData/GBPUSD1.csv", header=FALSE)
> 
> z=30
> 
> # load quantmod package
> library(quantmod)
Loading required package: xts
Loading required package: zoo

Attaching package: ‘zoo’

The following objects are masked from ‘package:base’:

    as.Date, as.Date.numeric

Loading required package: TTR
Version 0.4-0 included new data defaults. See ?getSymbols.
> 
> #convert the data frame into an xts object
> quotes <- as.ts(quotes)
> 
> 
> #convert time series into a zoo object
> quotes1 <- as.zoo(quotes)
> 
> quotes1 <- log(quotes1[, -(1:2)])
> 
> ## Predict 6 period return
> #calculate log returns
> 
> lr <- diff(quotes1)
> 
> 
> 
> 
> y <-nrow(quotes)
> 
> 
> model <- ar(lr[(y-1000):(y-1),4], aic=TRUE, method='ols')
> 
> pred <- predict(model, n.ahead=z)
> 
> 
> 
> 
> 
> # make the predictions
> 
> 
> 
> pClose=matrix(nrow=z,ncol=1)
> 
> psum=matrix(nrow=z,ncol=1)
> 
> psum[1] <- pred$pred[1]
> 
> pClose[1] <- exp(psum[1])*quotes[y-1,5]
> 
> 
> for ( i in 2:z)
+   
+ { 
+   
+   psum[i] <- psum[i-1]+pred$pred[i]
+   
+   pClose[i]  <- exp(psum[i])*pClose[i-1]
+ }
> 
> pClose
          [,1]
 [1,] 1.301508
 [2,] 1.301518
 [3,] 1.301538
 [4,] 1.301554
 [5,] 1.301598
 [6,] 1.301585
 [7,] 1.301567
 [8,] 1.301527
 [9,] 1.301487
[10,] 1.301457
[11,] 1.301414
[12,] 1.301366
[13,] 1.301308
[14,] 1.301243
[15,] 1.301168
[16,] 1.301084
[17,] 1.300994
[18,] 1.300895
[19,] 1.300789
[20,] 1.300674
[21,] 1.300552
[22,] 1.300421
[23,] 1.300282
[24,] 1.300135
[25,] 1.299980
[26,] 1.299817
[27,] 1.299646
[28,] 1.299467
[29,] 1.299280
[30,] 1.299085

In the above R code, we read a GBPUSD M1 csv file and ran an autoregessive model to make 30 predictions in the future. This is what we found. The predictions for the first 15 minutes were fairly good after that the accuracy started to suffer. Why? Simple. Making predictions too far in the future will never be accurate. The first few predictions will be accurate. After that standard deviation start increasing meaning the standard error becomes big. So we should reduce the prediction horizon to 15 instead of 30 for M1 data. We should use M5 data to make prediction for next 30 minutes.

> ## predict the next 6 candles
> 
> 
> # Import the csv file
> quotes <- read.csv("E:/MarketData/GBPUSD5.csv", header=FALSE)
> 
> z=6
> 
> # 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)
> 
> quotes1 <- log(quotes1[, -(1:2)])
> 
> ## Predict 6 period return
> #calculate log returns
> 
> lr <- diff(quotes1)
> 
> 
> 
> 
> y <-nrow(quotes)
> 
> 
> model <- ar(lr[(y-1000):(y-1),4], aic=TRUE, method='ols')
> 
> pred <- predict(model, n.ahead=z)
> 
> 
> 
> 
> 
> # make the predictions
> 
> 
> 
> pClose=matrix(nrow=z,ncol=1)
> 
> psum=matrix(nrow=z,ncol=1)
> 
> psum[1] <- pred$pred[1]
> 
> pClose[1] <- exp(psum[1])*quotes[y-1,5]
> 
> 
> for ( i in 2:z)
+   
+ { 
+   
+   psum[i] <- psum[i-1]+pred$pred[i]
+   
+   pClose[i]  <- exp(psum[i])*pClose[i-1]
+ }
> 
> pClose
         [,1]
[1,] 1.302708
[2,] 1.302906
[3,] 1.303334
[4,] 1.303762
[5,] 1.304122
[6,] 1.304356

In the above R code,we used M5 csv data to predict the next 6 candles. The predictions made by M1 and M5 are quite different. We need to work on this model more so that the predictions on 2 timeframes should converge meaning the prediction made by 1 minute data for 30 minutes should be fairly close to the prediction made by M5 for 30 minutes. How can we do that? Right now this is what we are doing. We are giving each return equal weight in our calculations. We know this fact that a price shock tends to lose momentum and then reverse. How do we introduce this thing in our model? One way to do that is to use the idea of exponential smoothing. We know that a price shock tends to fade away with time. Using exponential smoothing we can introduce the fading effect in our model and see if we get good predictions. More on that in the next post. In the meantime you can watch this Million Dollar Trader Mentoring Program Thailand Documentary.

0 Comments