Anchored VWAP Indicator for ThinkorSwim

The issue is that VWAPs are getting cut off at the new zigzag reversal point. Can you please change the script to make them continuous and not get cut off? I think they need to be cut off at the next zig-zag pivot that is of the same direction and not the opposite.

This extends each of the VWAPs to the last bar

Code:
#AnchoredVWAP_using_ZigZag

#ZigzagHighLow_Colored_Lines_Limited_Plots
#Added up to 5 VWAPs anchored at the zigzag swings ending at the next swing

input show_zz_lines = no;
input show_vwap_lines = yes;
input plot_limited_to_last_x = 5;
input priceH = high;
input priceL = low;
input percentageReversal = 0.05;
input absoluteReversal = 0.05;
input atrLength = 5;
input atrReversal = 2;
input tickReversal = 0;

def zigzag =  ZigZagHighLow("percentage reversal" = percentageReversal, "absolute reversal" = absoluteReversal, "atr length" = atrLength, "atr reversal" = atrReversal, "price h" = priceH, "price l" = priceL).zz;

def EIH = ZigZagHighLow("percentage reversal" = percentageReversal, "absolute reversal" = absoluteReversal, "atr length" = atrLength, "atr reversal" = atrReversal, "price h" = priceH, "price l" = priceL).lastH;

def EIL = ZigZagHighLow("percentage reversal" = percentageReversal, "absolute reversal" = absoluteReversal, "atr length" = atrLength, "atr reversal" = atrReversal, "price h" = priceH, "price l" = priceL).lastl;

#--------------------------

def cond      = if !IsNaN(EIH) or !IsNaN(EIL) then 1 else Double.NaN;
def dataCount = CompoundValue(1, if !IsNaN(cond) then dataCount[1] + 1 else dataCount[1], 0);

def SignalBUY = if HighestAll(dataCount) - dataCount <= plot_limited_to_last_x - 1 then
!IsNaN(EIL) else Double.NaN;
AddChartBubble(SignalBUY, low, "ZB", Color.WHITE, no);

def SignalSELL = if HighestAll(dataCount) - dataCount <= plot_limited_to_last_x - 1 then
!IsNaN(EIH) else Double.NaN;
AddChartBubble(SignalSELL, high, "ZS", Color.WHITE, yes);

def trend = if SignalBUY then 1 else if SignalSELL then -1 else trend[1];

plot zz = if show_zz_lines and HighestAll(dataCount) - dataCount <= plot_limited_to_last_x - 1 then
zigzag else Double.NaN;

zz.EnableApproximation();
zz.DefineColor("Up", Color.UPTICK);
zz.DefineColor("Down", Color.DOWNTICK);

zz.SetLineWeight(3);
zz.SetStyle(1);
zz.HideBubble();
zz.HideTitle();

zz.AssignValueColor(if trend[1] > 0 then zz.Color("Up") else zz.Color("Down"));

#Anchored VWAP to ZZ changes
def count = if !IsNaN(cond) then count[1] + 1 else count[1];
def ccond  = HighestAll(count) - count + 1;
def hl = if !IsNaN(EIH) then 1 else if !IsNaN(EIL) then -1 else hl[1];

plot a1 = if !show_vwap_lines or ccond > 1 or plot_limited_to_last_x < 1  then Double.NaN
else TotalSum(if ccond <= 1 then (if hl > 0 then priceH else priceL) * (volume) else 0) / TotalSum(if ccond <= 1 then volume else 0);

plot a2 = if !show_vwap_lines or ccond > 2 or plot_limited_to_last_x < 2 then Double.NaN
else TotalSum(if ccond <= 2 then (if hl > 0 then priceH else priceL) * (volume) else 0) / TotalSum(if ccond <= 2 then volume else 0);

plot a3 = if !show_vwap_lines or ccond > 3 or plot_limited_to_last_x < 3 then Double.NaN
else TotalSum(if ccond <= 3 then (if hl > 0  then priceH else priceL) * (volume) else 0) / TotalSum(if ccond <= 3 then volume else 0);

plot a4 = if !show_vwap_lines or ccond > 4 or plot_limited_to_last_x < 4 then Double.NaN
else TotalSum(if ccond <= 4 then (if hl > 0 then priceH else priceL) * (volume) else 0) / TotalSum(if ccond <= 4 then volume else 0);

plot a5 = if !show_vwap_lines or ccond > 5 or plot_limited_to_last_x < 5 then Double.NaN
else TotalSum(if ccond <= 5 then (if hl > 0 then priceH else priceL) * (volume) else 0) / TotalSum(if ccond <= 5 then volume else 0);

a1.SetPaintingStrategy(PaintingStrategy.LINE);
a2.SetPaintingStrategy(PaintingStrategy.LINE);
a3.SetPaintingStrategy(PaintingStrategy.LINE);
a4.SetPaintingStrategy(PaintingStrategy.LINE);
a5.SetPaintingStrategy(PaintingStrategy.LINE);
a1.SetDefaultColor(Color.WHITE);
a2.SetDefaultColor(Color.CYAN);
a3.SetDefaultColor(Color.WHITE);
a4.SetDefaultColor(Color.YELLOW);
a5.SetDefaultColor(Color.WHITE);
a1.SetLineWeight(3);
a2.SetLineWeight(3);
a3.SetLineWeight(3);
a4.SetLineWeight(3);
a5.SetLineWeight(3);

#
Screenshot 2026-03-20 154229.png
 

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
632 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