//+------------------------------------------------------------------+
//|                                               Heiken Ashi Ma.mq4 |
//+------------------------------------------------------------------+
//|                                                      mod by Raff |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2006, Forex-TSD.com "
#property link      "http://www.forex-tsd.com/"

#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 Aqua
#property indicator_color2 Red

//---- parameters
extern int MaMetod  = 1;
extern int MaPeriod = 15;
extern int MaPrice = 0;
//---- buffers
double ExtMapBuffer1[];
double ExtMapBuffer2[];
double ExtMapBuffer3[];
double ExtMapBuffer4[];
double trend[];
//----
int ExtCountedBars=0;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//|------------------------------------------------------------------|
int init()
  {
   IndicatorBuffers(5);
//---- indicators
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0, ExtMapBuffer1);
   SetIndexStyle(1,DRAW_LINE);
   SetIndexBuffer(1, ExtMapBuffer2);
   SetIndexBuffer(2, ExtMapBuffer3);
   SetIndexBuffer(3, ExtMapBuffer4);
   SetIndexBuffer(4, trend);
//----
   SetIndexDrawBegin(0,5);
   SetIndexDrawBegin(1,5);
//---- initialization done
   return(0);
  }
//+------------------------------------------------------------------+
//| Custor indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//---- TODO: add your code here
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   double maOpen, maClose, maLow, maHigh, ma, ma1;
   double haOpen, haHigh, haLow, haClose;
   if(Bars<=10) return(0);
   if(MaPrice<0||MaPrice>6) MaPrice=0;
   ExtCountedBars=IndicatorCounted();
//---- check for possible errors
   if (ExtCountedBars<0) return(-1);
//---- last counted bar will be recounted
   if (ExtCountedBars>0) ExtCountedBars--;
   int pos=Bars-ExtCountedBars-1;
   while(pos>=0)
     {
      maOpen=iMA(NULL,0,MaPeriod,0,MaMetod,MODE_OPEN,pos);
      maClose=iMA(NULL,0,MaPeriod,0,MaMetod,MODE_CLOSE,pos);
      maLow=iMA(NULL,0,MaPeriod,0,MaMetod,MODE_LOW,pos);
      maHigh=iMA(NULL,0,MaPeriod,0,MaMetod,MODE_HIGH,pos);
      ma=iMA(NULL,0,MaPeriod,0,MaMetod,MaPrice,pos);
      ma1=iMA(NULL,0,MaPeriod,0,MaMetod,MaPrice,pos+1);

      haOpen=(ExtMapBuffer3[pos+1]+ExtMapBuffer4[pos+1])/2;
      haClose=(maOpen+maHigh+maLow+maClose)/4;
      haHigh=MathMax(maHigh, MathMax(haOpen, haClose));
      haLow=MathMin(maLow, MathMin(haOpen, haClose));
      if (haOpen<haClose) 
        {
         trend[pos] =1;
         ExtMapBuffer1[pos]=ma;
         if (trend[pos+1]<0) ExtMapBuffer1[pos+1]=ma1;
         ExtMapBuffer2[pos]=EMPTY_VALUE;
        } 
      else
        {
         trend[pos] =-1;
         ExtMapBuffer2[pos]=ma;
         if (trend[pos+1]>0) ExtMapBuffer2[pos+1]=ma1;
         ExtMapBuffer1[pos]=EMPTY_VALUE;
        } 
      ExtMapBuffer3[pos]=haOpen;
      ExtMapBuffer4[pos]=haClose;
 	   pos--;
     }
//----
   return(0);
  }
//+------------------------------------------------------------------+