Linear Regression Slope - Help

 

.

Hi,

I'm trying to develop an indicator that gives the SLOPE of the regression line at each point.

The slope formula is this one: 

http://s12.postimg.org/fumrf7s3x/Sem_T_tulo.png


I've already wrote some code based on the Moving Average indicator.

But I not very sure about those cicles are given me the sums that I want...

Can anyone give me some help here with these cycles please.

 

I've attached the indicator that I've already done. :)

 

.

Files:
 

.

Please... A little help here guys... :)

This sums are too much for my acknowledgment!

 

 

//----


   double sumy=0,
          sumx=0,
          sumxy=0,
          sumx2=0;
           
   int    i,pos=Bars-ExtCountedBars-1;
   
   
//---- initial accumulation

   if(pos<RegLin_Period) pos=RegLin_Period;
   
   for(i=1;i<RegLin_Period;i++,pos--)
      {
      sumy+=Close[pos];
      sumxy+=Close[pos]*i;
      sumx+=i;
      sumx2+=i*i;
      }      

//---- main calculation loop

   while(pos>=0)
     {
      sumy+=Close[pos];
      sumxy+=Close[pos]*i;
      sumx+=i;
      sumx2+=i*i;
            
      SLOPE[pos] = (((RegLin_Period*sumxy)-(sumx*sumy)) / ((RegLin_Period*sumx)-(sumx*sumx)));

      sumy-=Close[pos+RegLin_Period-1];
      sumxy-=Close[pos+RegLin_Period-1]*i;      
      pos--;
     }
 

//----     

Are this sums correct?

I've done it based on the moving average indicator sum where it work fine! :(

 

.
    

 
strutch: Are this sums correct?
  1. Your Image shows
    Your Code doesn't match
    (RegLin_Period*sumxy)-(sumx*sumy)) / ((RegLin_Period*sumx)-(sumx*sumx))

    n(Exy) - (Ex)(Ey)
    -----------------
    n(Ex2) - (Ex)2

    n(Exy) - (Ex)(Ey)
    -----------------
    n(Ex)  - (Ex)2


  2. You sumx and sumx2 but you never remove them like the sumy sumxy (after SLOPE[]=)
  3. You are trying to do it in one pass - makes it unreadable.
  4. Factor out your functions and put it in a proper loop. Don't calculate needless bars.

    double slope(int iBar, int nBars){
       double sumy=0,
              sumx=0,
              sumxy=0,
              sumx2=0;
       int iLimit = iBar + nBars;
       for(; iBar < iLimit; iBar++)
          {
          sumy+=Close[iBar];
          sumxy+=Close[iBar]*iBar;
          sumx+=iBar;
          sumx2+=iBar*iBar;
          }      
       return( (nBars*sumxy - sumx*sumy) / (nBar*sumx2 - sumx*sumx) );
    }
    int start(){           
       int    count = IndicatorCounted();
       if(count < RegLin_Period) count = RegLin_Period; // Lookback
       for(int iBar = Bars - 1 - count; iBar >= 0; iBar--){
         {
          SLOPE[iBar] = slope(iBar, RegLin_Period);
         }
     }
You might also consider, instead of using least squares to compute the slope, using Theil-Sen See attached
Files:
theilsen.mq4  6 kb
 

.

Your the best!!! :D

I didn't know about TheilSen. It looks promissor...

I will make some tests betwen those two methods to see what fits better.

 

Thank you so much WHRoeder! Have a nice wekend :) 

 

 

.

What a complicated concepts you introduced me!!!

I'm struggling hard to understand this piece of code... But it will not be easy...

Do you have some indicator that uses Theil-Sen ?

.

Meanwhile I leave here the Linear Regression Slope indicator for other people that may need it. :)  

LinRegSlope 

 
strutch:cDo you have some indicator that uses Theil-Sen ?
You already have an indicator that displays slope. Substitute the calculation with the function provided.
 

.

Hi WHRoeder,

I'm trying to develope an indicator based on the piece of code (TheilSen.mq4) that you give me.

But I don't know how to get the Slope from the function TheilSen2D( ) ...

//+------------------------------------------------------------------+
//| FUNCTION - TheilSen2D                                            |
//+------------------------------------------------------------------+


void TheilSen2D(double& m, double& b, double v[][], int n1, int e2, int iBeg=0)
   {

   /*
    Theil–Sen estimator of a set of two-dimensional points (xi,yi) is the
    median m of the slopes (yj - yi)/(xj - xi)
    
    http://en.wikipedia.org/wiki/Theil-Sen_estimator
    
    2     2:1                                                 1
    3     2:1, 3:1, 3:2                                       3
    4     2:1, 3:1, 3:2, 4:1, 4:2, 4:3                        6
    5     2:1, 3:1, 3:2, 4:1, 4:2, 4:3  5:1, 5:2, 5:3, 5:4   10
    */

   static double slopes[];    

   int nReq = n1 * (n1-1) / 2;

   if(ArraySize(slopes) < nReq) 
   
   if(ArrayResize(slopes, nReq) <= 0)
     {
     DisableTrading("ArrayResize(TheilSen, " + nReq + ") Failed: " +GetLastError() );   
     return;  
     }
   
   int   nSlopes = 0;
   
   for(int i=iBeg + n1 - 1; i >  iBeg; i--)
   
   for(int j=i - 1; j >= iBeg; j--)
      {
      slopes[nSlopes] = (v[i][e2] - v[j][e2]) / (i-j);   
      nSlopes++;        
      }
   
   m = Median(slopes, nSlopes);
   
   for(i=0; i < n1; i++)   
      slopes[i] = v[iBeg+i][e2] - m * i;
   
   b = Median(slopes, n1);
      
   }


I was thinking in do something like the linear regression indicator.

But I really don't understand those input parameters...

TheilSen2D(double& m, double& b, double v[][], int n1, int e2, int iBeg=0)

Do you have some link where I can read more about that indicator/function?

Thank you! :)

.

 
strutch: But I really don't understand those input parameters...

TheilSen2D(double& m, double& b, double v[][], int n1, int e2, int iBeg=0)

I thought reading the function would be self explanatory.

v is the market array from ArrayCopyRates

e2 is the enumeration of which value

   #define ACR_TIME     0  // Array Copy Rates
   #define ACR_OPEN     1
   #define ACR_LOW      2
   #define ACR_HIGH     3
   #define ACR_CLOSE    4
// #define ACR_VOLUME   5
      #define ACR_COUNT    6
slopes[nSlopes] = (v[i][e2] - v[j][e2]) / (i-j);   

creates an array of slopes. You could easily change v[i][e2] to Close[i] for a standard 1D version.

 

.

I'm so noobie in this that I'm probably asking too basic things. And for that my apologies...

Let's see if now I can use it...

Thank you very much once again WHRoeder... :) 

 

.

 

Hi WHRoeder,

Thank you for introduce me that concept (Theil-Sen) but it's too much for me...

I've tried hard but this is too much for my math and programing knowledges.

For now I will focus my ideas on the least squares...

Once again, thank you so much for your help!

God bless you!! :D

 

 
strutch: too much for my math and programing knowledges.
If you had converted the 2D version (provided) to a 1D by making V single dimension and dropping the (then unused) e2, then the following change to your code is too much for you?
// SLOPE[iBar] = slope(iBar, RegLin_Period);
double m, b; TheilSen(m, b, Close, RegLin_Period, iBar); SLOPE[iBar] = m;