This article provides a complete MQL4 indicator source code that detects RSI divergences automatically. Divergence between price and RSI is one of the most reliable reversal signals in technical analysis. The indicator draws lines on the chart when bullish or bearish divergence is detected.
Indicator Features:
Parameters Explanation:
MQL4 Source Code:
```mql4
//+------------------------------------------------------------------+
//| RSIDivergenceDetector.mq4 |
//| |
//| 自主编译 / Self-compiled |
//+------------------------------------------------------------------+
#property copyright "Forex Trading Tools"
#property link ""
#property version "1.00"
#property strict
#property indicator_chart_window
#property indicator_buffers 0
#property indicator_color1 Red
#property indicator_color2 Green
//--- input parameters
input int RSIPeriod = 14;
input double Overbought = 70.0;
input double Oversold = 30.0;
input int MinBarsBetween = 10;
input bool DrawLines = true;
input bool ShowAlert = true;
input string AlertSound = "alert.wav";
//--- global variables
double rsiVal[];
datetime lastAlertTime = 0;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
IndicatorShortName("RSI Divergence Detector");
SetIndexBuffer(0,rsiVal);
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
ObjectsDeleteAll(0,"Div_");
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
const int prev_calculated,
const datetime &time[],
const double &open[],
const double &high[],
const double &low[],
const double &close[],
const long &tick_volume[],
const long &volume[],
const int &spread[])
{
int limit = rates_total - prev_calculated;
if(limit > 1) limit = rates_total - 2;
// Calculate RSI values
ArrayResize(rsiVal,rates_total);
for(int i=limit; i>=0 && !IsStopped(); i--)
{
rsiVal[i] = iRSI(_Symbol,0,RSIPeriod,PRICE_CLOSE,i);
}
// Detect divergences
for(int i=2; i
// Bullish divergence (price lower low, RSI higher low)
if(low[i] < low[i+1] && rsiVal[i] > rsiVal[i+1] && rsiVal[i] < Oversold)
{
if(DrawLines)
DrawDivLine(i,i+1,"Bullish",Green);
if(ShowAlert && TimeCurrent() - lastAlertTime > 60)
{
Alert("Bullish RSI Divergence detected on ",_Symbol);
lastAlertTime = TimeCurrent();
if(AlertSound != "") PlaySound(AlertSound);
}
}
// Bearish divergence (price higher high, RSI lower high)
if(high[i] > high[i+1] && rsiVal[i] < rsiVal[i+1] && rsiVal[i] > Overbought)
{
if(DrawLines)
DrawDivLine(i,i+1,"Bearish",Red);
if(ShowAlert && TimeCurrent() - lastAlertTime > 60)
{
Alert("Bearish RSI Divergence detected on ",_Symbol);
lastAlertTime = TimeCurrent();
if(AlertSound != "") PlaySound(AlertSound);
}
}
}
return(rates_total);
}
//+------------------------------------------------------------------+
//| Draw divergence trendline |
//+------------------------------------------------------------------+
void DrawDivLine(int idx1, int idx2, string type, color lineColor)
{
string objName = "Div_" + type + "_" + IntegerToString(TimeCurrent()) + "_" + IntegerToString(idx1);
ObjectCreate(0,objName,OBJ_TREND,0,Time[idx1],0,Time[idx2],0);
ObjectSetInteger(0,objName,OBJPROP_COLOR,lineColor);
ObjectSetInteger(0,objName,OBJPROP_STYLE,STYLE_DOT);
ObjectSetInteger(0,objName,OBJPROP_WIDTH,1);
ObjectSetInteger(0,objName,OBJPROP_BACK,false);
ObjectSetInteger(0,objName,OBJPROP_SELECTABLE,false);
ObjectSetInteger(0,objName,OBJPROP_HIDDEN,true);
}
//+------------------------------------------------------------------+
```
Compilation & Installation Guide:
1. Open MetaEditor in MT4 (F4 key)
2. File → New → Custom Indicator → Next
3. Paste the code and press Compile (F7)
4. Attach to chart via Navigator window (Ctrl+N)
5. Adjust parameters in the Inputs tab
How to Read Divergence Signals:
Parameter Tuning Tips:
Reference: 自主编译 / Self-compiled based on classic divergence detection methodology.
For advanced indicator suites including multi-timeframe divergence and automated trading systems, check our premium collection with lifetime updates.