Hourly VWAP for ThinkOrSwim

SleepyZ

Moderator - Expert
VIP
Lifetime
This indicator is the result of this discussion:
https://usethinkscript.com/threads/hourly-vwap.6837/

Code:
#####################################################################
#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);
 
Last edited by a moderator:

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

Hello. Hope everything is going well for you.

I found your Vwap by the hour ... https://usethinkscript.com/threads/hourly-vwap.6837/ ... and I think is by far one of the most useful indicators on this forum. However, there is one thing that I hope you could take a look at > these two lines of your original code:

1. input num_vwap_displayed = 2000;
2. input showlineinchartlength = 5; (The max allowed is 2000. After 2000... say 2001... TOS does not plot the indicator)

Is there anyway to modify this part of the code so there are no limits? It would make backtesting with your indicator a lot easier.

As you can see in the image, the indicator cuts off after a while. Please help. Thank you.

I5jq6Yv.png


@SleepyZ

Original Code

Ruby:
#####################################################################
#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);
 
Hello. Hope everything is going well for you.

I found your Vwap by the hour ... https://usethinkscript.com/threads/hourly-vwap.6837/ ... and I think is by far one of the most useful indicators on this forum. However, there is one thing that I hope you could take a look at > these two lines of your original code:

1. input num_vwap_displayed = 2000;
2. input showlineinchartlength = 5; (The max allowed is 2000. After 2000... say 2001... TOS does not plot the indicator)

Is there anyway to modify this part of the code so there are no limits? It would make backtesting with your indicator a lot easier.

As you can see in the image, the indicator cuts off after a while. Please help. Thank you.

I5jq6Yv.png


@SleepyZ

Original Code

Ruby:
#####################################################################
#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);

Glad you found it useful!

If you change the input lines from limited to lines, you should see the plot extend across the entire chart. For example, it will display the vwap for the entire 5m 180 Day chart in the image below.

The 2000 limitation was a TOS limitation where horizontal lines were being displayed using the highestall() function.

 
Glad you found it useful!

If you change the input lines from limited to lines, you should see the plot extend across the entire chart. For example, it will display the vwap for the entire 5m 180 Day chart in the image below.

The 2000 limitation was a TOS limitation where horizontal lines were being displayed using the highestall() function.
Oh man, I am such an *****. I did not try that out.

Perhaps it would be a good idea to attach this to the original thread just in case other people run into a similar situation.

Thank you for taking the time to explain how to properly use your indicator.
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
467 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top