//+------------------------------------------------------------------+
//|                                              HAMa Step Histo.mq4 |
//|                                    Heiken Ashi Step Ma Histogram |
//+------------------------------------------------------------------+
//|                                                      mod by Raff |
//+------------------------------------------------------------------+
#property copyright "Copyright � 2006, Forex-TSD.com "
#property link      "http://www.forex-tsd.com/"

#property indicator_separate_window
#property indicator_buffers 2
#property indicator_color1 Red
#property indicator_color2 Blue
#property indicator_width1 2
#property indicator_width2 2
#property indicator_minimum 0

//---- parameters
extern int StepMode  = 1;
extern int MaMetod  = 1;
extern int MaPeriod = 20;
extern int Step = 0;
extern int CountBars = 1500;
//---- buffers
double ExtMapBuffer1[];
double ExtMapBuffer2[];
double ExtMapBuffer3[];
double ExtMapBuffer4[];
//----
int ExtCountedBars=0;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//|------------------------------------------------------------------|
int init()
  {
//---- Indicators
   IndicatorBuffers(4);
   SetIndexStyle(0,DRAW_HISTOGRAM);
   SetIndexBuffer(0, ExtMapBuffer1);
   SetIndexStyle(1,DRAW_HISTOGRAM);
   SetIndexBuffer(1, ExtMapBuffer2);
//---- indicator buffers mapping
   SetIndexBuffer(0,ExtMapBuffer1);
   SetIndexBuffer(1,ExtMapBuffer2);
   SetIndexBuffer(2,ExtMapBuffer3);
   SetIndexBuffer(3,ExtMapBuffer4);
   string short_name="HAMa Step Histo"+" | "+StepMode+", "+MaMetod+", "+MaPeriod+", "+Step+" | ";
   IndicatorShortName(short_name); 

//---- initialization done
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   if (CountBars>Bars-1) CountBars=Bars-1;
   double maOpen, maClose, maLow, maHigh;
   double haOpen, haHigh, haLow, haClose;
   double val1, val2;
   
   if(Bars<=10) return(0);
   ExtCountedBars=IndicatorCounted();
//---- check for possible errors
   if (ExtCountedBars<0) return(-1);
//---- last counted bar will be recounted
   if (ExtCountedBars>0) ExtCountedBars--;
   int pos=CountBars;//-ExtCountedBars-1;
   while(pos>=0)
     {
      maOpen=iMA(NULL,0,MaPeriod,0,MaMetod,PRICE_OPEN,pos);
      maClose=iMA(NULL,0,MaPeriod,0,MaMetod,PRICE_CLOSE,pos);
      maLow=iMA(NULL,0,MaPeriod,0,MaMetod,MODE_LOW,pos);
      maHigh=iMA(NULL,0,MaPeriod,0,MaMetod,MODE_HIGH,pos);

      haOpen=(ExtMapBuffer3[pos+1]+ExtMapBuffer4[pos+1])/2;
      haClose=(maOpen+maHigh+maLow+maClose)/4;
      
      ExtMapBuffer3[pos]=haOpen;
      ExtMapBuffer4[pos]=haClose;
      
      val1=haOpen; 
      val2=haClose;
      
      if (StepMode>0)
      {
         if (Step>0) if( MathAbs(ExtMapBuffer3[pos]-ExtMapBuffer3[pos+1]) < Step*Point ) ExtMapBuffer3[pos]=ExtMapBuffer3[pos+1];
         if (Step>0) if( MathAbs(ExtMapBuffer4[pos]-ExtMapBuffer4[pos+1]) < Step*Point ) ExtMapBuffer4[pos]=ExtMapBuffer4[pos+1];
         val1=ExtMapBuffer3[pos];
         val2=ExtMapBuffer4[pos];
      }
      if (pos>CountBars-50) 
      {     
         ExtMapBuffer1[pos]=0;
         ExtMapBuffer2[pos]=0;
      }
      else if (pos<=CountBars-50)
      {
         ExtMapBuffer1[pos]=val1-val2;
         ExtMapBuffer2[pos]=val2-val1;
      }
 	   pos--;
     }
//----
   return(0);
  }
//+------------------------------------------------------------------+