Anchored VWAP Indicator for ThinkorSwim

I want to add something to this thread not code but a logical thread expressed in simple English. I have enough AI code to last me a lifetime of trading so I will communicate as follows Purpose of VWAP is to be on the right side of volume weighted average price no matter if long or short (reasons: keeps you on the side of the accumulation or distribution-whatever is most dominant price discovery in the time in which you are setting the VWAP). Second, you have a VWAP on TOS and on this Thread repeated in one form or another many times , that you can split into standard deviations of any length and width and duration (# of deviations down and up for a day, a week or even a month). ---Properly set up to desired specifications it should allow the individual chartist to accomplish the purpose desired by an active trader discretionary like myself - not, let me repeat, Not!, to build a black box but to build an indivualized guidance indicator to enable me to remain on the dominant side of the trade. It is already on the thread, so I will not clutter the thread with more Code: My personal philosophy has always been consistent, I am looking for an interactive real-time experience. not an abstrated MetaVerse of a cluttered Chart full of electronic signals that are jumble of noise/symbols. I suggest if the chartist keeps the monitor clean of clutter and concentrates one one best indicator on the Price Channel with one indicator for Price and and another best one indiicator for Volume, he will see price discovery unfold before his eyes: Price and volume order flow in real time. That is a Signal Price and Volume over time, not a predictor. There is no perfect predictor in my experience. You can try all kinds of Linear or non-linear signals but I tell you they are guesses not Price Discovery. Best to all. Excuse my caveman use of words to communicate in a computerized world. Best to all
Completely agree with you.....................most of the times, I keep my chart very clean after experiencing with lot many indicators having had on my chart...first i completely depend a lot on price action , support/resis levels to get a clean picture before adding/removing 2-3 other indicators for confirmation.
\I just follow these....as needed..RSI, ORB, ichimoku, MA, VWAP
 
Last edited:
sure, code below:


def anchorDate = highestall(GetValue(GetYYYYMMDD(), GetMinValueOffset(low, 145)));

def todayDate = GetYYYYMMDD();

def postAnchorDate = if todayDate >= anchorDate then 1 else 0;

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

anchoredVWAP.SetStyle(Curve.FIRM);
anchoredVWAP.SetLineWeight(3);
anchoredVWAP.SetDefaultColor(Color.CYAN);
Hello, I'm trying to get deviation bands on this specific code. I tried using code from other samples but was unsuccessful. Any assistance would be great. thanks

Thanks @MerryDay . Not quite what i was looking for. The deviations are to a moving average rather than the avwap which is what i'm looking for.

I presumed since we are referring to vwap, when mentioning deviation bands, it would be similar to regular vwap. the request is for assistance is to identify the Deviation formula for the anchored vwap in application of this script. example similar to this. thanks for the assistance and reply.
 
Last edited by a moderator:
Hello, I'm trying to get deviation bands on this specific code. I tried using code from other samples but was unsuccessful. Any assistance would be great. thanks

Thanks @MerryDay . Not quite what i was looking for. The deviations are to a moving average rather than the avwap which is what i'm looking for.

I presumed since we are referring to vwap, when mentioning deviation bands, it would be similar to regular vwap. the request is for assistance is to identify the Deviation formula for the anchored vwap in application of this script. example similar to this. thanks for the assistance and reply.
Sure thing! Your question is about using the standard deviation bands from https://usethinkscript.com/threads/vwap-standard-deviation-bands-for-thinkorswim.1315/ on an anchored VWAP script?

We need to establish a clear understanding of what you are expecting the anchored vwap to look like; as it is the basis of the bands.
I did try using the code that you specified earlier, but it seems like your thoughts have changed.

That is why this is a 2nd request that you provide an SCREENSHOT of a chart with the anchored vwap that you are using, as well as the script.
Until we have that image. And we are in agreement as to what the anchored vwap will look like. We cannot help you.
If you're unsure of how to upload screenshots to the forum, don't worry, we've included some easy-to-follow directions for you.
https://usethinkscript.com/threads/answers-to-commonly-asked-questions.6006/#post-58714
 
Hello, I'm trying to get deviation bands on this specific code. I tried using code from other samples but was unsuccessful. Any assistance would be great. thanks

Thanks @MerryDay . Not quite what i was looking for. The deviations are to a moving average rather than the avwap which is what i'm looking for.

I presumed since we are referring to vwap, when mentioning deviation bands, it would be similar to regular vwap. the request is for assistance is to identify the Deviation formula for the anchored vwap in application of this script. example similar to this. thanks for the assistance and reply.

Sure thing! Your question is about using the standard deviation bands from https://usethinkscript.com/threads/vwap-standard-deviation-bands-for-thinkorswim.1315/ on an anchored VWAP script?

We need to establish a clear understanding of what you are expecting the anchored vwap to look like; as it is the basis of the bands.
I did try using the code that you specified earlier, but it seems like your thoughts have changed.

That is why this is a 2nd request that you provide an SCREENSHOT of a chart with the anchored vwap that you are using, as well as the script.
Until we have that image. And we are in agreement as to what the anchored vwap will look like. We cannot help you.
If you're unsure of how to upload screenshots to the forum, don't worry, we've included some easy-to-follow directions for you.
https://usethinkscript.com/threads/answers-to-commonly-asked-questions.6006/#post-58714
No he has his anchored vwap without using std deviation, he is using a different method
Anchored VWAP Formula = Σ(Price x Volume) / ΣVolume
better https://school.stockcharts.com/doku.php?id=technical_indicators:anchored_vwap
 
Last edited by a moderator:
No he has his anchored vwap without using std deviation, he is using a different method
Anchored VWAP Formula = Σ(Price x Volume) / ΣVolume
better https://school.stockcharts.com/doku.php?id=technical_indicators:anchored_vwap

study("Anchored VWAP",overlay=true)
Year = input(2017, minval = 1, maxval = 2099, type=integer, title='Year')
Month = input(10, minval = 1, maxval = 12, type=integer, title='Month')
Day = input(10, minval = 1, maxval = 31, type=integer, title='Day')
Hour = input(12, minval = 0, maxval = 23, type=integer, title='Hour')
Minute = input(0, minval = 0, maxval = 59, type=integer, title='Minute')
DebugMode = input(false, type=bool, title='Debug Mode')
start = security(tickerid, '1', time)
impulse_func = iff(timestamp(Year,Month,Day,Hour,Minute) == time, 1, 0)
newSession = iff(change(start), 1, 0)
startSession = newSession * impulse_func
vwapsum = iff(startSession, ohlc4*volume, vwapsum[1]+ohlc4*volume)
volumesum = iff(startSession, volume, volumesum[1]+volume)
myvwap = vwapsum/volumesum
plot(myvwap, linewidth=3, transp=0, title='AVWAP')
plot(DebugMode ? hour : na)
plot(DebugMode ? minute : na)
 
Last edited by a moderator:
Trying out this Anchored Vwap on TOS, but for some reason even though I tell the indicator to anchor from 20220208 to the present, it's anchoring from the beginning of the week (20220206). Can anyone please help me figure out why the indicator is doing that? I'm on 5D5m chart
 
Last edited:
Trying out this Anchored Vwap on TOS, but for some reason even though I tell the indicator to anchor from 20220208 to the present, it's anchoring from the beginning of the week (20220206). Can anyone please help me figure out why the indicator is doing that? I'm on 5D5m chart

Try using 20230208 and it will work as I believe you want.
 
heres a way to do it, because thinkorswim doesnt allow to point and plot. basically what you do is you put a bar counter underneath on the lower studies and it will tell you the bar number that you desire to plot when you hover over it, then you simple click on the vwap line and that will already e plotted and change it to that bar number.

vwap- upper study

Code:
def Data = BarNumber();
input Number_Of_Bar = 1;
input price = close;
def bar =  Data >= Number_Of_Bar;
def pv = if bar then pv[1] + price * volume else 0;
def cumvolume = if bar then cumvolume[1] + volume else 0;
plot vw = pv / cumvolume;
def bars = data - number_Of_Bar;
def sample = if bar then sample[1] + sqr(price - vw) else 0;
def var = sample/bars;
def dev = sqrt(var);

bar counter- lower sudy

declare lower;
plot b = barnumber();
Every once in awhile I find a gem. Today you are that gem. This is amazing!!! Best idea I have ever seen for anchor vwap.
 
didnt read through the whole thread but did someone code an AVWAP where you could also anchor the vwap to the high or low of the bar being anchored?
 
yes i see hod and lod options in the script but im speaking of high or low of the bar.... where as most AVWAP studies just anchor to the average of the bar i think. For instance ..... yyyy/mm/day Time 00:00 options-High Low Open or Close

This modified the VwapH and VwapL off the high/low respective of PivotH/PivotL

Screenshot-2023-03-15-121439.png
Ruby:
#START STUDY
#Anchored_VWAPv3
#linus, 2014-03-10, v0.1

#10:24 linus: it carries over the previous pivot's lines for high, low and close. (it plots vwaps of the high, low and close that are reset each time a new pivot is found.)
#10:25 linus: i wrote it to experiment with vwap as stops. (the high and low vwaps that can be offset by the ticks input.)
#10:25 linus: but it should serve as an example of how to reset the vwaps based on a signal.
#10:35 linus: #hint: VWAP stops anchored off  fractalTrader pivots.
#10:37 linus: the code calculates the pivots as PivH and PivL, and then restarts the high, low and close vwaps when it finds a new pivot.  Otherwise it continues to calculate the high, low and close vwaps.
#10:37 linus: the dashed vwap plots are the saved from the previous pivot, and the solid vwap plots are since the last pivot.
#20220708 Sleepyz used missing logic from Mobius fractal pivots
#20230315 Sleepz modified the VwapH and VwapL off the high/low respective of PivotH/PivotL

#hint: VWAP stops anchored off  fractalTrader pivots.

#hint n: Lookback period for finding swing highs, lows.
input n = 20;

#hint ticks: Offset High/Low VWAP lines by this number of ticks.
input ticks  = 0.0;
def bn       = BarNumber();
def na       = Double.NaN;
def bnOK     = bn > n;

def isHigher = fold i = 1 to n + 1 with p = 1
               while p do high > GetValue(high, -i);

def HH       = if bnOK and isHigher and
                  high == Highest(high, n)
               then high else na;

def isLower  = fold j = 1 to n + 1 with q = 1
               while q do low < GetValue(low, -j);

def LL       = if bnOK and isLower and
                  low == Lowest(low, n)
               then low else na;

def PHBar    = if !IsNaN(HH)
               then bn
               else PHBar[1];

def PLBar    = if !IsNaN(LL)
               then bn
               else PLBar[1];

def PHL     = if !IsNaN(HH)
              then HH
              else PHL[1];

def PLL     = if !IsNaN(LL)
              then LL
              else PLL[1];

def priorPHBar = if PHL != PHL[1]
                 then PHBar[1]
                 else priorPHBar[1];

def priorPLBar = if PLL != PLL[1]
                 then PLBar[1]
                 else priorPLBar[1];

def HighPivots = bn >= HighestAll(priorPHBar);
def LowPivots  = bn >= HighestAll(priorPLBar);

def PivH       = if !IsNaN(HH) > 0 then HighPivots else na;
def PivL       = if !IsNaN(LL) > 0 then LowPivots  else na;

plot Up        = bn == PLBar;
Up.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
Up.SetLineWeight(3);
Up.SetDefaultColor(Color.WHITE);

plot Dn        = bn == PHBar;
Dn.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
Dn.SetLineWeight(3);
Dn.SetDefaultColor(Color.ORANGE);

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 PH;
rec VH;
rec PL;
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], na);
    VC = CompoundValue(1, volume + VC[1], na);
    PC2 = CompoundValue(1, LocC + PC2[1], na);
    VC2 = CompoundValue(1, volume + VC2[1], na);
}

if Dn {
    PH = LocH;
    VH = volume;
    PH2 = PH[1];
    VH2 = VH[1];
} else {
    PH = CompoundValue(1, LocH + PH[1], na);
    VH = CompoundValue(1, volume + VH[1], na);
    PH2 = CompoundValue(1, LocH + PH2[1], na);
    VH2 = CompoundValue(1, volume + VH2[1], na);
}
if Up  {
    PL = LocL;
    VL = volume;
    PL2 = PL[1];
    VL2 = VL[1];
} else {
    PL = CompoundValue(1, LocL + PL[1], na);
    VL = CompoundValue(1, volume + VL[1], na);
    PL2 = CompoundValue(1, LocL + PL2[1], na);
    VL2 = CompoundValue(1, volume + VL2[1], na);
}

plot VwapC = if Dn[-1] or Up[-1] then na else PC / VC;
plot VwapC2 = if Dn[-1] or Up[-1] then na else PC2 / VC2;
plot VwapH = if Dn[-1] then na else PH / VH;
plot VwapL = if Up[-1] then na else PL / VL;
plot VwapH2 = if Dn[-1] then na else PH2 / VH2;
plot VwapL2 = if Up[-1] then na 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();
VwapH.SetLineWeight(5);

VwapL.SetDefaultColor(Color.DARK_GREEN);
VwapL.HideBubble();
VwapL.SetLineWeight(5);

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

VwapL2.SetDefaultColor(Color.DARK_GREEN);
VwapL2.SetStyle(Curve.SHORT_DASH);
VwapL2.HideBubble();
#END STUDY
;
 
Looking for some clarity on why the AVWAP when anchored to 20230101, 9:30 AM, is perfect on a daily chart, but when changed to lower time frames the AVWAP changes depending on what timeframe I'm on. Even on a lower time frame like 180D 65Min that goes before the anchor date. It was doing this with my previous code so I tried the one below from page 1 of this thread and still getting the same result.

input anchorDate = 20230101;
input anchorTime = 0930;

def tradeStartEST = 0930;
def tradeEndEST = 1600;
def inPeriod = if SecondsTillTime(tradeStartEST) <= 0 and SecondsTillTime(tradeEndEST) > 0 then 1 else 0;

def revisedDate = if SecondsTillTime(anchorTime)<=0 and !inPeriod then anchorDate+1 else if SecondsTillTime(anchorTime)<=0 and inPeriod then anchorDate else anchorDate;

def postAnchorDate = if GetYYYYMMDD() >= revisedDate then 1 else 0;
def postAnchorTime = if SecondsTillTime(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);

anchoredVWAP.setStyle(Curve.Firm);
anchoredVWAP.SetLineWeight(3);
anchoredVWAP.SetDefaultColor(Color.Cyan);

#AddChartBubble(yes,close, revisedDate, color.yellow);
 
Hi SleepyZ, can you please help to add price bubble instead of arrow?

Here is optional price bubbles vs arrows at VwapH/VwapL

Code:
#START STUDY
#Anchored_VWAPv3
#linus, 2014-03-10, v0.1

#10:24 linus: it carries over the previous pivot's lines for high, low and close. (it plots vwaps of the high, low and close that are reset each time a new pivot is found.)
#10:25 linus: i wrote it to experiment with vwap as stops. (the high and low vwaps that can be offset by the ticks input.)
#10:25 linus: but it should serve as an example of how to reset the vwaps based on a signal.
#10:35 linus: #hint: VWAP stops anchored off  fractalTrader pivots.
#10:37 linus: the code calculates the pivots as PivH and PivL, and then restarts the high, low and close vwaps when it finds a new pivot.  Otherwise it continues to calculate the high, low and close vwaps.
#10:37 linus: the dashed vwap plots are the saved from the previous pivot, and the solid vwap plots are since the last pivot.
#20220708 Sleepyz used missing logic from Mobius fractal pivots
#20230315 Sleepyz modified the VwapH and VwapL off the high/low respective of PivotH/PivotL
#20230317 Sleepyz added optional bubbles vs arrows at VwapH/VwapL

#hint: VWAP stops anchored off  fractalTrader pivots.

#hint n: Lookback period for finding swing highs, lows.
input n = 20;
input showarrows  = yes;
input showbubbles = yes;

#hint ticks: Offset High/Low VWAP lines by this number of ticks.
input ticks  = 0.0;
def bn       = BarNumber();
def na       = Double.NaN;
def bnOK     = bn > n;

def isHigher = fold i = 1 to n + 1 with p = 1
               while p do high > GetValue(high, -i);

def HH       = if bnOK and isHigher and
                  high == Highest(high, n)
               then high else na;

def isLower  = fold j = 1 to n + 1 with q = 1
               while q do low < GetValue(low, -j);

def LL       = if bnOK and isLower and
                  low == Lowest(low, n)
               then low else na;

def PHBar    = if !IsNaN(HH)
               then bn
               else PHBar[1];

def PLBar    = if !IsNaN(LL)
               then bn
               else PLBar[1];

def PHL     = if !IsNaN(HH)
              then HH
              else PHL[1];

def PLL     = if !IsNaN(LL)
              then LL
              else PLL[1];

def priorPHBar = if PHL != PHL[1]
                 then PHBar[1]
                 else priorPHBar[1];

def priorPLBar = if PLL != PLL[1]
                 then PLBar[1]
                 else priorPLBar[1];

def HighPivots = bn >= HighestAll(priorPHBar);
def LowPivots  = bn >= HighestAll(priorPLBar);

def PivH       = if !IsNaN(HH) > 0 then HighPivots else na;
def PivL       = if !IsNaN(LL) > 0 then LowPivots  else na;

plot Up        = bn == PLBar;
Up.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
Up.SetLineWeight(3);
Up.SetDefaultColor(Color.WHITE);
Up.SetHiding(!showarrows);
AddChartBubble(showbubbles and bn == PLBar, low, AsText(low), Color.GREEN, no);

plot Dn        = bn == PHBar;
Dn.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
Dn.SetLineWeight(3);
Dn.SetDefaultColor(Color.ORANGE);
Dn.SetHiding(!showarrows);
AddChartBubble(showbubbles and bn == PHBar, high, AsText(high), Color.RED);

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 PH;
rec VH;
rec PL;
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], na);
    VC = CompoundValue(1, volume + VC[1], na);
    PC2 = CompoundValue(1, LocC + PC2[1], na);
    VC2 = CompoundValue(1, volume + VC2[1], na);
}

if Dn {
    PH = LocH;
    VH = volume;
    PH2 = PH[1];
    VH2 = VH[1];
} else {
    PH = CompoundValue(1, LocH + PH[1], na);
    VH = CompoundValue(1, volume + VH[1], na);
    PH2 = CompoundValue(1, LocH + PH2[1], na);
    VH2 = CompoundValue(1, volume + VH2[1], na);
}
if Up  {
    PL = LocL;
    VL = volume;
    PL2 = PL[1];
    VL2 = VL[1];
} else {
    PL = CompoundValue(1, LocL + PL[1], na);
    VL = CompoundValue(1, volume + VL[1], na);
    PL2 = CompoundValue(1, LocL + PL2[1], na);
    VL2 = CompoundValue(1, volume + VL2[1], na);
}

plot VwapC = if Dn[-1] or Up[-1] then na else PC / VC;
plot VwapC2 = if Dn[-1] or Up[-1] then na else PC2 / VC2;
plot VwapH = if Dn[-1] then na else PH / VH;
plot VwapL = if Up[-1] then na else PL / VL;
plot VwapH2 = if Dn[-1] then na else PH2 / VH2;
plot VwapL2 = if Up[-1] then na 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();
VwapH.SetLineWeight(5);

VwapL.SetDefaultColor(Color.DARK_GREEN);
VwapL.HideBubble();
VwapL.SetLineWeight(5);

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

VwapL2.SetDefaultColor(Color.DARK_GREEN);
VwapL2.SetStyle(Curve.SHORT_DASH);
VwapL2.HideBubble();
#END STUDY
;
 
Here is the standard TOS vwap with an Earnings option added.
Alright, I have to admit, getting into this event offset stuff makes my brain hurt. How do I get 4 anchored vwaps plotted off the last 4 qtrs of earnings? The day of earnings if its pre-market release and the next day if its after market. What is it about the current code posted by Sleepyz that's making it reset the plot for each earnings date instead of continuing to the current bar? I'm really surprised this hasn't been cranked out by somebody already. It seems all the avwap indicators out there are based solely on highs/lows or dates and never both or event/earnings related related. My concentration is no way near good enough to get my head around it atm. Any help would be GREATLY appreciated to get this rolling.
 
Last edited:

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

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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