Help with VWAP indicator

 
Hello,

I am trying to calculate a VWAP indicator where the averaging period increases as time elapses from the start of the trading day. Right now, however, I can only design an indicator that calculates VWAP using a fixed period.

In general, VWAP is calculated as:

SUM(PiQi) / SUM(Qi)

Where Pi is the price for the given moment and Qi is the volume for the same moment

If someone could show me how to get my VWAP period to increase as time elapses from the start of each new day I would be very grateful. The formula should go something like this:

Time 1: (P1*Q1) / (Q1)
Time 2: (P1*Q1)+(P2*Q2) / (Q1+Q2)
Time 3: (P1*Q1)+(P2*Q2)+(P3*Q3) / (Q1+Q2+Q3)
...
Time24: (P1*Q1)+(P2*Q2)+(P3*Q3)...+(P24*Q24) / (Q1+Q2+Q3...+Q24)
Time1: (P1*Q1) / (Q1)
...


Here is the code that I have so far. Notice that the variable VWAP_Period is fixed at 20.


//---------------------------------------------------------------------------------------
extern int VWAP_Period = 20;
double VWAP[];
//--------------------------------------------------------------------------------------
int init()
{
SetIndexBuffer(0,VWAP);
SetIndexStyle(0,DRAW_LINE,STYLE_SOLID,2);

return(0);
}
//-------------------------------------------------------------------------------------------
int start()
{
//------------------------------------------------------------------------------------------
int i,
n,
Counted_bars;
double Sum_PQ,
Sum_Q;

Counted_bars=IndicatorCounted();
i=Bars-Counted_bars-1;
while(i>=0)
{
Sum_PQ=0;
Sum_Q=0;
for (n=i;n<=i+VWAP_Period-1;n++)
{
Sum_PQ = Sum_PQ + Open[n]*Volume[n];
Sum_Q = Sum_Q + Volume[n];
}
VWAP[i] = Sum_PQ/Sum_Q;
i--;
}
//-----------------------------------------------------------------------------------------
return(0);
}//----------------------------------------------------------------------------------------


Thank you for any help you can provide.
Reason: