Thread: HedgeEA
View Single Post
  #10 (permalink)  
Old 10-24-2006, 07:11 PM
Nicholishen's Avatar
Nicholishen Nicholishen is offline
Senior Member
 
Join Date: Dec 2005
Posts: 531
Nicholishen is on a distinguished road
correlation function

Here is the correlation as an internal function. This will speed up the processing time for the EA.

PHP Code:
extern int cPeriod=20;
// CorrelationShift returns the shift value for the correlation indicator
double Correlation(string Symbol1,string Symbol2,int CorrelationShift=0){
   
double Correlation[],DiffBuffer1[],DiffBuffer2[],PowDiff1[],PowDiff2[];
   
ArrayResize(Correlation,cPeriod+1);ArrayResize(DiffBuffer1,cPeriod+1);
   
ArrayResize(DiffBuffer2,cPeriod+1);ArrayResize(PowDiff1,cPeriod+1);ArrayResize(PowDiff2,cPeriod+1);
   for( 
int shift=cPeriod+1shift>=0shift--){
      
DiffBuffer1[shift]=iClose(Symbol1,0,shift)-iMA(Symbol1,0,cPeriod,0,MODE_SMA,PRICE_CLOSE,shift);
      
DiffBuffer2[shift]=iClose(Symbol2,0,shift)-iMA(Symbol2,0,cPeriod,0,MODE_SMA,PRICE_CLOSE,shift);
      
PowDiff1[shift]=MathPow(DiffBuffer1[shift],2);
      
PowDiff2[shift]=MathPow(DiffBuffer2[shift],2);
      
double u=0,l=0,s=0;
      for( 
int i cPeriod-;>= ;i--){
         
+= DiffBuffer1[shift+i]*DiffBuffer2[shift+i];
         
+= PowDiff1[shift+i];
         
+= PowDiff2[shift+i];
      }
      if(
l*>0)Correlation[shift]=u/MathSqrt(l*s);
   }   
   return(
Correlation[CorrelationShift]);
   return(-
1); 

__________________
"Anyone who has never made a mistake has never tried anything new." -Albert Einstein
Reply With Quote