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[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];
}
}
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 = 0; j < BoxesInRange[BarCount - 1]; j++)
{
if(MaxVUcount == VUcount[j])
PVP = Bot[BarCount - 1] + j * TickSize;
}
Plot(PVP, "PVP", colorTurquoise, styleDots + styleNoTitle + styleNoRescale);
}