//+------------------------------------------------------------------+
//|                                                HamaSystem v1.mq4 |
//|                                                 mtf added by cja |
//|                                              cjatradingtools.com |
//+------------------------------------------------------------------+
#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 Red
#property indicator_width1 3
#property indicator_color2 RoyalBlue
#property indicator_width2 3
#property indicator_color3 Red
#property indicator_color4 RoyalBlue
#property indicator_color5 RoyalBlue
#property indicator_color6 Red

extern int timeFrame   = 0;
extern int MaPeriod    = 20;

double buf0[], buf1[], buf2[], buf3[], buf4[], buf5[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   SetIndexStyle(0,DRAW_HISTOGRAM);
   SetIndexBuffer(0, buf0);
   SetIndexStyle(1,DRAW_HISTOGRAM);
   SetIndexBuffer(1, buf1);   
   SetIndexStyle(2, DRAW_HISTOGRAM, STYLE_SOLID, 1);
   SetIndexBuffer(2, buf2);   
   SetIndexStyle(3, DRAW_HISTOGRAM, STYLE_SOLID, 1);   
   SetIndexBuffer(3, buf3);
   SetIndexStyle(4, DRAW_LINE, STYLE_SOLID, 0);
   SetIndexBuffer(4, buf4);
   SetIndexStyle(5, DRAW_LINE, STYLE_SOLID, 0);
   SetIndexBuffer(5, buf5);

//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   double maOpen, maClose, maLow, maHigh;
   double haOpen, haHigh, haLow, haClose;
   int counted_bars=IndicatorCounted();
   int limit;

   if(counted_bars < 0) return(-1);
   if(counted_bars>0) counted_bars--;
     limit = MathMax(MathMin(Bars-counted_bars,Bars-1),timeFrame/Period());
    
//---- main loop
     for(int i=limit; i>=0; i--)
        { 
     int y = iBarShift(NULL,timeFrame,Time[i]);
     
     if (timeFrame<Period()) timeFrame=Period();
      maOpen=iMA(NULL,timeFrame,MaPeriod,0,MODE_EMA,MODE_OPEN,y);
      maClose=iMA(NULL,timeFrame,MaPeriod,0,MODE_EMA,MODE_CLOSE,y);
      maLow=iMA(NULL,timeFrame,MaPeriod,0,MODE_EMA,MODE_LOW,y);
      maHigh=iMA(NULL,timeFrame,MaPeriod,0,MODE_EMA,MODE_HIGH,y);
     
      haOpen=(buf0[i+1]+buf1[i+1])/2.;
      haClose=(maOpen+maHigh+maLow+maClose)/4.;
      
      buf0[i]=haOpen;
      buf1[i]=haClose;
      
     if (buf0[i] < buf1[i]) {
         buf2[i] = iLow(NULL,timeFrame,y);
         buf3[i] = iHigh(NULL,timeFrame,y);
     }else{
         buf2[i] = iHigh(NULL,timeFrame,y);
         buf3[i] = iLow(NULL,timeFrame,y);
         }
    
 	  buf4[i] = iMA(NULL, timeFrame, MaPeriod, 0, MODE_EMA, PRICE_HIGH,y);
     buf5[i] = iMA(NULL, timeFrame, MaPeriod, 0, MODE_EMA, PRICE_LOW, y);
     
     }
   
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+