Auto AVWAP (Anchored-VWAP) For ThinkOrSwim

Markos4112

New member
VIP
Author states: Based on Brian Shannon's AVWAP - This indicator anchors vwap to the highest high, lowest low and highest volume bar of a user defined lookback period.

These levels act as a price magnet and strong levels of support and resistance. I use them to identify chart locations for where I want to do business and look for trade setups.

Unlike moving averages, AVWAP will maintain it's chart position no matter the chart resolution. One way to take advantage of this is to wait for price to get to one of these levels, go to lower timeframes and find low risk setups based on your trading strategy.

pnOZYJa.png


Here is the original Tradingview code:
https://www.tradingview.com/script/...d-Volume-Weighted-Average-Price-Custom-AVWAP/

For the new ThinkOrSwim code, you must scroll down to the next post
 
Last edited by a moderator:

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

Hello Team,

I was wondering if anyone could kindly help me convert the Auto AVWAP (Anchored-VWAP) indicator
https://www.tradingview.com/v/ShppRaZB/
into a TOS (Thinkorswim) indicator. It is an open-source script, and I would greatly appreciate any assistance or guidance you can provide. Thank you in advance!"

Note from the author:

"This indicator uses a Stochastic RSI measurement to define when low and high points (support and resistance levels) have occurred and applies an 'anchor' to these points.
It progressively updates these anchor points as they change. The "Next" potential AVWAP values are also displayed. When a support level is raised or a resistance level is lowered the main AVWAP value is replaced by the "Next" value.

Default Configuration:

This indicator (unconventionally) uses the high and low values instead of the HLC3 values used by a typical VWAP calculation. This allows for what appears more like support and resistance than simply an average price. Simply uncheck this configuration value to see the difference."




// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Electrified (electrifiedtrading)
// @version=4
// © Electrified (electrifiedtrading)
// @version=4
study(title="Auto AVWAP (Anchored-VWAP)", shorttitle="Auto AVWAP", overlay=true, format=format.price, precision=2, resolution="") // image v3
kcolor = #0094FF
dcolor = #FF6A00
WMA = "WMA", EMA = "EMA", SMA = "SMA", VWMA = "VWMA", VAWMA = "VAWMA"
///////////////////////////////////////////////////
// Input
useHiLow = input(true, "Use High/Low instead of HLC3", group="Anchored VWAP", tooltip="When true, high and low values are used to caclulate the value of each AVWAP instead of HLC3.")
useOpen = input(true, "Use open instead of close for alerts.", group="Anchored VWAP", tooltip="Using the open instead of the close is more confirmative of a breakout as live values are based upon the close value and could be transient.")
k_mode = input(WMA, "K Mode", group="Stochastic RSI", inline="Source", options=[SMA, EMA, WMA, VWMA, VAWMA])
src = input(hlc3, "Source", group="Stochastic RSI", inline="Source")
smoothK = input(4, "K", group="Stochastic RSI", inline="Values", minval=1)
smoothD = input(4, "D", group="Stochastic RSI", inline="Values", minval=1)

lengthRSI = input(64, "RSI", group="Lengths", inline="Lengths", minval=1)
lengthStoch = input(48, "Stochastic", group="Lengths", inline="Lengths", minval=1)

lowerBand = input(20, "Lower", group="Band", inline="Band", maxval=50, minval=0)
upperBand = input(80, "Upper", group="Band", inline="Band", minval=50, maxval=100)
lowerReversal = input(20, "Lower", group="Reversal", inline="Reversal", maxval=100, minval=0, tooltip="The level that if broken (down) signfies a reversal has begun/occured.\nDefault represents the lower band.")
upperReversal = input(80, "Upper", group="Reversal", inline="Reversal", minval=0, maxval=100, tooltip="The level that if broken (up) signfies a reversal has begun/occured.\nDefault represents the upper band.")
///////////////////////////////////////////////////
// Functions
vawma(src, len) =>
sum = 0.0
vol = 0.0
for m = 1 to len // m = triangular multiple
i = len - m
v = volume * m
vol := vol + v
sum := sum + src * v
sum/vol
////
getMA(series, mode, len) =>
mode==WMA ? wma(series, len) :
mode==EMA ? ema(series, len) :
mode==VWMA ? vwma(series, len) :
mode==VAWMA ? vawma(series, len) :
sma(series, len)
////
///////////////////////////////////////////////////
// Calculation
rsi1 = rsi(src, lengthRSI)
stoch = stoch(rsi1, rsi1, rsi1, lengthStoch)
k = getMA(stoch, k_mode, smoothK)
d = sma(k, smoothD)
k_c = change(k)
d_c = change(d)
kd = k - d
var hi = high
var lo = low
var phi = high
var plo = low
var state = 0
var float hiAVWAP_s = 0
var float loAVWAP_s = 0
var float hiAVWAP_v = 0
var float loAVWAP_v = 0
var float hiAVWAP_s_next = 0
var float loAVWAP_s_next = 0
var float hiAVWAP_v_next = 0
var float loAVWAP_v_next = 0
if(d<lowerBand or high>phi)
phi := high
hiAVWAP_s_next := 0
hiAVWAP_v_next := 0
if(d>upperBand or low<plo)
plo := low
loAVWAP_s_next := 0
loAVWAP_v_next := 0
if(high>hi)
hi := high
hiAVWAP_s := 0
hiAVWAP_v := 0
if(low<lo)
lo := low
loAVWAP_s := 0
loAVWAP_v := 0
vwapHi = useHiLow ? high : hlc3
vwapLo = useHiLow ? low : hlc3

hiAVWAP_s += vwapHi * volume
loAVWAP_s += vwapLo * volume
hiAVWAP_v += volume
loAVWAP_v += volume
hiAVWAP_s_next += vwapHi * volume
loAVWAP_s_next += vwapLo * volume
hiAVWAP_v_next += volume
loAVWAP_v_next += volume

if(state!=-1 and d<lowerBand)
state := -1
else if(state!=+1 and d>upperBand)
state := +1
if(hi>phi and state==+1 and k<d and k<lowerReversal)
hi := phi
hiAVWAP_s := hiAVWAP_s_next
hiAVWAP_v := hiAVWAP_v_next
if(lo<plo and state==-1 and k>d and k>upperReversal)
lo := plo
loAVWAP_s := loAVWAP_s_next
loAVWAP_v := loAVWAP_v_next


hiAVWAP = hiAVWAP_s / hiAVWAP_v
loAVWAP = loAVWAP_s / loAVWAP_v
hiAVWAP_next = hiAVWAP_s_next / hiAVWAP_v_next
loAVWAP_next = loAVWAP_s_next / loAVWAP_v_next

plot(hiAVWAP_next, "High Next",color.new(color.red, 75), 1, style=plot.style_circles)
plot(loAVWAP_next, "Low Next", color.new(color.green, 75), 1, style=plot.style_circles)
plot(hiAVWAP, "High",color.new(color.red, 50), 2, style=plot.style_circles)
plot(loAVWAP, "Low", color.new(color.green, 50), 2, style=plot.style_circles)
alertValue = useOpen ? open : close
resistance = alertValue - hiAVWAP
support = alertValue - loAVWAP
alertcondition(resistance>0 or support<0, title="Breakout (▲▼)", message="Breakout ({{ticker}} {{interval}})")
alertcondition(resistance>0 , title="Resistance Broken ▲", message="Resistance Broken ▲ ({{ticker}} {{interval}})")
alertcondition(support<0 , title="Support Broken ▼", message="Support Broken ▼ ({{ticker}} {{interval}})")
check the below:

CSS:
#// Indciator for TOS
#// © AJ7919
#study("AAVWAP - AJ", overlay = true, max_bars_back=5000)
# Converted by Sam4Cok@Samer800 - 02/2025

input showLabel = yes;
input displayOptions = {Default "AVWAP", "Horizontal Line", "AVWAP & Horizontal Line"};
input timeFrame = {default "DAY", "WEEK", "MONTH"};
input source = ohlc4;
input noOfRolledPeriod = 5; # "Period"


def na = Double.NaN;
def last = isNaN(close);
def hor = displayOptions!=displayOptions."AVWAP";
def wap = displayOptions!=displayOptions."Horizontal Line";
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 time = if isPeriodRolled then time[1] + 1 else time[1];
def timenow = highestAll(if last[-1] then time else 0);
def cond = (timenow - time) <= noOfRolledPeriod;
def anchor = if cond then yes else anchor[1];
def start = if anchor and !anchor[1] then 1 else 0;

#// volume vwap vars
def vol_vwap_volume_price_summation;
def vol_vwap_volume_summation;
def volest_vwap;

if anchor {
    if start == 1 {
        volest_vwap = volume;
        vol_vwap_volume_price_summation = volume * source;
        vol_vwap_volume_summation = volume;
    } else
        if (volest_vwap[1] < volume) {
            vol_vwap_volume_price_summation = volume * source;
            vol_vwap_volume_summation = volume;
            volest_vwap = volume;
        } else {
            volest_vwap = volest_vwap[1];
            vol_vwap_volume_price_summation = vol_vwap_volume_price_summation[1] + volume * source;
            vol_vwap_volume_summation = vol_vwap_volume_summation[1] + volume;
        }
} else {
    volest_vwap = na;
    vol_vwap_volume_price_summation = na;
    vol_vwap_volume_summation = na;
}
def vol_vwap = vol_vwap_volume_price_summation / vol_vwap_volume_summation;

#// high vwap vars
def high_vwap_volume_price_summation;
def high_vwap_volume_summation;
def highest_vwap;

if anchor {
    if start == 1 {
        highest_vwap = high;
        high_vwap_volume_price_summation = volume * source;
        high_vwap_volume_summation = volume;
    } else
        if (highest_vwap[1] < high) {
            high_vwap_volume_price_summation = volume * source;
            high_vwap_volume_summation = volume;
            highest_vwap = high;
        } else {
            highest_vwap = highest_vwap[1];
            high_vwap_volume_price_summation = high_vwap_volume_price_summation[1] + volume * source;
            high_vwap_volume_summation = high_vwap_volume_summation[1] + volume;
        }
} else {
    highest_vwap = na;
    high_vwap_volume_price_summation = na ;
    high_vwap_volume_summation = na;
}
def high_vwap = high_vwap_volume_price_summation / high_vwap_volume_summation;

#// low vwap vars
def low_vwap_volume_price_summation;
def low_vwap_volume_summation;
def lowest_vwap;

if anchor {
    if start == 1 {
        lowest_vwap = low;
        low_vwap_volume_price_summation = volume * source;
        low_vwap_volume_summation = volume;
    } else
        if (lowest_vwap[1] > low) {
            low_vwap_volume_price_summation = volume * source;
            low_vwap_volume_summation = volume;
            lowest_vwap = low;
        } else {
            lowest_vwap = lowest_vwap[1];
            low_vwap_volume_price_summation = low_vwap_volume_price_summation[1] + volume * source;
            low_vwap_volume_summation = low_vwap_volume_summation[1] + volume;
        }
} else {
    lowest_vwap = na;
    low_vwap_volume_price_summation = na;
    low_vwap_volume_summation = na;
}
def low_vwap = low_vwap_volume_price_summation / low_vwap_volume_summation;


#-- plots

plot hiVWAP = if wap and high_vwap then high_vwap else na;
plot voVWAP = if wap and vol_vwap then vol_vwap else na;
plot loVWAP = if wap and low_vwap then low_vwap else na;

hiVWAP.SetStyle(Curve.POINTS);
voVWAP.SetStyle(Curve.POINTS);
loVWAP.SetStyle(Curve.POINTS);
hiVWAP.SetDefaultColor(Color.CYAN);
voVWAP.SetDefaultColor(Color.ORANGE);
loVWAP.SetDefaultColor(Color.MAGENTA);

#-- lines;
def hi_Lines = if hor then highestAll(if last[-1] then high_vwap else 0) else na;
def vo_Lines = if hor then highestAll(if last[-1] then vol_vwap else 0) else na;
def Lo_Lines = if hor then highestAll(if last[-1] then low_vwap else 0) else na;

plot highLine = if !last and anchor then hi_Lines else na;
plot volLine  = if !last and anchor then vo_Lines else na;
plot lowLine  = if !last and anchor then Lo_Lines else na;

highLine.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
volLine.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
lowLine.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

highLine.SetDefaultColor(Color.CYAN);
volLine.SetDefaultColor(Color.ORANGE);
lowLine.SetDefaultColor(Color.MAGENTA);

#-- LAbel

AddLabel(showLabel, noOfRolledPeriod + " " + timeFrame + " High", Color.CYAN);
AddLabel(showLabel, noOfRolledPeriod + " " + timeFrame + " Vol", Color.ORANGE);
AddLabel(showLabel, noOfRolledPeriod + " " + timeFrame + " Low", Color.MAGENTA);

#- END of CODe
 
Samer800 could you have option to display high/low bands instead of single lines, so you could hid the lines to plots only a gray band using "fill" between the vwap of the highs and vwap of the lows, with the original vwap line of the hlc3 in the middle of it. aka gray bands straddling the original lines.? And to auto-display the 1 day vwaps on the 1minute chart, 2 day vwaps on 2minute chart, 30 day vwaps on the 30minute chart, etc
 
Samer800 could you have option to display high/low bands instead of single lines, so you could hid the lines to plots only a gray band using "fill" between the vwap of the highs and vwap of the lows, with the original vwap line of the hlc3 in the middle of it. aka gray bands straddling the original lines.? And to auto-display the 1 day vwaps on the 1minute chart, 2 day vwaps on 2minute chart, 30 day vwaps on the 30minute chart, etc

Yes, you can go into settings and hide the lines.

Yes, you can save the indicator multiple times so they will be setup up to default to your chosen settings
1. create a study for each of your choices with unique names:
1dayvwap, 2dayvwap, etc...
2. edit your settings to your choices
3. MAKE SURE to choose save as default settings
so the study will automatically display those settings.

For your cloud, paste this code snippet into the bottom of your script:
AddCloud(lowline, highline, color.gray, color.gray);
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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