Problem with VWAP

 

hey guys, i need some help. new to mql4 dont know what im doing, im making a custom indicator for an ea. i wanted to get the VWAP indicator that resets daily and has the upper and lower bands using standard deviation. this is where i got to. Dont try and open it on mt4 because it will crash your pc. I ask for some tips / help on this. Thanks

#property indicator_chart_window
#property indicator_buffers 4
#property indicator_color1 Yellow
#property indicator_color2 Red
#property indicator_color3 Green

extern bool Show_Daily=true;

double Daily[];
double SD[];
double UpperBand1[];
double UpperBand2[];
double LowerBand1[];
double LowerBand2[];
double WP[];


int init()
{
 IndicatorShortName("");
 IndicatorDigits(Digits);
 
 SetIndexStyle(0,DRAW_LINE);
 SetIndexBuffer(0,Daily);
 
 SetIndexStyle(1,DRAW_LINE);
 SetIndexBuffer(1,UpperBand1);
 
 SetIndexStyle(2,DRAW_LINE);
 SetIndexBuffer(2,UpperBand2);
 
 SetIndexStyle(3,DRAW_LINE);
 SetIndexBuffer(3,LowerBand1);
 
 SetIndexStyle(4,DRAW_LINE);
 SetIndexBuffer(4,LowerBand2);
 
 SetIndexBuffer(3,WP);

 return(0);
}

int deinit()
{

 return(0);
}

double CalcDaily(int pos)
{
 int index=pos;
 double Sum1=0.;
 double Sum2=0.;
 double Sum3=0.;
 int DT=TimeDay(Time[pos]);
 
 while (TimeDay(Time[index])==DT && index<Bars)
 {
  Sum1+=WP[index];
  Sum2+=Volume[index];
 }
  index++;
 
 if (Sum2!=0.)
 {
  return (Sum1/Sum2);
 }
 else
 {
  return (EMPTY_VALUE);
 }
}
double CalcSD(int pos)
{
 int index=pos;
 double Sum1=0.;

 int DT=TimeDay(Time[pos]);
 
 while (TimeDay(Time[index])==DT && index<Bars)
 {
  Sum1+=MathPow(Close[index]-Daily[pos],2);
  
 }
  index++;
 
 if (Sum1!=0.)
 {
  return (MathSqrt(Sum1/index));
 }
 else
 {
  return (EMPTY_VALUE);
 }
}


int start()
{
 if(Bars<=3) return(0);
 int ExtCountedBars=IndicatorCounted();
 if (ExtCountedBars<0) return(-1);
 int limit=Bars-2;
 if(ExtCountedBars>2) limit=Bars-ExtCountedBars-1;
 int pos;
 pos=limit;
 
 while(pos>=0)
 {
  WP[pos]=Volume[pos]*(High[pos]+Low[pos]+Close[pos])/3;
   UpperBand1[pos] = Daily[pos] + SD[pos]* 2.01;
   UpperBand2[pos] = Daily[pos] + SD[pos]* 2.51;
   LowerBand1[pos] = Daily[pos] - SD[pos]* 2.01;
   LowerBand2[pos] = Daily[pos] - SD[pos]* 2.51;
  pos--;
 }
 
 while(pos>=0)
 {
  if (Show_Daily)
  {
   Daily[pos]=CalcDaily(pos);
   SD[pos]=CalcSD(pos);
  }
 

  pos--;
 } 

 pos=limit;
 return(0);
}
 
Your index in the first while loop is outside the loop.

Therefore the loop never finishes.

Edit:

And on the second one as well.
 
WP and SD are arrays with no size. Make them non-visible buffers. IndicatorBuffers - Custom Indicators - MQL4 Reference
 
Dominik Christian Egert #:
Your index in the first while loop is outside the loop.

Therefore the loop never finishes.

Edit:

And on the second one as well.
Thanks guys