MACD_Divergence_v1
Download scriptTechnical Overview: MACD Divergence Indicator
This script is designed to identify "divergences" between price action and momentum. In trading, a divergence occurs when the price of an asset moves in one direction (making new highs or lows) while a technical indicator moves in the opposite direction. This often signals that the current trend is losing strength and a reversal may be approaching.
Core Components
The script utilizes three primary data streams to analyze the market:
- The MACD Line: This represents the difference between two moving averages (Fast and Slow), showing the momentum of the price.
- The Signal Line: A secondary moving average applied to the MACD line itself, used to smooth out fluctuations.
- The Histogram: The visual representation of the distance between the MACD Line and the Signal Line.
How the Logic Works
The script automatically scans historical data to find specific patterns where price behavior contradicts momentum:
1. Identifying Highs and Lows The tool looks for two distinct points in history:
- Primary Point: A significant peak or valley in price.
- Secondary Point: A subsequent peak or valley that is compared against the first.
2. Detecting Divergence Patterns The logic filters through historical candles to find "Lower Highs" or "Higher Lows" relative to price, while simultaneously checking the MACD values:
- Bearish Divergence: If the market creates a new high (or a high near the previous one) but the MACD indicator shows a lower peak than before, the script identifies this as a potential sign of weakening bullish momentum.
- Bullish Divergence: If the market creates a new low (or a low near the previous one) but the MACD indicator shows a higher bottom than before, it suggests that selling pressure is fading despite the lower prices.
3. Visual Mapping and Trendlines Once the script identifies these two points of interest, it performs two specific visual actions:
- Price Chart Mapping: It draws a trendline directly on your price candles connecting the actual highs or lows.
- Indicator Window Mapping: It simultaneously draws a corresponding trendline on the MACD indicator window.
By overlaying these lines on both the price chart and the indicator window, the tool allows traders to visually confirm when the "push" behind a move is disappearing, even if the price hasn't turned around yet.
Key Parameters
- Fast/Slow EMA & Signal SMA: These allow you to adjust the sensitivity of the MACD calculation (standard settings are 12, 26, and 9).
- DivSpread: This acts as a "buffer" or tolerance zone. It helps the script decide how close a new high or low must be to the previous one to qualify for a divergence calculation.
- ShowComments: If enabled, this provides a data readout in the corner of your screen, showing the raw numerical values being used for the calculations.
//+------------------------------------------------------------------+
//| MACD_Divergence.mq4 |
//| Copyright © 2006, Cartwright Software Corp. |
//| http://www.cartwright.net |
//| Author: Randy Cartwright |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2006, Cartwright Software Corp."
#property link "http://www.cartwright.net"
//----
#property indicator_separate_window
#property indicator_buffers 3
#property indicator_color1 Teal //Aqua
#property indicator_color2 Red
#property indicator_color3 Blue //DodgerBlue //Silver
//---- input parameters
extern int shift=1, DivSpread=15;
extern bool ShowComments=false;
extern int FastEMA=12;
extern int SlowEMA=26;
extern int SignalSMA=9;
//----
double macdHigh1, macdHigh2, val3, val4;
double macdLow1, macdLow2;
//---- buffers
double ExtMapBuffer3[];
//---- indicator buffers
double ExtSilverBuffer[];
double ExtRedBuffer[];
double ExtHistoryBuffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- drawing settings
SetIndexStyle(0,DRAW_LINE);
SetIndexStyle(1,DRAW_LINE);
SetIndexStyle(2,DRAW_HISTOGRAM);
SetIndexBuffer(2,ExtMapBuffer3);
//----
SetIndexDrawBegin(1,SignalSMA);
IndicatorDigits(5);
//---- indicator buffers mapping
SetIndexBuffer(0, ExtSilverBuffer);
SetIndexBuffer(1, ExtRedBuffer);
SetIndexBuffer(2, ExtHistoryBuffer);
//---- name for DataWindow and indicator subwindow label
IndicatorShortName("Randy_Cs_MACD("+FastEMA+","+SlowEMA+","+SignalSMA+")");
//---- initialization done
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
ObjectDelete("MACD_HL");
ObjectDelete("MACD_LL");
ObjectDelete("Chart_HL");
ObjectDelete("Chart_LL");
//----
return(0);
}
//+------------------------------------------------------------------+
//| Moving Averages Convergence/Divergence |
//+------------------------------------------------------------------+
int start()
{
int limit;
int counted_bars=IndicatorCounted();
//---- check for possible errors
if(counted_bars<0) return(-1);
//---- last counted bar will be recounted
if(counted_bars>0) counted_bars--;
limit=Bars-counted_bars;
//---- macd counted in the 1-st buffer
for(int i=0; i<limit; i++)
ExtSilverBuffer[i]=iMA(NULL,0,FastEMA,0,MODE_EMA,PRICE_CLOSE,i)-iMA(NULL,0,SlowEMA,0,MODE_EMA,PRICE_CLOSE,i);
//---- signal line counted in the 2-nd buffer
for(i=0; i<limit; i++)
ExtRedBuffer[i]=iMAOnArray(ExtSilverBuffer,Bars,SignalSMA,0,MODE_SMA,i);
for(i=0; i<limit; i++)
ExtHistoryBuffer[i]=iMA(NULL,0,FastEMA,0,MODE_EMA,PRICE_CLOSE,i)-iMA(NULL,0,SlowEMA,0,MODE_EMA,PRICE_CLOSE,i) - iMAOnArray(ExtSilverBuffer,Bars,SignalSMA,0,MODE_SMA,i);
/*
macdHigh1=iMA(NULL,0,FastEMA,0,MODE_EMA,PRICE_CLOSE,20)-iMA(NULL,0,SlowEMA,0,MODE_EMA,PRICE_CLOSE,20); //Teal__ExtSilverBuffer
macdHigh2=iMAOnArray(ExtSilverBuffer,Bars,SignalSMA,0,MODE_SMA,20); //Red Line ExtRedBuffer
val3=iMA(NULL,0,FastEMA,0,MODE_EMA,PRICE_CLOSE,10)-iMA(NULL,0,SlowEMA,0,MODE_EMA,PRICE_CLOSE,10) - iMAOnArray(ExtSilverBuffer,Bars,SignalSMA,0,MODE_SMA,10);
*/
string timeFrame=0; //PERIOD_M15
int firstHighTime, firstLowTime;
int secondHighTime, secondLowTime;
double firstHigh, secondHigh, firstLow, secondLow;
bool findsecondHigh=false, findsecondHighCompleted=false, findsecondLow=false, findsecondLowCompleted=false;
bool findsecondHighLow=false, findsecondLowHigh=false;
//----
firstHighTime=iTime(NULL,timeFrame, shift);
firstHigh=iHigh(NULL,timeFrame,shift);// + iLow(NULL,timeFrame,shift)) /2;
firstLow=iLow(NULL,timeFrame,shift);// + iLow(NULL,timeFrame,shift)) /2;
firstLowTime=iTime(NULL,timeFrame, shift);
//macdPrice
macdHigh1=iMA(NULL,timeFrame,FastEMA,0,MODE_EMA,PRICE_CLOSE,shift)-iMA(NULL,timeFrame,SlowEMA,0,MODE_EMA,PRICE_CLOSE,shift);
macdLow1=iMA(NULL,timeFrame,FastEMA,0,MODE_EMA,PRICE_CLOSE,shift)-iMA(NULL,timeFrame,SlowEMA,0,MODE_EMA,PRICE_CLOSE,shift);
//----
int candleHigh, candleLow;
double high, low;
//---
for(i=shift+1; i<Bars; i++)
{ //limit
// High Divergence
high=iHigh(NULL,timeFrame,i);// + iLow(NULL,timeFrame,i)) /2;
if (findsecondHigh==false && findsecondHighLow==false && high + DivSpread*Point <=firstHigh) //&& findsecondHigh==false
findsecondHigh=true;
if (findsecondHigh==true && findsecondHighCompleted==false && high>=firstHigh)
{
secondHigh=iHigh(NULL,timeFrame,i);// + iLow(NULL,timeFrame,i)) /2;
secondHighTime=iTime(NULL,timeFrame, i);
macdHigh2=iMA(NULL,timeFrame,FastEMA,0,MODE_EMA,PRICE_CLOSE,i)-iMA(NULL,timeFrame,SlowEMA,0,MODE_EMA,PRICE_CLOSE,i);
findsecondHighCompleted=true;
}
if (findsecondHigh==false && findsecondHighLow==false && high - DivSpread*Point >=firstHigh) //&& findsecondHigh==false
{findsecondHighLow=true;}
if (findsecondHighLow==true && findsecondHighCompleted==false && high<=firstHigh)
{
secondHigh=iHigh(NULL,timeFrame,i);// + iLow(NULL,timeFrame,i)) /2;
secondHighTime=iTime(NULL,timeFrame, i);
macdHigh2=iMA(NULL,timeFrame,FastEMA,0,MODE_EMA,PRICE_CLOSE,i)-iMA(NULL,timeFrame,SlowEMA,0,MODE_EMA,PRICE_CLOSE,i);
findsecondHighCompleted=true;
}
// Low Divergence
low=iLow(NULL,timeFrame,i);// + iLow(NULL,timeFrame,i)) /2;
if (findsecondLow==false && findsecondLowHigh==false && low - DivSpread*Point >=firstLow) //&& findsecondLow==false
findsecondLow=true;
//else
if (findsecondLow==false && findsecondLowHigh==false && low + DivSpread*Point <=firstLow) //&& findsecondHigh==false
{findsecondLowHigh=true;}
if (findsecondLow==true && findsecondLowCompleted==false && low<=firstLow) { // || high <= firstLow) {
secondLow=iLow(NULL,timeFrame,i);// + iLow(NULL,timeFrame,i)) /2;
secondLowTime=iTime(NULL,timeFrame, i);
macdLow2=iMA(NULL,timeFrame,FastEMA,0,MODE_EMA,PRICE_CLOSE,i)-iMA(NULL,timeFrame,SlowEMA,0,MODE_EMA,PRICE_CLOSE,i);
findsecondLowCompleted=true;
}
//else
if (findsecondLowHigh==true && findsecondLowCompleted==false && low>=firstLow) { // || high >= firstLow) {
secondLow=iLow(NULL,timeFrame,i);// + iLow(NULL,timeFrame,i)) /2;
secondLowTime=iTime(NULL,timeFrame, i);
macdLow2=iMA(NULL,timeFrame,FastEMA,0,MODE_EMA,PRICE_CLOSE,i)-iMA(NULL,timeFrame,SlowEMA,0,MODE_EMA,PRICE_CLOSE,i);
findsecondLowCompleted=true;
}
if (findsecondHighCompleted==true && findsecondLowCompleted==true) break;
} // END FOR
// MACD High Trendline
ObjectDelete("MACD_HL");
ObjectCreate("MACD_HL",OBJ_TREND,1,secondHighTime,macdHigh2,firstHighTime,macdHigh1);//secondLowTime,secondLow,firstLowTime,firstLow);
ObjectSet("MACD_HL",OBJPROP_COLOR,Lime); //Goldenrod
ObjectSet("MACD_HL",OBJPROP_WIDTH, 3);
ObjectSet("MACD_HL",OBJPROP_RAY, false);
// Chart High Trendline
ObjectDelete("Chart_HL");
ObjectCreate("Chart_HL",OBJ_TREND,0,secondHighTime,secondHigh,firstHighTime,firstHigh);//secondHighTime,secondHigh,firstHighTime,firstHigh);
ObjectSet("Chart_HL",OBJPROP_COLOR,Lime);
ObjectSet("Chart_HL",OBJPROP_WIDTH, 3);
ObjectSet("Chart_HL",OBJPROP_RAY, false);
// MACD Low Trendline
ObjectDelete("MACD_LL");
ObjectCreate("MACD_LL",OBJ_TREND,1,secondLowTime,macdLow2,firstLowTime,macdLow1);//secondLowTime,secondLow,firstLowTime,firstLow);
ObjectSet("MACD_LL",OBJPROP_COLOR,Red); //Goldenrod
ObjectSet("MACD_LL",OBJPROP_WIDTH, 3);
ObjectSet("MACD_LL",OBJPROP_RAY, false);
// Chart Low Trendline
ObjectDelete("Chart_LL");
ObjectCreate("Chart_LL",OBJ_TREND,0,secondLowTime,secondLow,firstLowTime,firstLow);//secondHighTime,secondHigh,firstHighTime,firstHigh);
ObjectSet("Chart_LL",OBJPROP_COLOR,Red);
ObjectSet("Chart_LL",OBJPROP_WIDTH, 3);
ObjectSet("Chart_LL",OBJPROP_RAY, false);
ObjectSet("Chart_LL",OBJPROP_BACK, false);
/*
ObjectSet("MACD_HL",OBJPROP_TIME1,firstLowTime); ObjectSet("MACD_HL",OBJPROP_TIME2,secondLowTime);
ObjectSet("MACD_HL",OBJPROP_PRICE1,firstLow); ObjectSet("MACD_HL",OBJPROP_PRICE2,secondLow);
ObjectSet("MACD_HL",OBJPROP_STYLE, STYLE_DOT);
ObjectSet("MACD_HL",OBJPROP_TIME1,firstLowTime); ObjectSet("MACD_HL",OBJPROP_TIME2,secondLowTime);
ObjectSet("MACD_HL",OBJPROP_PRICE1,firstLowPrice);ObjectSet("MACD_HL",OBJPROP_PRICE2,secondLowPrice);
ObjectsRedraw();
*/
if (ShowComments==true)
{
Comment("macdHigh1 = ",DoubleToStr(macdHigh1,6)," macdHigh2 = ",DoubleToStr(macdHigh2,6), " val3 = ",
DoubleToStr(val3,5), " Windows count = ", WindowsTotal(),
" MACD Win =",WindowFind("BillWin_MACD("+FastEMA+","+SlowEMA+","+SignalSMA+")")
, " MA = ",WindowFind("RandyTest"), " ExtHistoryBuffer[i] = ",ExtHistoryBuffer[i],"\n",
"firstHigh = ",firstHigh, " secondHigh = ",secondHigh,"\n",
" firstHighTime = ",firstHighTime, " secondHighTime = ",secondHighTime,"\n",
"candleHigh = ",candleHigh, " candleLow = ",candleLow);
}
//---- done
return(0);
}
//+------------------------------------------------------------------+?
Comments