Moving Regression indy not working in backtest, question.

 

Hi

i ve worked on some regression code i ve found and i altered it so to create a different version of it.

Although it seems to work as part of an indincator, when i m backtesting it the results are totally different! I really cant find where the problem is. Any ideas/help?

Here is the code:

reg240 =i_Regr_i(1,2,240,0,i);

// -------------------------  i-REGR-i ----------------------------
double i_Regr_i(int degree, double kstd, int bars, int shift, int real_shift)
{
   double fx[];//,sqh[],sql[];
 
   double ai[10,10],b[10],x[10],sx[20];
   double sum; 
   int ip,p,n,f;
   double qq,mm,tt;
   int ii,jj,kk,ll,nn;
   double sq;
 
   int i0 = 0;

     i0=real_shift;

     if (Bars < bars) return;
 
    
     int mi; 
     ip = bars;
     p=ip; 
     sx[1]=p+1; 
     nn = degree+1;

   //----------------------sx-------------------------------------------------------------------
     for(mi=1;mi<=nn*2-2;mi++) 
     {
       sum=0;
       for(n=i0;n<=i0+p;n++)
       {
          sum+=MathPow(n,mi);
       }
       sx[mi+1]=sum; 
     } 
     //----------------------syx-----------
     for(mi=1;mi<=nn;mi++)
     {
       sum=0.00000;
       for(n=i0;n<=i0+p;n++)
       {
          if(mi==1) sum+=Close[n];
          else sum+=Close[n]*MathPow(n,mi-1);
       }
       b[mi]=sum;
     } 
   //===============Matrix=======================================================================================================
     for(jj=1;jj<=nn;jj++)
     {
       for(ii=1; ii<=nn; ii++)
       {
          kk=ii+jj-1;
          ai[ii,jj]=sx[kk];
       }
     } 
   //===============Gauss========================================================================================================
     for(kk=1; kk<=nn-1; kk++)
     {
       ll=0;
       mm=0;
       for(ii=kk; ii<=nn; ii++)
       {
          if(MathAbs(ai[ii,kk])>mm)
          {
             mm=MathAbs(ai[ii,kk]);
             ll=ii;
          }
       }
       if(ll==0) return(0);   
       if (ll!=kk)
       {
          for(jj=1; jj<=nn; jj++)
          {
             tt=ai[kk,jj];
             ai[kk,jj]=ai[ll,jj];
             ai[ll,jj]=tt;
          }
          tt=b[kk];
          b[kk]=b[ll];
          b[ll]=tt;
       }  
       for(ii=kk+1;ii<=nn;ii++)
       {
          qq=ai[ii,kk]/ai[kk,kk];
          for(jj=1;jj<=nn;jj++)
          {
             if(jj==kk) ai[ii,jj]=0;
             else ai[ii,jj]=ai[ii,jj]-qq*ai[kk,jj];
          }
          b[ii]=b[ii]-qq*b[kk];
       }
     }  
     x[nn]=b[nn]/ai[nn,nn];
     
     for(ii=nn-1;ii>=1;ii--)
     {
       tt=0;
       for(jj=1;jj<=nn-ii;jj++)
       {
          tt=tt+ai[ii,ii+jj]*x[ii+jj];
          x[ii]=(1/ai[ii,ii])*(b[ii]-tt);
       }
     }
   //===========================================================================================================================

   //for(n=i0;n<=i0+p;n++)
         n=i0;
   //{
     sum=0;
     for(kk=1;kk<=degree;kk++)
     {
        sum+=x[kk+1]*MathPow(n,kk);
     }
     
     double result=x[1]+sum;
     
     fx[n]=result; 
    
   //}

    return ( result );

} //i_Regr_i function
 
athanfx: Although it seems to work as part of an indincator, when i m backtesting it the results are totally different! I really cant find where the problem is.
  1.        for(n=i0;n<=i0+p;n++)
           {
              if(mi==1) sum+=Close[n];
              else sum+=Close[n]*MathPow(n,mi-1);
    i0 is the shift so Close[n] make sense. If i0==99 why is your sum multiples of 99, 9801 etc.
  2. I posted my general implementation 3 days ago: Fitting a parabola - MQL4 forum


 
WHRoeder:
  1. i0 is the shift so Close[n] make sense. If i0==99 why is your sum multiples of 99, 9801 etc.
  2. I posted my general implementation 3 days ago: Fitting a parabola - MQL4 forum



Thanks for your answer.

Well I m not sure why it multiplies. As i said before i just found the code and altered it. I didnt go through how the regression is calculated.

The only change i ve made is that i placed the code in a i_Regr_i function and i added the real_shift.

So in the Start() in the while() loop i call something like

i_Regr_i(degree, kstd, bars, shift, i)
 

athanfx2013.10.10 10:36

Hi

i ve worked on some regression code i ve found and i altered it so to create a different version of it.

Although it seems to work as part of an indincator, when i m backtesting it the results are totally different! I really cant find where the problem is. Any ideas/help?

I am not sure what all that is you have coded, you should comment your code when you post it, anyway it looks like too much code to me, this is all it takes to code a regression line.

//+-------------------------------------------------------------+
//|                     testing least squares regression line   |
//|                                                  SDC 2013   |
//+-------------------------------------------------------------+
int start()
{
 double a,b,y,n,x,sumx,sumx2,sumxy,sumy,y1,y2;

 int begin = 10;   //bar shift of first bar (oldest)
 int end = 0;      //bar shift of end bar (newest)

 
//---- vals for least squares regression
 for(int i=begin; i>=end;i--)
 {
  y = Close[i];                 //-- loop through closes
  x=i;                          //-- bars by chart index
  n++;                          //-- number of bars in line
  sumx += x;                    //-- sum of bars in line
  sumy += y;                    //-- sum of close prices
  sumxy += (x*y);               //-- sum of bars*close
  sumx2 += (x*x);               //-- sum of bars squared
 }
//---- slope(b) intercept(a)
 b = (n*sumxy-sumx*sumy)/(n*sumx2-sumx*sumx);
 a = (sumy - (b*sumx))/n;
//---- insert bar numbers into equation
 y1 = a+b*begin;
 y2 = a+b*end;
//test equation with a trendline
 ObjectCreate("LSR1",OBJ_TREND,0,Time[begin],y1,Time[end],y2);
//----
 return(0);
}
 
SDC:

I am not sure what all that is you have coded, you should comment your code when you post it, anyway it looks like too much code to me, this is all it takes to code a regression line.


Thanks for your answer I ll try your code.

The way i ve created is so that it uses buffer not object to draw regression. I do that cause what i want to achieve is to draw on every buffer[i] of the very last part of the regression line (where regression line ends).
I m not sure how to do that so backtest will give same results with simple chart with this indicator

 

you can bar shift my code easily just use the begin and end variables, i was think about making an indicator like you described let me know if it was useful

 
SDC:

you can bar shift my code easily just use the begin and end variables, i was think about making an indicator like you described let me know if it was useful


Yes in theory my code already did this but i ll try yours and let you know. It would be usefull cause you all current regressions indicators show you the current situation and not the past