Code Library / MACD
MQL5 MACD Divergence
Scan for MACD divergences.
Language: MQL5 · Category: MACD · Tags: MACD, divergence, MQL5, scanner, reversal
Source code
enum DIV {NONE,BULL,BEAR};
DIV CheckDiv(int lookback) {
double m[],l[];
int h=iMACD(_Symbol,PERIOD_CURRENT,12,26,9,PRICE_CLOSE);
CopyBuffer(h,0,0,lookback,m); CopyLow(_Symbol,PERIOD_CURRENT,0,lookback,l);
int p1=0,p2=0;
for(int i=2;i<lookback-2;i++)
if(l[i]<l[i-1]&&l[i]<l[i+1]){if(p1==0)p1=i;else{p2=i;break;}}
if(p1>0&&p2>0&&l[p1]<l[p2]&&m[p1]>m[p2]){IndicatorRelease(h);return BULL;}
IndicatorRelease(h); return NONE;
}How to use this snippet
This is a building block, not a finished trading system. Paste it into your own MQL5 project, compile it, and test it on a demo account with your broker's real spreads before relying on it. If MetaEditor reports errors, the compilation error fixer can help.