RSI Divergence

If you need an Indicator or Signal developed or translated from other language, please post all Indicator development REQUESTS to this section here.

Moderator: admin

RSI Divergence

Postby Shadow77 » Tue Feb 23, 2010 8:25 am

I need help to translate this MT4 indicator to marketscope indicator.

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);
  }
//+------------------------------------------------------------------+
Shadow77
 
Posts: 13
Joined: Tue Feb 23, 2010 8:09 am

Re: RSI Divergence

Postby Nikolay.Gekht » Tue Feb 23, 2010 12:58 pm

Thank you for your request. This indicator requires label displaying ability, so, it will be possible to implement it in the next version of the Marketscope. I already have the first preview version and expect the production release during the next month. The indicator will also be published together with the new release of Marketscope.
Nikolay.Gekht
FXCodeBase: Site Admin
 
Posts: 1235
Joined: Wed Dec 16, 2009 6:39 pm
Location: Cary, NC

Re: RSI Divergence

Postby Shadow77 » Thu Apr 22, 2010 7:13 pm

Hello, Any idea when it will be available

Thanks in advance for your great job on all of this indicator
Shadow77
 
Posts: 13
Joined: Tue Feb 23, 2010 8:09 am

Re: RSI Divergence

Postby Nikolay.Gekht » Fri Apr 23, 2010 7:55 am

Oh. Thank you for kicking this indicator. It wasn't in the dev. queue. Added.
Nikolay.Gekht
FXCodeBase: Site Admin
 
Posts: 1235
Joined: Wed Dec 16, 2009 6:39 pm
Location: Cary, NC

Re: RSI Divergence

Postby Nikolay.Gekht » Mon Apr 26, 2010 4:09 pm

I apologize for such long delay. Please find the indicator and the signal here:
viewtopic.php?f=17&t=846
Nikolay.Gekht
FXCodeBase: Site Admin
 
Posts: 1235
Joined: Wed Dec 16, 2009 6:39 pm
Location: Cary, NC


Return to Indicator and Signal Requests

Who is online

Users browsing this forum: Bing [Bot] and 11 guests