//+------------------------------------------------------------------+
//| correlation_Pearson.mq4 |
//| Copyright ? 2006, zolero |
//| made to help albedo@email.it who shared a wish to see this |
//| indicator working ;) |
//+------------------------------------------------------------------+
#property copyright "Copyright ? 2006, idea: albedo@email.it"
#property indicator_separate_window
#property indicator_buffers 1
#property indicator_color1 Red
extern int R_Period=13;
extern string Pair1="GBPJPY";
extern string Pair2="USDJPY";
double R[];
double X[],Y[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- indicators
//---- drawing settings
SetIndexStyle(1,DRAW_LINE);
IndicatorShortName(Pair1+"/"+Pair2+" R("+R_Period+")");
SetIndexStyle(0,DRAW_LINE);
SetIndexBuffer(0,R);
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{
int counted_bars=IndicatorCounted();
//----
double X[],Y[];
int piir, limit, omega;
double sumxy, sumx, sumx2, sumy, sumy2;
double yks, kaks, kolm;
limit=Bars-counted_bars; //always just 1 bar after first calculation
if(counted_bars==0) limit=Bars-R_Period-1; //first time it should be enough bars to calculate
ArrayResize(X,limit); // min size of array is using less memory
ArrayResize(Y,limit);
ArrayCopySeries(X,MODE_OPEN,Pair1,0);
ArrayCopySeries(Y,MODE_OPEN,Pair2,0);
omega=0;
while(omega<=limit)
{
sumxy=0; sumx=0; sumx2=0;
sumy=0; sumy2=0;
for(int i=1; i<=R_Period; i++)
{
sumxy+=X[omega+i]*Y[omega+i];
sumx+=X[omega+i];
sumx2+=MathPow(X[omega+i],2.); //2. is neccesary because MathPow wants both parts as double
sumy+=Y[omega+i];
sumy2+=MathPow(Y[omega+i],2.);//but it seems to work with "2" as well
}
yks=sumxy-(sumx*sumy)/R_Period;
kaks=sumx2-(MathPow(sumx,2.)/R_Period);
kolm=sumy2-(MathPow(sumy,2.)/R_Period);
R[omega]=(yks/MathSqrt(kaks*kolm));
omega++;
}
//----
return(0);
}
//+------------------------------------------------------------------+
Comments