//+------------------------------------------------------------------+
//|                                                   3rsi alert.mq4 |
//+------------------------------------------------------------------+

#property indicator_separate_window
#property indicator_buffers 5
#property indicator_color1 Yellow 
#property indicator_color2 Red
#property indicator_color3 DeepSkyBlue
#property indicator_color4 clrDodgerBlue
#property indicator_color5 clrMagenta
#property indicator_level1 50


extern bool AlertAllow      = true;
extern int  Offset          = 50;
extern int  price_offset    = 10;

extern int  RSIperiod1      = 9;
extern int  RSIperiod2      = 14;
extern int  RSIperiod3      = 20;
extern int  applied_price   = 4;

double r1[];
double r2[];
double r3[];
double cu[];
double cd[];

datetime t0;
//+------------------------------------------------------------------+ 
int init() {
   IndicatorBuffers( 5 );
   
   SetIndexBuffer(0,r1);
   SetIndexBuffer(1,r2);
   SetIndexBuffer(2,r3);
   
   SetIndexLabel(0,"R1("+RSIperiod1+")");
   SetIndexLabel(1,"R2("+RSIperiod2+")");
   SetIndexLabel(2,"R3("+RSIperiod3+")");

   SetIndexStyle(0, DRAW_LINE, STYLE_SOLID );
   SetIndexStyle(1, DRAW_LINE, STYLE_SOLID );
   SetIndexStyle(2, DRAW_LINE, STYLE_SOLID );  
   
   SetIndexDrawBegin( 0, (10 - 1) * 3 );
   
   SetIndexStyle(3, DRAW_ARROW, EMPTY);
   SetIndexArrow(3, 233);
   SetIndexBuffer(3, cu);
   SetIndexStyle(4, DRAW_ARROW, EMPTY);
   SetIndexArrow(4, 234);
   SetIndexBuffer(4, cd);  
   
   IndicatorShortName("3_RSI("+RSIperiod1+","+RSIperiod2+","+RSIperiod3+")");
   
   return( 0 ); 
} 

//+------------------------------------------------------------------+ 
//| Custor indicator deinitialization function                       | 
//+------------------------------------------------------------------+ 
int deinit() { 

   for(int i=0;i<=Bars;i++)
   {
   ObjectDelete("up"+i);
   ObjectDelete("dn"+i);
   }
   
   return(0);
} 

//+------------------------------------------------------------------+ 
//| Custom indicator iteration function                              | 
//+------------------------------------------------------------------+ 
int start() {
   int limit;
   int counted_bars = IndicatorCounted();
   int i;

   if( counted_bars < 0 ) return(-1);
   if( counted_bars > 0 ) counted_bars --;
   limit = Bars - counted_bars;
      
   for( i = limit - 1; i >= 0; i -- )
      {
       r1[i]=iRSI(NULL,0,RSIperiod1,applied_price,i);
       r2[i]=iRSI(NULL,0,RSIperiod2,applied_price,i);
       r3[i]=iRSI(NULL,0,RSIperiod3,applied_price,i);
      }

   for(i = limit - 1; i >= 0; i -- )
      {
       if(r1[i+1] > 50 && r2[i+1] > 50 && r3[i+1] > 50 && r1[i+2] < 50 && r2[i+2] < 50 && r3[i+2] < 50)
         {
          cu[i+1]   = Offset;
          ObjectCreate("up"+i,OBJ_ARROW,0,Time[i+1],Low[i+1] - price_offset*Point);
          ObjectSet("up"+i,OBJPROP_ARROWCODE,233);
          ObjectSet("up"+i,OBJPROP_COLOR,clrDodgerBlue);
          if(AlertAllow && t0!=Time[0])
            {
             Alert("****** "+Symbol()+" 3 rsi cross 50 up");
             t0=Time[0];
            }
         }
      }
   
   for(i = limit - 1; i >= 0; i -- )
      {
       if(r1[i+1] < 50 && r2[i+1] < 50 && r3[i+1] < 50 && r1[i+2] > 50 && r2[i+2] > 50 && r3[i+2] > 50)
         {
          cd[i+1] = Offset;
          ObjectCreate("dn"+i,OBJ_ARROW,0,Time[i+1],High[i+1] + price_offset*Point);
          ObjectSet("dn"+i,OBJPROP_ARROWCODE,234);
          ObjectSet("dn"+i,OBJPROP_COLOR,clrMagenta);
          if(AlertAllow && t0!=Time[0])
            {
             Alert("****** "+Symbol()+" 3 rsi cross 50 dn");
             t0=Time[0];
            }
         }
      }
   
  return(0); 
}



