//+------------------------------------------------------------------+
//|                                                  rsi extreme.mq4 |
//|                                           Author: LordoftheMoney |
//|                                Expert advisor is in the codebase |
//|                                                    (Easiest RSI) |
//+------------------------------------------------------------------+

#property indicator_chart_window
#property indicator_buffers 2
#property indicator_width1 1
#property indicator_width2 1
#property indicator_color1 DodgerBlue
#property indicator_color2 DodgerBlue
extern int rsiperiod = 3;
extern double OB = 35;
extern double OS = 65;
double buffy1[];
double buffy2[];

double buffyd1[];
double buffyd2[];

string datebuff[400];
string alrt[11];
string current_time;

int cb=0;
int bar;
string rsi_found;
bool sendalert = false;
bool EmailOn = true;


 //datetime HigherTF;
string hgtf_date;
int hgtf_index;


extern ENUM_TIMEFRAMES timeframe = PERIOD_D1;
extern ENUM_TIMEFRAMES lowtimeframe = PERIOD_CURRENT;

static datetime lastAlertTime; // last alert on a Peak
static datetime lastAlertTimeTr; // last alert on a Trough
string settings;
bool rsi_buy = false;
bool rsi_sell = false;
int current_index;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
   int  draw;
   SetIndexStyle(0,DRAW_ARROW);
   SetIndexArrow(0,233);
   SetIndexStyle(1,DRAW_ARROW);
   SetIndexArrow(1,234);
   SetIndexEmptyValue(0,0.0);
   SetIndexLabel(0,"buy");
   SetIndexLabel(1,"sell");
   SetIndexDrawBegin(0,draw);
   SetIndexDrawBegin(1,draw);
   SetIndexBuffer(0,buffy1);
   SetIndexBuffer(1,buffy2);
   
   return(0);
  }
//+------------------------------------------------------------------+
int deinit()
  {
   ObjectsDeleteAll(0,OBJ_ARROW);
   //Alert(rsi_found);
   return(0);
  }
//+------------------------------------------------------------------+
int start()
  {
    settings = " - Settings" + rsiperiod + " - " + OB + " - " + OS;
   if (bar==Time[0]) return(0);
   int cb=IndicatorCounted();
   int x;
   if(Bars<=100) return(0);
   if (cb<0) return(-1);
   if (cb>0) cb--;
   x=Bars-cb;
   x=800;
   int i;
   
   
   
 
   
   //GetHigher(current_index, x) ;
   
   // GetHigher(i, x) ;
   // Alert(rsi_sell,current_index, rsi_buy);
    
    
    current_time = TimeToString(Time[0], TIME_DATE); 
    hgtf_date = TimeToString(Time[0], TIME_DATE); 
    
    
    
    
    
    GetHigher(hgtf_index,  x ) ;
     
    
    
    
    
   for(int y=0; y<x; y++) //"loop until first Arrow is found
   {
    double r1 = iRSI(NULL,lowtimeframe,rsiperiod,PRICE_CLOSE,y);
    double r2 = iRSI(NULL,lowtimeframe,rsiperiod,PRICE_CLOSE,y+1);
    
    
   
   if (current_time <= hgtf_date)
    {
     GetHigher(hgtf_index + 1,  x ) ;
    //Alert(current_time,"Dat", hgtf_date );
    }
    
    
     if(r1>OS && r2<OS && rsi_buy)
     
     {
   
     //  GetHigher(current_index+1, x) ;
   
     
       current_time = TimeToString(Time[y], TIME_DATE);     
     // Alert("currenttime Buy",current_time);
    
      
       buffy1[y] = Low[y+1]-15*Point;
       bar=Time[0]; 
       rsi_found =  "RSI Extreme - Bullish, " +  "\n " + y + Symbol() + ", TF=" + StringSubstr(EnumToString((ENUM_TIMEFRAMES)Period()),7) + " Time=" +  TimeToString(Time[y]) + "rsi_buy" +hgtf_date;   
       //    if (y >= 0 )  break;  // "Exit loop once found
    
    if (y <=2)
         {
           sendalert = true;
         //  break;
         }
        //break;
    
    //break;
        // rsi_sell = false;
        // rsi_buy = false;
        //GetHigher(current_index+1, x) ;
    
     }
    
     
 ///=========================================================================================//    
     
     if (r1<OB && r2>OB && rsi_sell)
     
      {
     
      
   //    GetHigher(current_index+1, x) ;
        current_time = TimeToString(Time[y], TIME_DATE);     
      //  Alert("currenttime SELL",current_time);
        buffy2[y] = High[y+1]+15*Point;
        bar=Time[0];       
        rsi_found =  "RSI Extreme - Bearish," + "\n-" + y + Symbol() + ", TF=" + StringSubstr(EnumToString((ENUM_TIMEFRAMES)Period()),7) + " Time=" +  TimeToString(Time[y]) + "rsi_sell" + hgtf_date;      
       
    //   break;
        
        if (y <=2)
         {
           sendalert = true;
        //   break;
         }
        //break;
        
       // rsi_sell = false;
        // rsi_buy = false;
        
        
        // GetHigher(current_index+1, x) ;
       
       
       
     //if (y >= 0 )        break;  // "Exit loop once found
      
      }
    
  
   // rsi_sell = false;
      //   rsi_buy = false;
        // GetHigher(current_index+1, x) ;
   } 

       





//=============================================================
   if (sendalert)
    {
    
      string msg = rsi_found;
      if ( EmailOn )
        {
          if ( Time[i] !=  lastAlertTime )
        {
        lastAlertTime = Time[i];
         SendMail(msg,msg);  
      //   Alert(rsi_found);         
       }  
            
      }      sendalert = false;
    }
//============================================================= 
  
  // return(0);
  }
  
  
  
  
   void GetHigher(int p_index, int nbbars) {
   
  // Alert(p_index);
      
          rsi_buy = false;
          rsi_sell = false;
          datetime  HigherTF;
      
         
   // check higher time frame
    for(p_index; p_index<nbbars; p_index++) //"loop until first Arrow is found
   {
    double H1 = iRSI(NULL,timeframe,rsiperiod,PRICE_CLOSE,p_index);
    double H2 = iRSI(NULL,timeframe,rsiperiod,PRICE_CLOSE,p_index+1);
    
    
    
    
    
    
   if(H1>OS && H2<OS)
     {
    //   buffyd1[p_index] = Low[p_index+1]-15*Point;
       bar=Time[0]; 
       rsi_found =  "RSI Extreme - Bullish, " +  "\n" +  Symbol() + ", TF=" + StringSubstr(EnumToString((ENUM_TIMEFRAMES)Period()),7) + " Time=" +  TimeToString(Time[p_index]) + rsi_sell ;   
       datebuff[p_index] =  TimeToString(Time[p_index], TIME_DATE);            
    //   Alert(datebuff[p_index],"--BUY--",Time[p_index], "ttT", p_index);
      
       rsi_sell = false;
       rsi_buy = true;
      
      
         if (p_index >= 0 )
         {
          rsi_buy = true;
          rsi_sell = false;
          
           HigherTF = iTime(NULL,timeframe, p_index) ;
          
           hgtf_date = TimeToString(HigherTF, TIME_DATE); ;
       hgtf_index = p_index ;
       
       
      //  HigherTF = iTime(NULL,timeframe, p_index) ;           // shift
          break;  // "Exit loop once found
         }
     }
  
  
  
 ///=========================================================================================//    
     
     if (H1<OB && H2>OB)
      {
     
      // buffyd2[p_index] = High[p_index+1]+15*Point;
        
        
        bar=Time[0];       
        rsi_found =  "RSI Extreme - Bearish," + "\n" +  Symbol() + ", TF=" + StringSubstr(EnumToString((ENUM_TIMEFRAMES)Period()),7) + " Time=" +  TimeToString(Time[p_index]) + settings;      
       
       datebuff[p_index] =  TimeToString(Time[p_index], TIME_DATE);            
     //  Alert(datebuff[p_index],"--SELLffffff--",Time[p_index], "ttT", p_index);
     if (p_index >= 0 )  {     
      
      
      
       rsi_sell = true;
       rsi_buy = false;
        
        
       HigherTF = iTime(NULL,timeframe, p_index) ;           // shift  
       hgtf_date = TimeToString(HigherTF, TIME_DATE); ;
       hgtf_index = p_index ;
      
      
       
        break;  // "Exit loop once found
         
      }      
     }
   } 
   
}
   
//+------------------------------------------------------------------+