Download This R Autoregression MT4 Indicator FREE

In the last post we showed you how you can connect the powerful R software with MT4. If you haven’t read the post on how to connect R with MT4, you should read it now before you continue reading this post. In this post how to connect R with MT4 I have explained how difficult it was to connect R with MT4 and how it was done. As said in that post R is a powerful data analysis and machine learning software that is available FREE. We will be using R more and more in the coming posts to develop powerful machine learning MT4 indicators. You should first download this software and install it on your computer.

What Is Autoregression?

Time series have this general property that past values are auto correlated This can be judged by drawing the Correlogram of a time series. A correlogram is a plot of the auto correlation of the different past values with one another.  What this means in simple terms is that past values have important information about the future values. One way of extracting that information is by regressing the present value of a time series with it’s past values. This is known as Autoregression. Autoregression also known as Autoregressive Modelling (AR).

AR modelling is a forecasting tool for predicting the short term future values of a time series. We can fit a moving average model to a time series. ARIMA is a model that fits an autogressive and a moving average model. ARIMA means Autoregressive Integrated Moving Average. If you are feeling confused you can watch the video below that explains these terms! But you don’t need to know how to do time series analysis in too much depth. R Indicator is going to do everything for you. You should however have some rudimentary understanding so that you know what you can achieve and what you cannot achieve when you use the time series analysis.

As said above autoregression is based on the idea that the past values of a time series can be used to predict the future values. This is precisely the premise on which Technical Analysis is based. When we look at the chart we are looking for patterns that can predict the future price action. If the above autoregression indicator confirms your technical analysis, it will help you reduce your losing trades. You can use this R indicator as a filter to reduce your losing trades.

MQL4 language has severe limitations. It does not have libraries that can implement different statistical algorithms. However by connecting R with MT4, we can overcome that limitation. When we connect R we can use its powerful data analysis and machine learning algorithms in analyzing the currency pair time series and then use the predictions in real time in our trading. This is precisely what we want.

How To Install Autoregression R MT4 Indicator?

In this post we give you the code for a R indicator that fits an autogressive model to the closing price of the currency pair time series. First you need to download R software from the R-Project site. Install it on your computer. Then you need to download these files:

MT4R.DLL

MT4R.MQH

COMMON_FUNCTIONS.MQH

The following infographic explains step by step how to install this Autoregression R MT4 indicator.

Autoregression R MT4 Indicator

Now as shown in the above infographic, first you will copy the following code in a notepad text file and save it as R-Indicator.mq4 file. I want to make it clear that without installing R software, this indicator is not going to work. R is a powerful data analysis and machine learning software that you can download free. In the above infographic you can copy the link from where you can download this R software. After downloading it, you should install it on your computer.

In the code you can see a bold line. In the comment above I have explained that you need to change the path of the R software as has been made bold. The path can be C drive just like that in the code or it can be E, D or whatever drive you install the R software on your computer. You will also change the R version if it is different than that shown in the code. You can do that in the text file and then save it as an mq4 file. You can open it in MetaEditor and then make the change and compile the code. It is upto you. It is not difficult. Once you have the R-Indicator.mq4 in the Indicator folder you are all set now. Just open MT4 and double click on R-Indicator name and you will see the indicator on chart.

 

//+——————————————————————+
//|                                                  R-Indicator.mq4 |
//|                                     Copyright 2010, Bernd Kreuss |
//|                                       https://www.doubledoji.com |
//+——————————————————————+
#property copyright “Copyright 2010, Bernd Kreuss”
#property link      “https://www.doubledoji.com”
#property version   “1.00”
#property strict
#property indicator_chart_window

#property indicator_buffers 1
#property indicator_color1 Red
#property indicator_width1 2

#include <mt4R.mqh>

extern int order = 200;
extern int back =  500;
extern int ahead = 20;

int R;
double buf_prediction[];

int init(){
SetIndexBuffer(0, buf_prediction);
SetIndexStyle(0, DRAW_LINE);
SetIndexShift(0, ahead);

//–You should change the following bold path in your MQL4 file according to your hard drive and R version

R = RInit(“C:/Program Files/R/R-3.2.5/bin/x64/Rterm.exe –no-save”, 2);
Comment(“history: ” + (string)back + ” bars, method: OLS, order: ” + (string)order);
return(0);
}

int deinit(){
RDeinit(R);

return(0);
}

int start(){
double hist[];
double pred[];
int i;

if (RIsBusy(R))
{
// last RExecuteAsync() is still not finished, do nothing.

return(0);
}

if(RGetInteger(R, “as.integer(exists(‘model’))”) == 1)
{
// there exists a model (the variable is set).
// This means a previously started RExecuteAsync() has finished.
// we can now predict from this model and plot it.
RAssignInteger(R, “ahead”, ahead);
RExecute(R, “pred <- predict(model, n.ahead=ahead)$pred”);
ArrayResize(pred, ahead);
RGetVector(R, “rev(pred)”, pred, ahead);
for (i=0; i<ahead; i++){
buf_prediction[i] = pred[i];
}
}

// make a (new) prediction
// move some history over to R
ArrayResize(hist, back);
for (i=0; i<back; i++){
hist[i] = Close[i];
}
RAssignVector(R, “hist”, hist, ArraySize(hist));
RExecute(R, “hist <- rev(hist)”);

// crunch the numbers in the background and return from the start() function
// RIsBusy() in the next ticks will tell us when it is finished.
RAssignInteger(R, “ord”, order);
RExecuteAsync(R, “model <- ar(hist, aic=FALSE, order=ord, method=’ols’)”);
return(0);
}

Now you can use this Autoregression R MT4 indicator. You can see in the screenshot below that this indicator predicts price action in the future. This is a great help when you trade. In the screenshot you can see price action has been predicted to go up and then drop. When you see that price is going up, a long trade will be appropriate.

Autoregression R MT4 Indicator