//+------------------------------------------------------------------+
//|                                                        HH_LL.mq4 |
//+------------------------------------------------------------------+

#property indicator_chart_window
#property indicator_buffers 3

#property indicator_color1 Red
#property indicator_color2 Aqua
#property indicator_color3 Yellow

#property indicator_width1 1
#property indicator_width2 1
#property indicator_width3 1

#property indicator_style1 0
#property indicator_style2 2
#property indicator_style3 2



extern int MaPeriod=9;
extern int MaMethod=1;
extern int MaPrice=0;

extern int HHLLPeriod=50;


extern int CountBars=1500;


double MA[];
double HH_MA[];
double LL_MA[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   IndicatorBuffers(3);
   SetIndexStyle(0,DRAW_LINE);
   SetIndexStyle(1,DRAW_LINE);
   SetIndexStyle(2,DRAW_LINE);
   SetIndexBuffer(0,MA);
   SetIndexBuffer(1,HH_MA);
   SetIndexBuffer(2,LL_MA);
//----
   return(0);
  }

//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int    counted_bars=IndicatorCounted();
//----
   if(Bars<=100) return(0);
   
   if (CountBars>Bars-1) CountBars=Bars-1;
   
   int i=CountBars-MathMax(HHLLPeriod,MaPeriod);

   
   while(i>=0)
     {
     
     MA[i]=iMA(NULL,0,MaPeriod,0,MaMethod,MaPrice,i);
     
      double max=-1000000;
      double min=1000000;
      int k = i + HHLLPeriod-1;
      while(k>=i)
        {
         if(max<MA[k]) max=MA[k];
         if(min>MA[k]) min=MA[k];
         k--;
        }
    
     HH_MA[i]=max;
     LL_MA[i]=min;

     i--;
     }
//----
   return(0);
  }
//+------------------------------------------------------------------+