Correlation_v3
Download scriptThis tool is designed to measure and visualize the statistical relationship between two different financial assets (for example, comparing the price movements of EURUSD against another currency pair). By analyzing how these two assets move in relation to one another over a specific period, the indicator helps traders identify whether the assets are moving in sync or in opposite directions.
How the Logic Works
1. Data Selection and Customization The script allows you to choose which specific market data points to focus on. You can configure the tool to look at just the opening prices, closing prices, highs, or lows of the candles. This flexibility ensures that the correlation is calculated based on the specific price action that matters most to your trading strategy.
2. The Calculation Window The indicator looks back at a user-defined number of previous bars (the "n" value). It gathers all the price information for both the current chart and the secondary asset chosen in the settings. By looking at this specific window of time, it can determine how consistently the two assets behave together.
3. Mathematical Correlation The core logic uses a standard statistical method to calculate the correlation coefficient:
- Average Movement: It first calculates the average price for both assets over the chosen period.
- Deviation Tracking: It then looks at how much each individual price point deviates from that average.
- Synchronization: Finally, it measures whether these deviations happen at the same time. If both assets rise and fall together relative to their averages, the correlation score will be high. If one rises while the other falls, the score will be low or negative.
4. Visual Representation The final result is displayed as a histogram in a separate window. This provides a clear, visual history of the relationship between the two assets. Instead of looking at complex numbers, you can see at a glance how "connected" the two markets are, allowing you to identify trends, divergences, or periods of high stability in the relationship between different financial instruments.
//+------------------------------------------------------------------+
//| Correlation.mq4 |
//| Áåçáîðîäîâ Àëåêñåé |
//| AlexeiBv@ya.ru |
//+------------------------------------------------------------------+
#property copyright "Áåçáîðîäîâ Àëåêñåé"
#property link "AlexeiBv@ya.ru"
//----
#property indicator_separate_window
#property indicator_buffers 1
#property indicator_color1 Silver
//----
extern string OtherChart="EURUSD";
extern int n=20;
extern bool UseOpen=true;
extern bool UseClose=false;
extern bool UseHigh=false;
extern bool UseLow=false;
extern int CalcBars=400;
//----
double Buffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- indicators
string short_name;
//---- 1 additional buffer used for counting.
IndicatorBuffers(1);
IndicatorDigits(Digits);
//---- indicator line
SetIndexStyle(0,DRAW_HISTOGRAM);
SetIndexBuffer(0,Buffer);
// SetIndexBuffer(1,TempBuffer);
//---- name for DataWindow and indicator subwindow label
short_name="Correlation("+n+")";
IndicatorShortName(short_name);
SetIndexLabel(0,short_name);
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{
// Print("True "+true+" false "+false );
int i,k,counted_bars=IndicatorCounted();
double sx,sy,x,y,cor1,cor2,cor3;
//----
if(Bars<=n) return(0);
//----
int limit=Bars-counted_bars;
if(counted_bars>0) limit++;
for(i=0; i<CalcBars; i++)
{
sx=0; sy=0;
for(k=0; k<n; k++)
{
if((UseOpen+UseClose+UseHigh+UseLow)==0) x=0;
else x=(Open[i+k]*UseOpen+Close[i+k]*UseClose+High[i+k]*UseHigh+Low[i+k]*UseLow)/(UseOpen+UseClose+UseHigh+UseLow);
if((UseOpen+UseClose+UseHigh+UseLow)==0) y=0;
else y=(iOpen(OtherChart,0,i+k)*UseOpen+iClose(OtherChart,0,i+k)*UseClose+iHigh(OtherChart,0,i+k)*UseHigh+iLow(OtherChart,0,i+k)*UseLow)/(UseOpen+UseClose+UseHigh+UseLow);
sx+=x;sy+=y;
}
sx/=n;sy/=n;
cor1=0;cor2=0;cor3=0;
for(k=0; k<n; k++)
{
if((UseOpen+UseClose+UseHigh+UseLow)==0) x=0;
else x=(Open[i+k]*UseOpen+Close[i+k]*UseClose+High[i+k]*UseHigh+Low[i+k]*UseLow)/(UseOpen+UseClose+UseHigh+UseLow);
if((UseOpen+UseClose+UseHigh+UseLow)==0) y=0;
else y=(iOpen(OtherChart,0,i+k)*UseOpen+iClose(OtherChart,0,i+k)*UseClose+iHigh(OtherChart,0,i+k)*UseHigh+iLow(OtherChart,0,i+k)*UseLow)/(UseOpen+UseClose+UseHigh+UseLow);
cor1+=(x-sx)*(y-sy);
cor2+=(x-sx)*(x-sx);
cor3+=(y-sy)*(y-sy);
}
Buffer[i]=cor1/MathSqrt(cor2)/MathSqrt(cor3);
}
//----
return(0);
}
//+------------------------------------------------------------------+
Comments