#hint StDev1: Standard deviations from VWAP (primary band).
#hint StDev2: Standard deviations from VWAP (secondary band).
#hint timeframe: Day (resetting at market open), Week (resetting on the first day of the week), or Month (resetting on the first day of the month).
#hint VWAP_Color: SlopeBasic plots either a gold (upsloping) or pink (downsloping) VWAP curve. \n SlopeSignal plots a blue VWAP curve based on VWAP slope for trend up, pink for trend down, and white for undetermined direction. \n Single plots a monochromatic (blue) VWAP curve.
#hint Background_Color_Signal:
Cloud: displays the Green/Red dynamic background colorization depending on position relative to VWAP. \n Fast_Lane: displays highlighting of 'fast lane' (surpassing +/- 1.0 standard deviations from VWAP) price action. \n Both: displays both. Obviously. \n None: displays neither. Duh.
#hint Show_Dev_Signal: Displays the secondary band (StDev2) Green/Red colorization depending on proximity/position relative to VWAP.
#hint Show_Tolerance_Bands: Displays additional tolerance bands around the primary (StDev1) curve.
#hint Tolerance_Band_Width: Standard deviation distance from primary (StDev1) used in tolerance bands.
#hint HH_LL_of_x_bars: Number of bars of which the current bar must be a high (or low) in order to qualify as a flagged/identified extreme. HH is for 'highest high' of X bars; LL is for 'lowest low' of X bars. Default is 0 (i.e., off) but recommended setting is ~30-60m for intraday trading (and thus, 30-60 bars for 1m charts or 6-12 bars for 5m charts, depending on user preference).
#
##hint Market_Open: The time of market open (0930 EST default).
##hint Show_Day_Divider: Displays a vertical line at market open to designate the start of regular trading hours (RTH) on a given calendar day.
##hint high_low_threshold: Standard deviation tolerance used to identify a qualifying high or low.
input StDev1 = 1.0;
input StDev2 = 2.0;
input timeFrame = {default DAY, WEEK, MONTH};
input VWAP_Color = {default SlopeBasic, SlopeSignal, Single};
input Background_Color_Signal = {default Cloud, Fast_Lane, Both, None};
def show_cloud = if background_color_signal == background_color_signal."Cloud" or background_color_signal == background_color_signal."Both" then 1 else 0;
def show_fast_lane = if background_color_signal == background_color_signal."Fast_Lane" or background_color_signal == background_color_signal."Both" then 1 else 0;
input Show_Dev_Signal = no;
input Show_Tolerance_Bands = no;
input Tolerance_Band_Width = 0.1;
input HH_LL_of_x_bars = 0;
def high_low_threshold = 0.25;
def HLBarLength = if HH_LL_of_x_Bars == 0 then Double.NaN else HH_LL_of_X_Bars;
def bandcolorslopereq = 1.5;
def Show_Day_Divider = Yes;
def Market_Open = RegularTradingStart(GetYYYYMMDD());
def Equities_Futures = if ticksize() * tickvalue() == 0.0001 then 1 else 0;
def tooearly = if Equities_Futures and GetTime() <= Market_Open + 1200000 then 1 else 0;
def cap = GetAggregationPeriod();
def errorInAggregation = timeFrame == timeFrame.DAY and cap >= AggregationPeriod.WEEK or timeFrame == timeFrame.WEEK and cap >= AggregationPeriod.MONTH;
def yyyyMmDd = GetYYYYMMDD();
def periodIndx;
switch (timeFrame) {
case DAY:
periodIndx = yyyyMmDd;
case WEEK:
periodIndx = Floor((DaysFromDate(First(yyyyMmDd)) + GetDayOfWeek(First(yyyyMmDd))) / 7);
case MONTH:
periodIndx = RoundDown(yyyyMmDd / 100, 0);
}
def NewDay = CompoundValue(1, periodIndx != periodIndx[1], yes);
def highestDays = TotalSum(NewDay);
rec VolumeSum;
rec MoneySum;
rec volumeVwap2Sum;
if (NewDay) {
VolumeSum = volume;
MoneySum = volume * vwap;
volumeVwap2Sum = volume * Sqr(vwap);
} else {
VolumeSum = CompoundValue(1, VolumeSum[1] + volume, volume);
MoneySum = CompoundValue(1, MoneySum[1] + volume * vwap, volume * vwap);
volumeVwap2Sum = CompoundValue(1, volumeVwap2Sum[1] + volume * Sqr(vwap), volume * Sqr(vwap));
}
def VW = MoneySum / VolumeSum;
def deviation = Sqrt(Max(volumeVwap2Sum / VolumeSum - Sqr(VW), 0));
def slopelength = 5;
def slopelookback = 8;
def slope = ((VW - VW[slopelength]) / slopelength) / close * 100000;
def slopeup = if slope[slopelookback] > 1 then 1 else 0;
def slopedn = if slope[slopelookback] < -1 then 1 else 0;
def nonslope = if !slopeup and !slopedn then 1 else 0;
def v_counter_low = fold index = 1 to 90 with v_tempvar = 0 while v_tempvar == 0 do if low[index] < low then index else 0;
def v_counter_high = fold index2 = 1 to 90 with v_tempvar2 = 0 while v_tempvar2 == 0 do if high[index2] > high then index2 else 0;
AddChartBubble(if !isNaN(HLBarLength) and v_counter_low > HLBarLength and between(close,VW-high_low_threshold*deviation,VW+high_low_threshold*deviation) then yes else no,low*0.9995,v_counter_low,color.PINK,no);
AddChartBubble(if !isNaN(HLBarLength) and v_counter_low > HLBarLength and between(close,VW-(1+high_low_threshold)*deviation,VW-(1-high_low_threshold)*deviation) then yes else no,low*0.9995,v_counter_low,color.PINK,no);
AddChartBubble(if !isNaN(HLBarLength) and v_counter_low > HLBarLength and between(close,VW+(1-high_low_threshold)*deviation,VW+(1+high_low_threshold)*deviation) then yes else no,low*0.9995,v_counter_low,color.PINK,no);
AddChartBubble(if !isNaN(HLBarLength) and v_counter_high > HLBarLength and between(close,VW-high_low_threshold*deviation,VW+high_low_threshold*deviation) then yes else no,high*1.0005,v_counter_high,color.LIGHT_GREEN,yes);
AddChartBubble(if !isNaN(HLBarLength) and v_counter_high > HLBarLength and between(close,VW-(1+high_low_threshold)*deviation,VW-(1-high_low_threshold)*deviation) then yes else no,high*1.0005,v_counter_high,color.LIGHT_GREEN,yes);
AddChartBubble(if !isNaN(HLBarLength) and v_counter_high > HLBarLength and between(close,VW+(1-high_low_threshold)*deviation,VW+(1+high_low_threshold)*deviation) then yes else no,high*1.0005,v_counter_high,color.LIGHT_GREEN,yes);
plot VWAP;
plot UpperBand_2;
plot LowerBand_2;
plot UpperBand_1;
plot LowerBand_1;
plot OuterBand_H;
plot InnerBand_H;
plot InnerBand_L;
plot OuterBand_L;
if (!errorInAggregation) {
VWAP = VW;
UpperBand_2 = VW + StDev2 * deviation;
LowerBand_2 = VW - StDev2 * deviation;
UpperBand_1 = VW + StDev1 * deviation;
LowerBand_1 = VW - StDev1 * deviation;
OuterBand_H = VW + ((1 + Tolerance_Band_Width) * deviation);
InnerBand_H = VW + ((1 - Tolerance_Band_Width) * deviation);
InnerBand_L = VW - ((1 - Tolerance_Band_Width) * deviation);
OuterBand_L = VW - ((1 + Tolerance_Band_Width) * deviation);
} else {
VWAP = Double.NaN;
UpperBand_2 = Double.NaN;
LowerBand_2 = Double.NaN;
UpperBand_1 = Double.NaN;
LowerBand_1 = Double.NaN;
OuterBand_H = Double.NaN;
InnerBand_H = Double.NaN;
InnerBand_L = Double.NaN;
OuterBand_L = Double.NaN;
}
UpperBand_2.AssignValueColor(if tooearly then Color.DARK_GRAY else if Show_Dev_Signal and slope >= 0 and slopeup then Color.GREEN else Color.GRAY);
UpperBand_2.SetDefaultColor(Color.GRAY);
UpperBand_2.HideBubble();
UpperBand_2.HideTitle();
OuterBand_H.SetDefaultColor(Color.GRAY);
OuterBand_H.HideBubble();
OuterBand_H.HideTitle();
OuterBand_H.SetHiding(!Show_Tolerance_Bands);
UpperBand_1.AssignValueColor(if tooearly then Color.DARK_GRAY else Color.WHITE);
UpperBand_1.SetDefaultColor(Color.WHITE);
UpperBand_1.SetStyle(Curve.SHORT_DASH);
UpperBand_1.HideBubble();
UpperBand_1.HideTitle();
InnerBand_H.SetDefaultColor(Color.GRAY);
InnerBand_H.HideBubble();
InnerBand_H.HideTitle();
InnerBand_H.SetHiding(!Show_Tolerance_Bands);
VWAP.SetDefaultColor(Color.CYAN);
VWAP.AssignValueColor(if VWAP_Color == VWAP_Color.SlopeSignal and slopeup and slopeup[1] and slopeup[2] and slopeup[3] and slopeup[4] then Color.CYAN
else if VWAP_Color == VWAP_Color.SlopeSignal and slopedn and slopedn[1] and slopedn[2] and slopedn[3] and slopedn[4] then Color.MAGENTA
else if VWAP_Color == VWAP_Color.SlopeBasic and slope > 0 then color.ORANGE
else if VWAP_Color == VWAP_Color.SlopeBasic and slope <= 0 then color.MAGENTA
else if VWAP_Color == VWAP_Color.Single then color.CYAN
else Color.WHITE);
InnerBand_L.SetDefaultColor(Color.GRAY);
InnerBand_L.HideBubble();
InnerBand_L.HideTitle();
InnerBand_L.SetHiding(!Show_Tolerance_Bands);
LowerBand_1.AssignValueColor(if tooearly then Color.DARK_GRAY else Color.WHITE);
LowerBand_1.SetDefaultColor(Color.WHITE);
LowerBand_1.SetStyle(Curve.SHORT_DASH);
LowerBand_1.HideBubble();
LowerBand_1.HideTitle();
OuterBand_L.SetDefaultColor(Color.GRAY);
OuterBand_L.HideBubble();
OuterBand_L.HideTitle();
OuterBand_L.SetHiding(!Show_Tolerance_Bands);
LowerBand_2.AssignValueColor(if tooearly then Color.DARK_GRAY else if Show_Dev_Signal and slope < 0 and slopedn then Color.RED else Color.GRAY);
LowerBand_2.SetDefaultColor(Color.GRAY);
LowerBand_2.HideBubble();
LowerBand_2.HideTitle();
def var_close = if Show_Cloud then close else Double.NaN;
AddCloud(var_close, VW, Color.LIGHT_GREEN, Color.DARK_RED);
def var_VWoneup = if Show_Fast_Lane then UpperBand_1 else Double.NaN;
def var_VWonedn = if Show_Fast_Lane then LowerBand_1 else Double.NaN;
AddCloud(close, var_VWoneup, CreateColor(0, 255, 255), color.BLACK);
AddCloud(close, var_VWonedn, Color.BLACK, CreateColor(150, 0, 50));
AddVerticalLine(if Show_Day_Divider then if GetTime() == Market_Open and !isNaN(close) then yes else if GetTime() <= Market_Open + 60000 and !isNaN(close) and periodIndx != periodIndx[1] then yes else no else no,"",CreateColor(40,40,40),curve.FIRM);