VWAP Crosses, Format, Watchlist, Label, Scan for ThinkorSwim

As you know you can plot and reference other studies in thinkscript.

EG:

Code:
plot AvgExpShort = MovAvgExponential(length = emaShort, price = price, displace = displace);

However...

Code:
plot VWAPplot = VWAP(-2.0,2.0,"DAY");
does not work. I think it's because the Study "VWAP" and the function "vwap" collide here. Copying the VWAP study to "VWAP_STUDY" doesn't fix it either, I cannot seemingly reference a custom study?


Is there a way I can reference the VWAP study so I can add it to my own custom study or am I stuck because of the poor naming convention on TOS' part?
 
It takes that syntax, but does not plot the line with the proper parameters.

kN9WZBA.png
 
I am having trouble with setting up a scan to find stocks that are within 0.20$ of VWAP. I'm using the scan below with a 1min timeframe yet the stocks it returns have values sometimes more than 2$ from VWAP. Can someone help with this?

Code:
def vw=vwap;
plot diff =absvalue((close -vw)) < 0.20 within 2 bars;
 
@ssaeed73 As an option, what I have is a column on my favorite (like Relative Volume) watchlist that provides me with the vwap value (colored). Very helpful to glance through before clicking a ticker or stock. The simple code below may also help you.

Code:
#Code

# Green vwap value means it is currently above the price.
# Red vwap value means it is currently below the price.

plot vw = reference vwap();
vw.AssignValueColor(if close > vw then Color.green else if close < vw then Color.red else Color.orANGE);

#end code

Good luck!
 
@Stockmaker All of those scan can be made very easily in the TOS Scanner... You know your criteria so all you need to do is combine calls to the VWAP and SMA Studies... Once you know how you won't be needing to rely on others to do your coding for you... Give it a try and if you have questions just ask...

Here is one scan to get you started... It's really just that easy...

X9SLPik.jpg
 
Last edited:
Thanks for your response and example. I'm able to create them now with help of this example.

Just tried 3rd option( List of the stocks if the price is below VWAP and touching 200 SMA WITHIN last candle) with the following setup.

FLp4xWT.png


Just to add (If anyone interested), the mentioned cases were work for intraday only..

Example : 1st case is applicable to Long trade
2nd case is for Go short
3rd case is for entry
4th case is for exit
 
Last edited by a moderator:
what does the -.20 mean in this script?
close > vw - 0.20 within 2 bars;

means....the current price's close is greater than the vwap value minus .20 within the last 2 bars or to specifically answer your questions it means.... minus point twenty.
 
Hello , Does anyone have a VWAP with the close extended into the next day for TOS ? The concept does a pretty good job of keeping you on the right side of the market. I will post a link from tradingview so you can see what I am talking about. Please let me know if this can be done on TOS.

Code:
study("VWAP MTF",overlay=true)

TimeFrame = input('W')
start = security(tickerid, TimeFrame, time)

//------------------------------------------------
newSession = iff(change(start), 1, 0)
//------------------------------------------------
vwapsum = iff(newSession, hl2*volume, vwapsum[1]+hl2*volume)
volumesum = iff(newSession, volume, volumesum[1]+volume)
v2sum = iff(newSession, volume*hl2*hl2, v2sum[1]+volume*hl2*hl2)
myvwap = vwapsum/volumesum
dev = sqrt(max(v2sum/volumesum - myvwap*myvwap, 0))
Coloring=close>myvwap?green:red
av=myvwap
showBcol = input(false, type=bool, title="Show barcolors")
showPrevVWAP = input(false, type=bool, title="Show previous VWAP close")
prevwap = iff(newSession, myvwap[1], prevwap[1])
plot(showPrevVWAP ? prevwap : na, style=circles, color=close > prevwap ? green : red)

A=plot(av, style=circles, color=Coloring)

barcolor(showBcol?Coloring:na)

https://www.tradingview.com/v/n2F64NVO/

Thanks , JADragon3
 
Plots the Previous Day's VWAP and the Current VWAP ( volume weighted average price )

Appreciation goes a long way... Remember to this the like button if you found this post useful.

h3dVErK.png


Code:
#
# Plot Previous Days VWAP
# By XeoNoX via https://usethinkscript.com/
#

input numDevDn = -2.0;
input 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;
def UpperBand = price + numDevUp * deviation;
def LowerBand = price + numDevDn * deviation;


input aggregationPeriod = AggregationPeriod.DAY;
input length = 1;
input displace = -1;
input showOnlyLastPeriod = no;
plot PrevDayVWAP;
if showOnlyLastPeriod and !IsNaN(vwap(period = aggregationPeriod)[-1]) {
    PrevDayVWAP = Double.NaN;
} else {
    PrevDayVWAP = Highest(vwap(period = aggregationPeriod)[-displace], length);
}
PrevDayVWAP.SetDefaultColor(GetColor(9));
PrevDayVWAP.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
 
Hi , I am looking for a 100% conversion from pinescript to thinkscript on the following "VWAP/MVWAP/EMA CROSSOVER" code please : Thanks in advance....:)-

Code:
//@version=3
study("VWAP/MVWAP/EMA CROSSOVER", overlay = true)

//created by @DerrickLaFlame

// Get user input
vwaplength= input(title="VWAP Length", type=integer, defval=1)
emaSource1= input(title="EMA 1 Source", type=source, defval=close)
emaLength1= input(title="EMA 1 Length", type=integer, defval=7)
emaSource2= input(title="EMA 2 Source", type=source, defval=close)
emaLength2= input(title="EMA 2 Length", type=integer, defval=25)   
rsilimit= input(title="RSI Limit (RISKY)", type=integer, defval=65)
rsiminimum= input(title="RSI Minimum (WAIT FOR DIP)", type=integer, defval=51)

/// MVWAP ///
avlength = input(title="MVWAP Length", type=integer, defval=21)
av = ema(vwap,avlength)
plotav = plot(av,color= fuchsia, transp=0, title = "MVWAP")
mvwap = av

// Get values
ema1= ema(emaSource1, emaLength1)
ema2= ema(emaSource2, emaLength2)


/// VWAP ///
cvwap = ema(vwap,vwaplength)
plotvwap = plot(cvwap,color= yellow, transp=0, title = "VWAP", linewidth=3)

Check1 = ema1 >= mvwap
Check2 = ema2 >= mvwap
Check3 = cvwap >= mvwap

Cross = Check1 and Check2
Crossup = Cross and Check3

Buy = Crossup and rsi(close,14)
Risky = Crossup and rsi(close,14) > rsilimit
BuyDip = Crossup and rsi(close,14) < rsiminimum
GoodBuy = Buy and rsi(close,14) < rsilimit
GreatBuy = GoodBuy and rsi(close,14) > rsiminimum

//Test
Long = Buy == 1 ? 1 : 0
Short = Buy != 1 ? 1 : 0

Longtrigger = crossover(Long, 0.5)
Shorttrigger = crossover(Short, 0.5)

plotshape(Longtrigger, title= "Long Entry", location=location.belowbar, color=green, transp=0, style=shape.triangleup, text="Long")
plotshape(Shorttrigger, title= "Short Entry", location=location.abovebar, color=red, transp=0, style=shape.triangledown, text="Short")


// Plot signals to chart
plotshape(Buy, title= "Buy Alert", location=location.belowbar, color=white, transp=100, style=shape.triangleup, text="")
plotshape(Risky, title= "Risky", location=location.abovebar, color=red, transp=100, style=shape.triangledown, text="")
plotshape(BuyDip, title= "WAIT", location=location.abovebar, color=orange, transp=100, style=shape.triangledown, text="")
plotshape(GreatBuy, title= "Enter Here", location=location.belowbar, color=green, transp=100, style=shape.triangleup, text="")
plot(ema(emaSource1, emaLength1), color=yellow, linewidth=1, transp=0, title='EMA1')
plot(ema(emaSource2, emaLength2), color=orange, linewidth=1, transp=0, title='EMA2')

// Alerts
alertcondition(Longtrigger, title="Long Alert", message="Long Alert")
alertcondition(Shorttrigger, title="Short Alert", message="Short Alert")

//Barcolor
barcolor(Long == 1 ? green : red, title = "Bar Color")

// Cloud
displacement_A = 26
displacement_B = 26
conversion_len = 9
base_line_len = 26
senkou_B_len = 51

f(x) =>
    total_volume = sum(volume, x)
    volume_weighted_high = sum(high*volume, x)/total_volume
    volume_weighted_low = sum(low*volume, x)/total_volume
    (volume_weighted_high + volume_weighted_low)/2
   
conversion_line = f(conversion_len)
base_line = f(base_line_len)
senkou_A = (conversion_line + base_line)/2
senkou_B = f(senkou_B_len)


p1 = plot(senkou_A, offset = displacement_A, color = green, title = "Senkou A", linewidth = 2, transp = 100)
p2 = plot(senkou_B, offset = displacement_B, color = red, title = "Senkou B", linewidth = 2, transp = 100)
fill(p1, p2 , color = senkou_A > senkou_B ? green : red, transp= 73)

**/

I already have the EMA/VWAP script , but I am looking for a script that contains VWAP/EMA/MVWAP crossover script . Please look at the above pasted code and if anyone can convert this to thinkscript please ?
 
Hi,
I am looking for "VWAP.LowerBand" value.

This does not seem to work?
def bull_cross = close crosses above VWAP.LowerBand;

Appreciate any help!!
 
Instead of cross we use the low of the then candle is below vwap and the high of the candle is above vwap ...this is better method of scanning for a crossing.

PRICE CROSSES MIDDLE VWAP

Code:
# Alerts
#
# TD Ameritrade IP Company, Inc. (c) 2011-2020
#

input numDevDn = -2.0;
input 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 VWAPx = price;
plot UpperBand = price + numDevUp * deviation;
plot LowerBand = price + numDevDn * deviation;

VWAPx.setDefaultColor(getColor(0));
UpperBand.setDefaultColor(getColor(2));
LowerBand.setDefaultColor(getColor(4));
#instead of cross we use the low the then candle is below vwap and the high of the candle is above vwap
#this is better method of scanning for a crossing
def vwap_cross = low < vwapx and high > vwapx;

Alert(vwap_cross, " ", Alert.BAR, Sound.Chimes);

if you want lower the replace "def vwap_cross = low < vwapx and high > vwapx" with

Code:
def vwap_cross = low < LowerBandand and  high > LowerBand;
 
Last edited:
@XeoNoX I was trying to backtest to see how many times that really happens.

@rad14733 I needed to use the reference keyword as well. So below seems to work! Thanks again!

Code:
def lowvwap = reference VWAP()."LowerBand";
 
Hi, Trying to create a scan that includes the condition that current price is within x% above OR Below VWAP. Any help is appreciated.
 

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