Download MetaTrader 5

Indicators: Correlation

To add comments, please log in or register
Log in to MQL5.community using OpenID. It is convenient!
MetaQuotes Software Corp.
Moderator
181343
MetaQuotes Software Corp. 2009.01.07 08:28 

Correlation:

The indicator calculates the correlation of the currency pairs.

Author: John Smith

Russell
287
Russell 2009.01.11 22:30  

For those who want to calculate the correlation between 2 arrays

double correlation(double dArrayX[] , double dArrayY[]){
   int liX = ArraySize(dArrayX);
   if(liX != ArraySize(dArrayY)){
      Print("correlation : arrays don't match");
      return(0);
   }
   
   if(liX <= 1){
      return(0);
   }
   
 
   double ldSumXAVG = 0;
   double ldSumYAVG = 0;
   double ldmuT = 0;
   double ldDevX = 0;
   double ldDevY = 0;
   double ldRoX = 0;
   double ldRoY = 0;
   
   for(int i = 0; i < liX; i++){
   
      ldSumXAVG += dArrayX[i];
      ldSumXAVG += dArrayY[i];      
   }
   ldSumXAVG /= liX;
   ldSumYAVG /= liX;
    
   for (i = 0; i < liX; i++){
   
      ldDevX = dArrayX[i]-ldSumXAVG;
      ldDevY = dArrayY[i]-ldSumYAVG;   
      ldmuT += ldDevX*ldDevY;
      ldRoX +=MathPow(ldDevX,2);   
      ldRoY +=MathPow(ldDevY,2);
   }
    
   double ldDivider = MathSqrt(ldRoX)*MathSqrt(ldRoY);
   if (ldDivider == 0){
      Print("correlation : can't divide by zero"); 
      return(0); 
   }
   return( ldmuT / ldDivider);
}
MQL4 Comments
16319
MQL4 Comments 2009.07.12 12:22  
Russell:

For those who want to calculate the correlation between 2 arrays

double correlation(double dArrayX[] , double dArrayY[]){
   int liX = ArraySize(dArrayX);
   if(liX != ArraySize(dArrayY)){
      Print("correlation : arrays don't match");
      return(0);
   }
   
   if(liX <= 1){
      return(0);
   }
   
 
   double ldSumXAVG = 0;
   double ldSumYAVG = 0;
   double ldmuT = 0;
   double ldDevX = 0;
   double ldDevY = 0;
   double ldRoX = 0;
   double ldRoY = 0;
   
   for(int i = 0; i < liX; i++){
   
      ldSumXAVG += dArrayX[i];
      ldSumXAVG += dArrayY[i];      
   }
   ldSumXAVG /= liX;
   ldSumYAVG /= liX;
    
   for (i = 0; i < liX; i++){
   
      ldDevX = dArrayX[i]-ldSumXAVG;
      ldDevY = dArrayY[i]-ldSumYAVG;   
      ldmuT += ldDevX*ldDevY;
      ldRoX +=MathPow(ldDevX,2);   
      ldRoY +=MathPow(ldDevY,2);
   }
    
   double ldDivider = MathSqrt(ldRoX)*MathSqrt(ldRoY);
   if (ldDivider == 0){
      Print("correlation : can't divide by zero"); 
      return(0); 
   }
   return( ldmuT / ldDivider);
}

Hello Russell,

Your function was giving incorrect results. I tried it with two hard-coded numeric arrays with identical values and it doesn't return 1. Initially I thought there was something wrong with my Correlation indicator. Took me a while to discover it was actually this routine that was giving me the error.

Scott

virtualreal
113
virtualreal 2012.08.17 11:58  

I was surprised to notice that this function wasn't included in the Math class.

Thanks for sharing!

tijo45
190
tijo45 2013.07.18 16:02  
Russell :

For those who want to calculate the correlation between 2 arrays

double correlation(double dArrayX[] , double dArrayY[]){
   int liX = ArraySize(dArrayX);
   if(liX != ArraySize(dArrayY)){
      Print("correlation : arrays don't match");
      return(0);
   }
   
   if(liX <= 1){
      return(0);
   }
   
 
   double ldSumXAVG = 0;
   double ldSumYAVG = 0;
   double ldmuT = 0;
   double ldDevX = 0;
   double ldDevY = 0;
   double ldRoX = 0;
   double ldRoY = 0;
   
   for(int i = 0; i < liX; i++){
   
      ldSumXAVG += dArrayX[i];
      ldSumXAVG += dArrayY[i];      
   }
   ldSumXAVG /= liX;
   ldSumYAVG /= liX;
    
   for (i = 0; i < liX; i++){
   
      ldDevX = dArrayX[i]-ldSumXAVG;
      ldDevY = dArrayY[i]-ldSumYAVG;   
      ldmuT += ldDevX*ldDevY;
      ldRoX +=MathPow(ldDevX,2);   
      ldRoY +=MathPow(ldDevY,2);
   }
    
   double ldDivider = MathSqrt(ldRoX)*MathSqrt(ldRoY);
   if (ldDivider == 0){
      Print("correlation : can't divide by zero"); 
      return(0); 
   }
   return( ldmuT / ldDivider);
}

Small issue with logic ...


it should be


double ldDivider = MathSqrt(ldRoX*ldRoY);
Roy Meshulam
118
Roy Meshulam 2016.04.15 17:53  
double correlation(double &dArrayX[],double &dArrayY[])
  {
   int liX= ArraySize(dArrayX);
   if(liX!=ArraySize(dArrayY))
     {
      Print("correlation : arrays don't match");
      return(0);
     }

   if(liX<=1)
     {
      return(0);
     }

   double ldSumXAVG = 0;
   double ldSumYAVG = 0;
   double ldmuT=0;
   double ldDevX = 0;
   double ldDevY = 0;
   double ldRoX = 0;
   double ldRoY = 0;

   for(int i=0; i<liX; i++)
     {
      ldSumXAVG += dArrayX[i];
      ldSumYAVG += dArrayY[i];
     }
   ldSumXAVG /= liX;
   ldSumYAVG /= liX;

   for(int i=0; i<liX; i++)
     {
      ldDevX=dArrayX[i]-ldSumXAVG;
      ldRoX+=MathPow(ldDevX,2);

      ldDevY=dArrayY[i]-ldSumYAVG;
      ldRoY +=MathPow(ldDevY,2);

      ldmuT+=ldDevX*ldDevY;
     }

   double ldDivider=MathSqrt(ldRoX*ldRoY);
   if(ldDivider==0)
     {
      Print("correlation : can't divide by zero");
      return(0);
     }
   return(ldmuT/ldDivider);

  }

 

Working code. 

To add comments, please log in or register