#AnchoredVWAP_high_low
input method = {default chart, range};
input period_begin = 20240701;
input period_end = 20241231;
def bn = BarNumber();
def c = close;
def v = volume;
def vw = vwap;
def anchorh;
def volumesumh;
def volumevwapsumh;
def volumevwap2sumh;
def priceh;
def deviationh;
def period_high = if GetYYYYMMDD()[1] < period_begin and GetYYYYMMDD() == period_begin then high else if GetYYYYMMDD() <= period_end and high > period_high[1] then high else period_high[1];
anchorh = if method == method.range and GetYYYYMMDD() <= period_end and high == period_high then bn else if method == method.chart and high == HighestAll(high) then bn else anchorh[1];
volumesumh = if bn >= HighestAll(anchorh) then volumesumh[1] + volume else 0;
volumevwapsumh = if bn >= HighestAll(anchorh) then volumevwapsumh[1] + volume * vwap else 0;
volumevwap2sumh = if bn >= HighestAll(anchorh) then volumevwap2sumh[1] + volume * Sqr(vwap) else 0;
priceh = volumevwapsumh / volumesumh;
deviationh = Sqrt(Max(volumevwap2sumh / volumesumh - Sqr(priceh), 0));
plot VWAPh = priceh;
VWAPh.SetDefaultColor(GetColor(0));
input showbands = yes;
input numDev1 = 1.0;
input numDev2 = 2.0;
input numDev3 = 3.0;
plot UpperBand1 = if !showbands then Double.NaN else VWAPh + numDev1 * deviationh;
plot LowerBand1 = if !showbands then Double.NaN else VWAPh - numDev1 * deviationh;
plot UpperBand2 = if !showbands then Double.NaN else VWAPh + numDev2 * deviationh;
plot LowerBand2 = if !showbands then Double.NaN else VWAPh - numDev2 * deviationh;
plot UpperBand3 = if !showbands then Double.NaN else VWAPh + numDev3 * deviationh;
plot LowerBand3 = if !showbands then Double.NaN else VWAPh - numDev3 * deviationh;
VWAPh.SetDefaultColor(Color.CYAN);
UpperBand1.SetDefaultColor(Color.GREEN);
LowerBand1.SetDefaultColor(Color.RED);
UpperBand2.SetDefaultColor(Color.GREEN);
LowerBand2.SetDefaultColor(Color.RED);
UpperBand3.SetDefaultColor(Color.GREEN);
LowerBand3.SetDefaultColor(Color.RED);
VWAPh.HideBubble();
UpperBand1.HideBubble();
LowerBand1.HideBubble();
UpperBand2.HideBubble();
LowerBand2.HideBubble();
UpperBand3.HideBubble();
LowerBand3.HideBubble();
input showclouds = yes;
AddCloud(if showclouds then UpperBand3 else Double.NaN, UpperBand2, Color.LIGHT_GREEN, Color.LIGHT_GREEN);
AddCloud(if showclouds then LowerBand3 else Double.NaN, LowerBand2, Color.LIGHT_RED, Color.LIGHT_RED);
#Lowest Low of Chart
def anchor1;
def volumesum1;
def volumevwapsum1;
def volumevwap2sum1;
def price1;
def deviation1;
def period_low = if GetYYYYMMDD()[1] < period_begin and GetYYYYMMDD() == period_begin then low else if GetYYYYMMDD() <= period_end and low < period_low[1] then low else period_low[1];
anchor1 = if method == method.range and GetYYYYMMDD() <= period_end and low == period_low then bn else if method == method.chart and low == LowestAll(low) then bn else anchor1[1];
volumesum1 = if bn >= HighestAll(anchor1) then volumesum1[1] + volume else 0;
volumevwapsum1 = if bn >= HighestAll(anchor1) then volumevwapsum1[1] + volume * vwap else 0;
volumevwap2sum1 = if bn >= HighestAll(anchor1) then volumevwap2sum1[1] + volume * Sqr(vwap) else 0;
price1 = volumevwapsum1 / volumesum1;
deviation1 = Sqrt(Max(volumevwap2sum1 / volumesum1 - Sqr(price1), 0));
plot VWAP1 = price1;
VWAP1.SetDefaultColor(GetColor(0));
plot UpperBand11 = if !showbands then Double.NaN else VWAP1 + numDev1 * deviation1;
plot LowerBand11 = if !showbands then Double.NaN else VWAP1 - numDev1 * deviation1;
plot UpperBand21 = if !showbands then Double.NaN else VWAP1 + numDev2 * deviation1;
plot LowerBand21 = if !showbands then Double.NaN else VWAP1 - numDev2 * deviation1;
plot UpperBand31 = if !showbands then Double.NaN else VWAP1 + numDev3 * deviation1;
plot LowerBand31 = if !showbands then Double.NaN else VWAP1 - numDev3 * deviation1;
VWAP1.SetDefaultColor(Color.CYAN);
UpperBand11.SetDefaultColor(Color.GREEN);
LowerBand11.SetDefaultColor(Color.RED);
UpperBand21.SetDefaultColor(Color.GREEN);
LowerBand21.SetDefaultColor(Color.RED);
UpperBand31.SetDefaultColor(Color.GREEN);
LowerBand31.SetDefaultColor(Color.RED);
VWAP1.HideBubble();
UpperBand11.HideBubble();
LowerBand11.HideBubble();
UpperBand21.HideBubble();
LowerBand21.HideBubble();
UpperBand31.HideBubble();
LowerBand31.HideBubble();
AddCloud(if showclouds then UpperBand31 else Double.NaN, UpperBand21, Color.LIGHT_GREEN, Color.LIGHT_GREEN);
AddCloud(if showclouds then LowerBand31 else Double.NaN, LowerBand21, Color.LIGHT_RED, Color.LIGHT_RED);
;