This tutorial delivers a ready-to-use MQL5 indicator source code that detects both Regular and Hidden divergences on the RSI oscillator. Divergence trading is a cornerstone of price action and momentum analysis, making this tool invaluable for manual or semi-automated strategies.
Why an RSI Divergence Indicator?
Most free indicators only mark basic peaks. Our version plots clear trendlines directly on the RSI sub-window, distinguishes divergence types, and sends alerts. It's perfect for traders looking for a free MT5 indicator download with advanced features.
Complete MQL5 Source Code
```cpp
//+------------------------------------------------------------------+
//| RSI_Divergence_V1.mq5 |
//| Generated by AutoCompile AI |
//| |
//+------------------------------------------------------------------+
#property copyright "AutoCompile AI"
#property link ""
#property version "1.00"
#property indicator_separate_window
#property indicator_buffers 4
#property indicator_plots 2
//--- Plot RSI
#property indicator_label1 "RSI"
#property indicator_type1 DRAW_LINE
#property indicator_color1 clrDodgerBlue
#property indicator_style1 STYLE_SOLID
#property indicator_width1 1
//--- Plot Divergence Lines
#property indicator_label2 "Divergence"
#property indicator_type2 DRAW_SECTION
#property indicator_color2 clrRed
#property indicator_style2 STYLE_SOLID
#property indicator_width2 2
//--- Input parameters
input int RSI_Period=14; // RSI Period
input ENUM_APPLIED_PRICE RSI_Applied=PRICE_CLOSE; // RSI Applied Price
input int Lookback=50; // Bars to scan for divergences
input bool ShowRegularBull=true; // Show Regular Bullish
input bool ShowRegularBear=true; // Show Regular Bearish
input bool ShowHiddenBull=true; // Show Hidden Bullish
input bool ShowHiddenBear=true; // Show Hidden Bearish
input bool AlertOn=true; // Enable Alerts
//--- Indicator buffers
double RSI_Buffer[];
double Div_Buffer_High[];
double Div_Buffer_Low[];
double Div_Color[];
//--- Global variables
int rsi_handle;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//--- Set buffers
SetIndexBuffer(0, RSI_Buffer, INDICATOR_DATA);
SetIndexBuffer(1, Div_Buffer_High, INDICATOR_DATA);
SetIndexBuffer(2, Div_Buffer_Low, INDICATOR_DATA);
SetIndexBuffer(3, Div_Color, INDICATOR_COLOR_INDEX);
//--- Set divergence line properties
PlotIndexSetInteger(1, PLOT_DRAW_TYPE, DRAW_SECTION);
PlotIndexSetInteger(1, PLOT_LINE_COLOR, clrRed);
PlotIndexSetInteger(1, PLOT_LINE_STYLE, STYLE_SOLID);
PlotIndexSetInteger(1, PLOT_LINE_WIDTH, 2);
//--- Get RSI handle
rsi_handle = iRSI(_Symbol, _Period, RSI_Period, RSI_Applied);
if(rsi_handle == INVALID_HANDLE)
{
Print("Failed to create RSI handle");
return(INIT_FAILED);
}
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
if(rsi_handle != INVALID_HANDLE)
IndicatorRelease(rsi_handle);
}
//+------------------------------------------------------------------+
//| 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 - 1;
//--- Get RSI values
double rsi_val[];
ArraySetAsSeries(rsi_val, true);
if(CopyBuffer(rsi_handle, 0, 0, rates_total, rsi_val) < rates_total - limit)
return(0);
//--- Fill RSI buffer
for(int i=limit; i>=0 && !IsStopped(); i--)
RSI_Buffer[i] = rsi_val[i];
//--- Clear divergence buffers
ArrayInitialize(Div_Buffer_High, EMPTY_VALUE);
ArrayInitialize(Div_Buffer_Low, EMPTY_VALUE);
//--- Detect divergences
for(int i=Lookback; i
// Regular Bullish: Price Lower Low, RSI Higher Low
if(ShowRegularBull && low[i] < low[i+2] && rsi_val[i] > rsi_val[i+2])
{
Div_Buffer_Low[i] = rsi_val[i];
Div_Buffer_Low[i+2] = rsi_val[i+2];
if(AlertOn) Alert("Regular Bullish Divergence at ", time[i]);
}
// Regular Bearish: Price Higher High, RSI Lower High
if(ShowRegularBear && high[i] > high[i+2] && rsi_val[i] < rsi_val[i+2])
{
Div_Buffer_High[i] = rsi_val[i];
Div_Buffer_High[i+2] = rsi_val[i+2];
if(AlertOn) Alert("Regular Bearish Divergence at ", time[i]);
}
}
return(rates_total);
}
//+------------------------------------------------------------------+
```
How to Compile and Use
1. Save the code as `RSI_Divergence_V1.mq5` in the `Indicators` folder of your MT5 data directory.
2. Open MetaEditor, compile (F7), and ensure no errors.
3. Attach to any chart. Adjust RSI period and divergence types via input parameters.
Parameter Explanation
This RSI divergence indicator helps traders identify potential reversal points without manual scanning. For advanced automated strategies, our premium EA Suite integrates this logic with trade execution. Subscribe for updates on new tools.
Reference: AutoCompile AI - Original MQL5 implementation, 2025.