Get indicator to get values for whole chart!!!

 

I am currently working on a VWAP indicator that starts calculating at the beginning of each week. I can get the indicator to restart each week and continue into future but cannot figure out how to correctly loop through all bars to get past values. Please help!

//+------------------------------------------------------------------+
//|                                         VWAP_Weekly.mq5 |
//|                                           Michael & Robert Maher |
//+------------------------------------------------------------------+
#property copyright "Standing on the shoulders of giants"
#property version   "1.0"
#property description "The indicator calculates standard VWAP (Volume Weighted Average Price) line"
#property description "The start point at the beginning of the week."
#property description " "
#property description "Purpose: Identify the average price from a certain point in time"
#property description " "
#property description "# IMPORTANT: Each line must have a unique ID #"
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_plots   2
//--- plot vwap
#property indicator_label1  "VWAP"
#property indicator_type1   DRAW_SECTION
#property indicator_style1  STYLE_SOLID
#property indicator_width1  2
//---
#property indicator_label2  "Week"
#property indicator_type2   DRAW_SECTION
#property indicator_style2  STYLE_SOLID
#property indicator_width2  2
enum PRICE_METHOD
  {
   Median,  // Median Price (HL/2)
   Typical, // Typical Price (HLC/3)
   Weighted // Weighted Close (HLCC/4)
  };
//--- input parameters
input string            vwapID    = "01";       // VWAP ID   ( must be unique )
input PRICE_METHOD      Method    = Typical;    // Price Calculation Method
input color             vwapColor = Aqua;    // VWAP Color

//--- indicator buffers
double         vwapBuffer[];
double         iWeek[];
//--- global variables
int            startVWAP;
int            endVWAP;
string         Prefix;
string         indicator_name;
int            iRatesTotal;
datetime       iTime[];
double         iOpen[],iHigh[],iLow[],iClose[],iVolume[];
long           obj_time;
bool           first=true;
int            counter=0;


//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
// indicator buffers mapping
   SetIndexBuffer(0,vwapBuffer,INDICATOR_DATA);
   SetIndexStyle(0,DRAW_LINE,0,2,vwapColor);
   SetIndexLabel(0,"VWAP");
   
   SetIndexBuffer(1,iWeek,INDICATOR_DATA);
   SetIndexStyle(1,DRAW_LINE,0,2,Magenta);
   SetIndexLabel(1,"Week");
   
   ArrayInitialize(iWeek,EMPTY_VALUE);
   
   return(INIT_SUCCEEDED);
   
   
  }
//+------------------------------------------------------------------+
//| Custom indicator Deinitialization function                       |
//+------------------------------------------------------------------+     
void OnDeinit(const int reason)
  {
  
   if(reason==REASON_INITFAILED)
     
      Print("ERROR");
      ChartIndicatorDelete(0,0,indicator_name);
     
  }
//+------------------------------------------------------------------+
//| Custom indicator Chart Event function                            |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,const long &lparam,const double &dparam,const string &sparam)
  {
   if(sparam==Prefix) EventSetMillisecondTimer(100);
   else EventKillTimer();
  }
//+------------------------------------------------------------------+
//| Custom indicator TIMER function                                  |
//+------------------------------------------------------------------+
void OnTimer()
  {
  
   
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
{
 
   datetime  BOW = iTime(NULL, PERIOD_W1, 0);
   Print("BOW: ",BOW);
   long EOW = BOW + 604800;
   Print("EOW: ",EOW);
    
// Initializes Buffers
   if(first)
     {
      ArrayResize(iTime,  (int)(1.5*rates_total),(int)(rates_total/5));
      ArrayResize(iOpen,  (int)(1.5*rates_total),(int)(rates_total/5));
      ArrayResize(iHigh,  (int)(1.5*rates_total),(int)(rates_total/5));
      ArrayResize(iLow,   (int)(1.5*rates_total),(int)(rates_total/5));
      ArrayResize(iClose, (int)(1.5*rates_total),(int)(rates_total/5));
      ArrayResize(iVolume,(int)(1.5*rates_total),(int)(rates_total/5));
      ArrayInitialize(vwapBuffer,EMPTY_VALUE);
      ArrayInitialize(iTime,EMPTY_VALUE);
      ArrayInitialize(iOpen,EMPTY_VALUE);
      ArrayInitialize(iHigh,EMPTY_VALUE);
      ArrayInitialize(iLow,EMPTY_VALUE);
      ArrayInitialize(iClose,EMPTY_VALUE);
      ArrayInitialize(iVolume,EMPTY_VALUE);
      
     }
// load the price vectors

   for(int i=rates_total-1; i>=0; i--)
     {
      iRatesTotal=rates_total;
      iTime[i]=time[i];
      iOpen[i]=open[i];
      iHigh[i]=high[i];
      iLow[i]=low[i];
      iClose[i]=close[i];
      iVolume[i]=(double)tick_volume[i];
     }
   
   
   

   if(prev_calculated>rates_total || first)
     {
      for(i=0; i<iRatesTotal-1; i++) { if(BOW>=(long)iTime[i]) {startVWAP=i; break;} }
      if(startVWAP>=1)
        {
         // Calculate VWAP
         CalculateVWAP();     
        }     
     }
   
   if(startVWAP!=0)
   {int a = startVWAP;}
   else
   {a = 0;}
      
   ArrayCopy(iWeek,vwapBuffer,0,0,a);

   return(rates_total);
  }
//+------------------------------------------------------------------+
void CalculateVWAP()
  {
// VWAP Calculation
   Print("calc");
   double sumPrice=0,sumVol=0;
   for(int i=startVWAP-1; i>=0; i--)
     {
      sumPrice    += Price(iOpen,iHigh,iLow,iClose,i)*iVolume[i];
      sumVol      += iVolume[i];
      if (sumVol > 0) vwapBuffer[i]= sumPrice/sumVol;
     }
  }
//+------------------------------------------------------------------+
double Price(const double &open[],
             const double &high[],
             const double &low[],
             const double &close[],
             int          index)
  {
   double output;
   if(Method==Median)   output=(high[index]+low[index])/2;
   else if(Method==Typical)  output=(high[index]+low[index]+close[index])/3;
   else if(Method==Weighted) output=(high[index]+low[index]+close[index]+close[index])/4;
   else                      output=close[index];
   return(output);
  }
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+

bool NewBar()
   {
   static datetime   prevTime    =  0;
   datetime          currentTime =  iTime(Symbol(), Period(), 0);
   if(currentTime!=prevTime) 
      {
      prevTime = currentTime;
      return(true);
      }
   return(false);
   }
//+------------------------------------------------------------------+