//+------------------------------------------------------------------+
//|                                           RSICross_Signal_v1.mq4 |
//|                                                                  |
//|                                   E-mail: igorad2003@yahoo.co.uk |
//+------------------------------------------------------------------+
#property copyright ""
#property link      ""
//---- indicator settings
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 Blue
#property indicator_color2 Red
#property indicator_width1 1
#property indicator_width2 1

//---- indicator parameters
extern int     RSI_Period1    = 14;    // Large Period of RSI
extern int     RSI_Period2    =  6;    // Small Period of RSI 
extern int     AlertMode      =  0;    // Alert Mode: 0-off,1-on

//---- indicator buffers
double UpSignal[];
double DnSignal[]; 
double trend[];

bool   UpTrendAlert=false, DownTrendAlert=false;
datetime prevTime; 
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
{
   string short_name;
//---- drawing settings
   IndicatorBuffers(3);
      
   SetIndexStyle(0,DRAW_ARROW);
   SetIndexArrow(0,108);
   SetIndexStyle(1,DRAW_ARROW);
   SetIndexArrow(1,108);
   
   IndicatorDigits(MarketInfo(Symbol(),MODE_DIGITS));
//---- indicator short name
   IndicatorShortName("RSICross_Signal("+RSI_Period1+","+RSI_Period2+")");
   SetIndexLabel(0,"UpSignal");
   SetIndexLabel(1,"DnSignal");
   SetIndexDrawBegin(0,RSI_Period1);
   SetIndexDrawBegin(1,RSI_Period1);
//---- indicator buffers mapping
   SetIndexBuffer(0,UpSignal);
   SetIndexBuffer(1,DnSignal);
   SetIndexBuffer(2,trend);
   
//---- initialization done
   return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
{
   int limit;
   if(Bars<=RSI_Period1) return(0);
   int CountedBars=IndicatorCounted();
//---- check for possible errors
   if (CountedBars<0) return(-1);
//---- last counted bar will be recounted
   if (CountedBars>0) CountedBars--;
   limit=Bars-CountedBars;
//---- 
   for(int i=limit; i>=0; i--)
   { 
   double RSI1  = iRSI(Symbol(),0,RSI_Period1,PRICE_CLOSE,i);
   double RSI2  = iRSI(Symbol(),0,RSI_Period2,PRICE_CLOSE,i);
   
   trend[i]=trend[i+1]; 
	   
	if ( RSI2 > RSI1)  trend[i]=1; 
	if ( RSI2 < RSI1)  trend[i]=-1;

      if(trend[i]>0)
	   {
	   if (trend[i+1]<0) UpSignal[i] = Low[i]-0.5*iATR(NULL,0,RSI_Period1,i);
	   DnSignal[i]=EMPTY_VALUE; 
	   }
	   else
	   if(trend[i]<0)
	   {
	   if (trend[i+1]>0) DnSignal[i] = High[i]+0.5*iATR(NULL,0,RSI_Period1,i);
	   UpSignal[i]=EMPTY_VALUE; 
	   }
   	         
//----------   
      if (i==0 && AlertMode > 0)
      {
         if (trend[i+1] > 0/* && Volume[0]>1*/)
         {
            if (trend[i+2]<0 && !UpTrendAlert)
	         {
	         string Message = " "+Symbol()+" M"+Period()+": Signal for BUY";
	         Alert (Message); 
	         UpTrendAlert=true; DownTrendAlert=false;
	         } 
	      }
	      else
	      if (trend[i+1] < 0 /* && Volume[0]>1*/)
	      {
            if ( trend[i+2]>0 && !DownTrendAlert)
	         {
	         Message = " "+Symbol()+" M"+Period()+": Signal for SELL";
	         Alert (Message); 
	         DownTrendAlert=true; UpTrendAlert=false;     
	         }
	      } 	  
      }	
	} 	         

//---- done
   return(0);
}
//+------------------------------------------------------------------+