Correlation_Index

Download script
Author: Copyright � 2006, albedo@email.it
Correlation_Index

This tool is designed to measure and visualize the statistical relationship between two specific currency pairs—specifically, how closely the price movements of the EUR/USD and the USD/CHF mirror each other over a set period of time.

How it Works

The script acts as a mathematical observer that looks at historical price data. Instead of just looking at where a price is currently sitting, it analyzes the "behavioral patterns" of the two pairs simultaneously. It calculates a correlation factor, which tells you if these two currencies are moving in tandem (synchronized) or if they are moving independently of one another.

Key Components of the Logic:

  • Multi-Currency Monitoring: While the script pulls data for several major currency pairs (including GBP/USD, AUD/USD, USD/JPY, and USDCAD), its primary focus is on identifying the interaction between EUR/USD and USD/CHF.
  • Dynamic Moving Window: The tool uses a user-defined period (defaulting to 13 bars) to perform its calculations. This means it constantly "slides" across the chart, calculating the correlation for the most recent prices while dropping off older data that is no longer relevant.
  • Mathematical Correlation: It utilizes a standard statistical formula to determine the strength of the link between the two pairs. By summing the values and their squares over the chosen period, it produces a specific coefficient.
  • Visual Output: The result is displayed as a dedicated line in a separate window on your trading platform. This allows you to see at a glance whether the relationship between these two pairs is tightening or loosening in real-time.

What this means for the user:

By using this indicator, you can visually identify "co-movement." If the correlation is high, it suggests that the EUR/USD and USD/CHF are reacting to similar market pressures. Understanding this relationship helps traders gauge market sentiment, as a breakdown in a normally strong correlation can sometimes signal a significant shift in global economic conditions or technical trends.

Miscellaneous
Implements a curve of type %1
13 Views
1 Downloads
0 Favorites
Correlation_Index
/*-----------------------------+
|			       |
| Shared by www.Aptrafx.com    |
|			       |
+------------------------------*/

//+------------------------------------------------------------------+
//|                                            Correlation_Index.mq4 |
//|                                Copyright © 2006, albedo@email.it |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2006, albedo@email.it"
#property indicator_separate_window
//#property indicator_minimum -1
//#property indicator_maximum 1
#property indicator_buffers 1
#property indicator_color1 Red
extern int R_Period=13;
double R[];
double EurUsd[],UsdJpy[],AudUsd[],UsdCad[],UsdChf[],GbpUsd[];
//----
int ExtCountedBars=0;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
   int    draw_begin;
   string short_name;
//---- drawing settings
   SetIndexStyle(1,DRAW_LINE);
   IndicatorDigits(MarketInfo(Symbol(),MODE_DIGITS));
   if(R_Period<2) R_Period=13;
   draw_begin=R_Period-1;
         short_name="R(";
   IndicatorShortName(short_name+R_Period+")");
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,R);
   
  return(0);
  }

int start()
  {
   ArrayCopySeries(EurUsd,MODE_CLOSE,"EURUSD");
   ArrayCopySeries(GbpUsd,MODE_CLOSE,"GBPUSD");
   ArrayCopySeries(AudUsd,MODE_CLOSE,"AUDUSD");
   ArrayCopySeries(UsdChf,MODE_CLOSE,"USDCHF");
   ArrayCopySeries(UsdJpy,MODE_CLOSE,"USDJPY");
   ArrayCopySeries(UsdCad,MODE_CLOSE,"USDCAD");
   
   if(Bars<=R_Period) return(0);
   ExtCountedBars=IndicatorCounted();
   if (ExtCountedBars<0) return(-1);
   if (ExtCountedBars>0) ExtCountedBars--;
//----
   double sumX=0;
   double sumY=0;
   double sumXY=0;
   double sumX2=0;
   double sumY2=0;
   int    a,b,c,d,e,acc=Bars-ExtCountedBars-1;
   if(acc<R_Period) acc=R_Period;
   
   for(a=1;a<R_Period;a++,acc--)      sumY+=UsdChf[acc];
   for(b=1;b<R_Period;b++,acc--)      sumX+=EurUsd[acc];
   for(c=1;c<R_Period;c++,acc--)      sumXY+=EurUsd[acc]*UsdChf[acc];   
   for(d=1;d<R_Period;d++,acc--)      sumX2+=MathPow(EurUsd[acc],2);
   for(e=1;e<R_Period;e++,acc--)      sumY2+=MathPow(UsdChf[acc],2);   

   while(acc>=0)
     {
      sumY+=UsdChf[acc];
      sumX+=EurUsd[acc];
      sumXY+=EurUsd[acc]*UsdChf[acc];
      sumX2+=MathPow(EurUsd[acc],2);
      sumY2+=MathPow(UsdChf[acc],2);

      double tmp = MathSqrt((sumX2-(MathPow(sumX,2)/R_Period))*(sumY2-(MathPow(sumY,2)/R_Period)));
      if (tmp == 0) tmp = Point;     
      R[acc]=(sumXY-((sumX*sumY)/R_Period))/ tmp;
//    The R correlation factor formula is correct, so why in the hell this indicator
//    shows alway a null value? >-(	   
	   
	   sumY-=UsdChf[acc+R_Period-1];
	   sumX-=EurUsd[acc+R_Period-1];
	   sumXY-=EurUsd[acc+R_Period-1]*UsdChf[acc+R_Period-1];
	   sumX2-=MathPow(EurUsd[acc+R_Period-1],2);
	   sumY2-=MathPow(UsdChf[acc+R_Period-1],2);

 	   acc--;
     }

   return(0);
  }

Comments