//+------------------------------------------------------------------+
//|                                   RSI Trend Color with alert.mq4 |
//|                                                           mladen |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "mladen - trend alterations by cja 2021"
#property link      ""


#property indicator_separate_window
#property indicator_buffers   3
#property indicator_color1 clrDimGray
#property indicator_color2 clrLimeGreen
#property indicator_color3 clrRed
#property indicator_width2 2
#property indicator_width3 2


//---- input parameters
//
//
//
//

extern int       RSIPeriod          =  13;
extern int       PriceType          =   5;
extern int       BarShift           =   1;
extern ENUM_TIMEFRAMES   timeFrame  =  PERIOD_CURRENT;
extern color     levelcolor         = clrDimGray;
extern int       upperlevel         =  55;
extern int       lowerlevel         =  45;
extern bool      showArrows         = true;
extern bool      alertsOn           = true;
extern bool      alertsMessage      = true;
extern bool      alertsNotification = false;
extern bool      alertsSound        = false;
extern bool      alertsEmail        = false;




//---- buffers
//
//
//
//
//

double   RSIBuffer[];
double   Upper[];
double   Lower[];

//
//
//
//
//

int      TimeFrame;
datetime TimeArray[];
int      maxArrows;


//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+

int init()
{
      SetIndexBuffer(0,RSIBuffer);
      SetIndexBuffer(1,Upper);
      SetIndexBuffer(2,Lower);
      SetIndexLabel(0,"RSI");
      SetIndexLabel(1,NULL);
      SetIndexLabel(2,NULL);
         
         //
         //
         //
         //
         //
         
         
      TimeFrame         = stringToTimeFrame(timeFrame);
      string shortName  = "RSI ("+TimeFrameToString(TimeFrame)+","+RSIPeriod+","+PriceTypeToString(PriceType);
            if (upperlevel < lowerlevel) upperlevel = lowerlevel;
            if (upperlevel < 100)      shortName  = shortName+","+upperlevel;
            if (lowerlevel   >   0)      shortName  = shortName+","+lowerlevel;
      IndicatorShortName(shortName+")");
   return(0);
}

//
//
//
//
//

int deinit()
{
   DeleteArrows();
   return(0);
}

//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+

int start()
{
   int    counted_bars=IndicatorCounted();
   int    limit;
   int    i,y;
   
   SetLevelValue(1,upperlevel); 
   SetLevelStyle(2,0,levelcolor);
   SetLevelValue(2,lowerlevel); 
  
   
   if(counted_bars<0) return(-1);
   limit=Bars-counted_bars;
         ArrayCopySeries(TimeArray ,MODE_TIME ,NULL,TimeFrame);
            
      
   //
   //
   //
   //
   //
   
   
   
   for(i=BarShift,y=BarShift; i<limit; i++)
      {
            if(Time[i]<TimeArray[y]) y++;
               RSIBuffer[i] = iRSI(NULL,TimeFrame,RSIPeriod,PriceType,y);
      }

   //
   //
   //
   //
   //
   
   for(i=limit; i>=BarShift; i--)
      {
            if (RSIBuffer[i] > upperlevel) { Upper[i] = RSIBuffer[i]; Upper[i+1] = RSIBuffer[i+1]; }
            else                           { Upper[i] = EMPTY_VALUE;
                                           if (Upper[i+2] == EMPTY_VALUE)
                                               Upper[i+1]  = EMPTY_VALUE; }
            if (RSIBuffer[i] < lowerlevel) { Lower[i] = RSIBuffer[i]; Lower[i+1] = RSIBuffer[i+1]; }
            else                           { Lower[i] = EMPTY_VALUE;
                                           if (Lower[i+2] == EMPTY_VALUE)
                                               Lower[i+1]  = EMPTY_VALUE; }
      }
   
   //
   //
   //
   //
   //
   
   if (showArrows)
      for (i=BarShift; i<WindowBarsPerChart() ;i++)
         {
            if (RSIBuffer[i]>upperlevel && RSIBuffer[i+1]<upperlevel) DrawArrow(i,"up");
            if (RSIBuffer[i]<lowerlevel && RSIBuffer[i+1]>lowerlevel) DrawArrow(i,"down");
                
           }
   if (alertsOn)
      { 
         if (RSIBuffer[BarShift]>upperlevel && RSIBuffer[BarShift+1]<upperlevel) doAlert("crossed "+upperlevel+" level Up");
         if (RSIBuffer[BarShift]<lowerlevel && RSIBuffer[BarShift+1]>lowerlevel) doAlert("crossed "+lowerlevel +" level Down");
      
           }


   //
   //
   //
   //
   //
 
   return(0);
}

//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//
//
//
//
//

void DrawArrow(int i,string type)
{
   maxArrows++;
   double atr=iATR(Symbol(),0,100,i)/2;  //typical ATR value to get distance
    
      ObjectCreate("RSISignal"+maxArrows,OBJ_ARROW,0,Time[i],0);
      if (type=="up")
         {
            ObjectSet("RSISignal"+maxArrows,OBJPROP_PRICE1,Low[i]-atr);
            ObjectSet("RSISignal"+maxArrows,OBJPROP_ARROWCODE,241);
            ObjectSet("RSISignal"+maxArrows,OBJPROP_COLOR,clrLimeGreen);
         }
      else
         {
            ObjectSet("RSISignal"+maxArrows,OBJPROP_PRICE1,High[i]+atr);
            ObjectSet("RSISignal"+maxArrows,OBJPROP_ARROWCODE,242);
            ObjectSet("RSISignal"+maxArrows,OBJPROP_COLOR,clrRed);
         }
}
void DeleteArrows()
{
   while(maxArrows>0) { ObjectDelete("RSISignal"+maxArrows); maxArrows--; }
}


//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+

void doAlert(string doWhat)
{
   static string   previousAlert="nothing";
   static datetime previousTime;
   string message;
   
      if (previousAlert != doWhat || previousTime != Time[0]) {
          previousAlert  = doWhat;
          previousTime   = Time[0];
             
          message =  StringConcatenate(_Symbol+" "+TimeFrameToString(TimeFrame)," at ",TimeToStr(TimeLocal(),TIME_SECONDS)," : RSI ( "+RSIPeriod+" ) ",doWhat);
             if (alertsMessage) Alert(message);
             if (alertsNotification) SendNotification(message);
             if (alertsSound)   PlaySound("alert2.wav");
             if (alertsEmail)   SendMail(StringConcatenate(_Symbol," RSI crossing"),message);
      }        
}

//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//
//
//
//
//

string PriceTypeToString(int pt)
{
   string answer;
   switch(pt)
   {
      case 0:  answer = "Close"    ; break; 
      case 1:  answer = "Open"     ; break;
      case 2:  answer = "High"     ; break;
      case 3:  answer = "Low"      ; break;
      case 4:  answer = "Median"   ; break;
      case 5:  answer = "Typical"  ; break;
      case 6:  answer = "Weighted"  ; break;
      default: answer = "Invalid price field requested";
                                    Alert(answer);
   }
   return(answer);
}
int stringToTimeFrame(string tfs)
{
   int tf=0;
       tfs = StringUpperCase(tfs);
         if (tfs=="M1" || tfs=="1")     tf=PERIOD_M1;
         if (tfs=="M5" || tfs=="5")     tf=PERIOD_M5;
         if (tfs=="M15"|| tfs=="15")    tf=PERIOD_M15;
         if (tfs=="M30"|| tfs=="30")    tf=PERIOD_M30;
         if (tfs=="H1" || tfs=="60")    tf=PERIOD_H1;
         if (tfs=="H4" || tfs=="240")   tf=PERIOD_H4;
         if (tfs=="D1" || tfs=="1440")  tf=PERIOD_D1;
         if (tfs=="W1" || tfs=="10080") tf=PERIOD_W1;
         if (tfs=="MN" || tfs=="43200") tf=PERIOD_MN1;
         if (tf<Period()) tf=Period();
  return(tf);
}
string TimeFrameToString(int tf)
{
   string tfs="Current time frame";
   switch(tf) {
      case PERIOD_M1:  tfs="M1"  ; break;
      case PERIOD_M5:  tfs="M5"  ; break;
      case PERIOD_M15: tfs="M15" ; break;
      case PERIOD_M30: tfs="M30" ; break;
      case PERIOD_H1:  tfs="H1"  ; break;
      case PERIOD_H4:  tfs="H4"  ; break;
      case PERIOD_D1:  tfs="D1"  ; break;
      case PERIOD_W1:  tfs="W1"  ; break;
      case PERIOD_MN1: tfs="MN1";
   }
   return(tfs);
}

//
//
//
//
//

string StringUpperCase(string str)
{
   string   s = str;
   int      lenght = StringLen(str) - 1;
   int      tchar;
   
   while(lenght >= 0)
      {
         tchar = StringGetChar(s, lenght);
         
         //
         //
         //
         //
         //
         
         if((tchar > 96 && tchar < 123) || (tchar > 223 && tchar < 256))
                  s = StringSetChar(s, lenght, tchar - 32);
          else 
              if(tchar > -33 && tchar < 0)
                  s = StringSetChar(s, lenght, tchar + 224);
                  
         //
         //
         //
         //
         //
                                 
         lenght--;
   }
   
   //
   //
   //
   //
   //
   
   return(s);
}