//+------------------------------------------------------------------+
//|                                                 Weekly Pivot.mq4 |
//|                                                                  |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright ""
#property link      ""

#property indicator_chart_window
#property indicator_buffers 7
#property indicator_color1 Crimson
#property indicator_color2 Crimson
#property indicator_color3 Crimson
#property indicator_color4 Blue
#property indicator_color5 SlateBlue
#property indicator_color6 SlateBlue
#property indicator_color7 SlateBlue
//---- buffers
double R3[];
double R2[];
double R1[];
double PIVOT[];
double S1[];
double S2[];
double S3[];


extern int HowManyWeeks = 2; 
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   SetIndexStyle(0,DRAW_LINE,STYLE_DOT);
   SetIndexBuffer(0,R1);
   SetIndexLabel(0,"R1");
   SetIndexStyle(1,DRAW_LINE,STYLE_DOT);
   SetIndexBuffer(1,R2);
   SetIndexLabel(1,"R2");
   SetIndexStyle(2,DRAW_LINE,STYLE_DOT);
   SetIndexBuffer(2,R3);
   SetIndexLabel(2,"R3");
   SetIndexStyle(3,DRAW_LINE);
   SetIndexBuffer(3,PIVOT);
   SetIndexLabel(3,"PIVOT");
   SetIndexStyle(4,DRAW_LINE,STYLE_DOT);
   SetIndexBuffer(4,S1);
   SetIndexLabel(4,"S1");
   SetIndexStyle(5,DRAW_LINE,STYLE_DOT);
   SetIndexBuffer(5,S2);
   SetIndexLabel(5,"S2");
   SetIndexStyle(6,DRAW_LINE,STYLE_DOT);
   SetIndexBuffer(6,S3);
   SetIndexLabel(6,"S3");
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int limit;
   int i=0,j=0,k=0,l=0;
   int counted_bars=IndicatorCounted();
//---- check for possible errors
   if(counted_bars<0) return(-1);
//---- last counted bar will be recounted
   if(counted_bars>0) counted_bars--;
   limit=Bars-counted_bars;
//----
//----
   datetime dailyTime[],weeklyTime[], monthlyTime[];
   double weeklyData[][6];
   datetime lastWeekTime;
   double lastWeekHigh=0; 
   double lastWeekLow=0; 
   double lastWeekClose=0;
   double lastWeekRange=0;       
   //double P, S1, R1, S2, R2, S3, R3;
   bool newEndWeek=false;
      
   ArrayCopyRates(weeklyData, Symbol(), PERIOD_W1);
   for (i=0; i<limit; i++){
      if (j==HowManyWeeks) break;
      if (newEndWeek) {newEndWeek=false;}
      
      lastWeekTime= weeklyData[j][0];
      lastWeekLow = weeklyData[j+1][2];
      lastWeekHigh = weeklyData[j+1][3];         
      lastWeekClose = weeklyData[j+1][4];
      lastWeekRange = lastWeekHigh - lastWeekLow;
      //Print("lastweekhigh=",lastWeekHigh);
      PIVOT[i] = (lastWeekHigh + lastWeekLow + lastWeekClose) / 3;
      R1[i] = PIVOT[i] + (lastWeekRange * 0.382);
      S1[i] = PIVOT[i] - (lastWeekRange * 0.382);
      R2[i] = PIVOT[i] + (lastWeekRange * 0.618);
      S2[i] = PIVOT[i] - (lastWeekRange * 0.618);
      R3[i] = PIVOT[i] + (lastWeekRange * 1.000);
      S3[i] = PIVOT[i] - (lastWeekRange * 1.000);
      
      if (Time[i+1] < lastWeekTime) { j++; newEndWeek=true; }
   }
   
//----
   return(0);
  }
//+------------------------------------------------------------------+