Auto AVWAP (Anchored-VWAP) For ThinkOrSwim

Markos4112

New member
VIP
I’ve been using VWAP in my trading, and it has been incredibly helpful. As part of my strategy, I explored Anchored VWAP and came across a TradingView indicator that works exceptionally well for me. I believe it could be a valuable addition for this wonderful community.
The author states,
An Anchored VWAP is the measure of the average price per share since a specific moment or event.
This 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.

IQWZXRN.png


Here is the original Tradingview code:
https://www.tradingview.com/script/ZmNNKbwE-Auto-AVWAP-Anchored-VWAP/

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 All,

I’ve been using VWAP in my trading, and it has been incredibly helpful. As part of my strategy, I explored Anchored VWAP and came across a TradingView indicator that works exceptionally well for me. I believe it could be a valuable addition for this wonderful community.

I attempted to convert the script to a Thinkorswim indicator using various tools, but unfortunately, I haven’t had any success.

I would greatly appreciate your support in converting this script. Please find the script and the link below for your reference.

Thank you for your time and assistance. I look forward to your guidance.

Best regards,


https://www.tradingview.com/script/ZmNNKbwE-Auto-AVWAP-Anchored-VWAP/

// 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
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:
#// Indicator for TOS
#// © Electrified (electrifiedtrading)
#study(title="Auto AVWAP (Anchored-VWAP)", shorttitle="Auto AVWAP"
# Converted by Sam4Cok@Samer800    - 03/2025

input useHiLow    = yes; #  "Use High/Low instead of HLC3"
input useOpen     = yes; # "Use open instead of close for alerts."
input k_mode      = {"SMA", "EMA", default "WMA", "VWMA", "VAWMA"}; #  "K Mode"
input rsiSource   = hlc3; #, "Source", group="Stochastic RSI", inline="Source")
input smoothK     = 4; #, "K", group="Stochastic RSI", inline="Values", minval=1)
input smoothD     = 4; # "D", group="Stochastic RSI", inline="Values", minval=1)
input lengthRSI   = 64; # "RSI", group="Lengths", inline="Lengths", minval=1)
input lengthStoch = 48; # "Stochastic", group="Lengths", inline="Lengths", minval=1)
input lowerBand   = 20; # "Lower", group="Band", inline="Band", maxval=50, minval=0)
input upperBand   = 80; # "Upper", group="Band", inline="Band", minval=50, maxval=100)
input lowerReversal   = 20; # "Lower"
input upperReversal   = 80; # "Upper"

def na = Double.NaN;
def last = IsNaN(close);

#// Functions
script stoch {
    input src = close;
    input h = high;
    input l = low;
    input len = 14;
    def hh = Highest(h, len);
    def ll = Lowest(l, len);
    def c1 = src - ll;
    def c2 = hh - ll;
    def stoch = if c2 != 0 then c1 / c2 * 100 else 0;
    plot return = stoch;
}
script VWMA {
    input src = close;
    input len = 14;
    input vol = volume;
    def nom = Average(src * vol, len);
    def den = Average(vol, len);
    def VWMA = nom / den;
    plot result = VWMA;
}
script vawma {
    input src = close;
    input len = 4;
    def sum = fold m = 1 to len with p do
          p + GetValue(src, len - m) * (GetValue(volume, len - m) * m);
    def vol = fold n = 1 to len with q do
              q + (GetValue(volume, len - n) * n);
    def vawma = sum / vol;
    plot out = vawma;
}
script getMA {
    input series = close;
    input mode = "WMA";
    input len = 4;
    def getMA = if mode == "WMA" then WMA(series, len) else
                if mode == "EMA" then ExpAverage(series, len) else
                if mode == "VWMA" then vwma(series, len) else
                if mode == "VAWMA" then vawma(series, len) else Average(series, len);
    plot out = getMA;
}
#// Calculation
def rsi1 = RSI(Price = rsiSource, Length = lengthRSI);
def stoch = stoch(rsi1, rsi1, rsi1, lengthStoch);
def k = getMA(stoch, k_mode, smoothK);
def d = Average(k, smoothD);
#def k_c = (k - k[1]);
#def d_c = (d - d[1]);
#def kd = k - d;
def hi;
def lo;
def hi2;
def lo2;
def phi;
def plo;
def hiAVWAP_s_next1;
def hiAVWAP_v_next1;
def loAVWAP_s_next1;
def loAVWAP_v_next1;
def hiAVWAP_s_next;
def hiAVWAP_v_next;
def loAVWAP_s_next;
def loAVWAP_v_next;

def phi1 = if isNaN(phi[1]) then high else if !phi[1] then high else phi[1];
def plo1 = if isNaN(plo[1]) then low else if !plo[1] then low else plo[1];
def hi1 = if isNaN(hi[1]) then high else if !hi[1] then high else hi[1];
def lo1 = if isNaN(lo[1]) then low else if !lo[1] then low else lo[1];

if (d<lowerBand or high>phi1) {
    phi = high;
    hiAVWAP_s_next1 = 0;
    hiAVWAP_v_next1 = 0;
} else {
    phi = phi1;
    hiAVWAP_s_next1 = hiAVWAP_s_next[1];
    hiAVWAP_v_next1 = hiAVWAP_v_next[1];
}
if (d>upperBand or low<plo1) {
    plo = low;
    loAVWAP_s_next1 = 0;
    loAVWAP_v_next1 = 0;
} else {
    plo = plo1;
    loAVWAP_s_next1 = loAVWAP_s_next[1];
    loAVWAP_v_next1 = loAVWAP_v_next[1];
}
def hiAVWAP_s1;
def hiAVWAP_v1;
def loAVWAP_s1;
def loAVWAP_v1;
def hiAVWAP_s;
def hiAVWAP_v;
def loAVWAP_s;
def loAVWAP_v;
if (high>hi1) {
    hi2 = high;
    hiAVWAP_s1 = 0;
    hiAVWAP_v1 = 0;
} else {
    hi2 = hi1;
    hiAVWAP_s1 = hiAVWAP_s[1];
    hiAVWAP_v1 = hiAVWAP_v[1];
}
if (low<lo1) {
    lo2 = low;
    loAVWAP_s1 = 0;
    loAVWAP_v1 = 0;
} else {
    lo2 = lo1;
    loAVWAP_s1 = loAVWAP_s[1];
    loAVWAP_v1 = loAVWAP_v[1];
}
def vwapHi = if useHiLow then high else hlc3;
def vwapLo = if useHiLow then  low else hlc3;
    
def hiAVWAP_s2 = hiAVWAP_s1 + vwapHi * volume;
def loAVWAP_s2 = loAVWAP_s1 + vwapLo * volume;
def hiAVWAP_v2 = hiAVWAP_v1 + volume;
def loAVWAP_v2 = loAVWAP_v1 + volume;
hiAVWAP_s_next = hiAVWAP_s_next1 + vwapHi * volume;
loAVWAP_s_next = loAVWAP_s_next1 + vwapLo * volume;
hiAVWAP_v_next = hiAVWAP_v_next1 + volume;
loAVWAP_v_next = loAVWAP_v_next1 + volume;

def state;
if(state[1]!=-1 and d<lowerBand) {
    state = -1;
} else if(state[1]!=1 and d>upperBand) {
    state = 1;
} else {
    state = state[1];
}

if (hi2>phi and state>0 and k<d and k<lowerReversal) {
    hi = phi;
    hiAVWAP_s = hiAVWAP_s_next;
    hiAVWAP_v = hiAVWAP_v_next;
} else {
    hi = hi2;
    hiAVWAP_s = hiAVWAP_s2;
    hiAVWAP_v = hiAVWAP_v2;
}
if (lo2<plo and state<0 and k>d and k>upperReversal) {
    lo = plo;
    loAVWAP_s = loAVWAP_s_next;
    loAVWAP_v = loAVWAP_v_next;
} else {
    lo = lo2;
    loAVWAP_s = loAVWAP_s2;
    loAVWAP_v = loAVWAP_v2;
}

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

plot hiVWAP = if hiAVWAP then hiAVWAP else na; #, "High"
plot loVWAP = if loAVWAP then loAVWAP else na; #, "Low"
plot HighNext = if hiAVWAP_next then hiAVWAP_next else na; #, "High Next"
plot LowNext = if loAVWAP_next then loAVWAP_next else na; #, "Low Next"

hiVWAP.SetLineWeight(2);
loVWAP.SetLineWeight(2);
HighNext.SetStyle(Curve.POINTS);
LowNext.SetStyle(Curve.POINTS);
hiVWAP.SetStyle(Curve.POINTS);
loVWAP.SetStyle(Curve.POINTS);

HighNext.SetDefaultColor(Color.DARK_RED);
LowNext.SetDefaultColor(Color.DARK_GREEN);
hiVWAP.SetDefaultColor(Color.RED);
loVWAP.SetDefaultColor(Color.GREEN);

#-- END of CODE
 
check the below:

CSS:
#// Indicator for TOS
#// © Electrified (electrifiedtrading)
#study(title="Auto AVWAP (Anchored-VWAP)", shorttitle="Auto AVWAP"
# Converted by Sam4Cok@Samer800    - 03/2025

input useHiLow    = yes; #  "Use High/Low instead of HLC3"
input useOpen     = yes; # "Use open instead of close for alerts."
input k_mode      = {"SMA", "EMA", default "WMA", "VWMA", "VAWMA"}; #  "K Mode"
input rsiSource   = hlc3; #, "Source", group="Stochastic RSI", inline="Source")
input smoothK     = 4; #, "K", group="Stochastic RSI", inline="Values", minval=1)
input smoothD     = 4; # "D", group="Stochastic RSI", inline="Values", minval=1)
input lengthRSI   = 64; # "RSI", group="Lengths", inline="Lengths", minval=1)
input lengthStoch = 48; # "Stochastic", group="Lengths", inline="Lengths", minval=1)
input lowerBand   = 20; # "Lower", group="Band", inline="Band", maxval=50, minval=0)
input upperBand   = 80; # "Upper", group="Band", inline="Band", minval=50, maxval=100)
input lowerReversal   = 20; # "Lower"
input upperReversal   = 80; # "Upper"

def na = Double.NaN;
def last = IsNaN(close);

#// Functions
script stoch {
    input src = close;
    input h = high;
    input l = low;
    input len = 14;
    def hh = Highest(h, len);
    def ll = Lowest(l, len);
    def c1 = src - ll;
    def c2 = hh - ll;
    def stoch = if c2 != 0 then c1 / c2 * 100 else 0;
    plot return = stoch;
}
script VWMA {
    input src = close;
    input len = 14;
    input vol = volume;
    def nom = Average(src * vol, len);
    def den = Average(vol, len);
    def VWMA = nom / den;
    plot result = VWMA;
}
script vawma {
    input src = close;
    input len = 4;
    def sum = fold m = 1 to len with p do
          p + GetValue(src, len - m) * (GetValue(volume, len - m) * m);
    def vol = fold n = 1 to len with q do
              q + (GetValue(volume, len - n) * n);
    def vawma = sum / vol;
    plot out = vawma;
}
script getMA {
    input series = close;
    input mode = "WMA";
    input len = 4;
    def getMA = if mode == "WMA" then WMA(series, len) else
                if mode == "EMA" then ExpAverage(series, len) else
                if mode == "VWMA" then vwma(series, len) else
                if mode == "VAWMA" then vawma(series, len) else Average(series, len);
    plot out = getMA;
}
#// Calculation
def rsi1 = RSI(Price = rsiSource, Length = lengthRSI);
def stoch = stoch(rsi1, rsi1, rsi1, lengthStoch);
def k = getMA(stoch, k_mode, smoothK);
def d = Average(k, smoothD);
#def k_c = (k - k[1]);
#def d_c = (d - d[1]);
#def kd = k - d;
def hi;
def lo;
def hi2;
def lo2;
def phi;
def plo;
def hiAVWAP_s_next1;
def hiAVWAP_v_next1;
def loAVWAP_s_next1;
def loAVWAP_v_next1;
def hiAVWAP_s_next;
def hiAVWAP_v_next;
def loAVWAP_s_next;
def loAVWAP_v_next;

def phi1 = if isNaN(phi[1]) then high else if !phi[1] then high else phi[1];
def plo1 = if isNaN(plo[1]) then low else if !plo[1] then low else plo[1];
def hi1 = if isNaN(hi[1]) then high else if !hi[1] then high else hi[1];
def lo1 = if isNaN(lo[1]) then low else if !lo[1] then low else lo[1];

if (d<lowerBand or high>phi1) {
    phi = high;
    hiAVWAP_s_next1 = 0;
    hiAVWAP_v_next1 = 0;
} else {
    phi = phi1;
    hiAVWAP_s_next1 = hiAVWAP_s_next[1];
    hiAVWAP_v_next1 = hiAVWAP_v_next[1];
}
if (d>upperBand or low<plo1) {
    plo = low;
    loAVWAP_s_next1 = 0;
    loAVWAP_v_next1 = 0;
} else {
    plo = plo1;
    loAVWAP_s_next1 = loAVWAP_s_next[1];
    loAVWAP_v_next1 = loAVWAP_v_next[1];
}
def hiAVWAP_s1;
def hiAVWAP_v1;
def loAVWAP_s1;
def loAVWAP_v1;
def hiAVWAP_s;
def hiAVWAP_v;
def loAVWAP_s;
def loAVWAP_v;
if (high>hi1) {
    hi2 = high;
    hiAVWAP_s1 = 0;
    hiAVWAP_v1 = 0;
} else {
    hi2 = hi1;
    hiAVWAP_s1 = hiAVWAP_s[1];
    hiAVWAP_v1 = hiAVWAP_v[1];
}
if (low<lo1) {
    lo2 = low;
    loAVWAP_s1 = 0;
    loAVWAP_v1 = 0;
} else {
    lo2 = lo1;
    loAVWAP_s1 = loAVWAP_s[1];
    loAVWAP_v1 = loAVWAP_v[1];
}
def vwapHi = if useHiLow then high else hlc3;
def vwapLo = if useHiLow then  low else hlc3;
  
def hiAVWAP_s2 = hiAVWAP_s1 + vwapHi * volume;
def loAVWAP_s2 = loAVWAP_s1 + vwapLo * volume;
def hiAVWAP_v2 = hiAVWAP_v1 + volume;
def loAVWAP_v2 = loAVWAP_v1 + volume;
hiAVWAP_s_next = hiAVWAP_s_next1 + vwapHi * volume;
loAVWAP_s_next = loAVWAP_s_next1 + vwapLo * volume;
hiAVWAP_v_next = hiAVWAP_v_next1 + volume;
loAVWAP_v_next = loAVWAP_v_next1 + volume;

def state;
if(state[1]!=-1 and d<lowerBand) {
    state = -1;
} else if(state[1]!=1 and d>upperBand) {
    state = 1;
} else {
    state = state[1];
}

if (hi2>phi and state>0 and k<d and k<lowerReversal) {
    hi = phi;
    hiAVWAP_s = hiAVWAP_s_next;
    hiAVWAP_v = hiAVWAP_v_next;
} else {
    hi = hi2;
    hiAVWAP_s = hiAVWAP_s2;
    hiAVWAP_v = hiAVWAP_v2;
}
if (lo2<plo and state<0 and k>d and k>upperReversal) {
    lo = plo;
    loAVWAP_s = loAVWAP_s_next;
    loAVWAP_v = loAVWAP_v_next;
} else {
    lo = lo2;
    loAVWAP_s = loAVWAP_s2;
    loAVWAP_v = loAVWAP_v2;
}

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

plot hiVWAP = if hiAVWAP then hiAVWAP else na; #, "High"
plot loVWAP = if loAVWAP then loAVWAP else na; #, "Low"
plot HighNext = if hiAVWAP_next then hiAVWAP_next else na; #, "High Next"
plot LowNext = if loAVWAP_next then loAVWAP_next else na; #, "Low Next"

hiVWAP.SetLineWeight(2);
loVWAP.SetLineWeight(2);
HighNext.SetStyle(Curve.POINTS);
LowNext.SetStyle(Curve.POINTS);
hiVWAP.SetStyle(Curve.POINTS);
loVWAP.SetStyle(Curve.POINTS);

HighNext.SetDefaultColor(Color.DARK_RED);
LowNext.SetDefaultColor(Color.DARK_GREEN);
hiVWAP.SetDefaultColor(Color.RED);
loVWAP.SetDefaultColor(Color.GREEN);

#-- END of CODE
check the below:

CSS:
#// Indicator for TOS
#// © Electrified (electrifiedtrading)
#study(title="Auto AVWAP (Anchored-VWAP)", shorttitle="Auto AVWAP"
# Converted by Sam4Cok@Samer800    - 03/2025

input useHiLow    = yes; #  "Use High/Low instead of HLC3"
input useOpen     = yes; # "Use open instead of close for alerts."
input k_mode      = {"SMA", "EMA", default "WMA", "VWMA", "VAWMA"}; #  "K Mode"
input rsiSource   = hlc3; #, "Source", group="Stochastic RSI", inline="Source")
input smoothK     = 4; #, "K", group="Stochastic RSI", inline="Values", minval=1)
input smoothD     = 4; # "D", group="Stochastic RSI", inline="Values", minval=1)
input lengthRSI   = 64; # "RSI", group="Lengths", inline="Lengths", minval=1)
input lengthStoch = 48; # "Stochastic", group="Lengths", inline="Lengths", minval=1)
input lowerBand   = 20; # "Lower", group="Band", inline="Band", maxval=50, minval=0)
input upperBand   = 80; # "Upper", group="Band", inline="Band", minval=50, maxval=100)
input lowerReversal   = 20; # "Lower"
input upperReversal   = 80; # "Upper"

def na = Double.NaN;
def last = IsNaN(close);

#// Functions
script stoch {
    input src = close;
    input h = high;
    input l = low;
    input len = 14;
    def hh = Highest(h, len);
    def ll = Lowest(l, len);
    def c1 = src - ll;
    def c2 = hh - ll;
    def stoch = if c2 != 0 then c1 / c2 * 100 else 0;
    plot return = stoch;
}
script VWMA {
    input src = close;
    input len = 14;
    input vol = volume;
    def nom = Average(src * vol, len);
    def den = Average(vol, len);
    def VWMA = nom / den;
    plot result = VWMA;
}
script vawma {
    input src = close;
    input len = 4;
    def sum = fold m = 1 to len with p do
          p + GetValue(src, len - m) * (GetValue(volume, len - m) * m);
    def vol = fold n = 1 to len with q do
              q + (GetValue(volume, len - n) * n);
    def vawma = sum / vol;
    plot out = vawma;
}
script getMA {
    input series = close;
    input mode = "WMA";
    input len = 4;
    def getMA = if mode == "WMA" then WMA(series, len) else
                if mode == "EMA" then ExpAverage(series, len) else
                if mode == "VWMA" then vwma(series, len) else
                if mode == "VAWMA" then vawma(series, len) else Average(series, len);
    plot out = getMA;
}
#// Calculation
def rsi1 = RSI(Price = rsiSource, Length = lengthRSI);
def stoch = stoch(rsi1, rsi1, rsi1, lengthStoch);
def k = getMA(stoch, k_mode, smoothK);
def d = Average(k, smoothD);
#def k_c = (k - k[1]);
#def d_c = (d - d[1]);
#def kd = k - d;
def hi;
def lo;
def hi2;
def lo2;
def phi;
def plo;
def hiAVWAP_s_next1;
def hiAVWAP_v_next1;
def loAVWAP_s_next1;
def loAVWAP_v_next1;
def hiAVWAP_s_next;
def hiAVWAP_v_next;
def loAVWAP_s_next;
def loAVWAP_v_next;

def phi1 = if isNaN(phi[1]) then high else if !phi[1] then high else phi[1];
def plo1 = if isNaN(plo[1]) then low else if !plo[1] then low else plo[1];
def hi1 = if isNaN(hi[1]) then high else if !hi[1] then high else hi[1];
def lo1 = if isNaN(lo[1]) then low else if !lo[1] then low else lo[1];

if (d<lowerBand or high>phi1) {
    phi = high;
    hiAVWAP_s_next1 = 0;
    hiAVWAP_v_next1 = 0;
} else {
    phi = phi1;
    hiAVWAP_s_next1 = hiAVWAP_s_next[1];
    hiAVWAP_v_next1 = hiAVWAP_v_next[1];
}
if (d>upperBand or low<plo1) {
    plo = low;
    loAVWAP_s_next1 = 0;
    loAVWAP_v_next1 = 0;
} else {
    plo = plo1;
    loAVWAP_s_next1 = loAVWAP_s_next[1];
    loAVWAP_v_next1 = loAVWAP_v_next[1];
}
def hiAVWAP_s1;
def hiAVWAP_v1;
def loAVWAP_s1;
def loAVWAP_v1;
def hiAVWAP_s;
def hiAVWAP_v;
def loAVWAP_s;
def loAVWAP_v;
if (high>hi1) {
    hi2 = high;
    hiAVWAP_s1 = 0;
    hiAVWAP_v1 = 0;
} else {
    hi2 = hi1;
    hiAVWAP_s1 = hiAVWAP_s[1];
    hiAVWAP_v1 = hiAVWAP_v[1];
}
if (low<lo1) {
    lo2 = low;
    loAVWAP_s1 = 0;
    loAVWAP_v1 = 0;
} else {
    lo2 = lo1;
    loAVWAP_s1 = loAVWAP_s[1];
    loAVWAP_v1 = loAVWAP_v[1];
}
def vwapHi = if useHiLow then high else hlc3;
def vwapLo = if useHiLow then  low else hlc3;
  
def hiAVWAP_s2 = hiAVWAP_s1 + vwapHi * volume;
def loAVWAP_s2 = loAVWAP_s1 + vwapLo * volume;
def hiAVWAP_v2 = hiAVWAP_v1 + volume;
def loAVWAP_v2 = loAVWAP_v1 + volume;
hiAVWAP_s_next = hiAVWAP_s_next1 + vwapHi * volume;
loAVWAP_s_next = loAVWAP_s_next1 + vwapLo * volume;
hiAVWAP_v_next = hiAVWAP_v_next1 + volume;
loAVWAP_v_next = loAVWAP_v_next1 + volume;

def state;
if(state[1]!=-1 and d<lowerBand) {
    state = -1;
} else if(state[1]!=1 and d>upperBand) {
    state = 1;
} else {
    state = state[1];
}

if (hi2>phi and state>0 and k<d and k<lowerReversal) {
    hi = phi;
    hiAVWAP_s = hiAVWAP_s_next;
    hiAVWAP_v = hiAVWAP_v_next;
} else {
    hi = hi2;
    hiAVWAP_s = hiAVWAP_s2;
    hiAVWAP_v = hiAVWAP_v2;
}
if (lo2<plo and state<0 and k>d and k>upperReversal) {
    lo = plo;
    loAVWAP_s = loAVWAP_s_next;
    loAVWAP_v = loAVWAP_v_next;
} else {
    lo = lo2;
    loAVWAP_s = loAVWAP_s2;
    loAVWAP_v = loAVWAP_v2;
}

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

plot hiVWAP = if hiAVWAP then hiAVWAP else na; #, "High"
plot loVWAP = if loAVWAP then loAVWAP else na; #, "Low"
plot HighNext = if hiAVWAP_next then hiAVWAP_next else na; #, "High Next"
plot LowNext = if loAVWAP_next then loAVWAP_next else na; #, "Low Next"

hiVWAP.SetLineWeight(2);
loVWAP.SetLineWeight(2);
HighNext.SetStyle(Curve.POINTS);
LowNext.SetStyle(Curve.POINTS);
hiVWAP.SetStyle(Curve.POINTS);
loVWAP.SetStyle(Curve.POINTS);

HighNext.SetDefaultColor(Color.DARK_RED);
LowNext.SetDefaultColor(Color.DARK_GREEN);
hiVWAP.SetDefaultColor(Color.RED);
loVWAP.SetDefaultColor(Color.GREEN);

#-- END of CODE
HI @samer800 Thank you very much.
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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