//+------------------------------------------------------------------+
//|                                      $_RSI_x3_alerts_arrows(mtf) |
//+------------------------------------------------------------------+
#property indicator_separate_window
#property indicator_buffers  5
#property indicator_color1   clrCyan
#property indicator_color2   clrGold
#property indicator_color3   clrBlue
#property indicator_level1   70
#property indicator_level2   50
#property indicator_level3   30

#property indicator_color4  clrLimeGreen
#property indicator_color5  clrRed


//
//
//
//
//

extern int    timeFrame        = 0;
extern int    RsiPeriod1       = 2; //was 7
extern ENUM_APPLIED_PRICE RSIPrice1 = PRICE_CLOSE;
extern int    RsiPeriod2       = 3; //was 14
extern ENUM_APPLIED_PRICE RSIPrice2 = PRICE_CLOSE;
extern int    RsiPeriod3       = 5; //was 21
extern ENUM_APPLIED_PRICE RSIPrice3 = PRICE_CLOSE;
extern string note1="----------------------------------------------";
extern string note2="Do you want to take RSI level 50 into account?";
extern string note3="(false = no); (true = yes)";
extern bool   UseRSIlevel50    = false;
extern string note4="----------------------------------------------";

extern bool   alertsOn         = false;
extern bool   alertsOnCurrent  = true;
extern bool   alertsMessage    = true;
extern bool   alertsSound      = true;
extern bool   alertsNotify     = false;
extern bool   alertsEmail      = false;
extern string soundFile        = "alert2.wav";

extern bool   arrowsVisible    = true;
extern string arrowsIdentifier = "RSI Arrows3";
extern double arrowsDisplaceUp = 1.0;
extern double arrowsDisplaceDn = 0.5;
extern color  arrowsUpColor    = clrDeepSkyBlue;
extern color  arrowsDnColor    = clrRed;
extern int    arrowsUpCode     = 233;
extern int    arrowsDnCode     = 234;
extern int    arrowsUpSize     = 1;
extern int    arrowsDnSize     = 1;



extern string _______________________Set_Arrows2;
//extern int            ArrowCodeUp     = 233;          // Arrow code up
//extern int            ArrowCodeDn     = 234;          // Arrow code down
extern double         ArrowGapUp      = 20.0;          // Gap for arrow up        
extern double         ArrowGapDn      = 80.0;          // Gap for arrow down
extern int            ArrowSize       = 2;            // Arrow size
double Up[];
double Down[];

enum e_arrowUp{
 Dot1=159,
 Dot2=108,
 Square1=167,
 Square2=110,
 Diamond1=119,
 Checkmark=252,
 Star=171,
 Star2=181,
 Smile=74,
 Center=164,
 Arrow1=233,
 Arrow2=225,
  Arrow3=241,
 Arrow4=221,
 Arrow5=200,
 Arrow6=217
};

enum e_arrowDn{
 Dot1_=159,
 Dot2_=108,
 Square1_=167,
 Square2_=110,
 Diamond1_=119,
 Checkmark_=252,
 Star_=171,
 Star2_=181,
 Smile_=74,
 Center_=164,
 Arrow1_=234,
 Arrow2_=226,
 Arrow3_=242,
 Arrow4_=222,
 Arrow5_=202,
 Arrow6_=218
};

input e_arrowUp   ArrowCodeUp    =  Arrow1;
input e_arrowDn   ArrowCodeDn     =  Arrow1_;

double rsi1[], rsi2[], rsi3[], trend[];
int    RSILevel = 50;
//+------------------------------------------------------------------+
int init()
{
   timeFrame = MathMax(timeFrame,_Period);
   IndicatorBuffers(6);
      SetIndexBuffer(0,rsi1); SetIndexLabel(0, "RSI(" + IntegerToString(RsiPeriod1) + ")");
      SetIndexBuffer(1,rsi2); SetIndexLabel(1, "RSI(" + IntegerToString(RsiPeriod2) + ")");
      SetIndexBuffer(2,rsi3); SetIndexLabel(2, "RSI(" + IntegerToString(RsiPeriod3) + ")");
      SetIndexBuffer(3,Up);    SetIndexStyle(3,DRAW_ARROW,0,ArrowSize); SetIndexArrow(3,ArrowCodeUp);
      SetIndexBuffer(4,Down);  SetIndexStyle(4,DRAW_ARROW,0,ArrowSize); SetIndexArrow(4,ArrowCodeDn);
      SetIndexBuffer(5,trend);
         RsiPeriod1 = MathMax(RsiPeriod1,1);
         RsiPeriod2 = MathMax(RsiPeriod2,1);
         RsiPeriod3 = MathMax(RsiPeriod3,1);
   
         
   IndicatorShortName("RSIx3 ("+RsiPeriod1+","+RsiPeriod2+","+RsiPeriod3+")");
   return(0);
}
int deinit()
{
   deleteArrows(); 
   
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(MathMax(Bars-counted_bars,3*timeFrame/_Period),Bars-1);

   //
   //
   //
   //
   //
   //

   for (i=limit; i >= 0; i--)
   {
      int y = iBarShift(NULL,timeFrame,Time[i]);
      rsi1[i]  = iRSI(NULL,timeFrame,RsiPeriod1,RSIPrice1,y);
      rsi2[i]  = iRSI(NULL,timeFrame,RsiPeriod2,RSIPrice2,y);
      rsi3[i]  = iRSI(NULL,timeFrame,RsiPeriod3,RSIPrice3,y);
      trend[i] = 0;
      if (UseRSIlevel50)
          {
            if (rsi1[i]>rsi2[i] && rsi2[i]>rsi3[i] && rsi1[i]>RSILevel && rsi2[i]>RSILevel && rsi3[i]>RSILevel) trend[i]= 1;
            if (rsi1[i]<rsi2[i] && rsi2[i]<rsi3[i] && rsi1[i]<RSILevel && rsi2[i]<RSILevel && rsi3[i]<RSILevel) trend[i]=-1;
          }
      else
          {
            if (rsi1[i]>rsi2[i] && rsi2[i]>rsi3[i]) trend[i]= 1;
            if (rsi1[i]<rsi2[i] && rsi2[i]<rsi3[i]) trend[i]=-1;   
            
            
            
           // if (i<Bars-1 && trend[i]!=trend[i+1])
         {
           // if (trend[i] ==  1) Up[i]   = Low[i] -iATR(NULL,0,15,i)*ArrowGapUp;
           // if (trend[i] == -1) Down[i] = High[i]+iATR(NULL,0,15,i)*ArrowGapDn;
            
           // if (trend[i] ==  1) Up[i]   =  ArrowGapUp ;
           // if (trend[i] == -1) Down[i] =  ArrowGapDn;
            
            
         }
            
                  
          }
            manageArrow(i); 
    }
    
    //
    //
    //
    //
    //
      
    if (alertsOn)
    {
       if (alertsOnCurrent)
            int whichBar = 0;
       else     whichBar = 1;
       if (trend[whichBar] != trend[whichBar+1])
       if (trend[whichBar] == 1)
             doAlert("Buy");
       else  doAlert("Sell");       
    }

return(0);
}

//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
//
//
//

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(timeFrame+" "+Symbol()," at ",TimeToStr(TimeLocal(),TIME_SECONDS)," RSIx3 ",doWhat);
             if (alertsMessage) Alert(message);
             if (alertsNotify)  SendNotification(StringConcatenate(Symbol(), Period() ," RSIx3 " +" "+message));
             if (alertsEmail)   SendMail(StringConcatenate(Symbol()," RSIx3 "),message);
             if (alertsSound)   PlaySound(soundFile);
      }
}

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

void manageArrow(int i)
{
   if (arrowsVisible)
   {
     if (trend[i] != trend[i+1])
     {
       deleteArrow(Time[i]);
       if (trend[i] == 1) drawArrow(i,arrowsUpColor,arrowsUpCode,arrowsUpSize,false);
       if (trend[i] ==-1) drawArrow(i,arrowsDnColor,arrowsDnCode,arrowsDnSize,true);
       
       if (trend[i] ==  1) Up[i]   =  ArrowGapUp ;
            if (trend[i] == -1) Down[i] =  ArrowGapDn;
       
       
       
       
       
     }                                            
      
   }
}               

//
//
//
//
//

void drawArrow(int i,color theColor,int theCode,int theSize, bool up)
{
   string name = arrowsIdentifier+":"+Time[i];
   double gap  = iATR(NULL,0,20,i);   
   
      //
      //
      //
      //
      //
      
      ObjectCreate(name,OBJ_ARROW,0,Time[i],0);
         ObjectSet(name,OBJPROP_ARROWCODE,theCode );
         ObjectSet(name,OBJPROP_COLOR,    theColor);
         ObjectSet(name,OBJPROP_WIDTH,    theSize );      
         
         if (up)
              ObjectSet(name,OBJPROP_PRICE1,High[i] + arrowsDisplaceDn * gap);      
         else ObjectSet(name,OBJPROP_PRICE1, Low[i] - arrowsDisplaceUp * gap);
}

//
//
//
//
//

void deleteArrows()
{
   string lookFor       = arrowsIdentifier+":";
   int    lookForLength = StringLen(lookFor);
   for (int i=ObjectsTotal()-1; i>=0; i--)
   {
      string objectName = ObjectName(i);
         if (StringSubstr(objectName,0,lookForLength) == lookFor) ObjectDelete(objectName);
   }
}

//
//
//
//
//

void deleteArrow(datetime time)
{
   string lookFor = arrowsIdentifier+":"+time; ObjectDelete(lookFor);
}


