/**
* ACD_2.mq4
* Pivot Range and Previous High/Low
**/

#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 Green
#property indicator_color2 Magenta

//Input Params
extern string PivotRangeStart = "13:30";
extern string PivotRangeEnd = "14::30";
extern int Line_Style = 0;
extern int Line_Width = 1;

bool DisplayPreviousHighLow = true;

double Buffer1[];
double Buffer2[];

double pivotRangeHigh;
double pivotRangeLow;
//double pivotRangeClose;
//double pivotPoint;
//double pivotDiff;

double pivotTop=0;
double pivotBottom=0;

int openBar;
    
int init()
{  
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,Buffer1);
   SetIndexLabel(0,"Previous Day High");
   
   SetIndexStyle(1,DRAW_LINE);
   SetIndexBuffer(1,Buffer2);
   SetIndexLabel(1,"Previous Day Low");
   
   return(0);
}

int deinit()
{
   return(0);
}

int start()
{   
   string barTime="", lastBarTime="";         
   string barDay="", lastBarDay="";
   int closeBar;
          
   for(int i=Bars; i>=0; i--)
   {  
      barTime = TimeToStr(Time[i], TIME_MINUTES);
      lastBarTime = TimeToStr(Time[i+1], TIME_MINUTES);
      barDay = TimeToStr(Time[i],TIME_DATE);
      lastBarDay = TimeToStr(Time[i+1],TIME_DATE); 
      
      //need to handle if pivotrangestart/end is 00:00
      if ((PivotRangeEnd == "00:00" && barTime>=PivotRangeEnd && barDay>lastBarDay) || (barTime>=PivotRangeEnd && lastBarTime<PivotRangeEnd))
      {
         closeBar = i + 1;
         
         if (openBar>0)
         {
            calculatePivotRangeValues(openBar, closeBar);
         }
      }
      
      if ((PivotRangeStart == "00:00" && barTime>=PivotRangeStart && barDay>lastBarDay) || (barTime>=PivotRangeStart && lastBarTime<PivotRangeStart))
      {          
          openBar = i;
      }
      
      if (openBar>0)
      {
          drawIndicators(i);
      }     
   }
   return(0);
}

void calculatePivotRangeValues(int openBar, int closeBar)
{
   pivotRangeHigh = High[Highest(NULL, 0, MODE_HIGH, (openBar - closeBar + 0), closeBar)];
  // pivotRangeHigh = High[0];
   pivotRangeLow = Low[Lowest(NULL, 0, MODE_LOW, ((openBar - closeBar) + 0), closeBar)];
  // pivotRangeLow = High[0];
  // pivotRangeClose = Close[closeBar];
   //pivotPoint = (pivotRangeHigh + pivotRangeLow + pivotRangeClose)/3;
   //pivotDiff = MathAbs(((pivotRangeHigh + pivotRangeLow)/2) - pivotPoint);
   //pivotTop = pivotPoint + pivotDiff;
   //pivotBottom = pivotPoint - pivotDiff;  
}

void drawIndicators(int curBar)
{
   if (DisplayPreviousHighLow)
   {
      Buffer1[curBar+2]=pivotRangeHigh;
      //Buffer1[curBar]= 0;
      Buffer2[curBar+2]=pivotRangeLow;
     // Buffer2[curBar]= 0;
   }
}