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); }
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
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);
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.
Where do i insert this block of code?
You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
Correlation:
Author: John Smith