# study name: TheVWAP_HighLow
# author: © Zachary M. Hurwitz
# date: April 14, 2020
#
# email: [email protected]
# twitter: @ZachHurwitz
# website: https://www.theVWAP.com
#hint: Plots VWAPs from the local (day) high and/or low of a (user-input) X days ago.
#
#hint Days_Ago: The number of days ago (current = 0) for which the HighVWAP or LowVWAP is calculated.
#hint VWAP_High_or_Low: Displays either the chart's Highest HighVWAP and Lowest LowVWAP ("HH_LL") on respective days, both HighVWAP <b> and </b> LowVWAP (default), or HighVWAP (or LowVWAP) individually.
#hint StDev1: Standard deviations from VWAP (primary band).
#hint StDev2: Standard deviations from VWAP (secondary band).
#hint Deviation_Bands: Displays standard deviation bands ('Yes) or hides them ('No') for the selected VWAPs.
#hint Deviation_Band_Color: Displays Green/Red colorization based on HighVWAP (Red to indicate likely resistance) or LowVWAP (Green to indicate likely support).
#hint price_type: Selects input price type for the VWAP calculation. VWAP in this context refers to the bar's "pure" vwap as designated by AMTD/TOS and is not reflective of the daily cumulative VWAP study.
DefineGlobalColor("High VWAP + Deviations", CreateColor(240, 0, 0));
DefineGlobalColor("Low VWAP + Deviations", CreateColor(0, 199, 145));
input Days_Ago = 0;
input VWAP_High_or_Low = {default Both, HighVWAP, LowVWAP, HH_LL};
input Deviation_Bands = No;
input Deviation_Band_Color = Yes;
input price_type = {High, Low, default VWAP};
input StDev1 = 1.0;
input StDev2 = 2.0;
def date = GetYYYYMMDD();
def dateReset = date != date[1];
def DayCount = CompoundValue(1, date != date[1] and !IsNaN(close), yes);
def Days_Elapsed = TotalSum(DayCount);
def highestDays = HighestAll(Days_Elapsed);
def onDaysAgo = highestDays - Days_Ago == Days_Elapsed;
def afterDaysAgo = if VWAP_High_or_Low == VWAP_High_or_Low."HH_LL" then 1 else Days_Elapsed >= (highestDays - Days_Ago);
def vol = volume();
def TypicalPrice;
switch (price_type) {
case High:
TypicalPrice = high;
case Low:
TypicalPrice = low;
default:
TypicalPrice = vwap;
}
def Money = TypicalPrice * vol;
def plotfilter_low = if Deviation_Bands and VWAP_High_or_Low != VWAP_High_or_Low."HighVWAP" then 1 else Double.NaN;
def plotfilter_high = if Deviation_Bands and VWAP_High_or_Low != VWAP_High_or_Low."LowVWAP" then 1 else Double.NaN;
def HH = HighestAll(high);
def LL = LowestAll(low);
def fut_mkt_open = 1800;
def day_high = high(period = AggregationPeriod.DAY);
def day_low = low(period = AggregationPeriod.DAY);
def track_High = CompoundValue(1, if dateReset then high else if high > track_High[1] then high else track_High[1], high);
def track_Low = CompoundValue(1, if dateReset then low else if low < track_Low[1] then low else track_Low[1], low);
def var_high = if IsNaN(day_high) or day_high != track_High then track_High else day_high;
def var_low = if IsNaN(day_low) or day_low != track_Low then track_Low else day_low;
def prc_High = CompoundValue(1, if afterDaysAgo then var_high else prc_High[1], if afterDaysAgo then var_high else 0);
def prc_Low = CompoundValue(1, if afterDaysAgo then var_low else prc_Low[1], if afterDaysAgo then var_low else 0);
def barNo = CompoundValue(1, barNo[1] + 1, 1);
def barNoH = CompoundValue(1, if high == prc_High then barNo else barNoH[1], 1);
def barNoL = CompoundValue(1, if low == prc_Low then barNo else barNoL[1], 1);
def latestBarNoH = HighestAll(if onDaysAgo then barNoH else Double.NaN);
def latestBarNoL = HighestAll(if onDaysAgo then barNoL else Double.NaN);
def showHVW = CompoundValue(1,
if ((VWAP_High_or_Low == VWAP_High_or_Low."Both" or VWAP_High_or_Low == VWAP_High_or_Low."HighVWAP") and barNo >= latestBarNoH)
or (VWAP_High_or_Low == VWAP_High_or_Low."HH_LL" and high == HH) then 1 else showHVW[1],
if ((VWAP_High_or_Low == VWAP_High_or_Low."Both" or VWAP_High_or_Low == VWAP_High_or_Low."HighVWAP") and barNo >= latestBarNoH)
or (VWAP_High_or_Low == VWAP_High_or_Low."HH_LL" and high == HH) then 1 else 0);
def showLVW = CompoundValue(1,
if ((VWAP_High_or_Low == VWAP_High_or_Low."Both" or VWAP_High_or_Low == VWAP_High_or_Low."LowVWAP") and barNo >= latestBarNoL)
or (VWAP_High_or_Low == VWAP_High_or_Low."HH_LL" and low == LL) then 1 else showLVW[1],
if ((VWAP_High_or_Low == VWAP_High_or_Low."Both" or VWAP_High_or_Low == VWAP_High_or_Low."LowVWAP") and barNo >= latestBarNoL)
or (VWAP_High_or_Low == VWAP_High_or_Low."HH_LL" and low == LL) then 1 else 0);
def afterHigh = CompoundValue(1, if high == prc_High then 1 else afterHigh[1], if high == prc_High then 1 else 0);
def afterLow = CompoundValue(1, if low == prc_Low then 1 else afterLow[1], if low == prc_Low then 1 else 0);
def TotalVolume_High = if showHVW then if IsNaN(TotalVolume_High[1]) then volume else TotalVolume_High[1] + volume else Double.NaN;
def TotalTransactions_High = if showHVW then if IsNaN(TotalTransactions_High[1]) then Money else TotalTransactions_High[1] + Money else Double.NaN;
def HighVW = if high == showHVW then TypicalPrice else if TotalVolume_High != 0 then TotalTransactions_High / TotalVolume_High else Double.NaN;
def startedVWAP_high = if !IsNaN(HighVW) then 1 else 0;
def deviation_High = Sqrt(TotalSum(Sqr(if startedVWAP_high then TypicalPrice - HighVW else 0) * volume) / TotalSum(if startedVWAP_high then volume else 0));
def volumeVWAP2SumH = if !IsNaN(HighVW) then volumeVWAP2SumH[1] + volume * Sqr(HighVW) else volume * Sqr(HighVW);
def DfVW_high = (close - HighVW) / deviation_High;
def TotalVolume_Low = if showLVW then if IsNaN(TotalVolume_Low[1]) then volume else TotalVolume_Low[1] + volume else Double.NaN;
def TotalTransactions_Low = if showLVW then if IsNaN(TotalTransactions_Low[1]) then Money else TotalTransactions_Low[1] + Money else Double.NaN;
def LowVW = if low == showLVW then TypicalPrice else if TotalVolume_Low != 0 then TotalTransactions_Low / TotalVolume_Low else Double.NaN;
def startedVWAP_low = if !IsNaN(LowVW) then 1 else 0;
def deviation_Low = Sqrt(TotalSum(Sqr(if startedVWAP_low then TypicalPrice - LowVW else 0) * volume) / TotalSum(if startedVWAP_low then volume else 0));
def volumeVWAP2SumL = if !IsNaN(LowVW) then volumeVWAP2SumL[1] + volume * Sqr(LowVW) else volume * Sqr(LowVW);
def DfVW_low = (close - LowVW) / deviation_Low;
def condition_High = if VWAP_High_or_Low == VWAP_High_or_Low."HH_LL" then HH else if afterDaysAgo then high(period = AggregationPeriod.DAY) else condition_High[1];
def condition_Low = if VWAP_High_or_Low == VWAP_High_or_Low."HH_LL" then LL else if afterDaysAgo then low(period = AggregationPeriod.DAY) else condition_Low[1];
plot VWAP_low = If(TotalVolume_Low != 0, TotalTransactions_Low / TotalVolume_Low, Double.NaN);
VWAP_low.SetHiding(VWAP_High_or_Low == VWAP_High_or_Low."HighVWAP");
VWAP_low.AssignValueColor(GlobalColor("Low VWAP + Deviations"));
VWAP_low.SetLineWeight(2);
plot VWAP_high = If(TotalVolume_High != 0, TotalTransactions_High / TotalVolume_High, Double.NaN);
VWAP_high.SetHiding(VWAP_High_or_Low == VWAP_High_or_Low."LowVWAP");
VWAP_high.AssignValueColor(GlobalColor("High VWAP + Deviations"));
VWAP_high.SetLineWeight(2);
plot r1L = VWAP_low + StDev1 * deviation_Low * plotfilter_low;
plot s1L = VWAP_low - StDev1 * deviation_Low * plotfilter_low;
plot r2L = VWAP_low + StDev2 * deviation_Low * plotfilter_low;
plot s2L = VWAP_low - StDev2 * deviation_Low * plotfilter_low;
r1L.AssignValueColor(if Deviation_Band_Color then GlobalColor("Low VWAP + Deviations") else Color.LIGHT_GRAY);
r2L.AssignValueColor(if Deviation_Band_Color then GlobalColor("Low VWAP + Deviations") else Color.LIGHT_GRAY);
s1L.AssignValueColor(if Deviation_Band_Color then GlobalColor("Low VWAP + Deviations") else Color.LIGHT_GRAY);
s2L.AssignValueColor(if Deviation_Band_Color then GlobalColor("Low VWAP + Deviations") else Color.LIGHT_GRAY);
r1L.SetStyle(Curve.SHORT_DASH);
r2L.SetStyle(Curve.FIRM);
s1L.SetStyle(Curve.SHORT_DASH);
s2L.SetStyle(Curve.FIRM);
r1L.HideBubble();
r2L.HideBubble();
s1L.HideBubble();
s2L.HideBubble();
plot r1H = VWAP_high + StDev1 * deviation_High * plotfilter_high;
plot s1H = VWAP_high - StDev1 * deviation_High * plotfilter_high;
plot r2H = VWAP_high + StDev2 * deviation_High * plotfilter_high;
plot s2H = VWAP_high - StDev2 * deviation_High * plotfilter_high;
r1H.AssignValueColor(if Deviation_Band_Color then GlobalColor("High VWAP + Deviations") else Color.LIGHT_GRAY);
r2H.AssignValueColor(if Deviation_Band_Color then GlobalColor("High VWAP + Deviations") else Color.LIGHT_GRAY);
s1H.AssignValueColor(if Deviation_Band_Color then GlobalColor("High VWAP + Deviations") else Color.LIGHT_GRAY);
s2H.AssignValueColor(if Deviation_Band_Color then GlobalColor("High VWAP + Deviations") else Color.LIGHT_GRAY);
r1H.SetStyle(Curve.SHORT_DASH);
r2H.SetStyle(Curve.FIRM);
s1H.SetStyle(Curve.SHORT_DASH);
s2H.SetStyle(Curve.FIRM);
r1H.HideBubble();
r2H.HideBubble();
s1H.HideBubble();
s2H.HideBubble();