//+------------------------------------------------------------------+
//|                                           RSI_X2 with Alert.mq4  |
//| Alert added by Linuxser on June 2007 for ForexTSD team and Valeo |
//+------------------------------------------------------------------+
#property copyright "Image3022"

#property indicator_separate_window
#property indicator_minimum 0
#property indicator_maximum 100
#property indicator_buffers 2
#property indicator_color1 DodgerBlue
#property indicator_color2 Red
#property indicator_width1 1
#property indicator_width2 1
#property indicator_levelcolor Peru
#property indicator_level1 30
#property indicator_level2 70

extern int     RSI_Period       = 14,
               RSI_Period2      = 6;
extern bool  Alarm=true;               
              

double         RSIBuffer[],
               RSI2Buffer[];
               
double     SignalBuffer[];
 
//---- Bar number the alert to be searched by
#define SIGNAL_BAR 1               
               
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
{
   SetIndexStyle(0,DRAW_LINE);
   SetIndexStyle(1,DRAW_LINE);

   SetIndexBuffer(0,RSIBuffer);
   SetIndexBuffer(1,RSI2Buffer);
   
   SetIndexLabel(0,"RSI");
   SetIndexLabel(1,"RSI 2");
   

   return(0);
}
//+------------------------------------------------------------------+
//| Custor indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
{

   return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
{
   int limit;
   int counted_bars=IndicatorCounted();
   if(counted_bars<0) counted_bars=0;
   if(counted_bars>0) counted_bars--;
   limit=Bars-counted_bars;
  
   for(int i = 0 ;i < limit ;i++){       
      RSIBuffer[i]  = iRSI(Symbol(),0,RSI_Period,PRICE_CLOSE,i);
   }
   
   for(int x = 0; x < (ArraySize(RSIBuffer));x++){
      RSI2Buffer[x] = iRSI(Symbol(),0,RSI_Period2,PRICE_CLOSE,x);
   }
//   SetLevelValue(1,RSIBuffer[0]);

//   Check if Alamr is Ok to sound

    if(Alarm == true ) {

//---- Static variables where the last bar time
    //---- and the last alert direction are stored
    static int PrevSignal = 0, PrevTime = 0;
 
    //---- If the bar selected to be analyzed is not a zero bar, 
    //     there is no sense to check the alert
    //---- several times. If no new bar starts to be formed, quit.
    if(SIGNAL_BAR > 0 && Time[0] <= PrevTime ) 
        return(0);
    //---- Mark that this bar was checked
    PrevTime = Time[0];
//---- If the preceding alert was SELL or this is the first launch (PrevSignal=0)
    if(PrevSignal <= 0)
      {
        //---- Check whether the lines have met in the preceding bar:
        if(RSIBuffer[SIGNAL_BAR] - RSI2Buffer[SIGNAL_BAR] > 0 && 
           RSI2Buffer[SIGNAL_BAR+1] - RSIBuffer[SIGNAL_BAR+1] >= 0)
          {
            //---- If yes, mark that the last alert was BUY
            PrevSignal = 1;
            //---- and display information:
            Alert("Fast RSI crossed Slow RSI to DOWN (", Symbol(), ", ", Period(), ")  -  SELL!!!");            
//            Print("sMACD (", Symbol(), ", ", Period(), ")  -  BUY!!!");
//            Comment("sMACD (", Symbol(), ", ", Period(), ")  -  BUY!!!");
//            PlaySound("Alert.wav");
          }
      }
          //---- Completely the same for the SELL alert
    if(PrevSignal >= 0)
      {
        if(RSI2Buffer[SIGNAL_BAR] - RSIBuffer[SIGNAL_BAR] > 0 && 
           RSIBuffer[SIGNAL_BAR+1] - RSI2Buffer[SIGNAL_BAR+1] >= 0)
          {
            PrevSignal = -1;
            Alert("Fast RSI crossed Slow RSI to UP (", Symbol(), ", ", Period(), ")  -  BUY!!!");
//            Print("sMACD (", Symbol(), ", ", Period(), ")  -  SELL!!!");
//            Comment("sMACD (", Symbol(), ", ", Period(), ")  -  SELL!!!");
//            PlaySound("Alert.wav");
          }
      }
    }  
   return(0);
}
//+------------------------------------------------------------------+      