This script is a specialized technical analysis tool designed to identify "divergences"—a phenomenon where the price movement of an asset (like a currency pair) disagrees with the momentum shown by a moving average indicator. By detecting these discrepancies, the tool aims to highlight potential trend reversals or shifts in market strength.
Core Logic and Functionality
1. Customized Momentum Calculation The script begins by calculating a modified version of the MACD (Moving Average Convergence Divergence). Instead of using standard formulas, it applies specific weighted averages to price data to create a custom momentum line. From this, it derives a "Signal" line. The difference between these two creates an OsMA (Oscillator Moving Average). This oscillator serves as the primary heartbeat for the script, showing the strength and direction of the current trend.
2. Identifying Peaks and Troughs To detect divergences, the script must first understand the "landscape" of the market. It systematically scans the history to identify significant high points (peaks) and low points (troughs) in the oscillator. It looks for specific structural changes where the momentum shifts from positive to negative or vice versa, ensuring it only focuses on meaningful turns rather than minor fluctuations.
3. Divergence Detection Logic The primary purpose of the script is to compare two different types of data: Actual Market Price vs. Oscillator Momentum. It looks for four specific scenarios:
- Classic Bearish Divergence: The market price makes a new high, but the oscillator fails to reach a previous high. This suggests that while the price is rising, the "fuel" behind the move is weakening, potentially signaling a coming drop.
- Reverse Bearish Divergence: The market price makes a lower high, but the oscillator reaches a higher peak. This indicates unusual strength in momentum despite falling prices, often signaling a trend shift.
- Classic Bullish Divergence: The market price makes a new low, but the oscillator fails to reach a previous low. This suggests the selling pressure is exhausting, potentially signaling an upcoming rise.
- Reverse Bullish Divergence: The market price makes a higher low, but the oscillator reaches a lower trough. This indicates significant momentum building despite rising prices, often signaling a shift in trend.
4. Visual Feedback and Alerts When the script identifies any of these four conditions, it provides immediate feedback to the user:
- Visual Markers: It places specific arrows on the chart to point out exactly where a divergence has occurred.
- Trend Lines: It automatically draws lines connecting the price peaks/troughs and the corresponding oscillator points. This allows you to see the visual "gap" between price action and momentum clearly.
- Real-Time Notifications: The script can trigger an instant alert on your platform the moment a divergence is confirmed on a new price bar, allowing for quicker reaction times.
Summary for the User
Essentially, this tool acts as a "truth checker" for the market. It tells you when the price movement of an asset is no longer supported by the underlying momentum, helping traders identify potential turning points before they become obvious to the naked eye.
//+------------------------------------------------------------------+
//| FX5_MACD_Correct.mq4 |
//| FX5 |
//| hazem@uk2.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2007, FX5"
#property link "hazem@uk2.net"
//----
#property indicator_separate_window
#property indicator_buffers 5
#property indicator_color1 LimeGreen
#property indicator_color2 FireBrick
#property indicator_color3 Green
#property indicator_color4 Red
//---- input parameters
extern string separator1 = "*** MACD Settings ***";
extern int fastEMA = 5;
extern int slowEMA = 34;
extern int signal = 5;
extern string separator2 = "*** Indicator Settings ***";
extern double positiveSensitivity = 0.0001;
extern double negativeSensitivity = -0.0001;
extern double historyBarsCount = 0;
extern bool drawDivergenceLines = true;
extern bool displayAlert = true;
//---- buffers
double upOsMA[];
double downOsMA[];
double bullishDivergence[];
double bearishDivergence[];
double OsMA[];
double MACD[];
double Signal[];
double alertBuffer[];
//----
int chartBarsCount;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- indicators
IndicatorBuffers(8);
SetIndexStyle(0, DRAW_HISTOGRAM, STYLE_SOLID, 2);
SetIndexBuffer(0, upOsMA);
SetIndexStyle(1, DRAW_HISTOGRAM, STYLE_SOLID, 2);
SetIndexBuffer(1, downOsMA);
SetIndexStyle(2, DRAW_ARROW);
SetIndexBuffer(2, bullishDivergence);
SetIndexArrow(2, 233);
SetIndexStyle(3, DRAW_ARROW);
SetIndexBuffer(3, bearishDivergence);
SetIndexArrow(3, 234);
SetIndexStyle(4, DRAW_NONE);
SetIndexBuffer(4, OsMA);
// additional buffers
SetIndexBuffer(5, MACD);
SetIndexBuffer(6, Signal);
SetIndexBuffer(7, alertBuffer);
//----
SetIndexDrawBegin(0, signal);
SetIndexDrawBegin(1, signal);
//----
IndicatorDigits(Digits + 2);
IndicatorShortName("FX5_MACD_Correct(" + fastEMA + "," + slowEMA + "," + signal + ")");
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
for(int i = ObjectsTotal() - 1; i >= 0; i--)
{
string label = ObjectName(i);
if(StringSubstr(label, 0, 14) != "DivergenceLine")
continue;
ObjectDelete(label);
}
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{
if(historyBarsCount <= 0 || historyBarsCount > Bars)
chartBarsCount = Bars;
else
chartBarsCount = historyBarsCount;
int countedBars = IndicatorCounted();
if(countedBars < 0)
countedBars = 0;
CalculateOsMA(countedBars);
CalculateDivergence(countedBars);
return(0);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void CalculateDivergence(int countedBars)
{
double arrowSeparation = 1 / MathPow(10, Digits + 2) * 50;
for(int i = chartBarsCount - countedBars; i >= 0; i--)
{
bearishDivergence[i] = EMPTY_VALUE;
bullishDivergence[i] = EMPTY_VALUE;
//----
int firstPeakOrTroughShift = GetFirstPeakOrTrough(i);
double firstPeakOrTroughOsMA = OsMA[firstPeakOrTroughShift];
if(firstPeakOrTroughOsMA > 0)
{
int peak_0 = GetIndicatorLastPeak(i);
int trough_0 = GetIndicatorLastTrough(peak_0);
int peak_1 = GetIndicatorLastPeak(trough_0);
int trough_1 = GetIndicatorLastTrough(peak_1);
}
else
{
trough_0 = GetIndicatorLastTrough(i);
peak_0 = GetIndicatorLastPeak(trough_0);
trough_1 = GetIndicatorLastTrough(peak_0);
peak_1 = GetIndicatorLastPeak(trough_1);
}
if(peak_0 == -1 || peak_1 == -1 || trough_0 == -1 || trough_1 == -1)
continue;
double indicatorLastPeak = OsMA[peak_0];
double indicatorThePeakBefore = OsMA[peak_1];
double indicatorLastTrough = OsMA[trough_0];
double indicatorTheTroughBefore = OsMA[trough_1];
/*
int pricePeak_0 = GetPriceLastPeak(peak_0);
int pricePeak_1 = GetPriceLastPeak(peak_1);
int priceTrough_0 = GetPriceLastTrough(trough_0);
int priceTrough_1 = GetPriceLastTrough(trough_1);
*/
int pricePeak_0 = peak_0;
int pricePeak_1 = peak_1;
int priceTrough_0 = trough_0;
int priceTrough_1 = trough_1;
//----
double priceLastPeak = High[pricePeak_0];
double priceThePeakBefore = High[pricePeak_1];
double priceLastTrough = Low[priceTrough_0];
double priceTheTroughBefore = Low[priceTrough_1];
// Classic bearish divergence condition
if(priceLastPeak > priceThePeakBefore &&
indicatorLastPeak < indicatorThePeakBefore)
{
DisplayAlert("Classic bearish divergence on: ", i, pricePeak_0);
bearishDivergence[peak_0] = upOsMA[peak_0] + arrowSeparation;
if(drawDivergenceLines)
{
PriceDrawLine(Time[pricePeak_0], Time[pricePeak_1],
priceLastPeak, priceThePeakBefore,
Red, STYLE_SOLID);
IndicatorDrawLine(Time[peak_0], Time[peak_1],
indicatorLastPeak, indicatorThePeakBefore,
Red, STYLE_SOLID);
}
continue;
}
// Reverse bearsih divergence condition
if(priceLastPeak < priceThePeakBefore && indicatorLastPeak > indicatorThePeakBefore)
{
DisplayAlert("Reverse bearish divergence on: ", i, pricePeak_0);
bearishDivergence[peak_0] = upOsMA[peak_0] + arrowSeparation;
if(drawDivergenceLines)
{
PriceDrawLine(Time[pricePeak_0], Time[pricePeak_1], priceLastPeak,
priceThePeakBefore, Red, STYLE_DOT);
IndicatorDrawLine(Time[peak_0], Time[peak_1], indicatorLastPeak,
indicatorThePeakBefore, Red, STYLE_DOT);
}
continue;
}
// Classic bullish divergence condition
if(priceLastTrough < priceTheTroughBefore &&
indicatorLastTrough > indicatorTheTroughBefore)
{
DisplayAlert("Classic bullish divergence on: ", i, priceTrough_0);
bullishDivergence[trough_0] = downOsMA[trough_0] - arrowSeparation;
if(drawDivergenceLines)
{
PriceDrawLine(Time[priceTrough_0], Time[priceTrough_1], priceLastTrough,
priceTheTroughBefore, Green, STYLE_SOLID);
IndicatorDrawLine(Time[trough_0], Time[trough_1], indicatorLastTrough,
indicatorTheTroughBefore, Green, STYLE_SOLID);
}
continue;
}
// Hidden bullish divergence condition
if(priceLastTrough > priceTheTroughBefore &&
indicatorLastTrough < indicatorTheTroughBefore)
{
DisplayAlert("Reverse bullish divergence on: ", i, priceTrough_0);
bullishDivergence[trough_0] = downOsMA[trough_0] - arrowSeparation;
if(drawDivergenceLines)
{
PriceDrawLine(Time[priceTrough_0], Time[priceTrough_1], priceLastTrough,
priceTheTroughBefore, Green, STYLE_DOT);
IndicatorDrawLine(Time[trough_0], Time[trough_1], indicatorLastTrough,
indicatorTheTroughBefore, Green, STYLE_DOT);
}
continue;
}
}
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void DisplayAlert(string message, int barIndex, int signalIndex)
{
if(displayAlert == true && barIndex == 0 && alertBuffer[signalIndex] != 1)
{
Alert(message, Symbol(), " , at: ", Bid);
alertBuffer[signalIndex] = 1;
}
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void CalculateOsMA(int countedBars)
{
for(int i = Bars - countedBars; i >= 0; i--)
{
//double shortema = (0.15*iClose(Symbol(),0,i)) + (0.85*iMA(NULL, 0, fastEMA, 0, MODE_EMA, PRICE_CLOSE, i+1));
MACD[i]=(0.15*iClose(Symbol(),0,i))+(0.85*iMA(Symbol(),0,fastEMA,0,MODE_EMA,PRICE_CLOSE,i+1)) -
(0.075*iClose(Symbol(),0,i))+(0.925*iMA(Symbol(),0,slowEMA,0,MODE_EMA,PRICE_CLOSE,i+1));
}
for(i = Bars - countedBars; i >= 0; i--)
{
Signal[i]=iMAOnArray(MACD, Bars, signal, 0, MODE_SMA, i);
OsMA[i] = MACD[i] - Signal[i];
//----
if (OsMA[i] > 0)
{
upOsMA[i] = OsMA[i];
downOsMA[i] = 0;
}
else
if(OsMA[i] < 0)
{
downOsMA[i] = OsMA[i];
upOsMA[i] = 0;
}
else
{
upOsMA[i] = 0;
downOsMA[i] = 0;
}
}
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int GetPositiveRegionStart(int index)
{
int regionStart;
for(int i = index + 1; i < Bars; i++)
{
//if(OsMA[i] >= OsMA[i-1] && OsMA[i] >= OsMA[i+1] &&
if(OsMA[i] >= OsMA[i+1] &&
OsMA[i] >= OsMA[i+2] && OsMA[i] > positiveSensitivity)
return(i);
}
return(-1);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int GetNegativeRegionStart(int index)
{
for(int i = index + 1; i < Bars; i++)
{
//if(OsMA[i] <= OsMA[i-1] && OsMA[i] <= OsMA[i+1] &&
if(OsMA[i] <= OsMA[i+1] &&
OsMA[i] <= OsMA[i+2] && OsMA[i] < negativeSensitivity)
return(i);
}
return(-1);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int GetFirstPeakOrTrough(int index)
{
for(int i = index + 1; i < Bars; i++)
{
if (//(OsMA[i] >= OsMA[i-1] && OsMA[i] >= OsMA[i+1] &&
(OsMA[i] >= OsMA[i+1] &&
OsMA[i] >= OsMA[i+2] && OsMA[i] > positiveSensitivity) ||
//(OsMA[i] <= OsMA[i-1] && OsMA[i] <= OsMA[i+1] &&
(OsMA[i] <= OsMA[i+1] &&
OsMA[i] <= OsMA[i+2] && OsMA[i] < negativeSensitivity))
return(i);
}
return(-1);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int GetIndicatorLastPeak(int index)
{
int regionStart = GetPositiveRegionStart(index);
//----
if(regionStart == -1)
return(-1);
int peakShift = 0;
double peakValue = 0;
//----
for(int i = regionStart; i < Bars; i++)
{
//if(OsMA[i] > peakValue && OsMA[i] >= OsMA[i-1] &&
if(OsMA[i] > peakValue &&
OsMA[i] >= OsMA[i+1] && OsMA[i] >= OsMA[i+2] &&
OsMA[i] > positiveSensitivity)
{
peakValue = OsMA[i];
peakShift = i;
}
if(OsMA[i] < 0)
break;
}
return(peakShift);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int GetIndicatorLastTrough(int index)
{
int regionStart = GetNegativeRegionStart(index);
//----
if(regionStart == -1)
return(-1);
int troughShift = 0;
double troughValue = 0;
//----
for(int i = regionStart; i < Bars; i++)
{
//if(OsMA[i] < troughValue && OsMA[i] <= OsMA[i-1] &&
if(OsMA[i] < troughValue &&
OsMA[i] <= OsMA[i+1] && OsMA[i] <= OsMA[i+2] &&
OsMA[i] < negativeSensitivity)
{
troughValue = OsMA[i];
troughShift = i;
}
if(OsMA[i] > 0)
break;
}
return(troughShift);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void PriceDrawLine(datetime x1, datetime x2, double y1, double y2,
color lineColor, double style)
{
string label = "DivergenceLine# " + DoubleToStr(x1, 0);
ObjectDelete(label);
ObjectCreate(label, OBJ_TREND, 0, x1, y1, x2, y2, 0, 0);
ObjectSet(label, OBJPROP_RAY, 0);
ObjectSet(label, OBJPROP_COLOR, lineColor);
ObjectSet(label, OBJPROP_STYLE, style);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void IndicatorDrawLine(datetime x1, datetime x2, double y1, double y2,
color lineColor, double style)
{
int indicatorWindow = WindowFind("FX5_MACD_Correct(" + fastEMA + "," +
slowEMA + "," + signal + ")");
if(indicatorWindow < 0)
return;
string label = "DivergenceLine$# " + DoubleToStr(x1, 0);
ObjectDelete(label);
ObjectCreate(label, OBJ_TREND, indicatorWindow, x1, y1, x2, y2, 0, 0);
ObjectSet(label, OBJPROP_RAY, 0);
ObjectSet(label, OBJPROP_COLOR, lineColor);
ObjectSet(label, OBJPROP_STYLE, style);
}
//+------------------------------------------------------------------+
Comments