#define indicatorName "All MACD"
#property indicator_separate_window
#property  indicator_buffers 4
#property  indicator_color1  DarkGray
#property  indicator_color2  Lime
#property  indicator_color3  Red
#property  indicator_color4  Yellow
#property indicator_level1  0
#property indicator_levelcolor DimGray
extern int FastEMA=12;
extern int SlowEMA=26;
extern int SignalSMA=9;
extern string timeFrames           = "H1;H4";
extern int    barsPerTimeFrame     = 50;
extern bool   shiftRight           = False;
extern bool   currentFirst         = False; 
extern color  txtColor             = Silver; 
extern color  separatorColor       = Red; 
int multiple[9]={128,128,64,32,16,8,4,2,1};
double     MacdBuffer0[];
double     MacdBuffer1[];
double     MacdBuffer2[];
double     SignalBuffer[];
string shortName;
string labels[];
int    periods[];
int    Shift; 

int init()
{
   if (shiftRight) Shift = 1;      else Shift = 0;
   barsPerTimeFrame= MathMax(barsPerTimeFrame,15);
   shortName = "All TF MACD("+FastEMA+","+SlowEMA+","+SignalSMA+")";
   IndicatorShortName(shortName);
   SetIndexBuffer(0,MacdBuffer0);
   SetIndexBuffer(1,MacdBuffer1);
   SetIndexBuffer(2,MacdBuffer2);
   SetIndexBuffer(3,SignalBuffer);
   SetIndexStyle(0,DRAW_LINE);
   SetIndexStyle(1,DRAW_HISTOGRAM);
   SetIndexStyle(2,DRAW_HISTOGRAM);
   SetIndexStyle(3,DRAW_LINE);
   SetIndexDrawBegin(3,SignalSMA);
   IndicatorDigits(Digits+1);
   SetIndexShift(0,Shift*(barsPerTimeFrame+1));
   SetIndexShift(1,Shift*(barsPerTimeFrame+1));
   SetIndexShift(2,Shift*(barsPerTimeFrame+1));
   SetIndexShift(3,Shift*(barsPerTimeFrame+1));
   SetIndexLabel(0,"MACD");
   SetIndexLabel(1,"MACD");
   SetIndexLabel(2,"MACD");   
   SetIndexLabel(3,"Signal");
      timeFrames = StringUpperCase(StringTrimLeft(StringTrimRight(timeFrames))); 
      if (StringSubstr(timeFrames,StringLen(timeFrames),1) != ";") timeFrames = StringConcatenate(timeFrames,";");
      int s = 0;
      int i = StringFind(timeFrames,";",s);
      int time;
      string current;
      while (i > 0)
        {
           current = StringSubstr(timeFrames,s,i-s);
           time    = stringToTimeFrame(current);
           if (time > 0) {
                 ArrayResize(labels ,ArraySize(labels)+1);
                 ArrayResize(periods,ArraySize(periods)+1);
                 labels[ArraySize(labels)-1] = current;
                 periods[ArraySize(periods)-1] = time; }
           s = i + 1; 
           i = StringFind(timeFrames,";",s);
        }
      if(currentFirst)  
         for (i=1;i<ArraySize(periods);i++)
         if (Period()==periods[i])
           {
             string tmpLbl = labels[i];
             int    tmpPer = periods[i];
             for (int k=i ;k>0; k--)
                {
                   labels[k]  = labels[k-1];
                   periods[k] = periods[k-1];
                }                     
             labels[0]  = tmpLbl;
             periods[0] = tmpPer;
           }
   return(0);
}

int deinit()
{
   for(int l=0;l<ArraySize(periods);l++) {
         ObjectDelete(indicatorName+l);
         ObjectDelete(indicatorName+l+"label");
      }         
   return(0);
}

int start()
{
     string separator;
     int    window=WindowFind(shortName);
     int    k=0;
     for(int p=0; p<ArraySize(periods);p++)
          {
            for(int i=0; i<barsPerTimeFrame;i++,k++)
                {
                 MacdBuffer0[k] = iMACD(NULL,periods[p],FastEMA,SlowEMA,SignalSMA,0,0,i)*multiple[p];
                 SignalBuffer[k]= iMACD(NULL,periods[p],FastEMA,SlowEMA,SignalSMA,0,1,i)*multiple[p];
                 double temp=(MacdBuffer0[k]-SignalBuffer[k])*1.3;
                 if(temp>=0)  {MacdBuffer1[k]=temp;MacdBuffer2[k]=EMPTY_VALUE;} else {MacdBuffer2[k]=temp;MacdBuffer1[k]=EMPTY_VALUE;}
                }
            MacdBuffer0[k]  = EMPTY_VALUE;
            SignalBuffer[k] = EMPTY_VALUE;
            MacdBuffer1[k]  = EMPTY_VALUE;
            MacdBuffer2[k]  = EMPTY_VALUE;
            k += 1;
            separator = indicatorName+p;
            if(ObjectFind(separator)==-1)   ObjectCreate(separator,OBJ_TREND,window,0,0);
            ObjectSet(separator,OBJPROP_TIME1,barTime(k-Shift*(barsPerTimeFrame+1)-1));
            ObjectSet(separator,OBJPROP_TIME2,barTime(k-Shift*(barsPerTimeFrame+1)-1));
            ObjectSet(separator,OBJPROP_PRICE1,-0.1);
            ObjectSet(separator,OBJPROP_PRICE2,0.1);
            ObjectSet(separator,OBJPROP_COLOR ,separatorColor);
            ObjectSet(separator,OBJPROP_WIDTH ,2);
            separator = indicatorName+p+"label";
            if(ObjectFind(separator)==-1)   ObjectCreate(separator,OBJ_TEXT,window,0,0);
            ObjectSet(separator,OBJPROP_TIME1,barTime(k-Shift*(barsPerTimeFrame+1)-5));
            ObjectSet(separator,OBJPROP_PRICE1,0);            
            ObjectSetText(separator,labels[p],9,"Arial",txtColor);
         }
     SetIndexDrawBegin(0,Bars-k);
     SetIndexDrawBegin(1,Bars-k);              
     return(0);
}

int barTime(int a)
{
   if(a<0) return(Time[0]+Period()*60*MathAbs(a));
   else  return(Time[a]);   
}

int stringToTimeFrame(string TimeFrame)
{
      int TimeFrameInt=0;
      if (TimeFrame=="M1")  TimeFrameInt=PERIOD_M1;
      if (TimeFrame=="M5")  TimeFrameInt=PERIOD_M5;
      if (TimeFrame=="M15") TimeFrameInt=PERIOD_M15;
      if (TimeFrame=="M30") TimeFrameInt=PERIOD_M30;
      if (TimeFrame=="H1")  TimeFrameInt=PERIOD_H1;
      if (TimeFrame=="H4")  TimeFrameInt=PERIOD_H4;
      if (TimeFrame=="D1")  TimeFrameInt=PERIOD_D1;
      if (TimeFrame=="W1")  TimeFrameInt=PERIOD_W1;
      if (TimeFrame=="MN")  TimeFrameInt=PERIOD_MN1;
      return(TimeFrameInt);
}

string StringUpperCase(string str)
{
   string   s = str;
   int      lenght = StringLen(str) - 1;
   int      char;
   while(lenght >= 0)
   {
         char = StringGetChar(s, lenght);
         if((char > 96 && char < 123) || (char > 223 && char < 256))   s = StringSetChar(s, lenght, char - 32);
         else  if(char > -33 && char < 0)    s = StringSetChar(s, lenght, char + 224);
         lenght--;
   }
   return(s);
}


