//+------------------------------------------------------------------+
//|                                               RSI_Strike(AM).mq4 |
//|                                               Andrey Matvievskiy |
//|                                                                  |
//| this version made by mladen                                      |
//| www.forex-tsd.com                                                |
//+------------------------------------------------------------------+
#property copyright "Andrey Matvievskiy"
#property link      ""
//----
#property indicator_chart_window
#property indicator_buffers 8
#property indicator_color1 Aqua
#property indicator_color2 Aqua
#property indicator_color3 Silver
#property indicator_color4 Silver
#property indicator_color5 Orange
#property indicator_color6 Orange
#property indicator_color7 Red
#property indicator_color8 Red

//
//
//
//
//

extern string TimeFrame          = "Current time frame";
extern int    RsiPeriod1        = 2;
extern int    RsiPeriod2        = 4;
extern int    RsiPrice1         = 0;
extern int    RsiPrice2         = 0;
extern int    RsiPeriod3        = 3;
extern int    RsiPeriod4        = 6;
extern int    RsiPrice3         = 0;
extern int    RsiPrice4         = 0;
extern int    RsiPeriod5        = 4;
extern int    RsiPeriod6        = 8;
extern int    RsiPrice5         = 0;
extern int    RsiPrice6         = 0;
extern int    RsiPeriod7        = 5;
extern int    RsiPeriod8        = 10;
extern int    RsiPrice7         = 0;
extern int    RsiPrice8         = 0;
extern bool   alertsOn           = false;
extern bool   alertsOnEveryArrow = false;
extern bool   alertsOnLevel1     = true;
extern bool   alertsOnLevel2     = true;
extern bool   alertsOnLevel3     = true;
extern bool   alertsOnLevel4     = true;
extern bool   alertsOnCurrent    = false;
extern bool   alertsMessage      = true;
extern bool   alertsSound        = false;
extern bool   alertsEmail        = false;

//
double CrossUp1[];
double CrossDown1[];
double CrossUp2[];
double CrossDown2[];
double CrossUp3[];
double CrossDown3[];
double CrossUp4[];
double CrossDown4[];

//
//
//
//
//

string indicatorFileName;
bool   returnBars;
bool   calculateValue;
int    timeFrame;

//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
//
//

int init()
{
   SetIndexBuffer(0, CrossUp1);   SetIndexStyle(0, DRAW_ARROW,0,0); SetIndexArrow(0, 233);
   SetIndexBuffer(1, CrossDown1); SetIndexStyle(1, DRAW_ARROW,0,0); SetIndexArrow(1, 234);
   SetIndexBuffer(2, CrossUp2);   SetIndexStyle(2, DRAW_ARROW,0,0); SetIndexArrow(2, 233);
   SetIndexBuffer(3, CrossDown2); SetIndexStyle(3, DRAW_ARROW,0,0); SetIndexArrow(3, 234);
   SetIndexBuffer(4, CrossUp3);   SetIndexStyle(4, DRAW_ARROW,0,0); SetIndexArrow(4, 233);
   SetIndexBuffer(5, CrossDown3); SetIndexStyle(5, DRAW_ARROW,0,0); SetIndexArrow(5, 234);
   SetIndexBuffer(6, CrossUp4);   SetIndexStyle(6, DRAW_ARROW,0,0); SetIndexArrow(6, 233);
   SetIndexBuffer(7, CrossDown4); SetIndexStyle(7, DRAW_ARROW,0,0); SetIndexArrow(7, 234);
   
      //
      //
      //
      //
      //
      
         indicatorFileName = WindowExpertName();
         calculateValue    = TimeFrame=="calculateValue"; if (calculateValue) { return(0); }
         returnBars        = TimeFrame=="returnBars";     if (returnBars)     { return(0); }
         timeFrame         = stringToTimeFrame(TimeFrame);
      
      //
      //
      //
      //
      //
   
   return(0);
}
int deinit()
{
   return(0);
}

//------------------------------------------------------------------
//
//------------------------------------------------------------------
int start() 
{
   int counted_bars=IndicatorCounted();
      if(counted_bars<0) return(-1);
      if(counted_bars>0) counted_bars--;
           int limit=MathMin(Bars-counted_bars,Bars-1);
           if (returnBars) { CrossUp1[0] = MathMin(limit+1,Bars-1); return(0); }

   //
   //
   //
   //
   //
      
   if (calculateValue || timeFrame == Period())
   {
     for(int i=0; i<=limit; i++) 
     {
      double Range=0;
        for(int k=0 ;k<=10;k++) Range+=MathAbs(High[i+k]-Low[i+k]);
                                Range/=10;
     
         double A_1 = iRSI(NULL, 0, RsiPeriod1, RsiPrice1, i);
         double A_2 = iRSI(NULL, 0, RsiPeriod1, RsiPrice1, i+1);
         double B_1 = iRSI(NULL, 0, RsiPeriod2, RsiPrice2, i);
         double B_2 = iRSI(NULL, 0, RsiPeriod2, RsiPrice2, i+1);
         double C_1 = iRSI(NULL, 0, RsiPeriod3, RsiPrice3, i);
         double C_2 = iRSI(NULL, 0, RsiPeriod3, RsiPrice3, i+1);
         double D_1 = iRSI(NULL, 0, RsiPeriod4, RsiPrice4, i);
         double D_2 = iRSI(NULL, 0, RsiPeriod4, RsiPrice4, i+1);
         double E_1 = iRSI(NULL, 0, RsiPeriod5, RsiPrice5, i);
         double E_2 = iRSI(NULL, 0, RsiPeriod5, RsiPrice5, i+1);
         double F_1 = iRSI(NULL, 0, RsiPeriod6, RsiPrice6, i);
         double F_2 = iRSI(NULL, 0, RsiPeriod6, RsiPrice6, i+1);
         double G_1 = iRSI(NULL, 0, RsiPeriod7, RsiPrice7, i);
         double G_2 = iRSI(NULL, 0, RsiPeriod7, RsiPrice7, i+1);
         double K_1 = iRSI(NULL, 0, RsiPeriod8, RsiPrice8, i);
         double K_2 = iRSI(NULL, 0, RsiPeriod8, RsiPrice8, i+1);

         CrossUp1[i]   = EMPTY_VALUE;
         CrossUp2[i]   = EMPTY_VALUE;
         CrossUp3[i]   = EMPTY_VALUE;
         CrossUp4[i]   = EMPTY_VALUE;
         CrossDown1[i] = EMPTY_VALUE;
         CrossDown2[i] = EMPTY_VALUE;
         CrossDown3[i] = EMPTY_VALUE;
         CrossDown4[i] = EMPTY_VALUE;

         if ((A_1 > B_1) &&(A_2 < B_2))
         {
            CrossUp1[i]=Low[i] - Range*0.2;
         }
         else  if ((A_1 < B_1) &&(A_2 > B_2))
         {
            CrossDown1[i]=High[i] + Range*0.2;
         }
        
         if ((C_1 > D_1) &&(C_2 < D_2))
         {
            CrossUp2[i]=Low[i] - Range*0.4;
         }
         else  if ((C_1 < D_1) &&(C_2 > D_2))
         {
            CrossDown2[i]=High[i] + Range*0.4;
         }
        
         if ((E_1 > F_1) &&(E_2 < F_2))
         {
            CrossUp3[i]=Low[i] - Range*0.6;
         }
         else  if ((E_1 < F_1) &&(E_2 > F_2))
         {
            CrossDown3[i]=High[i] + Range*0.6;
         }
         if ((G_1 > K_1) &&(G_2 < K_2))
         {
            CrossUp4[i]=Low[i] - Range*0.8;
         }
         else  if ((G_1 < K_1) &&(G_2 > K_2))
         {
            CrossDown4[i]=High[i] + Range*0.8;
         }
      }
      manageAlerts();
      return(0);
   }
   
   //
   //
   //
   //
   //

   limit = MathMax(limit,MathMin(Bars-1,iCustom(NULL,timeFrame,indicatorFileName,"returnBars",0,0)*timeFrame/Period()));
   for (i=limit; i>=0; i--)
   {
      CrossUp1[i]   = EMPTY_VALUE;
      CrossUp2[i]   = EMPTY_VALUE;
      CrossUp3[i]   = EMPTY_VALUE;
      CrossUp4[i]   = EMPTY_VALUE;
      CrossDown1[i] = EMPTY_VALUE;
      CrossDown2[i] = EMPTY_VALUE;
      CrossDown3[i] = EMPTY_VALUE;
      CrossDown4[i] = EMPTY_VALUE;
         int y = iBarShift(NULL,timeFrame,Time[i]);
         int z = iBarShift(NULL,timeFrame,Time[i+1]);
            if (y==z) continue;
            
            //
            //
            //
            //
            //
            
            CrossUp1[i]   = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",RsiPeriod1,RsiPeriod2,RsiPrice1,RsiPrice2,RsiPeriod3,RsiPeriod4,RsiPrice3,RsiPrice4,RsiPeriod5,RsiPeriod6,RsiPrice5,RsiPrice6,RsiPeriod7,RsiPeriod8,RsiPrice7,RsiPrice8,alertsOn,alertsOnEveryArrow,alertsOnLevel1,alertsOnLevel2,alertsOnLevel3,alertsOnLevel4,alertsOnCurrent,alertsMessage,alertsSound,alertsEmail,0,y);
            CrossDown1[i] = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",RsiPeriod1,RsiPeriod2,RsiPrice1,RsiPrice2,RsiPeriod3,RsiPeriod4,RsiPrice3,RsiPrice4,RsiPeriod5,RsiPeriod6,RsiPrice5,RsiPrice6,RsiPeriod7,RsiPeriod8,RsiPrice7,RsiPrice8,alertsOn,alertsOnEveryArrow,alertsOnLevel1,alertsOnLevel2,alertsOnLevel3,alertsOnLevel4,alertsOnCurrent,alertsMessage,alertsSound,alertsEmail,1,y);
            CrossUp2[i]   = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",RsiPeriod1,RsiPeriod2,RsiPrice1,RsiPrice2,RsiPeriod3,RsiPeriod4,RsiPrice3,RsiPrice4,RsiPeriod5,RsiPeriod6,RsiPrice5,RsiPrice6,RsiPeriod7,RsiPeriod8,RsiPrice7,RsiPrice8,alertsOn,alertsOnEveryArrow,alertsOnLevel1,alertsOnLevel2,alertsOnLevel3,alertsOnLevel4,alertsOnCurrent,alertsMessage,alertsSound,alertsEmail,2,y);
            CrossDown2[i] = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",RsiPeriod1,RsiPeriod2,RsiPrice1,RsiPrice2,RsiPeriod3,RsiPeriod4,RsiPrice3,RsiPrice4,RsiPeriod5,RsiPeriod6,RsiPrice5,RsiPrice6,RsiPeriod7,RsiPeriod8,RsiPrice7,RsiPrice8,alertsOn,alertsOnEveryArrow,alertsOnLevel1,alertsOnLevel2,alertsOnLevel3,alertsOnLevel4,alertsOnCurrent,alertsMessage,alertsSound,alertsEmail,3,y);
            CrossUp3[i]   = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",RsiPeriod1,RsiPeriod2,RsiPrice1,RsiPrice2,RsiPeriod3,RsiPeriod4,RsiPrice3,RsiPrice4,RsiPeriod5,RsiPeriod6,RsiPrice5,RsiPrice6,RsiPeriod7,RsiPeriod8,RsiPrice7,RsiPrice8,alertsOn,alertsOnEveryArrow,alertsOnLevel1,alertsOnLevel2,alertsOnLevel3,alertsOnLevel4,alertsOnCurrent,alertsMessage,alertsSound,alertsEmail,4,y);
            CrossDown3[i] = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",RsiPeriod1,RsiPeriod2,RsiPrice1,RsiPrice2,RsiPeriod3,RsiPeriod4,RsiPrice3,RsiPrice4,RsiPeriod5,RsiPeriod6,RsiPrice5,RsiPrice6,RsiPeriod7,RsiPeriod8,RsiPrice7,RsiPrice8,alertsOn,alertsOnEveryArrow,alertsOnLevel1,alertsOnLevel2,alertsOnLevel3,alertsOnLevel4,alertsOnCurrent,alertsMessage,alertsSound,alertsEmail,5,y);
            CrossUp4[i]   = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",RsiPeriod1,RsiPeriod2,RsiPrice1,RsiPrice2,RsiPeriod3,RsiPeriod4,RsiPrice3,RsiPrice4,RsiPeriod5,RsiPeriod6,RsiPrice5,RsiPrice6,RsiPeriod7,RsiPeriod8,RsiPrice7,RsiPrice8,alertsOn,alertsOnEveryArrow,alertsOnLevel1,alertsOnLevel2,alertsOnLevel3,alertsOnLevel4,alertsOnCurrent,alertsMessage,alertsSound,alertsEmail,6,y);
            CrossDown4[i] = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",RsiPeriod1,RsiPeriod2,RsiPrice1,RsiPrice2,RsiPeriod3,RsiPeriod4,RsiPrice3,RsiPrice4,RsiPeriod5,RsiPeriod6,RsiPrice5,RsiPrice6,RsiPeriod7,RsiPeriod8,RsiPrice7,RsiPrice8,alertsOn,alertsOnEveryArrow,alertsOnLevel1,alertsOnLevel2,alertsOnLevel3,alertsOnLevel4,alertsOnCurrent,alertsMessage,alertsSound,alertsEmail,7,y);
   }
   
   //
   //
   //
   //
   //

   return(0);
}

//-------------------------------------------------------------------
//
//-------------------------------------------------------------------
//
//
//
//
//

void manageAlerts()
{
   if (alertsOn)
   {
      if (alertsOnCurrent)
           int whichBar = 0;
      else     whichBar = 1;
      
      //
      //
      //
      //
      //
            
      if (alertsOnLevel1 && (CrossUp1[whichBar]!=EMPTY_VALUE || CrossDown1[whichBar]!=EMPTY_VALUE))
      {
         static datetime time1 = 0;
         static string   mess1 = "";
            if (!alertsOnEveryArrow) time1 = Time[0];
            if (CrossUp1[whichBar]   != EMPTY_VALUE) doAlert(time1,mess1," rsi strike level 1 signal up");
            if (CrossDown1[whichBar] != EMPTY_VALUE) doAlert(time1,mess1," rsi strike level 1 signal down");
      }
      if (alertsOnLevel2 && (CrossUp2[whichBar]!=EMPTY_VALUE || CrossDown2[whichBar]!=EMPTY_VALUE))
      {
         static datetime time2 = 0;
         static string   mess2 = "";
            if (!alertsOnEveryArrow) time2 = Time[0];
            if (CrossUp2[whichBar]   != EMPTY_VALUE) doAlert(time2,mess2," rsi strike level 2 signal up");
            if (CrossDown2[whichBar] != EMPTY_VALUE) doAlert(time2,mess2," rsi strike level 2 signal down");
      }
      if (alertsOnLevel3 && (CrossUp3[whichBar]!=EMPTY_VALUE || CrossDown3[whichBar]!=EMPTY_VALUE))
      {
         static datetime time3 = 0;
         static string   mess3 = "";
            if (!alertsOnEveryArrow) time3 = Time[0];
            if (CrossUp3[whichBar]   != EMPTY_VALUE) doAlert(time3,mess3," rsi strike level 3 signal up");
            if (CrossDown3[whichBar] != EMPTY_VALUE) doAlert(time3,mess3," rsi strike level 3 signal down");
      }
      if (alertsOnLevel4 && (CrossUp4[whichBar]!=EMPTY_VALUE || CrossDown4[whichBar]!=EMPTY_VALUE))
      {
         static datetime time4 = 0;
         static string   mess4 = "";
            if (!alertsOnEveryArrow) time4 = Time[0];
            if (CrossUp4[whichBar]   != EMPTY_VALUE) doAlert(time4,mess4," rsi strike level 4 signal up");
            if (CrossDown4[whichBar] != EMPTY_VALUE) doAlert(time4,mess4," rsi strike level 4 signal down");
      }
   }
}

//
//
//
//
//

void doAlert(datetime& previousTime, string& previousAlert, string doWhat)
{
   string message;
   
   if (previousAlert != doWhat || previousTime != Time[0]) {
       previousAlert  = doWhat;
       previousTime   = Time[0];

       //
       //
       //
       //
       //

       message = timeFrameToString(Period())+" "+Symbol()+" at "+TimeToStr(TimeLocal(),TIME_SECONDS)+doWhat;
          if (alertsMessage) Alert(message);
          if (alertsEmail)   SendMail(Symbol()+" rsi strike",message);
          if (alertsSound)   PlaySound("alert2.wav");
   }
}

//-------------------------------------------------------------------
//
//-------------------------------------------------------------------
//
//
//
//
//

string sTfTable[] = {"M1","M5","M15","M30","H1","H4","D1","W1","MN"};
int    iTfTable[] = {1,5,15,30,60,240,1440,10080,43200};

//
//
//
//
//

int stringToTimeFrame(string tfs)
{
   tfs = stringUpperCase(tfs);
   for (int i=ArraySize(iTfTable)-1; i>=0; i--)
         if (tfs==sTfTable[i] || tfs==""+iTfTable[i]) return(MathMax(iTfTable[i],Period()));
                                                      return(Period());
}
string timeFrameToString(int tf)
{
   for (int i=ArraySize(iTfTable)-1; i>=0; i--) 
         if (tf==iTfTable[i]) return(sTfTable[i]);
                              return("");
}

//
//
//
//
//

string stringUpperCase(string str)
{
   string   s = str;

   for (int length=StringLen(str)-1; length>=0; length--)
   {
      int char = StringGetChar(s, length);
         if((char > 96 && char < 123) || (char > 223 && char < 256))
                     s = StringSetChar(s, length, char - 32);
         else if(char > -33 && char < 0)
                     s = StringSetChar(s, length, char + 224);
   }
   return(s);
}