//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void CatchBullishDivergence(int shift)
{
if(IsIndicatorTrough(shift) == false)
return;
int currentTrough = shift;
int lastTrough = GetIndicatorLastTrough(shift);
if(OsMA[currentTrough] > OsMA[lastTrough] && Low[currentTrough] < Low[lastTrough])
{
bullishDivergence[currentTrough] = OsMA[currentTrough];
if(drawDivergenceLines == true)
{
DrawPriceTrendLine(Time[currentTrough], Time[lastTrough], Low[currentTrough],
Low[lastTrough], Green, STYLE_SOLID);
DrawIndicatorTrendLine(Time[currentTrough], Time[lastTrough], OsMA[currentTrough],
OsMA[lastTrough], Green, STYLE_SOLID);
}
if(displayAlert == true)
DisplayAlert("Classical bullish divergence on: ", currentTrough);
}
if(OsMA[currentTrough] < OsMA[lastTrough] && Low[currentTrough] > Low[lastTrough])
{
bullishDivergence[currentTrough] = OsMA[currentTrough];
if(drawDivergenceLines == true)
{
DrawPriceTrendLine(Time[currentTrough], Time[lastTrough], Low[currentTrough],
Low[lastTrough], Green, STYLE_DOT);
DrawIndicatorTrendLine(Time[currentTrough], Time[lastTrough], OsMA[currentTrough],
OsMA[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(OsMA[currentPeak] < OsMA[lastPeak] && High[currentPeak] > High[lastPeak])
{
bearishDivergence[currentPeak] = OsMA[currentPeak];
if(drawDivergenceLines == true)
{
DrawPriceTrendLine(Time[currentPeak], Time[lastPeak], High[currentPeak],
High[lastPeak], Red, STYLE_SOLID);
DrawIndicatorTrendLine(Time[currentPeak], Time[lastPeak], OsMA[currentPeak],
OsMA[lastPeak], Red, STYLE_SOLID);
}
if(displayAlert == true)
DisplayAlert("Classical bearish divergence on: ", currentPeak);
}
if(OsMA[currentPeak] > OsMA[lastPeak] && High[currentPeak] < High[lastPeak])
{
bearishDivergence[currentPeak] = OsMA[currentPeak];
if(drawDivergenceLines == true)
{
DrawPriceTrendLine(Time[currentPeak], Time[lastPeak], High[currentPeak],
High[lastPeak], Red, STYLE_DOT);
DrawIndicatorTrendLine(Time[currentPeak], Time[lastPeak], OsMA[currentPeak],
OsMA[lastPeak], Red, STYLE_DOT);
}
if(displayAlert == true)
DisplayAlert("Reverse bearish divergence on: ", currentPeak);
}
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool IsIndicatorPeak(int shift)
{
if(OsMA[shift] > 0 && OsMA[shift] > OsMA[shift+1] && OsMA[shift] > OsMA[shift-1])
{
for(int i = shift + 1; i < Bars; i++)
{
if(OsMA[i] < 0)
return(true);
if(OsMA[i] > OsMA[shift])
break;
}
}
return(false);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool IsIndicatorTrough(int shift)
{
if(OsMA[shift] < 0 && OsMA[shift] < OsMA[shift+1] && OsMA[shift] < OsMA[shift-1])
{
for(int i = shift + 1; i < Bars; i++)
{
if(OsMA[i] > 0)
return(true);
if(OsMA[i] < OsMA[shift])
break;
}
}
return(false);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int GetIndicatorLastPeak(int shift)
{
for(int i = shift + 5; i < Bars; i++)
{
if(OsMA[i] >= OsMA[i+1] && OsMA[i] > OsMA[i+2] &&
OsMA[i] >= OsMA[i-1] && OsMA[i] > OsMA[i-2])
return(i);
}
return(-1);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int GetIndicatorLastTrough(int shift)
{
for(int i = shift + 5; i < Bars; i++)
{
if(OsMA[i] <= OsMA[i+1] && OsMA[i] < OsMA[i+2] &&
OsMA[i] <= OsMA[i-1] && OsMA[i] < OsMA[i-2])
return(i);
}
return(-1);
}