//+------------------------------------------------------------------+
//|                                                      Heiken Ashi |
//+------------------------------------------------------------------+
#property link      "http://www.forex-tsd.com/"
#property indicator_separate_window
#property indicator_buffers 3
#property indicator_color1 DodgerBlue
#property indicator_color2 Red
#property indicator_color3 Teal
#property indicator_width1 2
#property indicator_width2 2
#property indicator_width3 1

extern int AvPeriod = 6;
extern int AvMethod = MODE_SMMA;
//
//
//
//
//

double ExtMapBuffer3[];
double ExtMapBuffer4[];
double FextMapBuffer1[];
double FextMapBuffer2[];
double FextMapBuffer3[];
double FextMapBuffer4[];

//+-------------------------------------------------------------------
//|                                                                  
//+-------------------------------------------------------------------
//
//
//
//
//

int init()
{
   IndicatorBuffers(6);
   SetIndexBuffer(0, FextMapBuffer1); SetIndexStyle(0,DRAW_HISTOGRAM);
   SetIndexBuffer(1, FextMapBuffer2); SetIndexStyle(1,DRAW_HISTOGRAM);
   SetIndexBuffer(2, FextMapBuffer3);
   SetIndexBuffer(3, FextMapBuffer4);
   SetIndexBuffer(4, ExtMapBuffer3);
   SetIndexBuffer(5, ExtMapBuffer4);
   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);
          
   //
   //
   //
   //
   //
   
      for(int i=limit; i >= 0; i--)
      {
         double haOpen  = (ExtMapBuffer3[i+1]+ExtMapBuffer4[i+1])/2;
         double haClose = (Open[i]+High[i]+Low[i]+Close[i])/4;
         double haHigh  = MathMax(High[i], MathMax(haOpen, haClose));
         double haLow   = MathMin(Low[i] , MathMin(haOpen, haClose));
         
            ExtMapBuffer3[i]=haOpen;
            ExtMapBuffer4[i]=haClose;
         
         double cval = (ExtMapBuffer3[i  ]+ExtMapBuffer4[i  ])/2;
         double pval = (ExtMapBuffer3[i+1]+ExtMapBuffer4[i+1])/2;
      
            FextMapBuffer4[i] = MathAbs(cval - pval);
               if(haOpen > haClose)
               {
                  FextMapBuffer2[i] = FextMapBuffer4[i];
                  FextMapBuffer1[i] = EMPTY_VALUE;
               }
               else 
               {
                  FextMapBuffer1[i] = FextMapBuffer4[i];
                  FextMapBuffer2[i] = EMPTY_VALUE;
               }
      }  
      for(i=limit; i>=0; i--) FextMapBuffer3[i] = iMAOnArray(FextMapBuffer4,0,AvPeriod,0,AvMethod,i);
      return(0);
}