#Anchored_VWAP_Continuous_from_YTD_Points
input select_start = {default YTD, YTDHigh, YTDlow};
input showlabel = no;
input showverticals = no;
def bn = BarNumber();
def ymd = GetYYYYMMDD();
def yy = if GetYear()[1] == GetYear() - 1 and GetYear() then 1 else yy[1] + 1;
def hh = if yy == 1 then high else if yy > 1 and high > hh[1] then high else hh[1];
def ll = if yy == 1 then low else if yy > 1 and low < ll[1] then low else ll[1];
def ytdhigh = HighestAll(if high == hh then ymd else Double.NaN);
def ytdlow = HighestAll(if bn == 1 then Double.NaN else if low == ll then ymd else Double.NaN);
def ytd = HighestAll(if GetYear()[1] != GetLastYear() then ymd else Double.NaN);
AddLabel(showlabel, AsPrice(GetYear()) + " YTD High " + AsText(hh) + " @ " + AsPrice(ytdhigh) , Color.LIGHT_GREEN);
AddLabel(showlabel, AsPrice(GetYear()) + " YTD Low " + AsText(ll) + " @ " + AsPrice(ytdlow), Color.LIGHT_RED);
AddVerticalLine(showverticals and ymd[1] != ytd and ymd == ytd, AsPrice(ytd) + " START", Color.WHITE);
AddVerticalLine(showverticals and ymd[1] != ytdhigh and ymd == ytdhigh, AsPrice(ytdhigh) + " HIGH", Color.WHITE);
AddVerticalLine(showverticals and ymd[1] != ytdlow and ymd == ytdlow, AsPrice(ytdlow) + " LOW" , Color.WHITE);
def start;
input numDevDn = -2.0;
input numDevUp = 2.0;
switch (select_start) {
case YTD:
start = ytd;
case YTDHigh:
start = ytdhigh;
case YTDlow:
start = ytdlow;
}
def anchor = GetYYYYMMDD() >= start;
def volumeSum = if anchor then CompoundValue(1, volumeSum[1] + volume, volume) else 0;
def volumeVwapSum = if anchor then CompoundValue(1, volumeVwapSum[1] + volume * vwap, volume * vwap) else 0;
def volumeVwap2Sum = if anchor then CompoundValue(1, volumeVwap2Sum[1] + volume * Sqr(vwap), volume * Sqr(vwap)) else 0;
def price = volumeVwapSum / volumeSum;
def deviation = Sqrt(Max(volumeVwap2Sum / volumeSum - Sqr(price), 0));
plot VWAP = price;
input showbands = yes;
plot UpperBand = if !showbands then Double.NaN else price + numDevUp * deviation;
plot LowerBand = if !showbands then Double.NaN else price + numDevDn * deviation;
VWAP.SetDefaultColor(GetColor(0));
UpperBand.SetDefaultColor(GetColor(2));
LowerBand.SetDefaultColor(GetColor(4));
#