Correlation_v1_v1
Download scriptThis tool is designed to measure and visualize the statistical relationship between two different financial instruments—such as two different currency pairs or assets—on a single chart. By analyzing how these two markets move in relation to one another over a specific timeframe, it helps traders identify whether the assets are moving in tandem (positively correlated) or in opposite directions (negatively correlated).
How the Logic Works
The script follows a structured mathematical process to determine the "Correlation Coefficient" between your chosen symbols:
1. Data Input and Selection The user defines two specific financial symbols and a look-back period (the number of past data points to analyze). The script then captures the closing prices for both assets simultaneously.
2. Establishing Movement Trends To understand how each asset is performing, the tool calculates a Simple Moving Average (SMA) for both symbols. Instead of looking at raw price alone, the logic measures the "difference" between the current price and its moving average. This helps filter out minor market noise and focuses on the actual trend momentum of each asset.
3. Statistical Calculation The script then performs a series of mathematical operations over the defined period:
- It calculates the product of the movements of both assets.
- It squares the differences to determine the variance (how much each price fluctuates independently).
- It aggregates these values to produce a correlation value.
4. Visual Output The final result is presented as a line graph that oscillates between specific boundaries (typically ranging from -1.05 to 1.05):
- Positive Correlation: When the line moves toward the upper limit, it indicates that both assets are moving in the same direction at similar times.
- Negative Correlation: When the line moves toward the lower limit, it indicates that the two assets are moving in opposite directions (when one goes up, the other tends to go down).
- Neutral Correlation: A value near zero suggests that the movements of the two assets are unrelated to each other.
Practical Application for Traders
By using this logic, traders can identify "Pairs Trading" opportunities or diversify their portfolios more effectively. If two assets that usually move together suddenly show a breakdown in correlation, it may signal a significant market shift or a potential trading opportunity based on the divergence of those prices.
/*-----------------------------+
| |
| Shared by www.Aptrafx.com |
| |
+------------------------------*/
//+------------------------------------------------------------------+
//| 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="GBPUSD";
extern string Symbol2="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