AutoRegressive program help.

  • Thread starter Thread starter Durham
  • Start date Start date
Joined
8/9/12
Messages
3
Points
11
I wrote this program in MQL4 and I cannot find out what is going wrong. My return coefficients constantly keep changing when they should only change at each timestep. I have gone over the code a million times but cannon figure out what I am doing wrong. Right now I am just trying to get this to work for a simple AR(1) (so some of the code is not used) model that will forecast 1 step in the future. Any help is greatly appreciated.

Code:
//+------------------------------------------------------------------+
//|                                                        AR(1).mq4 |
//|                              Thomas Brittain (thomas@pamexx.com) |
//|                                                  www.pamexx.com |
//+------------------------------------------------------------------+
#property copyright "Thomas Brittain (thomas@pamexx.com)"
#property link      "www.pamexx.com"
 
#property indicator_chart_window
#property indicator_buffers 3
#property indicator_color1 Blue
#property indicator_color2 Blue
#property indicator_color3 Blue
 
double BufferArray0[], BufferArray1[], BufferArray2[];  // Arrays for indicator buffers
 
double phi0, phi1;
double rbar;
double rsum=0;
double rbar_back1;
double rsum_back1=0;
double a=0;
double b=0;
double forecast0,forecast1, forecast2, forecast3, forecast4, forecast5;
int i;
 
 
 
//+------------------------------------------------------------------+
//| expert initialization function                                  |
//+------------------------------------------------------------------+
int init()
  {
  SetIndexBuffer(0, BufferArray0);
  SetIndexStyle(0,DRAW_ARROW,STYLE_SOLID,2);
 
  SetIndexBuffer(1, BufferArray1);
  SetIndexStyle(1,DRAW_ARROW,STYLE_SOLID,2);
 
  SetIndexBuffer(2, BufferArray2);
  SetIndexStyle(2,DRAW_LINE,STYLE_SOLID,2);
 
  return(0);
  }
 
 
 
 
//+------------------------------------------------------------------+
//| expert deinitialization function                                |
//+------------------------------------------------------------------+
int deinit()
  {
 
  return(0);
  }
 
 
 
 
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
 
  double ReturnArray[121];
  ReturnArray[0]=0;
 
  // for loop that is used to populate the array ReturnArray
for(i=1; i<121; i++)  // i=120 is the 121st element of the array
{
ReturnArray[i] = ReturnRate(Close[i], Open[i]);
}
 
// Calcuate the average return over the last 100 periods, i.e. candles 1 - 100
for(i=1; i<101; i++)
  {
  rsum = ReturnArray[i] + rsum;
  }
rbar = rsum / 100;
 
// Calculate the average return over the candle periods 2-101
for(i=2; i<102; i++)
  {
    rsum_back1 = ReturnArray[i] + rsum_back1;
  }
rbar_back1 = rsum_back1 / 100;
 
 
 
// Find the value of phi1
 
for(i=1; i<101; i++)
  {
    a = ((ReturnArray[i+1] - rbar_back1)*(ReturnArray[i]-rbar)) + a;
    b = ((ReturnArray[i+1]-rbar_back1)*(ReturnArray[i+1]-rbar_back1)) + b;
      if (i==100)
        {
          phi1 = a/b;
        }
  }
 
// Find the value of phi0
 
phi0 = rbar - (phi1*rbar_back1);
 
// detemine forecasts for candle 0 and the five candles that follow it forward in time
  double r0 = phi0 + phi1*ReturnArray[1];
  forecast0 = Close[1]*(1+r0);
 
 
 
  Comment("rbar is ", rbar, " rbar_back1 is: ", rbar_back1, " phi0 is: ", phi0, " phi1 is: ", phi1, " The next forecast price is: ", forecast0);
  //Comment("The AR(1) function is: r_t = ", phi0, " + ", phi1, " * ", ReturnArray[1],  ". The next forecast price is: ", forecast0, "ReturnArray[1] is ", ReturnArray[1]); /// NOT WORKING!!!!! ALERT IS NOT DISPLAYED!!!!
  // Plot line indicating the forecasts
 
  return(0);
  }
 
 
 
 
 
 
//+------------------------------------------------------------------+
//| Custom Functions                                                |
//+------------------------------------------------------------------+
 
  // Input a new price and an old price into the function which will retrun the value of the rate of return
double ReturnRate (double newprice, double oldprice)
{
double R = (newprice - oldprice) / oldprice;
return(R);
}
 
 
//+------------------------------------------------------------------+
 
Back
Top Bottom