#property indicator_separate_window
#property indicator_color1 DodgerBlue
#property indicator_color2 Lime
#property indicator_buffers 2
#property indicator_level3 0
#property indicator_levelcolor DimGray

extern string  TimeFrame    = "Current time frame";		
extern int  CCIPeriod       = 14;
extern int  MaPeriod        = 50;
extern int  MaType          = MODE_EMA;
extern bool Alert_MA        = true;
extern bool Alert_Level     = true;
extern int  Bottom_Level    = -90;
extern int  Top_Level       = 90;
extern bool AlertsMessage   = true;
extern bool AlertsSound     = false;
extern bool AlertsEmail     = false;
extern bool AlertsMobile    = false;
extern int  SignalBar       = 0;

int TimeBar,timeFrame;
double cci[],
       ema[],
       cciTF[],
       emaTF[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   IndicatorBuffers(4);
   
   SetIndexBuffer(0,cci);
   SetIndexBuffer(1,ema);
   
   SetIndexStyle(0,DRAW_LINE,STYLE_SOLID,2);
   SetIndexStyle(1,DRAW_LINE,STYLE_SOLID,2);
   
   SetIndexBuffer(2,cciTF);
   SetIndexBuffer(3,emaTF);
   
   SetLevelValue (0,Bottom_Level);
   SetLevelValue (1,Top_Level);
   
   timeFrame = stringToTimeFrame(TimeFrame);
   IndicatorShortName("MTF CCI("+CCIPeriod+") "+timeFrameToString(timeFrame));
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   
//----
     int bbshift,limit,limitTF;
     limit=Bars;
     limitTF = iBars(Symbol(),TimeFrame);
     string sym=(string)Symbol();
     
     for(int i=0; i<limitTF; i++) cciTF[i] = iCCI(Symbol(),TimeFrame,CCIPeriod,PRICE_TYPICAL,i);
     for(int j=0; j<limitTF; j++) emaTF[j] = iMAOnArray(cciTF,0,MaPeriod,0,MaType,j);
     
     for(int t=0; t<limit; t++)
     {
         bbshift = iBarShift(Symbol(),TimeFrame,Time[t]);
         cci[t] = cciTF[bbshift];
         ema[t] = emaTF[bbshift];
     }
     
for(i=0; i<limitTF; i++) 
{    
if(AlertsMessage || AlertsEmail || AlertsMobile || AlertsSound)
 { 
 if(Alert_MA)
  {
   string message1 = ("CCI_EMA_MTF - "+sym+" TF("+Period()+") - Trend Up");
   string message2 = ("CCI_EMA_MTF - "+sym+" TF("+Period()+") - Trend Dn");
       
    if(TimeBar!=Time[0] && cci[SignalBar]>ema[SignalBar] && cci[SignalBar+1]<ema[SignalBar+1])
     { 
        if (AlertsMessage)Alert(message1);
        if (AlertsEmail)  SendMail(sym+" CCI_EMA_MTF",message1);
        if (AlertsMobile) SendNotification(message1);
        if (AlertsSound)  PlaySound("alert2.wav");
        TimeBar=Time[0];
     }
    if(TimeBar!=Time[0] && cci[SignalBar]<ema[SignalBar] && cci[SignalBar+1]>ema[SignalBar+1])
     { 
        if (AlertsMessage)Alert(message2);
        if (AlertsEmail)  SendMail(sym+" CCI_EMA_MTF",message2);
        if (AlertsMobile) SendNotification(message2);
        if (AlertsSound)  PlaySound("alert2.wav");
        TimeBar=Time[0];
    }
   }
   
  if(Alert_Level)
   {
   string message3 = ("CCI_EMA_MTF - "+sym+" TF("+Period()+") - Top Level: "+Top_Level);
   string message4 = ("CCI_EMA_MTF - "+sym+" TF("+Period()+") - Bottom Level: "+Bottom_Level);
       
    if(TimeBar!=Time[0] && cci[SignalBar]>Top_Level && cci[SignalBar+1]<Top_Level)
     { 
        if (AlertsMessage)Alert(message3);
        if (AlertsEmail)  SendMail(sym+" CCI_EMA_MTF",message3);
        if (AlertsMobile) SendNotification(message3);
        if (AlertsSound)  PlaySound("alert2.wav");
        TimeBar=Time[0];
     }
    if(TimeBar!=Time[0] && cci[SignalBar]<Bottom_Level && cci[SignalBar+1]>Bottom_Level)
     { 
        if (AlertsMessage)Alert(message4);
        if (AlertsEmail)  SendMail(sym+" CCI_EMA_MTF",message4);
        if (AlertsMobile) SendNotification(message4);
        if (AlertsSound)  PlaySound("alert2.wav");
        TimeBar=Time[0];
    }
   }   
 }
} 
//----
   return(0);
}
//+------------------------------------------------------------------+

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 charc = StringGetChar(s, length);
         if((charc > 96 && charc < 123) || (charc > 223 && charc < 256))
                     s = StringSetChar(s, length, charc - 32);
         else if(charc > -33 && charc < 0)
                     s = StringSetChar(s, length, charc + 224);
   }
   return(s);
}