//+------------------------------------------------------------------+
//|                              Auto-Pivot Plotter Weekly V1-00.mq4 |
//|                                  Copyright © 2007, BundyRaider   |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2007, BundyRaider"
#property link      ""

#property indicator_chart_window
#property indicator_buffers 8
#property indicator_color1 LightGray
#property indicator_color2 LightGray
#property indicator_color3 LightGray
#property indicator_color4 YellowGreen
#property indicator_color5 LightGray
#property indicator_color6 LightGray
#property indicator_color7 LightGray
#property indicator_color8 LimeGreen

#property indicator_width4 1

//---- input parameters
//extern bool ChangeToFibonacci=false;
extern bool AutoSwitchFibPivots = true;
extern double PercentADR = 20;
bool Fib = false;
int    MyPeriod;
int multiplier;

//---- buffers
double Res3[];
double Res2[];
double Res1[];
double Pivot[];
double Supp1[];
double Supp2[];
double Supp3[];
double Extra1[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
   if(Digits == 2 || Digits == 4) multiplier = 1;
   if(Digits == 3 || Digits == 5) multiplier = 10;

//---- indicators
   SetIndexStyle(0,DRAW_LINE,STYLE_SOLID);
   SetIndexBuffer(0,Res3);
   SetIndexStyle(1,DRAW_LINE,STYLE_SOLID);
   SetIndexBuffer(1,Res2);
   SetIndexStyle(2,DRAW_LINE,STYLE_SOLID);
   SetIndexBuffer(2,Res1);
   SetIndexStyle(3,DRAW_LINE,STYLE_DASH);
   SetIndexBuffer(3,Pivot);
   SetIndexStyle(4,DRAW_LINE,STYLE_SOLID);
   SetIndexBuffer(4,Supp1);
   SetIndexStyle(5,DRAW_LINE,STYLE_SOLID);
   SetIndexBuffer(5,Supp2);
   SetIndexStyle(6,DRAW_LINE,STYLE_SOLID);
   SetIndexBuffer(6,Supp3);
   SetIndexStyle(7,DRAW_LINE,STYLE_DASHDOTDOT);
   SetIndexBuffer(7,Extra1);
   
   IndicatorDigits(Digits);
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int    counted_bars=IndicatorCounted();
   
   if(counted_bars<0) return(-1);
   //---- last counted bar will be recounted
   if(counted_bars>0) counted_bars--;

   int limit=Bars-counted_bars;

      //---- exit if period is greater than daily charts
   if(Period() > 1440)
   {
      Print("Error - Chart period is greater than 1 day.");
      return(-1); // then exit
   }

   //---- Get new  prices
   if(Period()==1440)
   {
      MyPeriod = 43200;
   }

   if(Period()==240)
   {
      MyPeriod = 10080;
   }

   if(Period()<240)
   {
      MyPeriod = 1440;
   }

//****************************
   
   for(int i=0; i<limit; i++)
   { 

      //if ( (i<((DaysToPlot+1)*BarsInDay))||DaysToPlot==0)   // Used to limit the number of days
      {                                                     // that are mapped out. Less waiting ;)
      
      // *****************************************************
      //    Find previous day's opening and closing bars.
      // *****************************************************
      
      //Find Our Week date.

      datetime RangeDate=Time[i];
      int RangeBar        =  iBarShift( NULL, MyPeriod, RangeDate, false)+1; 
      double PreviousHigh  =  iHigh(NULL, MyPeriod ,RangeBar);
      double PreviousLow   =  iLow(NULL, MyPeriod, RangeBar);
      double PreviousClose =  iClose(NULL, MyPeriod, RangeBar);



/*      
      int PreviousClosingBar = FindLastTimeMatchFast(CloseMinutesIntoDay,i+1);
      int PreviousOpeningBar = FindLastTimeMatchFast(StartMinutesIntoDay,PreviousClosingBar+1);
      
      double PreviousHigh= High[PreviousClosingBar];
      double PreviousLow = Low [PreviousClosingBar];
      double PreviousClose = Close[PreviousClosingBar];
*/      
      // *****************************************************
      //    Find previous day's high and low.
      // *****************************************************
      

      
      // ************************************************************************
      //    Calculate Pivot lines and map into indicator buffers.
      // ************************************************************************
      
      double P =  (PreviousHigh+PreviousLow+PreviousClose)/3;
      double R1 = (2*P)-PreviousLow;
      double S1 = (2*P)-PreviousHigh;
      double R2 =  P+(PreviousHigh - PreviousLow);
      double S2 =  P-(PreviousHigh - PreviousLow);
      double R3 = (2*P)+(PreviousHigh-(2*PreviousLow));
      double S3 = (2*P)-((2* PreviousHigh)-PreviousLow); 
      //NormalizeDouble( P, Digits); 

      Fib = false;
      if(AutoSwitchFibPivots)
      {
         int Range2=0,Range3=0,Range4=0,Rg1,Rg2,Rg3,Rg4;
         double Range1=0,RAvg=0,RangePercent;
         int j=0;

         //---- Get new  prices
         MyPeriod = 1440;
         Rg1 = 1;
         Rg2 = 5;
         Rg3 = 10;
         Rg4 = 20;

         Range1 = (iHigh(NULL,MyPeriod,RangeBar)-iLow(NULL,MyPeriod,RangeBar))/(Point*multiplier);
         for(j=RangeBar;j<RangeBar+Rg2;j++)
            Range2 = Range2+(iHigh(NULL,MyPeriod,j)-iLow(NULL,MyPeriod,j))/(Point*multiplier);
         for(j=RangeBar;j<RangeBar+Rg3;j++)
            Range3 = Range3+(iHigh(NULL,MyPeriod,j)-iLow(NULL,MyPeriod,j))/(Point*multiplier);
         for(j=RangeBar;j<RangeBar+Rg4;j++)
            Range4 = Range4+(iHigh(NULL,MyPeriod,j)-iLow(NULL,MyPeriod,j))/(Point*multiplier);

         Range2 = Range2/Rg2;
         Range3 = Range3/Rg3;
         Range4 = Range4/Rg4;
         RAvg   =(Range1+Range2+Range3+Range4)/4;    
         RangePercent = (Range1 - RAvg) / RAvg;

         if(RangePercent > PercentADR/100)
         {
            Fib = true;
   //      if(ChangeToFibonacci==False)
            R1 = P + ((PreviousHigh-PreviousLow) * 0.382);
            S1 = P - ((PreviousHigh-PreviousLow) * 0.382);
            R2 = P + ((PreviousHigh-PreviousLow) * 0.618);
            S2 = P - ((PreviousHigh-PreviousLow) * 0.618);
            R3 = P + ((PreviousHigh-PreviousLow) * 1.000);
            S3 = P - ((PreviousHigh-PreviousLow) * 1.000);
            //NormalizeDouble( P, Digits); 
         } 
       }// end if(AutoSwitchFibPivots)
    
      // Res3[i] =R3;  
      // Res2[i] =R2;  
      // Res1[i] =R1;    
      // Pivot[i]=NormalizeDouble( P, Digits); 
      // Supp1[i]=S1;   
      // Supp2[i]=S2;  
      // Supp3[i]=S3;
      // Extra1[i]=OpenPriceAt+i;

      //   **********************************************
      //      Calculate the mid-levels in to the buffers. 
      //      (for mid-levels version)
      //   **********************************************
      Res3[i] =((R3+R2)/2);  //M5
      Res2[i] =((R2+R1)/2);  //M4
      Res1[i] =((R1+P)/2);   //M3
      Supp1[i]=((P+S1)/2);   //M2
      Supp2[i]=((S1+S2)/2);  //M1
      Supp3[i]=((S2+S3)/2);  //M0


      } //End of 'DaysToPlot 'if' statement.
   }   
      // ***************************************************************************************
      //                            End of Main Loop
      // ***************************************************************************************


   // *****************************************
   //    Return from Start() (Main Routine)
   return(0);
  }
//+-------------------------------------------------------------------------------------------------------+
//  END Custom indicator iteration function
//+-------------------------------------------------------------------------------------------------------+


// *****************************************************************************************
// *****************************************************************************************
// -----------------------------------------------------------------------------------------
//    The following routine will use "StartingBar"'s time and use it to find the 
//    general area that SHOULD contain the bar that matches "TimeToLookFor"
// -----------------------------------------------------------------------------------------


