Anchored VWAP Indicator for ThinkorSwim

Looking at my current VWAP from TOS (slightly modified of course). :)

How can I change the "input timeframe" to just be "Day" only instead of {default DAY, WEEK, MONTH}? Each time I try to modify that it throws errors about "def cap" and "def error". I'd like to be able to "def" the timeframe instead of "inputting" it. So basically I want to carve out and remove "week" and "Month" from the script. If that makes sense. :)


Code:
def numDevDn = -2.0;
def numDevUp = 2.0;
input timeFrame = {default DAY, WEEK, MONTH};

def cap = getAggregationPeriod();
def errorInAggregation =
    timeFrame == timeFrame.DAY and cap >= AggregationPeriod.WEEK or
    timeFrame == timeFrame.WEEK and cap >= AggregationPeriod.MONTH;
assert(!errorInAggregation, "timeFrame should be not less than current chart aggregation period");

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 isPeriodRolled = compoundValue(1, periodIndx != periodIndx[1], yes);

def volumeSum;
def volumeVwapSum;
def 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));

plot VWAP = price;
VWAP.SetDefaultColor(CreateColor (220,68,220));
 

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

@Ricorichie

Code:
def numDevDn = -2.0;
def numDevUp = 2.0;

def cap = getAggregationPeriod();

def yyyyMmDd = getYyyyMmDd();
def periodIndx = yyyyMmDd;
def isPeriodRolled = compoundValue(1, periodIndx != periodIndx[1], yes);

def volumeSum;
def volumeVwapSum;
def 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));

plot VWAP = price;
VWAP.SetDefaultColor(CreateColor (220,68,220));
 
Wow that's awesome!!! It works beautifully!
Thanks to SleepyZ!!!

I made sure to add your username at the top of the script so years from now I can remember who scripted it. :)

Thanks again!
 
See if this helps. Each day's bars are denoted the same value starting with 0. So if you wanted the VWAP to be anchored for the last 5 days, you would input length = 5 and use thisday < length as the constraint for daily and less than daily timeframe charts.
Code:
input length = 5;
def ymd      = GetYYYYMMDD();
def dayCount = CompoundValue(1, if !IsNaN(close) and ymd != ymd[1] then dayCount[1] + 1 else dayCount[1], 0);
def thisDay  = (HighestAll(dayCount) - dayCount);

plot trailingVWAP = TotalSum(if thisDay < length then ((high + low + close) / 3) * (volume) else 0) / TotalSum(if thisDay < length then volume else 0);
i figured it out; thanks SleepyZ. If anyone else is interested in a trailingVWAP script its below where length = number of trailing days and ext = numbers of future days in your chart's extension area
input length = 0;
input ext = 2;

def dayCount = CountTradingDays(First(GetYYYYMMDD()), GetYYYYMMDD());

plot trailingVWAP = TotalSum(if dayCount + length > highestAll(dayCount) - ext then ((high+low+close)/3)*(volume) else 0)/TotalSum(if dayCount + length > highestAll(dayCount) - ext then volume else 0);
 
Last edited:
@Ricorichie

Code:
def numDevDn = -2.0;
def numDevUp = 2.0;

def cap = getAggregationPeriod();

def yyyyMmDd = getYyyyMmDd();
def periodIndx = yyyyMmDd;
def isPeriodRolled = compoundValue(1, periodIndx != periodIndx[1], yes);

def volumeSum;
def volumeVwapSum;
def 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));

plot VWAP = price;
VWAP.SetDefaultColor(CreateColor (220,68,220));
Hey SleepyZ! Nice work as always! I was curious if it would be possible to alter the inputs so that VWAP can be anchored to the pivot high and/or pivot low over a given period (day, week, month, etc)?
 
Hey SleepyZ! Nice work as always! I was curious if it would be possible to alter the inputs so that VWAP can be anchored to the pivot high and/or pivot low over a given period (day, week, month, etc)?

Here is something I have not thoroughly tested, but might be helpful to your request. It uses Mobius' Fractal Pivots, modified to be MTF, with a VWAP based upon the MTF period chosen.
Code:
# FractalPivots_MTF_VWAP Using:
# Support and Resistance Fractal Pivots
# Mobius
# V01.01.2011

# Sleepyz - Usethinkscript request
#   Modified to add an aggregation input and an agg based VWAP anchored at the last agg based pivot

 
# User Inputs
input n = 2;             #hint n: Length for calculations.

# Internal Script Reference
script LinePlot {
    input LineLimit = 0;
    input OnExpansion = yes;
    input data = close;
    input bar = 0;
    def ThisBar = HighestAll(bar);
    def cLine = if bar == ThisBar
                then data
                else Double.NaN;
    plot P = if ThisBar - LineLimit <= bar
             then HighestAll(cLine)
             else Double.NaN;
}
# Variables
input agg = AggregationPeriod.DAY;
def o = open(period = agg);
def h = high(period = agg);
def l = low(period = agg);
def c = close(period = agg);

def bar = BarNumber();
def hh = fold i = 1 to n + 1
         with p = 1
         while p
         do h > GetValue(h, -i);
def PH = if (bar > n and
             h == Highest(h, n) and
                hh)
         then h
         else Double.NaN;
def ll = fold j = 1 to n + 1
         with q = 1
         while q
         do l < GetValue(l, -j);
def PL = if (bar > n and
             l == Lowest(l, n) and
             ll)
         then l
            else Double.NaN;
def PHBar = if !IsNaN(PH)
               then bar
               else PHBar[1];
def PLBar = if !IsNaN(PL)
               then bar
               else PLBar[1];
def PHL = if !IsNaN(PH)
             then PH
             else PHL[1];
def priorPHBar = if PHL != PHL[1]
                    then PHBar[1]
                    else priorPHBar[1];
def PLL = if !IsNaN(PL)
             then PL
             else PLL[1];
def priorPLBar = if PLL != PLL[1]
                    then PLBar[1]
                    else priorPLBar[1];
def HighPivots = bar >= HighestAll(priorPHBar);
def LowPivots = bar >= HighestAll(priorPLBar);
def R1pivot = if PHL > 0 and
            HighPivots
         then PHL
         else Double.NaN;
def S1pivot = if LowPivots
                  then PL
                  else Double.NaN;
def R1value = R1pivot;
def R1bar = PHBar;
def R1limit = bar - R1bar;
def R2bar = priorPHBar;
def R2limit = bar - R2bar;
def R2value = if BarNumber() == HighestAll(R2bar)
              then h
              else R2value[1];
def R3bar = if priorPHBar != priorPHBar[1]
            then priorPHBar[1]
            else R3bar[1];
def R3limit = bar - R3bar;
def R3value = if BarNumber() == HighestAll(R3bar)
              then h
              else R3value[1];
def S1value = S1pivot;
def S1bar = PLBar;
def S1limit = bar - S1bar;
def S2bAR = priorPLBar;
def S2limit = bar - S2bAR;
def S2value = if bar == HighestAll(S2bAR)
              then l
              else S2value[1];
def S3bar = if priorPLBar != priorPLBar[1]
            then priorPLBar[1]
            else S3bar[1];
def S3limit = bar - S3bar;
def S3value = if BarNumber() == HighestAll(S3bar)
              then l
              else S3value[1];
# Plots
input x = 1;
def  x1 = x + 1;
input showbubbles = yes;
plot R1 = LinePlot(Linelimit = R1limit, data = R1value, bar = R1bar);
R1.SetDefaultColor(Color.GREEN);
AddChartBubble(showbubbles and IsNaN(close[x]) and !IsNaN(close[x1]), R1, "R1", Color.GREEN);
plot R2 = LinePlot(LineLimit = R2limit, data = R2value, bar = R2bar).P;
R2.SetDefaultColor(Color.YELLOW);
AddChartBubble(showbubbles and IsNaN(close[x]) and !IsNaN(close[x1]), R2value, "R2", Color.YELLOW);
plot R3 = LinePlot(LineLimit = R3limit, data = R3value, bar = R3bar).P;
R3.SetDefaultColor(Color.BLUE);
AddChartBubble(showbubbles and IsNaN(close[x]) and !IsNaN(close[x1]), R3value, "R3", Color.BLUE);
plot S1 = LinePlot(LineLimit = S1limit, data = S1value, bar = S1bar).P;
S1.SetDefaultColor(Color.RED);
AddChartBubble(showbubbles and IsNaN(close[x]) and !IsNaN(close[x1]), S1, "S1", Color.RED);
plot S2 = LinePlot(LineLimit = S2limit, data = S2value, bar = S2bAR).P;
S2.SetDefaultColor(Color.PLUM);
AddChartBubble(showbubbles and IsNaN(close[x]) and !IsNaN(close[x1]), S2, "S2", Color.PLUM);
plot S3 = LinePlot(LineLimit = S3limit, data = S3value, bar = S3bar).P;
S3.SetDefaultColor(Color.MAGENTA);
AddChartBubble(showbubbles and IsNaN(close[x]) and !IsNaN(close[x1]), S3, "S3", Color.MAGENTA);


# End Code Support and Resistance Fractal Pivots


#VWAP Anchored from Date
def v   = volume(period = agg);
def wap = vwap(period = agg);
def xx  = Max(R1bar, Max(R2bar, Max(R3bar, Max(S1bar, Max(S2bAR, S3bar)))));
def startdate   = if BarNumber() == xx then low else startdate[1];
input numDevUp  = 2.0;
def   anchor    = BarNumber() >= HighestAll(xx) and startdate;
def volumeSum   = if anchor then CompoundValue(1, volumeSum[1] + v, v) else 0;
def volumeVwapSum  = if anchor then CompoundValue(1, volumeVwapSum[1] + v * wap, v * wap) else 0;
def volumeVwap2Sum = if anchor then CompoundValue(1, volumeVwap2Sum[1] + v * Sqr(wap), v * Sqr(wap)) else 0;
def price     = volumeVwapSum / volumeSum;
def deviation = Sqrt(Max(volumeVwap2Sum / volumeSum - Sqr(price), 0));

plot VWAP       = price;
input showbands = yes;
plot UpperBand  = if !showbands then Double.NaN else price + numDevUp * deviation;
plot LowerBand  = if !showbands then Double.NaN else price - numDevUp * deviation;

VWAP.SetDefaultColor(GetColor(0));
UpperBand.SetDefaultColor(GetColor(2));
LowerBand.SetDefaultColor(GetColor(4));

## END STUDY
Capture.jpg
 
Here is something I have not thoroughly tested, but might be helpful to your request. It uses Mobius' Fractal Pivots, modified to be MTF, with a VWAP based upon the MTF period chosen.
Code:
# FractalPivots_MTF_VWAP Using:
# Support and Resistance Fractal Pivots
# Mobius
# V01.01.2011

# Sleepyz - Usethinkscript request
#   Modified to add an aggregation input and an agg based VWAP anchored at the last agg based pivot


# User Inputs
input n = 2;             #hint n: Length for calculations.

# Internal Script Reference
script LinePlot {
    input LineLimit = 0;
    input OnExpansion = yes;
    input data = close;
    input bar = 0;
    def ThisBar = HighestAll(bar);
    def cLine = if bar == ThisBar
                then data
                else Double.NaN;
    plot P = if ThisBar - LineLimit <= bar
             then HighestAll(cLine)
             else Double.NaN;
}
# Variables
input agg = AggregationPeriod.DAY;
def o = open(period = agg);
def h = high(period = agg);
def l = low(period = agg);
def c = close(period = agg);

def bar = BarNumber();
def hh = fold i = 1 to n + 1
         with p = 1
         while p
         do h > GetValue(h, -i);
def PH = if (bar > n and
             h == Highest(h, n) and
                hh)
         then h
         else Double.NaN;
def ll = fold j = 1 to n + 1
         with q = 1
         while q
         do l < GetValue(l, -j);
def PL = if (bar > n and
             l == Lowest(l, n) and
             ll)
         then l
            else Double.NaN;
def PHBar = if !IsNaN(PH)
               then bar
               else PHBar[1];
def PLBar = if !IsNaN(PL)
               then bar
               else PLBar[1];
def PHL = if !IsNaN(PH)
             then PH
             else PHL[1];
def priorPHBar = if PHL != PHL[1]
                    then PHBar[1]
                    else priorPHBar[1];
def PLL = if !IsNaN(PL)
             then PL
             else PLL[1];
def priorPLBar = if PLL != PLL[1]
                    then PLBar[1]
                    else priorPLBar[1];
def HighPivots = bar >= HighestAll(priorPHBar);
def LowPivots = bar >= HighestAll(priorPLBar);
def R1pivot = if PHL > 0 and
            HighPivots
         then PHL
         else Double.NaN;
def S1pivot = if LowPivots
                  then PL
                  else Double.NaN;
def R1value = R1pivot;
def R1bar = PHBar;
def R1limit = bar - R1bar;
def R2bar = priorPHBar;
def R2limit = bar - R2bar;
def R2value = if BarNumber() == HighestAll(R2bar)
              then h
              else R2value[1];
def R3bar = if priorPHBar != priorPHBar[1]
            then priorPHBar[1]
            else R3bar[1];
def R3limit = bar - R3bar;
def R3value = if BarNumber() == HighestAll(R3bar)
              then h
              else R3value[1];
def S1value = S1pivot;
def S1bar = PLBar;
def S1limit = bar - S1bar;
def S2bAR = priorPLBar;
def S2limit = bar - S2bAR;
def S2value = if bar == HighestAll(S2bAR)
              then l
              else S2value[1];
def S3bar = if priorPLBar != priorPLBar[1]
            then priorPLBar[1]
            else S3bar[1];
def S3limit = bar - S3bar;
def S3value = if BarNumber() == HighestAll(S3bar)
              then l
              else S3value[1];
# Plots
input x = 1;
def  x1 = x + 1;
input showbubbles = yes;
plot R1 = LinePlot(Linelimit = R1limit, data = R1value, bar = R1bar);
R1.SetDefaultColor(Color.GREEN);
AddChartBubble(showbubbles and IsNaN(close[x]) and !IsNaN(close[x1]), R1, "R1", Color.GREEN);
plot R2 = LinePlot(LineLimit = R2limit, data = R2value, bar = R2bar).P;
R2.SetDefaultColor(Color.YELLOW);
AddChartBubble(showbubbles and IsNaN(close[x]) and !IsNaN(close[x1]), R2value, "R2", Color.YELLOW);
plot R3 = LinePlot(LineLimit = R3limit, data = R3value, bar = R3bar).P;
R3.SetDefaultColor(Color.BLUE);
AddChartBubble(showbubbles and IsNaN(close[x]) and !IsNaN(close[x1]), R3value, "R3", Color.BLUE);
plot S1 = LinePlot(LineLimit = S1limit, data = S1value, bar = S1bar).P;
S1.SetDefaultColor(Color.RED);
AddChartBubble(showbubbles and IsNaN(close[x]) and !IsNaN(close[x1]), S1, "S1", Color.RED);
plot S2 = LinePlot(LineLimit = S2limit, data = S2value, bar = S2bAR).P;
S2.SetDefaultColor(Color.PLUM);
AddChartBubble(showbubbles and IsNaN(close[x]) and !IsNaN(close[x1]), S2, "S2", Color.PLUM);
plot S3 = LinePlot(LineLimit = S3limit, data = S3value, bar = S3bar).P;
S3.SetDefaultColor(Color.MAGENTA);
AddChartBubble(showbubbles and IsNaN(close[x]) and !IsNaN(close[x1]), S3, "S3", Color.MAGENTA);


# End Code Support and Resistance Fractal Pivots


#VWAP Anchored from Date
def v   = volume(period = agg);
def wap = vwap(period = agg);
def xx  = Max(R1bar, Max(R2bar, Max(R3bar, Max(S1bar, Max(S2bAR, S3bar)))));
def startdate   = if BarNumber() == xx then low else startdate[1];
input numDevUp  = 2.0;
def   anchor    = BarNumber() >= HighestAll(xx) and startdate;
def volumeSum   = if anchor then CompoundValue(1, volumeSum[1] + v, v) else 0;
def volumeVwapSum  = if anchor then CompoundValue(1, volumeVwapSum[1] + v * wap, v * wap) else 0;
def volumeVwap2Sum = if anchor then CompoundValue(1, volumeVwap2Sum[1] + v * Sqr(wap), v * Sqr(wap)) else 0;
def price     = volumeVwapSum / volumeSum;
def deviation = Sqrt(Max(volumeVwap2Sum / volumeSum - Sqr(price), 0));

plot VWAP       = price;
input showbands = yes;
plot UpperBand  = if !showbands then Double.NaN else price + numDevUp * deviation;
plot LowerBand  = if !showbands then Double.NaN else price - numDevUp * deviation;

VWAP.SetDefaultColor(GetColor(0));
UpperBand.SetDefaultColor(GetColor(2));
LowerBand.SetDefaultColor(GetColor(4));

## END STUDY
Capture.jpg
Nice! I’ll definitely check this out. Thank you for sharing.
 
new here. I want to create an anchoredVWAP with standard deviation bands. I've searched all over but can't find a clue. on the one hand... VWAP itself has a [timeFrame] variable that allows for defined intervals, but nothing that would allow for defining a starting yyyymmddhhmmss and then having no end time. I've also found the anchoredVWAP code, but it sidesteps the issue by manually running the VWAP calculation at all points of the chart where the begin date and begin time are true. I'm guessing that it works because TotalSum is constantly updated with an increasing [volume] fundamental at all times. I think the reason there's no deviation bands in this custom code is because the bands would require a length for the calculation and the custom code sidesteps this.

so i'm guessing the trick is to try and define a "CUSTOM" time frame in the original VWAP code? maybe there's someway to define a period that constantly increments over time in the original VWAP code?

alternatively, I tried to create a standard deviation on the custom anchoredVWAP code and the result was interesting, but not at all what I want. Here's a look at that if you're curious

Code:
input anchorDate = 20210322;
input anchorTime = 0930;

def postAnchorDate = if GetYYYYMMDD() >= anchorDate then 1 else 0;
def postAnchorTime = if SecondsFromTime(anchorTime) >= 0 then 1 else 0;

plot anchoredVWAP = TotalSum(if postAnchorDate and postAnchorTime then ((high+low+close)/3)*(volume) else 0)/TotalSum(if postAnchorDate and postAnchorTime then volume else 0);

#adapted the stdevall code example from tlc with some of the variables in the custom anchoredVWAP code
def regression = InertiaAll(data = anchoredVWAP, startDate = anchorDate, startTime = anchorTime, extendToRight = Yes);
def stdDeviation = StDevAll(data = anchoredVWAP, startDate = anchorDate, startTime = anchorTime, extendToRight = Yes);

plot UpperLine = regression + stdDeviation;
plot LowerLine = regression - stdDeviation;

anchoredVWAP.setStyle(Curve.Firm);
anchoredVWAP.setLineWeight(2);
anchoredVWAP.SetDefaultColor(Color.Cyan);
 
Hi @BenTen ,

I changed "n" to 1 so it gives signals after one bar. It seems to still work well but you have to manually refresh it to get the arrows, it does not signal in real-time. Any idea how to make it automatic?
 
vwap is not only used intraday, vwap is used all the time, i use the 5 day, monthly and 6 month vwap.

100% agree!
I´ve been doing a lot of research and backtesting lately with VWAP looking at historical levels and it´s a bit fascinating how well they work as support/resistance for intraday trading. A trader on twitter introduced me to what he refers to as "VWAP boulevard" and it´s basically just a way at looking at past high volume days (usually gap ups) and then use the VWAP from that day as a way of identifying where overhead supply is at. So the next time stock ABC gaps up, we can use the historical VWAP level from daily as a resistance level on our intraday chart.
So far, i´ve seen one example of a TOS study that auto plots these historical VWAP levels on lower timeframe charts and i think it would be super useful to have a study like this that automatically identifies the VWAP price from the last high volume day (from daily chart) and then auto plots this on a let´s say a 5min chart. An additional label with the price would also be of use.

If you or anyone else could help code this into a study, would be much appreciated.

Here are some recent examples of the VWAP boulevard and how they acted as resistance on the following gap up day


 
Hi Ben,

RE: Anchored VWAP

Anchored VWAP signals are delayed. I am using a 1 minute chart and cant figure out what is the delay Can you tell me please ?

Also can you please tell what is the best time frame for this ?

Thanks
Abdulla Mangalore
 
@amangaloreab I believe your question was answered in post#8.
Before using an indicator, it is best to read the entire thread to learn about the pros and weaknesses of an indicator.
HTH
 
Last edited:
Code:
# VWAP Standard Deviation Bands
# lar
# 12.12.2015

# V1.0 - 12.12.2015 - lar     - Initial release of VWAP Standard Deviation bands
# V1.1 - 12.17.2019 - tomsk   - Minor edits

input timeFrame = {Day, Week, Month, default Year, Chart, "March"};
input BandType = {default Standard, "1/4 Day Range", "3x Avg Bar Range", ThinkScripter, None};
input ShowCloud = yes;
input HideSdLines = no;

def cap = GetAggregationPeriod();
def errorInAggregation =
    timeFrame == timeFrame.Day and cap >= AggregationPeriod.WEEK or
    timeFrame == timeFrame.Week and cap >= AggregationPeriod.MONTH;
Assert(!errorInAggregation, "timeFrame should be not less than current chart aggregation period");

def yyyyMmDd = GetYYYYMMDD();
def seconds = SecondsFromTime(0);
def month = GetYear() * 12 + GetMonth();
def year = GetYear();
def day_number = DaysFromDate(First(yyyyMmDd)) + GetDayOfWeek(First(yyyyMmDd));
def dom = GetDayOfMonth(yyyyMmDd);
def dow = GetDayOfWeek(yyyyMmDd - dom + 1);
def expthismonth = (if dow > 5 then 27 else 20) - dow;
def exp_opt = month + (dom > expthismonth);
def periodIndx;

switch (timeFrame) {
case Chart:
    periodIndx = 0;

case Day:
    periodIndx = CountTradingDays(Min(First(yyyyMmDd), yyyyMmDd), yyyyMmDd) - 1;

case Week:
    periodIndx = Floor(day_number / 7);

case Month:
    periodIndx = Floor(month - First(month));

case Year:
    periodIndx = Floor(year - First(year));

case "March":
    periodIndx = Floor(2020 - First(2020));
}
def isPeriodRolled = CompoundValue(1, periodIndx != periodIndx[1], yes);

def volumeSum;
def volumeVwapSum;
def 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;
switch (BandType) {

case Standard:
    deviation = Sqrt(Max(volumeVwap2Sum / volumeSum - Sqr(price), 0));

case "1/4 Day Range":
    deviation = Sqrt(AbsValue(high(Period = timeFrame) - low(Period = timeFrame)) * .25);

case "3x Avg Bar Range":
    deviation = Sqrt(Average(TrueRange(high,  close,  low),  20) * 3);

case ThinkScripter:
    deviation = Sqrt(TotalSum(Sqr(((open + high + low + close) / 4) - price) * volume) / TotalSum(volume));

case None:
    deviation = Double.NaN;
}

plot VWAP = price;
VWAP.AssignValueColor(if VWAP > VWAP[1] then Color.Cyan
                      else if VWAP < VWAP[1] then Color.Red
                      else Color.Yellow);
VWAP.SetStyle(Curve.SHORT_DASH);
VWAP.SetLineWeight(2);

# TS_CHART_VWAP_SD_BANDS
# http://www.thinkscripter.com
# [email protected]
# Last Update 03 APR 2010

input VWAPStdev1 = 1.0;
input VWAPStdev2 = 2.0;
input VWAPStdev3 = 3.0;

plot r1 = VWAP + VWAPStdev1 * deviation;
plot s1 = VWAP - VWAPStdev1 * deviation;
plot r2 = VWAP + VWAPStdev2 * deviation;
plot s2 = VWAP - VWAPStdev2 * deviation;
plot r3 = VWAP + VWAPStdev3 * deviation;
plot s3 = VWAP - VWAPStdev3 * deviation;

DefineGlobalColor("sDev1", (CreateColor(40, 40, 40)));
DefineGlobalColor("sDev2", (CreateColor(128, 128, 128)));
DefineGlobalColor("sDev3", (CreateColor(100, 100, 100)));

r1.SetDefaultColor(GlobalColor("sDev1"));
s1.SetDefaultColor(GlobalColor("sDev1"));
r2.SetDefaultColor(GlobalColor("sDev2"));
s2.SetDefaultColor(GlobalColor("sDev2"));
r3.SetDefaultColor(GlobalColor("sDev2"));
s3.SetDefaultColor(GlobalColor("sDev2"));

r1.SetStyle(Curve.SHORT_DASH);
r2.SetStyle(Curve.SHORT_DASH);
r3.SetStyle(Curve.SHORT_DASH);
s1.SetStyle(Curve.SHORT_DASH);
s2.SetStyle(Curve.SHORT_DASH);
s3.SetStyle(Curve.SHORT_DASH);

VWAP.HideBubble();
r1.HideBubble();
r2.HideBubble();
r3.HideBubble();
s1.HideBubble();
s2.HideBubble();
s3.HideBubble();

r1.SetHiding(HideSdLines);
r2.SetHiding(HideSdLines);
r3.SetHiding(HideSdLines);
s1.SetHiding(HideSdLines);
s2.SetHiding(HideSdLines);
s3.SetHiding(HideSdLines);

# CLOUD ##########################
def CloudR1 = if ShowCloud then r1 else Double.NaN;
def CloudR2 = if ShowCloud then r2 else Double.NaN;
def CloudR3 = if ShowCloud then r3 else Double.NaN;
def CloudS1 = if ShowCloud then s1 else Double.NaN;
def CloudS2 = if ShowCloud then s2 else Double.NaN;
def CloudS3 = if ShowCloud then s3 else Double.NaN;
AddCloud(CloudR1, CloudR2, GlobalColor("sDev1"), GlobalColor("sDev1"));
AddCloud(CloudS1, CloudS2, GlobalColor("sDev1"), GlobalColor("sDev1"));
AddCloud(CloudR2, CloudR3, GlobalColor("sDev2"), GlobalColor("sDev2"));
AddCloud(CloudS2, CloudS3, GlobalColor("sDev2"), GlobalColor("sDev2"));
# End VWAP Standard Deviation Bands
My title may not make a lot of sense, but let me explain it.

As you know, with the anchored VWAP, you can anchor it at any date you want. I've been trying to translate this specific date code over to a VWAP w/ Standard Deviation Bands indicator I found on this website, but to no avail. The VWAP w/ SD Bands indicator only allows me to choose between: year, day, week and quarter. I don't want any of those, I want to pick the date where the bands and VWAP start, similar to an anchored VWAP. For example, I want the date to start at the bottom of the COVID March crash, March 23, 2020. Any ideas?

EDIT
https://www.tradingview.com/script/h8mk3PRk-Anchored-VWAP-w-STD-bands/

Here's an indicator in tradingview, with the idea I'm talking about, where it lets you anchor it at whatever date you want.
 
Last edited:
Repainted? Arrows are not painting timely.. if I open the chart again.. it shows the arrows again.. http://tos.mx/jbD4hcW
I modified and merged Mobius Fractal Pivots to just R1/S1 with Linus VWAP code, which does not plot properly without refreshing the chart. Now this should plot timely, but since it uses future bars, it will plot the pivot arrows/lenes only after the future bar requirement is met. As this worked to overcome the problem in Linus' code, I did not spend anytime to try to figure out how to fix Linus' code

[Edit] - Corrected R1/S1 lines to appear on their most recent pivots. As Linus code was a zigzag type pattern for drawing the VWAP, I had excluded the pivots in the same direction with the PointCount code. These are now included for just drawing the R1/S1 lines and may be useful.

Code:
# Mobius Fractal Pivots and Linus Anchored VWAP Combo
# 20180223 BLT modified Linus Anchored VWAP to work with Mobius Fractal Pivots

# 20210427 Sleepyz modified Mobius Fractal Pivots to just R1/S1 to more mimic Linus VWAP code, which does not plot properly without refreshing the chart. Now it should plot timely, but since it uses future bars, it will plot the pivot only after the future bar requirement is met. As this worked to overcome the problem in Linus' code, I did not spend anytime to try to figure out how to fix Linus' code
# ___________________________________________
# Support and Resistance Fractal Pivots
# Mobius
# V01.01.2011

# User Inputs
input n = 5;             #hint n: Length for calculations.

# Internal Script Reference
script LinePlot {
    input LineLimit = 0;
    input OnExpansion = yes;
    input data = close;
    input bar = 0;
    def ThisBar = HighestAll(bar);
    def cLine = if bar == ThisBar
                then data
                else Double.NaN;
    plot P = if ThisBar - LineLimit <= bar
             then HighestAll(cLine)
             else Double.NaN;
}

# Variables
def o = open;
def h = high;
def l = low;
def c = close;
def bar = BarNumber();
def hh = fold i = 1 to n + 1
         with p = 1
         while p
         do h > GetValue(h, -i);
def PH = if (bar > n and
             h == Highest(h, n) and
                hh)
         then h
         else Double.NaN;
def ll = fold j = 1 to n + 1
         with q = 1
         while q
         do l < GetValue(l, -j);
def PL = if (bar > n and
             l == Lowest(l, n) and
             ll)
         then l
            else Double.NaN;
def PHBar = if !IsNaN(PH)
               then bar
               else PHBar[1];
def PLBar = if !IsNaN(PL)
               then bar
               else PLBar[1];
def PointCount = if BarNumber() == 1 then 0 else
                 if IsNaN(c) then PointCount[1] else
                 if !IsNaN(PH) then Max(1, PointCount[1] + 1) else
                 if !IsNaN(PL) then Min(-1, PointCount[1] - 1)
                 else PointCount[1];
def PHL = if !IsNaN(PH)
             then PH
             else PHL[1];
def priorPHBar = if PHL != PHL[1]
                    then PHBar[1]
                    else priorPHBar[1];
def PLL = if !IsNaN(PL)
             then PL
             else PLL[1];
def priorPLBar = if PLL != PLL[1]
                    then PLBar[1]
                    else priorPLBar[1];
def HighPivots = bar >= HighestAll(priorPHBar);
def LowPivots = bar >= HighestAll(priorPLBar);
def R1pivot = if PHL > 0 and PointCount == 1
                 then PH
                 else Double.NaN;
def S1pivot = if PLL > 0 and PointCount == -1
                 then PL
                 else Double.NaN;
def R1value = R1pivot;
def S1value = S1pivot;

# Plots
input x = 1;
def  x1 = x + 1;
input showbubbles = yes;
input showlines = yes;
plot R1 = if !showlines then Double.NaN else LinePlot(Linelimit = 0, data = phl, bar = PHBar);
R1.SetDefaultColor(Color.ORANGE);
R1.SetLineWeight(3);
AddChartBubble(showbubbles and IsNaN(c[x]) and !IsNaN(c[x1]), R1, "R1", Color.RED);

plot S1 = if !showlines then Double.NaN else LinePlot(LineLimit = 0, data = pll, bar = PLBar);
S1.SetDefaultColor(Color.WHITE);
S1.SetLineWeight(3);
AddChartBubble(showbubbles and IsNaN(c[x]) and !IsNaN(c[x1]), S1, "S1", Color.GREEN);

input showarrows = yes;
plot R1arrow = if showarrows and R1value then high else double.nan;
R1arrow.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
R1arrow.SetLineWeight(3);
R1arrow.SetDefaultColor(Color.ORANGE);
plot S1arrow = if showarrows and S1value then low else double.nan;
S1arrow.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
S1arrow.SetLineWeight(3);
S1arrow.SetDefaultColor(Color.WHITE);
# End Code Support and Resistance Fractal Pivots

# ______________________________
# Linus Anchored VWAP code
def up = !IsNaN(S1value);#modified
def dn = !IsNaN(R1value);#modified
input ticks = 0.0;
def LocH = (high + (TickSize() * ticks)) * volume;
def LocL = (low - (TickSize() * ticks)) * volume;
def LocC = close * volume;

rec PC;
rec VC;
rec PC2;
rec VC2;
rec PHV;#modified
rec VH;
rec PLV;#modified
rec VL;
rec PH2;
rec VH2;
rec PL2;
rec VL2;

if dn or up {
    PC = LocC;
    VC = volume;
    PC2 = PC[1];
    VC2 = VC[1];
} else {
    PC = CompoundValue(1, LocC + PC[1], Double.NaN);
    VC = CompoundValue(1, volume + VC[1], Double.NaN);
    PC2 = CompoundValue(1, LocC + PC2[1], Double.NaN);
    VC2 = CompoundValue(1, volume + VC2[1], Double.NaN);
}

if dn {
    PHV = LocH;
    VH = volume;
    PH2 = PHV[1];
    VH2 = VH[1];
} else {
    PHV = CompoundValue(1, LocH + PHV[1], Double.NaN);
    VH = CompoundValue(1, volume + VH[1], Double.NaN);
    PH2 = CompoundValue(1, LocH + PH2[1], Double.NaN);
    VH2 = CompoundValue(1, volume + VH2[1], Double.NaN);
}
if up  {
    PLV = LocL;
    VL = volume;
    PL2 = PLV[1];
    VL2 = VL[1];
} else {
    PLV = CompoundValue(1, LocL + PLV[1], Double.NaN);
    VL = CompoundValue(1, volume + VL[1], Double.NaN);
    PL2 = CompoundValue(1, LocL + PL2[1], Double.NaN);
    VL2 = CompoundValue(1, volume + VL2[1], Double.NaN);
}

plot VwapC  = if dn or up then Double.NaN else PC / VC;
plot VwapC2 = if dn or up then Double.NaN else PC2 / VC2;
plot VwapH  = if dn then Double.NaN else PHV / VH;
plot VwapL  = if up then Double.NaN else PLV / VL;
plot VwapH2 = if dn then Double.NaN else PH2 / VH2;
plot VwapL2 = if up then Double.NaN else PL2 / VL2;

VwapC.SetDefaultColor(Color.YELLOW);
VwapC.SetLineWeight(2);
VwapC.HideBubble();

VwapC2.SetDefaultColor(Color.YELLOW);
VwapC2.SetLineWeight(2);
VwapC2.SetStyle(Curve.SHORT_DASH);
VwapC2.HideBubble();

VwapH.SetDefaultColor(Color.DARK_RED);
VwapH.HideBubble();

VwapL.SetDefaultColor(Color.GREEN);
VwapL.HideBubble();

VwapH2.SetDefaultColor(Color.DARK_RED);
VwapH2.SetStyle(Curve.SHORT_DASH);
VwapH2.HideBubble();

VwapL2.SetDefaultColor(Color.GREEN);
VwapL2.SetStyle(Curve.SHORT_DASH);
VwapL2.HideBubble();

## END modified Linus Anchored VWAP STUDY
 
Last edited:
Anchored VWAP looks damn good if I look at old arrows.....we can make lot of money if there is no delay.....unfortunately very long delay on every time frame :( ..not sure how everyone else using this...

notice this if someone never noticed on just tos VWAP.... previous day's start and end VWAP acts as great support/resis for the current day...if you are a day trader, just draw the lines from the previous day just before market starts for the current day.
if a script can draw those two lines...thats even better.
 
Hi there, I am looking for a floating (short line) anchored VWAP for 1D 2D 3D and weekly, similar to the picture showing below. The idea is to use 1D 2D and 3D to stay in the trade and use weekly as a possible pin level for Friday. I believe Alpha Trend may have something along the line. I would appreciate it a great deal if someone can help to assemble them. the FLOATING red dotted line, green line, and blue line are 1D 2D 3D Anchored VWAP
 
Last edited:

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
200 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