//+------------------------------------------------------------------+
//|                                                NB Fourths_v2.mq4 |
//| Edited for NanningBob H4 systems                                 |
//+------------------------------------------------------------------+
#property copyright "© 2010.02.17 SwingMan"
#property link      ""
/*+------------------------------------------------------------------+
v2 - get also HighLow of Open/Close
+------------------------------------------------------------------+*/

#property indicator_chart_window

#property indicator_buffers 5

#property indicator_color1  Goldenrod
#property indicator_color2  Goldenrod
#property indicator_color3  Goldenrod
#property indicator_color4  Goldenrod
#property indicator_color5  Goldenrod

#property indicator_width1 2
#property indicator_width2 2
#property indicator_width3 1
#property indicator_width4 1
#property indicator_width5 1

//+------------------------------------------------------------------+
extern int Period_Back = 120; // 4H = 6 bars/day * 20 days = 120 bars
extern double Factor = 0.25;
extern bool Draw_MiddleLine  = false;
extern bool Draw_FourthLines = true;
extern bool Draw_MinMaxLines = true;
extern bool Use_OpenClose = false;
//+------------------------------------------------------------------+

//----
double UpperBuf[], LowerBuf[];
double UpperBuf25[], LowerBuf25[], MiddleBuf[];

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void init()
{       
//----
   SetIndexBuffer(0, UpperBuf25); SetIndexStyle(0, DRAW_LINE); SetIndexLabel(0,"Upper25");
   SetIndexBuffer(1, LowerBuf25); SetIndexStyle(1, DRAW_LINE); SetIndexLabel(1,"Lower25");   
   SetIndexBuffer(2, UpperBuf);   SetIndexStyle(2, DRAW_LINE); SetIndexLabel(2,"HigherHigh");
   SetIndexBuffer(3, LowerBuf);   SetIndexStyle(3, DRAW_LINE); SetIndexLabel(3,"LowerLow");
   if (Draw_MiddleLine)
        { SetIndexBuffer(4, MiddleBuf);  SetIndexStyle(4, DRAW_LINE); SetIndexLabel(4,"Middle"); }     
   else { SetIndexBuffer(4, MiddleBuf);  SetIndexStyle(4, DRAW_LINE); SetIndexLabel(4,NULL); }
   
//----   
   SetIndexDrawBegin(0,Period_Back);
   SetIndexDrawBegin(1,Period_Back);
   SetIndexDrawBegin(2,Period_Back);
   SetIndexDrawBegin(3,Period_Back);
   SetIndexDrawBegin(4,Period_Back);
   
   IndicatorShortName("NB Fourth_v2 (" + Period_Back + ")" );
   IndicatorDigits(Digits);
   return;
}
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void deinit() 
{
   return;
}  
  
//####################################################################  
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void start() 
{
double maxValue, minValue, diff25;
   int counted = IndicatorCounted();
//----
   if(counted < 0) 
       return (-1);
//----  
   if(counted > 0) 
       counted--;
   int limit = Bars - counted;
//----  
   for(int i = 0; i < limit; i++) 
   {
      //---- min/max values
      if (Use_OpenClose)
      {   
         double maxOpen  = iOpen(NULL, 0, iHighest(NULL, 0, MODE_OPEN, Period_Back, i));
         double maxClose = iClose(NULL,0, iHighest(NULL, 0, MODE_CLOSE,Period_Back, i));
         double minOpen  = iOpen(NULL, 0, iLowest(NULL,  0, MODE_OPEN, Period_Back, i));
         double minClose = iClose(NULL,0, iLowest(NULL,  0, MODE_CLOSE,Period_Back, i));
         maxValue = MathMax(maxOpen,maxClose);
         minValue = MathMin(minOpen,minClose);
      }
      else
      {
         maxValue = iHigh(NULL, 0, iHighest(NULL, 0, MODE_HIGH, Period_Back, i));
         minValue = iLow(NULL, 0, iLowest(NULL, 0, MODE_LOW, Period_Back, i));
      }

      //---- lines
      diff25 = (maxValue - minValue) * Factor;
      double upperLine25 = maxValue - diff25;
      double lowerLine25 = minValue + diff25;
      
      if (Draw_FourthLines)
      {
         UpperBuf25[i] = upperLine25;
         LowerBuf25[i] = lowerLine25;
      }
      
      if (Draw_MiddleLine)
         MiddleBuf[i] = (upperLine25 + lowerLine25) * 0.50;
      
      if (Draw_MinMaxLines)
      {
         UpperBuf[i] = maxValue;
         LowerBuf[i] = minValue;
      }
   }
   
   //Comment(WindowBarsPerChart(),"  ",WindowPriceMax(0),"  ",WindowPriceMin(0));
   return;  
}
//+------------------------------------------------------------------+

