//+------------------------------------------------------------------+
//|                                                    HMA histo.mq4 |
//|                                                           mladen |
//+------------------------------------------------------------------+
#property copyright "mladen"
#property link ""

#property indicator_separate_window
#property indicator_buffers 3
#property indicator_color1  Yellow
#property indicator_color2  Green
#property indicator_color3  Red
#property indicator_width1  2
#property indicator_width2  2
#property indicator_width3  2
#property indicator_maximum 1
#property indicator_minimum 0

//
//
//
//
//

extern int  HMA_Period    = 15;
extern int  HMA_PriceType =  0;
extern int  HMA_Method    = MODE_LWMA;

//
//
//
//
//

double ind_buffer0[];
double ind_buffer1[];
double ind_buffer2[];
double buffer[];


//+------------------------------------------------------------------
//|                                                                 |
//+------------------------------------------------------------------

int init()
{
   
   //
   //
   //
   //
   //
   
   IndicatorShortName("HMA("+HMA_Period+")");
   IndicatorBuffers(4);
   SetIndexBuffer(0,ind_buffer0);
   SetIndexBuffer(1,ind_buffer1);
   SetIndexBuffer(2,ind_buffer2);
   SetIndexBuffer(3,buffer);
   SetIndexStyle(0,DRAW_HISTOGRAM);
   SetIndexStyle(1,DRAW_HISTOGRAM);
   SetIndexStyle(2,DRAW_HISTOGRAM);

   int draw_begin=HMA_Period+MathFloor(MathSqrt(HMA_Period));
   for (int i = 0; i < indicator_buffers; i++)
   {
         SetIndexDrawBegin(i,draw_begin);
         SetIndexLabel(i,"Hull Moving Average");
   }         
   return(0);
}


//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+

int start()
{
   int HalfPeriod   = MathFloor(HMA_Period/2);
   int HullPeriod   = MathFloor(MathSqrt(HMA_Period));
   int counted_bars = IndicatorCounted();
   int limit,i;
   

   if(counted_bars<0) return(-1);
   if(counted_bars>0) counted_bars--;
      limit=Bars-counted_bars;

   //
   //
   //
   //
   //
   
   for(i=limit; i>=0; i--)
         buffer[i]=iMA(NULL,0,HalfPeriod,0,HMA_Method,HMA_PriceType,i)*2-
                   iMA(NULL,0,HMA_Period,0,HMA_Method,HMA_PriceType,i);
   for(i=limit; i>=0; i--)
   {
      ind_buffer0[i] = iMAOnArray(buffer,0,HullPeriod,0,HMA_Method,i);
      ind_buffer1[i] = EMPTY_VALUE;
      ind_buffer2[i] = EMPTY_VALUE;
      
      //
      //
      //
      //
      //
            
      if (ind_buffer0[i] > ind_buffer0[i+1]) ind_buffer1[i] = 1;
      if (ind_buffer0[i] < ind_buffer0[i+1]) ind_buffer2[i] = 1;
   }
   return(0);
}