Correlation

Author: Copyright � 2006, TrendLaboratory Ltd.
Correlation

This tool is designed to measure and visualize the statistical relationship between two different financial assets (such as two different currency pairs). By analyzing how these two assets move in relation to one another over a specific period of time, the script helps traders understand whether they are moving in tandem, in opposite directions, or independently.

How the Logic Works

The script operates by comparing the price action of your chosen primary asset against a secondary asset. Here is the step-by-step breakdown of its internal logic:

1. Identifying Deviations Instead of looking at the raw price of the assets, the tool first calculates how much each asset has deviated from its own average price over a set timeframe (determined by the user-defined period). This helps smooth out "noise" and focuses on the actual momentum of the price movement relative to its recent history.

2. Calculating the Relationship The script then looks at these deviations simultaneously for both assets. It performs a mathematical calculation to determine how closely the fluctuations of Asset A mirror the fluctuations of Asset B.

3. Correlation Output The final result is expressed as a correlation coefficient, which is displayed on your chart:

  • A positive result indicates that both assets are moving in the same direction.
  • A negative result suggests they are moving in opposite directions.
  • The closer the value is to 1 or -1, the stronger the relationship between the two assets.

Key Features for the User

  • Customizable Pairs: You can define which two symbols you want to compare (e.g., GBP/JPY vs. CHF/JPY).
  • Adjustable Timeframe: You can set the "Period" to look at a specific window of recent history, allowing you to see short-term trends or longer-term historical patterns.
  • Visual Representation: The data is plotted on a separate window, providing a clear line graph that makes it easy to spot periods where the correlation strengthens or breaks down, which can be vital for identifying hedging opportunities or confirming trend strengths.
Price Data Components
Series array that contains close prices for each bar
Indicators Used
Moving average indicator
Miscellaneous
Implements a curve of type %1
10 Views
0 Downloads
0 Favorites
Correlation
//+------------------------------------------------------------------+
//|                                               Correlation_v1.mq4 |
//|                           Copyright © 2006, TrendLaboratory Ltd. |
//|            http://finance.groups.yahoo.com/group/TrendLaboratory |
//|                                       E-mail: igorad2004@list.ru |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2006, TrendLaboratory Ltd."
#property link      "http://finance.groups.yahoo.com/group/TrendLaboratory"

#property indicator_separate_window
#property indicator_buffers 1
#property indicator_color1 DeepSkyBlue
#property indicator_maximum 1.05
#property indicator_minimum -1.05

//---- input parameters
extern string    Symbol1="GBPJPY";//"GBPUSD";
extern string    Symbol2="CHFJPY";//"EURUSD";
extern int       cPeriod=20;

//---- buffers
double Correlation[];
double DiffBuffer1[];
double DiffBuffer2[];
double PowDiff1[];
double PowDiff2[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   IndicatorBuffers(5);
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,Correlation);
   SetIndexBuffer(1,DiffBuffer1);
   SetIndexBuffer(2,DiffBuffer2);
   SetIndexBuffer(3,PowDiff1);
   SetIndexBuffer(4,PowDiff2);
//----
   string short_name="Correlation("+Symbol1+","+Symbol2+","+cPeriod+")";
   IndicatorShortName(short_name);
   SetIndexLabel(0,short_name);
   
   SetIndexDrawBegin(0,2*cPeriod);
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int shift,limit,counted_bars=IndicatorCounted();
   
   if ( counted_bars < 0 ) return(-1);
   if ( counted_bars ==0 ) limit=Bars-cPeriod-1;
   if ( counted_bars < 1 ) 
   for(int i=1;i<2*cPeriod;i++) Correlation[Bars-i]=0;    
      
   if(counted_bars>0) limit=Bars-counted_bars;
   limit--;
   
   for( shift=limit; shift>=0; shift--)
   {
//----
   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( i = cPeriod-1 ;i >= 0 ;i--)
      {
      u += DiffBuffer1[shift+i]*DiffBuffer2[shift+i];
      l += PowDiff1[shift+i];
      s += PowDiff2[shift+i];
      }
      
   if(l*s >0) Correlation[shift]=u/MathSqrt(l*s);
   
  }
  
//----
   return(0);
  }
//+------------------------------------------------------------------+

Comments