//+------------------------------------------------------------------+
//|                                             HMA CD Histo MTF.mq4 !
//|                                                           mladen |
//+------------------------------------------------------------------+
//mod2009fxtsd macd

#property copyright "mladen"
#property link ""

#property indicator_separate_window
#property indicator_buffers 3
#property indicator_color1  DarkViolet
#property indicator_color2  Orange
#property indicator_color3  Red
#property indicator_width1  1
#property indicator_width2  1
#property indicator_width3  1

extern ENUM_TIMEFRAMES TimeFrame  = PERIOD_CURRENT;

extern int HMA_Period1 = 21;
extern int HMA_Period2 = 34;

extern ENUM_MA_METHOD HMA_Method1 = MODE_LWMA;
extern ENUM_MA_METHOD HMA_Method2 = MODE_LWMA;
extern ENUM_APPLIED_PRICE HMA_PriceType1 = 0;
extern ENUM_APPLIED_PRICE HMA_PriceType2 = 0;

extern int smzPeriod = 5;
extern ENUM_MA_METHOD smzMethod = MODE_EMA;

string IndicatorFileName;

double ind_buffer1[];
double ind_buffer2[];
double sig_buffer[];
double buffer0[];
double buffer1[];
double buffer2[];
//+------------------------------------------------------------------
//|                                                                 |
//+------------------------------------------------------------------
int init()
  {
   TimeFrame=MathMax(TimeFrame,Period());

   IndicatorShortName("HMA CD ("+HMA_Period1+","+HMA_Period2+","+smzPeriod+") M"+TimeFrame);

   IndicatorBuffers(6);
   SetIndexBuffer(0,ind_buffer1);
   SetIndexBuffer(1,ind_buffer2);
   SetIndexBuffer(2,sig_buffer);
   SetIndexBuffer(3,buffer0);
   SetIndexBuffer(4,buffer1);
   SetIndexBuffer(5,buffer2);
   SetIndexStyle(0,DRAW_HISTOGRAM);
   SetIndexStyle(1,DRAW_HISTOGRAM);
   SetIndexStyle(2,DRAW_LINE);

   int draw_begin=smzPeriod+HMA_Period1+HMA_Period2;
   for(int i=0; i<indicator_buffers; i++)
     {
      SetIndexDrawBegin(i,draw_begin);
      SetIndexLabel(i,"Hull Moving Average");
     }

   IndicatorFileName=WindowExpertName();

   return(0);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int start()
  {
   int HalfPeriod1=MathFloor(HMA_Period1/2);
   int HullPeriod1=MathFloor(MathSqrt(HMA_Period1));

   int HalfPeriod2=MathFloor(HMA_Period2/2);
   int HullPeriod2=MathFloor(MathSqrt(HMA_Period2));

   int counted_bars=IndicatorCounted();
   int limit,i;

   if(counted_bars<0) return(-1);
   if(counted_bars>0) counted_bars--;
   limit=Bars-counted_bars;


   if(TimeFrame!=Period())
     {
      limit=MathMax(limit,TimeFrame/Period());

      for(i=limit; i>=0; i--)
        {
         int y=iBarShift(NULL,TimeFrame,Time[i]);
         ind_buffer1[i]=iCustom(NULL,TimeFrame,IndicatorFileName,HMA_Period1,HMA_Period2,HMA_Method1,HMA_Method2,
                                HMA_PriceType1,HMA_PriceType2,smzPeriod,smzMethod,0,y);
         ind_buffer2[i]=iCustom(NULL,TimeFrame,IndicatorFileName,HMA_Period1,HMA_Period2,HMA_Method1,HMA_Method2,
                                HMA_PriceType1,HMA_PriceType2,smzPeriod,smzMethod,1,y);
         sig_buffer[i]=iCustom(NULL,TimeFrame,IndicatorFileName,HMA_Period1,HMA_Period2,HMA_Method1,HMA_Method2,
                               HMA_PriceType1,HMA_PriceType2,smzPeriod,smzMethod,2,y);
        }

      return(0);
     }

   for(i=limit; i>=0; i--)
     {
      buffer1[i]=iMA(NULL,0,HalfPeriod1,0,HMA_Method1,HMA_PriceType1,i)*2-
                 iMA(NULL,0,HMA_Period1,0,HMA_Method1,HMA_PriceType1,i);

      buffer2[i]=iMA(NULL,0,HalfPeriod2,0,HMA_Method2,HMA_PriceType2,i)*2-
                 iMA(NULL,0,HMA_Period2,0,HMA_Method2,HMA_PriceType2,i);
     }

   for(i=limit; i>=0; i--)

      buffer0[i]=iMAOnArray(buffer1,0,HullPeriod1,0,HMA_Method1,i)-
                 iMAOnArray(buffer2,0,HullPeriod2,0,HMA_Method2,i);

   for(i=limit; i>=0; i--)
     {
      sig_buffer[i]=iMAOnArray(buffer0,0,smzPeriod,0,smzMethod,i);

      ind_buffer1[i]=EMPTY_VALUE;
      ind_buffer1[i]=EMPTY_VALUE;

      if(buffer0[i]>buffer0[i+1]) ind_buffer1[i]=buffer0[i];

      else
         if(buffer0[i]<buffer0[i+1]) ind_buffer2[i]=buffer0[i];
      else                      
        {
         ind_buffer1[i]=buffer1[i+1];
         ind_buffer2[i]=buffer2[i+1];
        }
     }

   return(0);
  }
//+------------------------------------------------------------------+
