Thanks in advance
- Code: Select all
//+------------------------------------------------------------------+
//| RSI Divergence.mq4 |
//+------------------------------------------------------------------+
//----
#property indicator_separate_window
#property indicator_buffers 3
#property indicator_color1 DodgerBlue
#property indicator_color2 Lime
#property indicator_color3 Red
#property indicator_maximum 100
#property indicator_minimum 0
#property indicator_levelcolor Gray
#property indicator_levelstyle STYLE_DOT
#property indicator_levelwidth 1
#property indicator_level1 70
#property indicator_level2 50
#property indicator_level3 30
//---- input parameters
extern string separator1 = "*** RSI Settings ***";
extern int RSI_PERIOD = 7;
extern string separator2 = "*** Indicator Settings ***";
extern bool drawDivergenceLines = true;
extern bool displayAlert = true;
//---- buffers
double B_RSI[];
double bullishDivergence[];
double bearishDivergence[];
//double B_RSI[];
//----
static datetime lastAlertTime;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- indicators
SetIndexStyle(0, DRAW_LINE, STYLE_SOLID, 1);
SetIndexStyle(1, DRAW_ARROW);
SetIndexStyle(2, DRAW_ARROW);
//SetIndexStyle(4, DRAW_NONE);
//----
SetIndexBuffer(0, B_RSI);
SetIndexBuffer(1, bullishDivergence);
SetIndexBuffer(2, bearishDivergence);
//SetIndexBuffer(4, OsMA);
//----
SetIndexArrow(1, 233);
SetIndexArrow(2, 234);
//----
IndicatorDigits(Digits + 2);
IndicatorShortName("RSI Divergence(" + RSI_PERIOD + ")");
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) != "Divergence Line")
continue;
ObjectDelete(label);
}
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{
int countedBars = IndicatorCounted();
if(countedBars < 0)
countedBars = 0;
CalculateIndicator(countedBars);
return(0);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void CalculateIndicator(int countedBars)
{
for(int i = Bars - countedBars; i >= 0; i--)
{
CalculateRSI(i);
CatchBullishDivergence(i + 2);
CatchBearishDivergence(i + 2);
}
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void CalculateRSI(int i)
{
B_RSI[i] = iRSI(NULL, 0, RSI_PERIOD, PRICE_CLOSE, i);
//----
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void CatchBullishDivergence(int shift)
{
if(IsIndicatorTrough(shift) == false)
return;
int currentTrough = shift;
int lastTrough = GetIndicatorLastTrough(shift);
if(B_RSI[currentTrough] > B_RSI[lastTrough] && Low[currentTrough] < Low[lastTrough])
{
bullishDivergence[currentTrough] = B_RSI[currentTrough];
if(drawDivergenceLines == true)
{
DrawPriceTrendLine(Time[currentTrough], Time[lastTrough], Low[currentTrough],
Low[lastTrough], Green, STYLE_SOLID);
DrawIndicatorTrendLine(Time[currentTrough], Time[lastTrough], B_RSI[currentTrough],
B_RSI[lastTrough], Green, STYLE_SOLID);
}
if(displayAlert == true)
DisplayAlert("Classical bullish divergence on: ", currentTrough);
}
if(B_RSI[currentTrough] < B_RSI[lastTrough] && Low[currentTrough] > Low[lastTrough])
{
bullishDivergence[currentTrough] = B_RSI[currentTrough];
if(drawDivergenceLines == true)
{
DrawPriceTrendLine(Time[currentTrough], Time[lastTrough], Low[currentTrough],
Low[lastTrough], Green, STYLE_DOT);
DrawIndicatorTrendLine(Time[currentTrough], Time[lastTrough], B_RSI[currentTrough],
B_RSI[lastTrough], Green, STYLE_DOT);
}
if(displayAlert == true)
DisplayAlert("Reverse bullish divergence on: ", currentTrough);
}
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void CatchBearishDivergence(int shift)
{
if(IsIndicatorPeak(shift) == false)
return;
int currentPeak = shift;
int lastPeak = GetIndicatorLastPeak(shift);
if(B_RSI[currentPeak] < B_RSI[lastPeak] && High[currentPeak] > High[lastPeak])
{
bearishDivergence[currentPeak] = B_RSI[currentPeak];
if(drawDivergenceLines == true)
{
DrawPriceTrendLine(Time[currentPeak], Time[lastPeak], High[currentPeak],
High[lastPeak], Red, STYLE_SOLID);
DrawIndicatorTrendLine(Time[currentPeak], Time[lastPeak], B_RSI[currentPeak],
B_RSI[lastPeak], Red, STYLE_SOLID);
}
if(displayAlert == true)
DisplayAlert("Classical bearish divergence on: ", currentPeak);
}
if(B_RSI[currentPeak] > B_RSI[lastPeak] && High[currentPeak] < High[lastPeak])
{
bearishDivergence[currentPeak] = B_RSI[currentPeak];
if(drawDivergenceLines == true)
{
DrawPriceTrendLine(Time[currentPeak], Time[lastPeak], High[currentPeak],
High[lastPeak], Red, STYLE_DOT);
DrawIndicatorTrendLine(Time[currentPeak], Time[lastPeak], B_RSI[currentPeak],
B_RSI[lastPeak], Red, STYLE_DOT);
}
if(displayAlert == true)
DisplayAlert("Reverse bearish divergence on: ", currentPeak);
}
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool IsIndicatorPeak(int shift)
{
if(B_RSI[shift] > 70 && B_RSI[shift] > B_RSI[shift+1] && B_RSI[shift] > B_RSI[shift-1])
{
for(int i = shift + 1; i < Bars; i++)
{
if(B_RSI[i] < 70)
return(true);
if(B_RSI[i] > B_RSI[shift])
break;
}
}
return(false);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool IsIndicatorTrough(int shift)
{
if(B_RSI[shift] < 30 && B_RSI[shift] < B_RSI[shift+1] && B_RSI[shift] < B_RSI[shift-1])
{
for(int i = shift + 1; i < Bars; i++)
{
if(B_RSI[i] > 30)
return(true);
if(B_RSI[i] < B_RSI[shift])
break;
}
}
return(false);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int GetIndicatorLastPeak(int shift)
{
for(int i = shift + 5; i < Bars; i++)
{
if(B_RSI[i] >= B_RSI[i+1] && B_RSI[i] > B_RSI[i+2] &&
B_RSI[i] >= B_RSI[i-1] && B_RSI[i] > B_RSI[i-2])
return(i);
}
return(-1);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int GetIndicatorLastTrough(int shift)
{
for(int i = shift + 5; i < Bars; i++)
{
if(B_RSI[i] <= B_RSI[i+1] && B_RSI[i] < B_RSI[i+2] &&
B_RSI[i] <= B_RSI[i-1] && B_RSI[i] < B_RSI[i-2])
return(i);
}
return(-1);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void DisplayAlert(string message, int shift)
{
if(shift <= 2 && Time[shift] != lastAlertTime)
{
lastAlertTime = Time[shift];
Alert(message, Symbol(), " , ", Period(), " minutes chart");
}
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void DrawPriceTrendLine(datetime x1, datetime x2, double y1,
double y2, color lineColor, double style)
{
string label = "Price Divergence Line # " + 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 DrawIndicatorTrendLine(datetime x1, datetime x2, double y1,
double y2, color lineColor, double style)
{
int indicatorWindow = WindowFind("RSI Divergence(" + RSI_PERIOD + ")");
if(indicatorWindow < 0)
return;
string label = "RSI Divergence Line # " + 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);
}
//+------------------------------------------------------------------+