PairDifference
Download scriptThis tool is designed to help traders visualize the relative performance and price momentum of different currency pairs simultaneously on a single chart. Rather than looking at individual prices, this script calculates a specific percentage-based difference, allowing you to compare how various markets are moving in relation to one another.
Core Functionality
The primary goal of this script is to isolate "movement" from absolute price values. It does this by calculating a coefficient for up to eight different currency pairs. Each coefficient represents the percentage change between a short-term moving average and a longer-term moving average.
How the Logic Works
To provide a clean, comparable view of multiple assets, the script follows these logical steps:
- Dual Moving Average Comparison: For every currency pair you select, the tool looks at two different points in time: a very recent price trend (the "Short" period) and a much older price trend (the "Neutral" period).
- Percentage Calculation: Instead of simply subtracting one price from another, the script calculates the percentage difference between these two averages. This normalization is crucial because it allows you to compare high-value assets (like the EUR/USD) directly against lower-value assets on the same scale.
- Inversion Capability: The tool includes a toggle to "invert" the data for specific pairs. This is useful if you want to track an inverse relationship—for example, if you want to see a pair move in the opposite direction of your primary focus to identify hedging opportunities or opposing market sentiments.
- Baseline Subtraction: To ensure all lines on your chart are relative to each other, the script identifies a "base" value (the current chart's own movement) and subtracts it from all other pairs. This centers the data, making it easier to see which pairs are outperforming or underperforming the main pair you are currently viewing.
Visual Representation
The result is a series of lines displayed in a separate window below your main price chart. Each line represents one of the pairs you have configured. By observing these lines together, you can quickly identify:
- Correlation: Which pairs are moving in harmony with each other.
- Divergence: Which pairs are breaking away from the norm, potentially signaling a shift in market sentiment.
- Relative Strength: Which assets are gaining momentum faster than others over your chosen timeframes.
By using this logic, the tool simplifies complex multi-pair analysis into a single visual summary, helping you spot market trends that might be difficult to see by switching between individual charts.
//+------------------------------------------------------------------+
//| PairDifference.mq4 |
//| |
//| |
//+------------------------------------------------------------------+
#property copyright "Created by RoBiK"
#property link ""
#property indicator_separate_window
#property indicator_buffers 8
#property indicator_color1 Red
#property indicator_color2 Green
#property indicator_color3 Blue
#property indicator_color4 White
#property indicator_color5 Yellow
#property indicator_color6 Purple
#property indicator_color7 Gray
#property indicator_color8 Olive
//---- input parameters
extern int NeutralPeriod = 48;
extern int MaxPeriod = 960;
extern int MAPeriod = 12;
extern int MAMethod = MODE_EMA;
extern int MAPrice = PRICE_CLOSE;
extern string Pair1 = "EURUSD";
extern bool Inverted1 = false;
extern string Pair2;
extern bool Inverted2 = false;
extern string Pair3;
extern bool Inverted3 = false;
extern string Pair4;
extern bool Inverted4 = false;
extern string Pair5;
extern bool Inverted5 = false;
extern string Pair6;
extern bool Inverted6 = false;
extern string Pair7;
extern bool Inverted7 = false;
extern string Pair8;
extern bool Inverted8 = false;
//---- buffers
double b1[];
double b2[];
double b3[];
double b4[];
double b5[];
double b6[];
double b7[];
double b8[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- indicators
IndicatorShortName("PD");
if (StringLen(Pair1) > 0)
{
SetIndexStyle(0,DRAW_LINE);
SetIndexBuffer(0,b1);
SetIndexDrawBegin(0, Bars - MaxPeriod);
SetIndexLabel(0, Pair1);
}
else SetIndexStyle(0,DRAW_NONE);
if (StringLen(Pair2) > 0)
{
SetIndexStyle(1,DRAW_LINE);
SetIndexBuffer(1,b2);
SetIndexDrawBegin(1, Bars - MaxPeriod);
SetIndexLabel(1, Pair2);
}
else SetIndexStyle(1,DRAW_NONE);
if (StringLen(Pair3) > 0)
{
SetIndexStyle(2,DRAW_LINE);
SetIndexBuffer(2,b3);
SetIndexDrawBegin(2, Bars - MaxPeriod);
SetIndexLabel(2, Pair3);
}
else SetIndexStyle(2,DRAW_NONE);
if (StringLen(Pair4) > 0)
{
SetIndexStyle(3,DRAW_LINE);
SetIndexBuffer(3,b4);
SetIndexDrawBegin(3, Bars - MaxPeriod);
SetIndexLabel(3, Pair4);
}
else SetIndexStyle(3,DRAW_NONE);
if (StringLen(Pair5) > 0)
{
SetIndexStyle(4,DRAW_LINE);
SetIndexBuffer(4,b5);
SetIndexDrawBegin(4, Bars - MaxPeriod);
SetIndexLabel(4, Pair5);
}
else SetIndexStyle(4,DRAW_NONE);
if (StringLen(Pair6) > 0)
{
SetIndexStyle(5,DRAW_LINE);
SetIndexBuffer(5,b6);
SetIndexDrawBegin(5, Bars - MaxPeriod);
SetIndexLabel(5, Pair6);
}
else SetIndexStyle(5,DRAW_NONE);
if (StringLen(Pair7) > 0)
{
SetIndexStyle(6,DRAW_LINE);
SetIndexBuffer(6,b7);
SetIndexDrawBegin(6, Bars - MaxPeriod);
SetIndexLabel(6, Pair7);
}
else SetIndexStyle(6,DRAW_NONE);
if (StringLen(Pair8) > 0)
{
SetIndexStyle(7,DRAW_LINE);
SetIndexBuffer(7,b8);
SetIndexDrawBegin(7, Bars - MaxPeriod);
SetIndexLabel(7, Pair8);
}
else SetIndexStyle(7,DRAW_NONE);
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{
if (Bars < MaxPeriod) return(0);
//----
for (int i = 0; i < MaxPeriod; i++)
{
double c = Coeficient(NULL, false, i);
if (StringLen(Pair1) > 0) b1[i] = Coeficient(Pair1, Inverted1, i) - c;
if (StringLen(Pair2) > 0) b2[i] = Coeficient(Pair2, Inverted2, i) - c;
if (StringLen(Pair3) > 0) b3[i] = Coeficient(Pair3, Inverted3, i) - c;
if (StringLen(Pair4) > 0) b4[i] = Coeficient(Pair4, Inverted4, i) - c;
if (StringLen(Pair5) > 0) b5[i] = Coeficient(Pair5, Inverted5, i) - c;
if (StringLen(Pair6) > 0) b6[i] = Coeficient(Pair6, Inverted6, i) - c;
if (StringLen(Pair7) > 0) b7[i] = Coeficient(Pair7, Inverted7, i) - c;
if (StringLen(Pair8) > 0) b8[i] = Coeficient(Pair8, Inverted8, i) - c;
}
//----
return(0);
}
//+------------------------------------------------------------------+
double Coeficient(string pair, bool inverted, int shift)
{
double c1, c2;
c1 = iMA(pair, 0, MAPeriod, shift, MAMethod, MAPrice, 0);
c2 = iMA(pair, 0, MAPeriod, NeutralPeriod, MAMethod, MAPrice, 0);
if (c1 == c2) return(0);
if (inverted)
{
c1 = 1 / c1;
c2 = 1 / c2;
}
return (100 * (c1 - c2)/ c2);
}
Comments