//------------------------------------------------------------------
#property link      "www.forex-station.com"
#property copyright "www.forex-station.com"
//------------------------------------------------------------------
#property indicator_separate_window
#property indicator_buffers    4
#property indicator_color1     clrGreen
#property indicator_width1     2
#property indicator_color2     clrRed
#property indicator_width2     2
#property indicator_color3    clrNONE
#property indicator_width3     1



extern ENUM_TIMEFRAMES    TimeFrame       = PERIOD_CURRENT;
extern int RSIPeriod1=14;
extern int RSIPeriod2=28;
extern bool               alertsOn        = false;
extern bool               alertsOnCurrent = true;
extern bool               alertsMessage   = true;
extern bool               alertsSound     = false;
extern bool               alertsNotify    = false;
extern bool               alertsEmail     = false;
extern string             soundFile       = "alert2.wav";
extern bool               Interpolate   = true;

extern color              main_color     =clrGreen;
extern int                main_width    = 2;
extern color              lev_color   =clrRed;
extern int                lev_width    = 1;

extern string             name = "Waddah Def RSI";


double buffer1[];
double buffer2[];
double buffer3[],dz[];
double work[];
double prices[];

string indicatorFileName;
bool   returnBars;

string name2 = name + " " + TimeFrame;

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int init()
  {
   
 
   
   
   IndicatorBuffers(5);
   SetIndexBuffer(0,buffer1);
   SetIndexStyle(0,DRAW_LINE,0,main_width,main_color);
   SetIndexBuffer(1,buffer2);
   SetIndexStyle(1,DRAW_LINE,0,lev_width,lev_color);
   SetIndexBuffer(2,buffer3);
   SetIndexStyle(2,DRAW_LINE);
   SetIndexBuffer(3,dz);
   SetIndexBuffer(4,work);


   indicatorFileName = WindowExpertName();
   returnBars        = TimeFrame==-99;
   TimeFrame         = fmax(TimeFrame,_Period);
  
   IndicatorShortName(name2);

   return(0);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int deinit()
{

   return(0);

  }

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int start()
  {
  

   
   int counted_bars=IndicatorCounted();
   int i,limit;


   if(counted_bars < 0)
      return(-1);
   if(counted_bars > 0)
      counted_bars--;
   limit = MathMin(Bars-counted_bars,Bars-1);
   if(returnBars)
     {
      buffer1[0] = limit+1;
      return(0);
     }
 

   if(TimeFrame == Period())
     {

      for(i=limit; i>=0; i--)
        {
          work[i] = iRSI(NULL, 0, RSIPeriod1, PRICE_CLOSE, i)-iRSI(NULL, 0, RSIPeriod2, PRICE_CLOSE, i);
       

         buffer3[i]=  iMAOnArray(work,0,14,0,MODE_SMA,i);
        
        
            buffer1[i] =iMAOnArray(work,0,14,0,MODE_SMA,i);
            buffer2[i]=  0;
           


        }


 
       if (alertsOn)
       {
         if (alertsOnCurrent)
              int whichBar = 0;
         else     whichBar = 1; 
      
         //
         //
         //
         //
         //
      
         if (buffer1[whichBar+1] < buffer2[whichBar+1]&& buffer1[whichBar] > buffer2[whichBar]) doAlert(whichBar,"up");
         if (buffer1[whichBar+1] > buffer2[whichBar+1]&& buffer1[whichBar] < buffer2[whichBar]) doAlert(whichBar,"down");
      }



      return(0);
     }



   limit = MathMax(limit,MathMin(Bars-1,iCustom(NULL,TimeFrame,indicatorFileName,-99,0,0)*TimeFrame/Period()));
   for(i=limit; i>=0; i--)
     {
      int y = iBarShift(NULL,TimeFrame,Time[i]);
      
      


      buffer1[i] = iCustom(NULL,TimeFrame,indicatorFileName,PERIOD_CURRENT,RSIPeriod1,RSIPeriod2,alertsOn,alertsOnCurrent,alertsMessage,alertsSound,alertsNotify,alertsEmail,soundFile, 0,y);
      buffer2[i] = iCustom(NULL,TimeFrame,indicatorFileName,PERIOD_CURRENT,RSIPeriod1,RSIPeriod2,alertsOn,alertsOnCurrent,alertsMessage,alertsSound,alertsNotify,alertsEmail,soundFile, 1,y);
      buffer3[i] = iCustom(NULL,TimeFrame,indicatorFileName,PERIOD_CURRENT,RSIPeriod1,RSIPeriod2,alertsOn,alertsOnCurrent,alertsMessage,alertsSound,alertsNotify,alertsEmail,soundFile, 2,y);
     
      //
      //
      //
      //
      //

      if(!Interpolate || y==iBarShift(NULL,TimeFrame,Time[i-1]))
         continue;

      //
      //
      //
      //
      //

      datetime time = iTime(NULL,TimeFrame,y);
      for(int n = 1; i+n < Bars && Time[i+n] >= time; n++)
         continue;
      for(int x = 1; x < n; x++)
        {
         buffer3[i+x] = buffer3[i] + (buffer3[i+n] - buffer3[i]) * x/n;
       

        
            buffer1[i+x] = buffer1[i] + (buffer3[i+n] - buffer1[i]) * x/n;
       
            buffer2[i+x] = buffer2[i] + (buffer2[i+n] - buffer2[i]) * x/n;




        }
     }







   return(0);
  }

//
//
//
//
//

void doAlert(int forBar, string doWhat)
{
   static string   previousAlert="nothing";
   static datetime previousTime;
   string message;
   
      if (previousAlert != doWhat || previousTime != Time[forBar]) {
          previousAlert  = doWhat;
          previousTime   = Time[forBar];

          //
          //
          //
          //
          //

           message =  StringConcatenate(Symbol()," at ",TimeToStr(TimeLocal(),TIME_SECONDS)," Waddah Def RSI ",doWhat);
             if (alertsMessage) Alert(message);
             if (alertsNotify)  SendNotification(message);
             if (alertsEmail)   SendMail(StringConcatenate(Symbol(), Period(), " Waddah Def RSI "),message);
             if (alertsSound)   PlaySound("alert2.wav");
      }
}



