Correlation2in1
Download scriptThis tool is designed to help traders understand the relationship between different currency pairs by measuring their "correlation." In simple terms, it looks at how two different markets move in relation to one another—whether they tend to move in the same direction at the same time, move in opposite directions, or show no significant connection at all.
How It Works
The script analyzes the price data of four specific currency pairs simultaneously, grouping them into two distinct sets:
- Set 1: Compares the relationship between Pair 1 and Pair 2.
- Set 2: Compares the relationship between Pair 3 and Pair 4.
By looking at these pairings, the tool helps you identify shared market trends. For example, if two different currency pairs are highly correlated, a move in one often signals a likely move in the other.
Key Features
- Dynamic Movement Tracking: Rather than giving you a single static number, the script calculates the correlation across a rolling window of time (defined by the "Period" setting). This allows you to see how the strength of the relationship between these currencies fluctuates over time.
- Dual Visualization: The tool displays two separate lines on your chart simultaneously:
- The first line represents the correlation strength between the first pair of currencies.
- The second line represents the correlation strength between the second pair.
- Statistical Accuracy: The logic uses a standard mathematical approach to calculate correlation coefficients. It evaluates the historical price movements within your chosen timeframe to determine how closely the prices "track" each other.
What This Means for Your Trading
This indicator is primarily used for market analysis and risk management. By observing these correlations, you can:
- Identify Divergences: See when two pairs that usually move together suddenly start moving apart, which could signal a major change in market sentiment.
- Diversify Portfolios: Ensure that the different trades you are taking aren't all tied to the same underlying movement (i.e., making sure you aren't "double-dipping" on the same risk).
- Confirm Trends: Use the correlation of a secondary pair to confirm a move in your primary trade, providing an extra layer of validation for your entries and exits.
//+------------------------------------------------------------------+
//| correlations between Pair1 and Pair2 and between Pair3 and Pair4 |
//| |
//+------------------------------------------------------------------+
#property copyright " "
#property indicator_separate_window
#property indicator_buffers 2
#property indicator_color1 Red
#property indicator_color2 Yellow
extern int R_Period=20;
extern string Pair1="GBPJPY";
extern string Pair2="USDJPY";
extern string Pair3="GBPJPY";
extern string Pair4="GBPUSD";
double R[],Ra[];
double X[],Y[],A[],B[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- indicators
//---- drawing settings
IndicatorShortName(Pair1+"/"+Pair2+" R("+R_Period+")");
SetIndexStyle(0,DRAW_LINE);
SetIndexBuffer(0,R);
SetIndexStyle(1,DRAW_LINE);
SetIndexBuffer(1,Ra );
//SetIndexStyle(2,DRAW_LINE);
//SetIndexBuffer(2,P );
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{
int counted_bars=IndicatorCounted();
//----
double X[],Y[];
int piir, limit, omega ;
double sumxy, sumx, sumx2, sumy, sumy2,sumxya, sumxa, sumx2a, sumya, sumy2a;
double yks, kaks, kolm,yksa, kaksa, kolma;
limit=Bars-counted_bars; //always just 1 bar after first calculation
if(counted_bars==0) limit=Bars-R_Period-1; //first time it should be enough bars to calculate
ArrayResize(X,limit); // min size of array is using less memory
ArrayResize(Y,limit);
ArrayResize(A,limit); // min size of array is using less memory
ArrayResize(B,limit);
ArrayCopySeries(X,MODE_OPEN,Pair1,0);
ArrayCopySeries(Y,MODE_OPEN,Pair2,0);
ArrayCopySeries(A,MODE_OPEN,Pair3,0);
ArrayCopySeries(B,MODE_OPEN,Pair4,0);
omega=0;
while(omega<=limit)
{
sumxy=0; sumx=0; sumx2=0; sumxya=0; sumxa=0; sumx2a=0;
sumy=0; sumy2=0; sumya=0; sumy2a=0;
for(int i=1; i<=R_Period; i++)
{
sumxy+=X[omega+i]*Y[omega+i]; sumxya+=A[omega+i]*B[omega+i];
sumx+=X[omega+i]; sumxa+=A[omega+i];
sumx2+=MathPow(X[omega+i],2.); sumx2a+=MathPow(A[omega+i],2.);//2. is neccesary because MathPow wants both parts as double
sumy+=Y[omega+i]; sumya+=B[omega+i];
sumy2+=MathPow(Y[omega+i],2.); sumy2a+=MathPow(B[omega+i],2.);//but it seems to work with "2" as well
}
yks=sumxy-(sumx*sumy)/R_Period; yksa=sumxya-(sumxa*sumya)/R_Period;
kaks=sumx2-(MathPow(sumx,2.)/R_Period); kaksa=sumx2a-(MathPow(sumxa,2.)/R_Period);
kolm=sumy2-(MathPow(sumy,2.)/R_Period); kolma=sumy2a-(MathPow(sumya,2.)/R_Period);
R[omega]=(yks/MathSqrt(kaks*kolm)); Ra[omega]=(yksa/MathSqrt(kaksa*kolma));
omega++;
}
//----
return(0);
}
//+------------------------------------------------------------------+
Comments