//+------------------------------------------------------------------+
//|                                                      HAMA_20.mq4 |
//+------------------------------------------------------------------+
//|                                                      mod by Raff |
//+------------------------------------------------------------------+
#property copyright "Raff/Personalized and Adapted for HAMA Z3 TradingWithPALS"
#property link      "http://www.forex-tsd.com/"

#property indicator_chart_window
#property indicator_buffers 4
#property indicator_color1 Red
#property indicator_color2 Lime
#property indicator_color3 Red
#property indicator_color4 Lime
//---- parameters
extern int MaMetod  = 1;
extern int MaPeriod = 20;
//---- buffers
double ExtMapBuffer1[];
double ExtMapBuffer2[];
double ExtMapBuffer3[];
double ExtMapBuffer4[];
//----
int ExtCountedBars=0;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//|------------------------------------------------------------------|
int init()
  {

//---- indicators
   GlobalVariableSet("HL_"+Period()+Symbol(), 1);
   SetIndexStyle(0,DRAW_NONE, 0, 1, Red); //Kale, How do I remove this from the Indicator PopUp Box when attached to the Chart//
   SetIndexBuffer(0, ExtMapBuffer1);
   SetIndexStyle(1,DRAW_NONE, 0, 1, Lime); //Kale, How do I remove this from the Indicator PopUp Box when attached to the Chart//
   SetIndexBuffer(1, ExtMapBuffer2);
   SetIndexStyle(2,DRAW_LINE, 2, 0, Red);
   SetIndexBuffer(2, ExtMapBuffer3);
   SetIndexStyle(3,DRAW_LINE, 2, 0, Lime);
   SetIndexBuffer(3, ExtMapBuffer4);
//----
   SetIndexDrawBegin(0,5);
//---- indicator buffers mapping
   SetIndexBuffer(0,ExtMapBuffer1);
   SetIndexBuffer(1,ExtMapBuffer2);
   SetIndexBuffer(2,ExtMapBuffer3);
   SetIndexBuffer(3,ExtMapBuffer4);
//---- initialization done

watermark();
   return(0);
  }
//+------------------------------------------------------------------+
//| Custor indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
   GlobalVariableDel("HL_"+Period()+Symbol()); 
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
   {
   if(Bars<=10) return(0);
   
   int pos=Bars;
   
   if(!checkSystem())
   {
      for (int i=pos; i>=0; i--) 
      { 
         ExtMapBuffer1[i] = 0;
         ExtMapBuffer2[i] = 0;
         ExtMapBuffer3[i] = 0;
         ExtMapBuffer4[i] = 0;
      }   
      return(0);
   }
  else
  {
   double maOpen, maClose, maLow, maHigh;
   double haOpen, haHigh, haLow, haClose;
   
   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);

      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) 
        {
         ExtMapBuffer1[pos]=haLow;
         ExtMapBuffer2[pos]=haHigh;
        } 
      else
        {
         ExtMapBuffer1[pos]=haHigh;
         ExtMapBuffer2[pos]=haLow;
        } 
      ExtMapBuffer3[pos]=haOpen;
      ExtMapBuffer4[pos]=haClose;
 	   pos--;
     }
//----
}
   return(0);
  }
//+------------------------------------------------------------------+

void watermark()
  {
   ObjectCreate("fxcruiser HAMA PAD", OBJ_LABEL, 0, 0, 0);
   ObjectSetText("fxcruiser HAMA PAD", "fxcruiser HAMA PAD", 6, "Lucida Handwriting", RoyalBlue);
   ObjectSet("fxcruiser HAMA PAD", OBJPROP_CORNER, 2);
   ObjectSet("fxcruiser HAMA PAD", OBJPROP_XDISTANCE, 5);
   ObjectSet("fxcruiser HAMA PAD", OBJPROP_YDISTANCE, 25);
   return(0);
  }
  bool checkSystem()  
{
   int ha = GlobalVariableGet("HA_"+Period()+Symbol());
   int hb = GlobalVariableGet("HB_"+Period()+Symbol());
   int hl = GlobalVariableGet("HL_"+Period()+Symbol());
   
   return(true);
   
   
   Alert("MISSING SYSTEM INDICATORS!!!\nSYSTEM WILL TURN OFF AT NEXT TICK");
   return(false);
   
}