#####################################################################
#VWAP_Limited_Full_Lines 2011.11.12 14:45pst zzz added limited lines and full lines # instead of normal curves
#VWAP Study: thinkorswim, inc. (c) 2011
# 2012-02-06 duplicated the thinkorswim code
# modified some thinkorswim variable names
# modified default colors for plotting
# and added code for 2nd deviation
#
input num1stDevUp = 1.0;
input num2ndDevUp = 2.0;
input num3rdDevUp = 3.0;
def num1stDevDn = -num1stDevUp;
def num2ndDevDn = -num2ndDevUp;
def num3rdDevDn = -num3rdDevUp;
input multiplier = 1.0;#Hint multiplier: Change to +/- number of "Days", "Weeks", etc
input lines = {default limited, horizontal, lines};
input timeFrame = {MINUTE, MIN2, MIN3, MIN5, MIN10, MIN15, MIN20, MIN30, default HOUR, TWOHOUR, FOURHOUR, DAY, WEEK, MONTH, CHART};
def cap = GetAggregationPeriod();
def errorInAggregation =
timeFrame == timeFrame.DAY and cap >= AggregationPeriod.WEEK or
timeFrame == timeFrame.WEEK and cap >= AggregationPeriod.MONTH;
Assert(!errorInAggregation, "vwap timeFrame should be not less than current chart aggregation period");
def yyyyMmDd = GetYYYYMMDD();
def periodIndx;
def seconds = SecondsFromTime(0);
def minutes;
def day_number = DaysFromDate(First(yyyyMmDd)) + GetDayOfWeek(First(yyyyMmDd));
switch (timeFrame) {
case MINUTE:
periodIndx = Floor(seconds / 60 + day_number * 24 * 60);
minutes = 1;
case MIN2:
periodIndx = Floor(seconds / 120 + day_number * 24);
minutes = 2;
case MIN3:
periodIndx = Floor(seconds / 180 + day_number * 24);
minutes = 3;
case MIN5:
periodIndx = Floor(seconds / 300 + day_number * 24);
minutes = 5;
case MIN10:
periodIndx = Floor(seconds / 600 + day_number * 24);
minutes = 10;
case MIN15:
periodIndx = Floor(seconds / 900 + day_number * 24);
minutes = 15;
case MIN20:
periodIndx = Floor(seconds / 1200 + day_number * 24);
minutes = 20;
case MIN30:
periodIndx = Floor(seconds / 1800 + day_number * 24);
minutes = 30;
case HOUR:
periodIndx = Floor(seconds / 3600 + day_number * 24);
minutes = 60;
case TWOHOUR:
periodIndx = Floor(seconds / 7200 + day_number * 24);
minutes = 120;
case FOURHOUR:
periodIndx = Floor(seconds / 14400 + day_number * 24);
minutes = 240;
case DAY:
periodIndx = CountTradingDays(Min(First(yyyyMmDd), yyyyMmDd), yyyyMmDd) - 1;
minutes = 720;
case WEEK:
periodIndx = Floor((DaysFromDate(First(yyyyMmDd)) + GetDayOfWeek(First(yyyyMmDd))) / 7);
minutes = 3600;
case MONTH:
periodIndx = RoundDown(yyyyMmDd / 100, 0);
minutes = 15120;
case CHART:
periodIndx = 0;
minutes = 0;
}
def countindx = CompoundValue(1, if periodIndx != periodIndx[1] then (countindx[1] + periodIndx - periodIndx[1]) % multiplier else countindx[1], 0);
def isPeriodRolled = countindx < countindx[1] + periodIndx - periodIndx[1];
#def isPeriodRolled = CompoundValue(1, periodIndx != periodIndx[1], yes);
input num_vwap_displayed = 2000;
def Count = num_vwap_displayed;
def cond = if isPeriodRolled
then 1
else Double.NaN;
def dataCount = CompoundValue(1, if !IsNaN(cond)
then dataCount[1] + 1
else dataCount[1], 0);
rec volumeSum;
rec volumeVwapSum;
rec volumeVwap2Sum;
if (isPeriodRolled) {
volumeSum = volume;
volumeVwapSum = volume * vwap;
volumeVwap2Sum = volume * Sqr(vwap);
} else {
volumeSum = CompoundValue(1, volumeSum[1] + volume, volume);
volumeVwapSum = CompoundValue(1, volumeVwapSum[1] + volume * vwap, volume * vwap);
volumeVwap2Sum = CompoundValue(1, volumeVwap2Sum[1] + volume * Sqr(vwap), volume * Sqr(vwap));
}
def price = volumeVwapSum / volumeSum;
def deviation = Sqrt(Max(volumeVwap2Sum / volumeSum - Sqr(price), 0));
rec v = if IsNaN(vwap) then v[1] else price;
rec v1 = if IsNaN(vwap) then v1[1] else price + num1stDevUp * deviation;
rec v2 = if IsNaN(vwap) then v2[1] else price - num1stDevUp * deviation;
rec v3 = if IsNaN(vwap) then v3[1] else price + num2ndDevUp * deviation;
rec v4 = if IsNaN(vwap) then v4[1] else price - num2ndDevUp * deviation;
rec v5 = if IsNaN(vwap) then v5[1] else price + num3rdDevUp * deviation;
rec v6 = if IsNaN(vwap) then v6[1] else price - num3rdDevUp * deviation;
plot VWAP;
plot FirstUpperBand;
plot FirstLowerBand;
plot SecondUpperBand;
plot SecondLowerBand;
plot ThirdUpperBand;
plot ThirdLowerBand;
input showlineinchartlength = 5;
input showlines = yes;
if (!errorInAggregation) and
lines == lines.limited {
VWAP = if IsNaN(close)
then Double.NaN
else if IsNaN(close[-showlineinchartlength]) and
HighestAll(dataCount) - dataCount <= Count - 1
then v
else Double.NaN ;
FirstUpperBand = if IsNaN(close[-showlineinchartlength]) and
HighestAll(dataCount) - dataCount <= Count - 1
then v1
else Double.NaN ;
FirstLowerBand = if IsNaN(close[-showlineinchartlength]) and
HighestAll(dataCount) - dataCount <= Count - 1
then v2
else Double.NaN ;
SecondUpperBand = if IsNaN(close[-showlineinchartlength]) and
HighestAll(dataCount) - dataCount <= Count - 1
then v3
else Double.NaN ;
SecondLowerBand = if IsNaN(close[-showlineinchartlength]) and
HighestAll(dataCount) - dataCount <= Count - 1
then v4
else Double.NaN ;
ThirdUpperBand = if IsNaN(close[-showlineinchartlength]) and
HighestAll(dataCount) - dataCount <= Count - 1
then v5
else Double.NaN ;
ThirdLowerBand = if IsNaN(close[-showlineinchartlength]) and
HighestAll(dataCount) - dataCount <= Count - 1
then v6
else Double.NaN ;
} else if (!errorInAggregation) and
lines == lines.horizontal and
HighestAll(dataCount) - dataCount <= Count - 1 {
VWAP = HighestAll(if IsNaN(close) then v else Double.NaN );
FirstUpperBand = HighestAll(if IsNaN(close) then v1 else Double.NaN );
FirstLowerBand = HighestAll(if IsNaN(close) then v2 else Double.NaN );
SecondUpperBand = HighestAll(if IsNaN(close) then v3 else Double.NaN );
SecondLowerBand = HighestAll(if IsNaN(close) then v4 else Double.NaN );
ThirdUpperBand = HighestAll(if IsNaN(close) then v5 else Double.NaN );
ThirdLowerBand = HighestAll(if IsNaN(close) then v6 else Double.NaN );
} else if (!errorInAggregation) and
lines == lines.lines and
HighestAll(dataCount) - dataCount <= Count - 1 {
VWAP = price;
FirstUpperBand = price + num1stDevUp * deviation;
FirstLowerBand = price - num1stDevUp * deviation;
SecondUpperBand = price + num2ndDevUp * deviation;
SecondLowerBand = price - num2ndDevUp * deviation;
ThirdUpperBand = price + num3rdDevUp * deviation;
ThirdLowerBand = price - num3rdDevUp * deviation;
} else {
VWAP = Double.NaN;
FirstUpperBand = Double.NaN;
FirstLowerBand = Double.NaN;
SecondUpperBand = Double.NaN;
SecondLowerBand = Double.NaN;
ThirdUpperBand = Double.NaN;
ThirdLowerBand = Double.NaN;
}
#VWAP.SetDefaultColor(Color.BLUE);
VWAP.AssignValueColor(if VWAP > VWAP[1]
then Color.GREEN
else Color.RED);
FirstUpperBand.SetDefaultColor(Color.BLUE);
FirstLowerBand.SetDefaultColor(Color.BLUE);
SecondUpperBand.SetDefaultColor(Color.BLUE);
SecondLowerBand.SetDefaultColor(Color.BLUE);
ThirdUpperBand.SetDefaultColor(Color.BLUE);
ThirdLowerBand.SetDefaultColor(Color.BLUE);
VWAP.SetPaintingStrategy(PaintingStrategy.LINE);
FirstUpperBand.SetStyle(Curve.SHORT_DASH);
FirstLowerBand.SetStyle(Curve.SHORT_DASH);
SecondUpperBand.SetStyle(Curve.SHORT_DASH);
SecondLowerBand.SetStyle(Curve.SHORT_DASH);
ThirdUpperBand.SetStyle(Curve.SHORT_DASH);
ThirdLowerBand.SetStyle(Curve.SHORT_DASH);
VWAP.SetLineWeight(2);
FirstUpperBand.SetLineWeight(1);
FirstLowerBand.SetLineWeight(1);
SecondUpperBand.SetLineWeight(1);
SecondLowerBand.SetLineWeight(1);
ThirdUpperBand.SetLineWeight(1);
ThirdLowerBand.SetLineWeight(1);
VWAP.HideBubble();
FirstUpperBand.HideBubble();
FirstLowerBand.HideBubble();
SecondUpperBand.HideBubble();
SecondLowerBand.HideBubble();
ThirdUpperBand.HideBubble();
ThirdLowerBand.HideBubble();
FirstUpperBand.SetHiding(!showlines);
FirstLowerBand.SetHiding(!showlines);
SecondUpperBand.SetHiding(!showlines);
SecondLowerBand.SetHiding(!showlines);
ThirdUpperBand.SetHiding(!showlines);
ThirdLowerBand.SetHiding(!showlines);
input showbubblesVSTD = yes;
input bubblemoverVSTD = 5;#Hint bubblemoverVSTD: Number of Spaces VSTD bubble offset in expansion
def p = bubblemoverVSTD;
def p1 = p + 1;
AddChartBubble(showbubblesVSTD and IsNaN(close[p]) and !IsNaN(close[p1]) ,
close[p1],
"VSTD" + "\n" + AsPercent((close[p1] - price[p1]) / deviation[p1] / 100) +
"\n" + Round(close[p1], 2) ,
if close[p1] > VWAP[p1]
then if close[p1] > close[p1 + 1]
then Color.GREEN
else Color.LIGHT_GREEN
else if close[p1] > close[p1 + 1]
then Color.LIGHT_RED
else Color.RED );
input showclouds = yes;
def green = if low >= VWAP and close > open
then 1
else if green[1] == 1 and high >= VWAP
then 1
else if green[1] == 1 and close > open
then 1
else 0;
def g = !isnan(green);
AddCloud(if showclouds and g
then ThirdUpperBand
else Double.NaN,
VWAP,
Color.LIGHT_GRAY, Color.LIGHT_GRAY);
AddCloud(if showclouds and !g
then VWAP
else Double.NaN,
ThirdLowerBand,
Color.PINK, Color.PINK);
plot x = if showclouds
then if g
then ThirdUpperBand
else ThirdLowerBand
else Double.NaN;
x.AssignValueColor(if g
then Color.GREEN
else Color.RED);
input showbubblescurrent = no;
input bubblemovercurrent = 5;#Hint bubblemoverVSTD: Number of Spaces Above/Below VWAP bubble offset in expansion
def q = bubblemovercurrent;
def q1 = q + 1;
AddChartBubble(showbubblescurrent and IsNaN(close[q]) and !IsNaN(close[q1]) ,
close[q1],
"V" ,
if g[q1]
then Color.GREEN
else Color.RED );
input usealerts = yes;
Alert(usealerts and g[1] != g, "VWAP - " + if g == 1 then "Green" else "Red", Alert.BAR, Sound.Chimes);
# end of VWAP Study
#Vertical Line @VWAP timeframe
input showverticalline = yes;
input begin = 0930;
input end = 1600;
def start = if (timeFrame == timeFrame.TWOHOUR or timeFrame == timeFrame.FOURHOUR)
then SecondsTillTime(0800) == 0 and
SecondsFromTime(0800) == 0
else if timeFrame == timeFrame.HOUR
then SecondsTillTime(0900) == 0 and
SecondsFromTime(0900) == 0
else SecondsTillTime(0930) == 0 and
SecondsFromTime(0930) == 0;
def bar = if start
then 0
else bar[1] + 1;
AddVerticalLine(if showverticalline and GetDay() == GetLastDay() and SecondsFromTime(begin) >= 0
then bar % ((Ceil(minutes) * multiplier) / (GetAggregationPeriod() / 60000)) == 0
else Double.NaN,
color = Color.BLUE, stroke = Curve.FIRM);