correlation_v1
Download scriptThis tool is designed to measure and visualize the statistical relationship between two different financial assets (for example, how closely the price movements of the British Pound vs. the US Dollar mimic one another). Instead of simply looking at raw prices, it focuses on how the "momentum" or "deviation" of these two assets moves in relation to their historical averages.
How the Logic Works
1. Identifying Deviations The script begins by analyzing each asset individually. It calculates a Simple Moving Average (SMA)—which represents the "normal" average price over a specific period—and compares it to the current actual price. By subtracting the average from the actual price, the tool identifies the specific deviation. This helps filter out background "noise" and highlights how much an asset is currently overperforming or underperforming its trend.
2. Mathematical Correlation Once the deviations for both assets are identified, the script performs a mathematical comparison between them over a set number of previous bars (the user-defined period). It looks for patterns such as:
- Positive Correlation: Do both assets move away from their averages in the same direction at the same time?
- Negative Correlation: Does one asset rise while the other falls in a consistent pattern?
- No Correlation: Are the movements completely unrelated?
3. The Resulting Score The tool processes these interactions into a single numerical value, typically ranging between -1.05 and 1.05:
- A score near 1.0 indicates a very strong positive correlation (they move almost in lockstep).
- A score near -1.0 indicates a very strong inverse correlation (they move in opposite directions).
- A score near 0 suggests that the movements of the two assets are disconnected from one another.
Practical Application
For a trader, this provides a visual "sync" meter. By observing the line on your chart, you can quickly determine if two markets are behaving similarly. This is often used to identify diversification opportunities, confirm trends across different currencies, or spot unusual market behavior where a usually correlated pair suddenly begins to diverge.
//+------------------------------------------------------------------+
//| 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