All the functions discussed here are part of the Statistics library available for purchase.

While watching Wimbledon finals yesterday in the UK. I was thinking of all the significant economical events we are going through in Europe, the Brexit, elections etc. and how are the different pairs I am trading got affected. The statistical value which show the significance of the dependency between two products is called Correlation.

According to https://en.wikipedia.org/wiki/Correlation_and_dependence, Correlation can be calculated using the following equation:

Correlation(EURUSD,USDJPY)=Covariance(EURUSD,USDJPY) / STD(EURUSD)*STD(USDJPY)

Correlation ranges between -1, that when EURUSD trends upwards, USDJPY expected to trend downwards. 0, meaning there is no dependency between these currencies at all. Or +1, meaning the currencies are expected to trend in the same direction.

### Why would you care?

EURUSD and EURCHF have correlation of -1 in most of the times. Imagine you went long on EURUSD believing there is an upwards potential for the currency and profit to be made. Now, if indeed EURCHF is reversed correlated to EURUSD, opening a short position on EURCHF will increase your risk twice when EURUSD changes direction. Because now, EURUSD will be short, and EURCHF which is reversed correlated will go long. You are losing both on the long EURUSD position AND the short EURCHF one.

If you are not able to calculate Correlations dynamically, you would not identify this situation and make this mistake, not able to recognize what went wrong. The challenge with calculating correlation is the dynamics of the products. The values are always changing, it is not a static set of values which are easy to calculate.

### Calculating Correlation step by step

We start with the simple part of the equation and that is the denominator. Meaning, we need to calculate the standard deviation of the currency for a given values over a period of time. Standard deviation is defined as the squared root of the sample variance, which is the sum of differences of the sample from the sample mean:

Standard Deviation = Square(Variance(EURUSD)) = Square(∑(EURUSDi – mean(EURUSD)) / Sample Size)

#### Calculating the mean

Let’s start by calculating the sample mean. This is basically running over the sample which is captured in an array, summing the values and dividing with the sample size.

double Mean(double &argArr[]) export
{
double length=ArraySize(argArr);

double sum=0;
for(int i=(int)(length-1); i>=0; i–)
sum+=argArr[i];
return sum/length;
}
Since I place this function in a library, I used the keyword export. This function runs on argArr, summing it’s values and then dividing by the array size.

#### Calculating the variance

In the previous step, we calculated the sample mean. Now we will use it to calculate the sample variance. As per the variance formula, we need to sum up the square difference of the sample values from it’s mean:

double Variance(double &argArr[]) export
{
double length=ArraySize(argArr);
double mean=Mean(argArr);

double sum=0;
for(int i=(int)(length-1); i>=0; i–)
sum+=MathPow(argArr[i]-mean,2);
return sum/(length-1);
}

Again, I am using this function from within a library, so I used the export reserved keyword. Then, calling the Mean function introduced in the previous part, mean saves the argArr mean value. All we have to do, is to run over the array and sum up the difference between the array values in index i to the mean we calculated. The returned value is the sample’s variance, thus we divide it with (n-1).

#### Calculating the standard deviation

This part is rather simple. Since we have the sample’s variance calculated already, all we need to do is to return it’s squared root:

double StandardDeviation(double &argArr[]) export
{
return MathSqrt(Variance(argArr));
}

As you can see, the standard deviation function only calculates the squared root on the variance function return value.

#### Calculating Covariance

double Covariance(double &arrX[],double &arrY[]) export
{
if(ArraySize(arrX)!=ArraySize(arrY))
return -1;

double length=ArraySize(arrX);
double meanX=Mean(arrX);
double meanY=Mean(arrY);

numerator=0;
for(int i=(int)(length-1); i>=0; i–)
numerator+=(arrX[i]-meanX)*(arrY[i]-meanY);
return numerator/(length-1);
}

Covariance is the sum of differences between a value to it’s mean between two series. So, in this case we calculate the mean of EURUSD, the mean of USDJPY and sum up the differences of the values from the currency series with the mean. Then dividing it with the series length-1 as it is a sample.

#### Putting it all together

Now that we have it all the necessary code to calculate the correlation between two products, it’s time to put it into action. The following code shows as an example how to print the correlation between the current product to the other open orders you have. You may use this code and print to the screen the correlation between the different products:

const int CORRELATION_HISTORY=2524;
double X[],Y[];
ArrayResize(X,CORRELATION_HISTORY);
ArrayInitialize(X,0);

for(int i=0;i<CORRELATION_HISTORY;i++)
X[i]=iClose(Symbol(),PERIOD_H1,i);

ArrayResize(Y,CORRELATION_HISTORY);
string CorrelationStr=””;
string CurrSymbol=””;
for(int i=OrdersTotal()-1;i>=0;i–)
if(OrderSelect(i,SELECT_BY_POS) && StringFind(CorrelationStr,OrderSymbol())==-1 && StringCompare(OrderSymbol(),Symbol())!=0)
{
CurrSymbol=OrderSymbol();
ArrayInitialize(Y,0);
for(int j=0;j<CORRELATION_HISTORY;j++)
Y[j]=iClose(CurrSymbol,PERIOD_H1,j); CorrelationStr=StringConcatenate(CurrSymbol,” “,DoubleToString(Correlation(X,Y),2),” “,CorrelationStr);
}
Print(CorrelationStr);

I normally take a period of 240 values, but of course you may take larger values if you want. The following output shows the correlation between USDJPY to the other products I have open orders on: This entry was posted by . Bookmark the . Titled