//------------------------------------------------------------------
#property copyright "mladen"
#property link      "mladenfx@gmail.com"
//------------------------------------------------------------------
#property indicator_separate_window
#property indicator_buffers 5
#property indicator_color1  Lime
#property indicator_color2  Orange
#property indicator_color3  Gray
#property indicator_color4  PaleVioletRed
#property indicator_color5  Gold
#property indicator_width3  2
#property indicator_width4  2
#property indicator_style5  STYLE_DOT

//
//
//
//
//

extern int FastEma1 = 12;
extern int SlowEma1 = 26;
extern int Price1   = PRICE_CLOSE;
extern int FastEma2 = 24;
extern int SlowEma2 = 52;
extern int Price2   = PRICE_CLOSE;
extern int FastEma3 = 0;
extern int SlowEma3 = 0;
extern int Price3   = PRICE_CLOSE;
extern int FastEma4 = 0;
extern int SlowEma4 = 0;
extern int Price4   = PRICE_CLOSE;
extern int FastEma5 = 0;
extern int SlowEma5 = 0;
extern int Price5   = PRICE_CLOSE;
extern int FastEma6 = 0;
extern int SlowEma6 = 0;
extern int Price6   = PRICE_CLOSE;
extern int FastEma7 = 0;
extern int SlowEma7 = 0;
extern int Price7   = PRICE_CLOSE;
extern int FastEma8 = 0;
extern int SlowEma8 = 0;
extern int Price8   = PRICE_CLOSE;
extern int SignalPeriod = 9;
extern int SignalMaMode = MODE_EMA;
extern bool   LinesVisible = false;
extern string LinesID      = "CompMACD1";
extern color  LinesUpColor = LimeGreen;
extern color  LinesDnColor = OrangeRed;
extern int    LinesStyle   = STYLE_SOLID;
extern int    LinesWidth   = 0;

double macd[];
double hist[];
double histu[];
double histd[];
double sign[];
double cross[];
string shortName;
//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
//
//
//

int init()
{
   IndicatorBuffers(6);
   SetIndexBuffer(0,histu); SetIndexStyle(0,DRAW_HISTOGRAM);
   SetIndexBuffer(1,histd); SetIndexStyle(1,DRAW_HISTOGRAM);
   SetIndexBuffer(2,hist);
   SetIndexBuffer(3,macd);
   SetIndexBuffer(4,sign);
   SetIndexBuffer(5,cross);
      shortName = LinesID+" MACD - composite";
   IndicatorShortName(shortName);
   return(0);
}
int deinit()
{
   string find = LinesID+":";
   for (int i=ObjectsTotal()-1; i>= 0; i--)
   {
      string name = ObjectName(i); if (StringFind(name,find)==0) ObjectDelete(name);
   }
   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);
           int window = WindowFind(shortName);

   //
   //
   //
   //
   //
   
 	for(int i = limit; i>=0; i--)
 	{
 	    macd[i] = 0;
 	    if (FastEma1>0 && SlowEma1>0) macd[i] += iMACD(NULL,0,FastEma1,SlowEma1,1,Price1,MODE_MAIN,i);
 	    if (FastEma2>0 && SlowEma2>0) macd[i] += iMACD(NULL,0,FastEma2,SlowEma2,1,Price2,MODE_MAIN,i);
 	    if (FastEma3>0 && SlowEma3>0) macd[i] += iMACD(NULL,0,FastEma3,SlowEma3,1,Price3,MODE_MAIN,i);
 	    if (FastEma4>0 && SlowEma4>0) macd[i] += iMACD(NULL,0,FastEma4,SlowEma4,1,Price4,MODE_MAIN,i);
 	    if (FastEma5>0 && SlowEma5>0) macd[i] += iMACD(NULL,0,FastEma5,SlowEma5,1,Price5,MODE_MAIN,i);
 	    if (FastEma6>0 && SlowEma6>0) macd[i] += iMACD(NULL,0,FastEma6,SlowEma6,1,Price6,MODE_MAIN,i);
 	    if (FastEma7>0 && SlowEma7>0) macd[i] += iMACD(NULL,0,FastEma7,SlowEma7,1,Price7,MODE_MAIN,i);
 	    if (FastEma8>0 && SlowEma8>0) macd[i] += iMACD(NULL,0,SlowEma8,SlowEma8,1,Price8,MODE_MAIN,i);
   } 	    
 	for(i = limit; i>=0; i--)
 	{
 	    sign[i]  = iMAOnArray(macd,0,SignalPeriod,0,SignalMaMode,i);
 	    hist[i]  = macd[i]-sign[i];
 	    histu[i] = EMPTY_VALUE;
 	    histd[i] = EMPTY_VALUE;
 	    cross[i] = cross[i+1];
 	       if (hist[i]>0) { histu[i] = hist[i]; cross[i]= 1; }
 	       if (hist[i]<0) { histd[i] = hist[i]; cross[i]=-1; }
 	       if (LinesVisible && window>-1)
 	       {
 	          string name = LinesID+":"+Time[i];
 	             ObjectDelete(name);
 	             if (cross[i]!=cross[i+1])
 	             {
 	                color theColor  = LinesUpColor; if (cross[i]==-1) theColor = LinesDnColor;
 	                   ObjectCreate(name,OBJ_VLINE,window,Time[i],0);
 	                      ObjectSet(name,OBJPROP_WIDTH,LinesWidth);
 	                      ObjectSet(name,OBJPROP_STYLE,LinesStyle);
 	                      ObjectSet(name,OBJPROP_COLOR,theColor);
 	             }
 	       }
 	}
   return(0);
}