#property copyright "Personalized for HAMA PAD Approach - Copyright � 2009"
#property link      "http://www.forex-tsd.com"
// modified by keekkenen 2009-02-15
#property indicator_chart_window
#property indicator_buffers 6
#property indicator_color1 OrangeRed
#property indicator_color2 RoyalBlue
#property indicator_color3 Red
#property indicator_color4 Blue
#property indicator_color5 Coral
#property indicator_color6 Aqua

extern int MaPeriod  = 21;
extern int Ma1Period = 9;
extern bool hahisto =true;
double buf0[], buf1[], buf2[], buf3[], buf4[], buf5[];
string Copyright = "fxcruiser HAMA PAD (Copyright � 2009)";
//+------------------------------------------------------------------+
int init(){
   SetIndexStyle(2,DRAW_HISTOGRAM, 0, 3);
   SetIndexBuffer(2, buf0);
   SetIndexStyle(3,DRAW_HISTOGRAM, 0, 3);
   SetIndexBuffer(3, buf1);   
   SetIndexStyle(0, DRAW_HISTOGRAM, 0,2 );
   SetIndexBuffer(0, buf2);   
   SetIndexStyle(1, DRAW_HISTOGRAM, 0, 2);   
   SetIndexBuffer(1, buf3);
   
   if(hahisto) {
   SetIndexStyle(4, DRAW_HISTOGRAM, 0,2 );
   SetIndexStyle(5, DRAW_HISTOGRAM, 0,2 );
               }
   else        {         
   SetIndexStyle(4, DRAW_LINE, STYLE_SOLID, 0);
   SetIndexStyle(5, DRAW_LINE, STYLE_SOLID, 0);
               }
   SetIndexBuffer(4, buf4);
   SetIndexBuffer(5, buf5);

   setLabel(Copyright,Copyright,RoyalBlue,2,5,10,false,9,"Lucida Handwriting");
   return(0);
}
//+------------------------------------------------------------------+
int deinit(){ObjectDelete(Copyright);}
//+------------------------------------------------------------------+
int start()  {
   int ExtCountedBars = 0;
   double maOpen, maClose, maLow, maHigh;
   double MaOpen, MaClose, MaLow, MaHigh;
   double haOpen, haHigh, haLow, haClose;
   if(Bars<=10) return(0);
   ExtCountedBars=IndicatorCounted();
   if (ExtCountedBars<0) return(-1);
   if (ExtCountedBars>0) ExtCountedBars--;
   int pos=Bars-ExtCountedBars-1;
   while(pos>=0){
      MaOpen=iMA(NULL,0,MaPeriod,0,MODE_EMA,MODE_OPEN,pos);
      MaClose=iMA(NULL,0,MaPeriod,0,MODE_EMA,MODE_CLOSE,pos);
      MaLow=iMA(NULL,0,MaPeriod,0,MODE_EMA,MODE_LOW,pos);
      MaHigh=iMA(NULL,0,MaPeriod,0,MODE_EMA,MODE_HIGH,pos);

      maOpen=(buf0[pos+1]+buf1[pos+1])/2.;
      maClose=(MaOpen+MaHigh+MaLow+MaClose)/4.;
      maHigh=MathMin(MaHigh, MathMin(MaOpen,MaClose));
      maLow =MathMax(MaLow,  MathMax(MaOpen,MaClose));
     
      buf0[pos]=maOpen;
      buf1[pos]=maClose;
      
      if (buf0[pos] > buf1[pos]) {
         buf2[pos] = maLow;
         buf3[pos] = maHigh;
      }else{
         buf2[pos] = maHigh;
         buf3[pos] = maLow;
      }
 	   pos--;
   }
   for (int i = Bars-ExtCountedBars-1; i >= 0; i--) {
 haClose=(Open[i]+High[i]+Low[i]+Close[i])/4; 
 buf5[i] =  haClose;     
 buf4[i] =  (buf4[i+1]+buf5[i+1])/2.0;

//      buf4[i] = iMA(NULL, 0, MaPeriod, 0, MODE_EMA, PRICE_HIGH, i);
//      buf5[i] = iMA(NULL, 0, MaPeriod, 0, MODE_EMA, PRICE_LOW, i);
   }
   return(0);
}
//+------------------------------------------------------------------+
void setLabel(string name, string text, color col, int corner,
            int x, int y, bool back = false, int fontsize = 9, 
            string fontname = "MS Sans Serif") {
   if (ObjectFind(name)==-1){
      // �������� �������, ���� �� ���������� ��� ��� ������
      ObjectCreate(name, OBJ_LABEL, 0, 0, 0);
      ObjectSetText(name, text, fontsize, fontname, col);
      ObjectSet(name, OBJPROP_CORNER, corner);
      ObjectSet(name, OBJPROP_BACK,back);
      ObjectSet(name, OBJPROP_XDISTANCE, x);
      ObjectSet(name, OBJPROP_YDISTANCE, y);          
   }else{
      ObjectSetText(name, text, fontsize, fontname, col);
      ObjectSet(name, OBJPROP_CORNER, corner);
      ObjectSet(name, OBJPROP_BACK,back);
      ObjectSet(name, OBJPROP_XDISTANCE, x);
      ObjectSet(name, OBJPROP_YDISTANCE, y); 
   }  
}