VWAP and SDs

#1
Error into this AFL please help:

error 10 in line 69 col 28 Array subscript out of range. You must nut access array elements outside 0..(BarcOUNT-1) RANGE. You attempted to access non-existing 200-th element of array. :annoyed:

ND = Day() != Ref(Day(), -1);

///// VWAP and SDs /////

P = (H + L) / 2;
VWP = P * V;
BI = BarIndex();
BeginBI = ValueWhen(ND, BI);
BeginBI = BeginBI[BarCount -1];
if(BeginBI < BarCount - 1)
{
InRange = BI >= BeginBI;
CumV = Cum(V * InRange);
CumVWP = Cum(VWP * InRange);
VWAP = CumVWP / CumV;
S = Cum(Ref(CumV, -1) * V * (P - Ref(VWAP, -1))^2 / CumV);
Variance = S / CumV;
SD = sqrt(Variance);
VWAP = IIf(InRange, VWAP, Null);
Plot(VWAP, "VWAP", colorYellow, styleNoTitle + styleNoRescale);
Plot(VWAP + SD, "+1SD", colorGreen, styleDashed + styleNoTitle + styleNoRescale);
Plot(VWAP - SD, "-1SD", colorRed, styleDashed + styleNoTitle + styleNoRescale);
Plot(VWAP + 2*SD, "+2SD", colorSeaGreen, styleDashed + styleNoTitle + styleNoRescale);
Plot(VWAP - 2*SD, "-2SD", colorOrange, styleDashed + styleNoTitle + styleNoRescale);
Plot(VWAP + 3*SD, "+3SD", colorPaleGreen, styleDashed + styleNoTitle + styleNoRescale);
Plot(VWAP - 3*SD, "-3SD", colorLightOrange, styleDashed + styleNoTitle + styleNoRescale);
}

///// PVP /////

BarSinceND = BarsSince(ND);
iStart = Max(BarCount - 1 - BarSinceND[BarCount - 1], 0);
Top = HighestSince(ND, High);
Bot = LowestSince(ND, Low);
Range = Top - Bot;
BoxesInRange = Range / TickSize + 1;
VolUnit = Volume / ((High - Low) / TickSize + 1);
VUcount = 0;
MaxVUcount = 0;
PVP = Null;

if(iStart > 0)
{
for(i = iStart; i < BarCount; i++)
{
jShift = round((Bot[i - 1] - Low) / TickSize);
if((BoxesInRange < BarCount))
{
if(jShift > 0)
{
LastVUcount = VUcount;
VUcount = 0;
for(j = jShift; j < BoxesInRange; j++)
{
VUCount[j] = LastVUCount[j - jShift];
}
}
jStart = round((Low - Bot) / TickSize);
jEnd = round((High - Bot) / TickSize);
for(j = jStart; j <= jEnd; j++)
{
VUcount[j] = VUcount[j] + VolUnit;
MaxVUcount = Max(MaxVUcount, VUcount[j]);
}
}
}
for(j = 0; j < BoxesInRange[BarCount - 1]; j++)
{
if(MaxVUcount == VUcount[j])
PVP = Bot[BarCount - 1] + j * TickSize;
}
Plot(PVP, "PVP", colorTurquoise, styleDots + styleNoTitle + styleNoRescale);
}
 

pratapvb

Well-Known Member
#2
#3
THANKS PRATAPVB but I choose this one because I'm interested on PVP - Peak of Volume at Price - that I suppose it's different from a very common Point of control used in vwap. Anyone could help me to fix it?
 

KelvinHand

Well-Known Member
#4
THANKS PRATAPVB but I choose this one because I'm interested on PVP - Peak of Volume at Price - that I suppose it's different from a very common Point of control used in vwap. Anyone could help me to fix it?
This fixed is to get rid of the "Array subscript out of range" nightmare.
This correction did not lead to plot anything.
Also it does not means i am doing it right.

But hope it help you in your code study that lead you to the right way.


PHP:
if(iStart > 0)
{
	for(i = iStart; i < BarCount; i++)	
  {
		jShift = round((Bot[i - 1] - Low[i]) / TickSize);
		if((BoxesInRange[i] < BarCount))
		{
			if(jShift > 0)	
			{
				LastVUcount = VUcount;
				VUcount = 0;
				for(j = jShift; j < BoxesInRange[i]; j++)
					VUCount[j] = LastVUCount[j - jShift];
			} //if jShift

			jStart = round((Low[i] - Bot[i]) / TickSize);
			jEnd = round((High[i] - Bot[i]) / TickSize);
			for(j = jStart; j <= jEnd; j++)
			{
				VUcount[j] = VUcount[j] + VolUnit[i];
				MaxVUcount = Max(MaxVUcount, VUcount[j]);
			}//for j

			//-- Move to here-------------------	
                        for(j = 0; j < BoxesInRange[BarCount - 1]; j++)
			{
				if(MaxVUcount == VUcount[j])
				PVP[j] = Bot[BarCount - 1] + j * TickSize;
			}//for j

		} //if((BoxesInRange[i] < BarCount))

  } //for i

	Plot(PVP, "PVP", colorTurquoise, styleDots + styleNoTitle + styleNoRescale);
}
 
Last edited:

Similar threads

Broker Special Offers

Intraday Higher Leverage

Save up to 90% in brokerage and get higher leverage for intraday trades.

Name:Phone:
Email:City:
State:
Are you a day trader?