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

Hello, I'm trying to use this script as a scanner. Is that possible? I have it added to my studies but it is not an option in my scanner. Will this work or is it not possible?
 
I know it's here.
I'm using two new strategies - 34 / VWAP / 200 and the SMA8 / VWMA34. Somewhere I've seen ways to get alerts or having some sort of feedback when 8 crosses 34. or when the stock crosses the 34 on the first... cant find it here. what am I doing wrong? :) thanks!
 
@BenTen is there a way to do this same thing for the upper band?

I typically see very bullish activity above the VWAP Upper line when set to 1 SD.

Hopeful this is an easy edit!!!

Wait - I think I figured it out!

plot VWAPUB1 = reference VWAP("num dev up" = 1.0)."UpperBand";

AssignBackgroundColor(if close > VWAPUB1 then Color.DARK_GREEN else if close < VWAPUB1 then Color.DARK_RED else Color.Dark_ORANGE);

Nope, I lied - I am still missing something...
 
Last edited by a moderator:
@C4men Try this one:

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

input numDevDn = -1.0;
input numDevUp = 1.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));

def VWAP = price;
plot UpperBand = price + numDevUp * deviation;
#plot LowerBand = price + numDevDn * deviation;

AssignBackgroundColor(if close > UpperBand then Color.DARK_GREEN else Color.Gray);
 
Thanks @BenTen!

I am pretty close, wondering if I might ask one more favor. I am trying to color it based on 4 different "conditions", but when I put in this code at the bottom, the colors don't always match the reality on the chart.

Above the Upper = Strong Bull
Between Upper and VWAP = Bullish
Between VWAP and Lower = Bear
Below the Lower = Strong Bearish

I typically trade on the 5 min chart - if that makes a difference - and price crossing above the VWAP Upper at 1 StDev has been a good signal.

Can you take a look and tell me what I am missing (Note: I replace UpperBand with VWAP_Upper)?

Code:
def VWAP_Upper = price + numDevUp * deviation;
def VWAP_Lower = price + numDevDn * deviation;

def VWAP_StrongBull = close > VWAP_Upper;
def VWAP_Bull = close < VWAP_Upper and close > price;
def VWAP_Bear = close > VWAP_Lower and close < price;
def VWAP_StrongBear = close < VWAP_Lower;

plot VWAP_Num = if VWAP_StrongBull is true then 2 else if VWAP_Bull is true then 1 else if VWAP_Bear is true then -1 else if VWAP_StrongBear is true then -2 else 0;

AssignBackgroundColor(if VWAP_Num == 2 then Color.DARK_GREEN else if VWAP_Num == 1 then color.GREEN else if VWAP_Num == -1 then color.RED else if VWAP_Num == -2 then Color.Dark_Red else Color.White);

Edit: I'm an ***** - I think setting the timeframe in the watchlist code editor to '5 min' did the trick (maybe).
 
@Bung Bang A scan code that looks for EMA(9) crossing VWAP can be simply coded as follows. If you want this to lookback 2 bars, just change the value of the variable lookback to 2. Be default it is set to 0, which means it looks at the current bar.

Code:
def lookback = 0;
def EMA = ExpAverage(close, 9);
def vwapValue = VWAP()[lookback];

plot scan = EMA crosses vwapValue;

Any competent coder can write a scan for the EMA to cross over lower VWAP line as well as the upper? Perhaps include all 3 VWAP lines so that you can choose which one you want to scan for in one scan by commenting out the rest? Also include both BULL/BEAR scans?

I was testing this out 9 EMA crossing VWAP and its very interesting on a 5 min timeframe...but it does not work good on higher timeframes as the 9 EMA crossover VWAP hits much later...,however, IF the scans that I mentioned above could be created on a higher timeframes such as 15min and up could work very well IF the EMA crossed over lower VWAP line when going LONG and vice versa when SHORTING.

Thanks in advance.
 
Hey everyone,

I tried repurposing some existing code, but fall short when it comes to the watchlist column.

Trying to have a column that turns green if the plot - an EMA crossing above VWAP Upper Band at 1.0 SD - is true within X bars.

Maybe I am missing a lookback? Or the code to color the column is wrong? Any thoughts?

Note: I am on a 10min chart.

Code:
input numDevDn = -1.0;
input numDevUp = 1.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));

def VWAP = price;

def VWAP_Upper = price + numDevUp * deviation;
def VWAP_Lower = price + numDevDn * deviation;

def SignalEMA = movAvgExponential (3);

def VWAP_StrongBull = SignalEMA crosses above VWAP_Upper within 5 bars;

plot VWAP_Num = if VWAP_StrongBull is true then 2 else 0;

AssignBackgroundColor(if VWAP_Num == 2 then Color.BLUE else Color.Black);
 
@C4men Try this:

Code:
# EMA crossing above Upper VWAP Band Watchlist Column
# By BenTen of useThinkScript.com

declare upper;

# Moving Average
input priceMA = close;
input lengthMA = 9;
input displace = 0;
input showBreakoutSignals = yes;
input price = close;

def EMA = ExpAverage(priceMA[-displace], lengthMA);

# VWAP
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 priceVWAP = volumeVwapSum / volumeSum;
def deviation = Sqrt(Max(volumeVwap2Sum / volumeSum - Sqr(priceVWAP), 0));

def VWAP = priceVWAP;
def UpperBand = priceVWAP + numDevUp * deviation;
#plot LowerBand = priceVWAP + numDevDn * deviation;

def bullish_signal = EMA crosses above UpperBand;
#def bearish_signal = EMA crosses below priceVWAP;

# Plot Signals
plot bullish = bullish_signal;

AssignBackgroundColor(if bullish then color.dark_green else color.gray);

We're using the 9 EMA in the code. Make sure to change it.
 
Good afternoon. @BenTen

Hmmm....how to explain this.
I am looking for a scan that will show me around 5 minutes before the potential moment when the vWAP and EMA9 cross. The scanner I have in place now will give me the cross, but it's a minute or two after the cross.

I am looking for a way to get ahead of the potential cross, even if it doesn't end up making the actual cross.

Any help would be greatly appreciated!
 
Last edited:
Newbie here Looking for an intraday scan to find EMA9 ("trade line") crossing above VWAP.....

Can you point me in the right direction?
 
@BenTen I have been using this vwap ema cross over, I was wondering if there was away to set arrows up or down arrow when the input 9 ema crosses either above the +1 deviation band (up arrow), or below -1 deviation band (down arrow) of vwap. Below is script I am using, would like any help to expand it to also trigger off of deviation band crosses.

Thanks

Code:
input maType = AverageType.EXPONENTIAL;
input maPrice = close;
input maLength = 9;
input numDevDn = -1.0;
input numDevUp = 1.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;
plot UpperBand = price + numDevUp * deviation;
plot LowerBand = price + numDevDn * deviation;
VWAP.setDefaultColor(getColor(0));
UpperBand.setDefaultColor(getColor(2));
LowerBand.setDefaultColor(getColor(4));
plot ma = MovingAverage(maType, maPrice, maLength);
ma.SetStyle(Curve.MEDIUM_DASH);
plot crossAbove = ma[1] < VWAP[1] and ma > VWAP;
crossAbove.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
crossAbove.SetLineWeight(3);
crossAbove.SetDefaultColor(Color.CYAN);
plot crossBelow = ma[1] > VWAP[1] and ma < VWAP;
crossBelow.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
crossBelow.SetLineWeight(3);
crossBelow.SetDefaultColor(Color.MAGENTA);
 
@PapaBear10 Sure, here you go. Just add the following code to the bottom of your existing script.

Code:
plot condition1 = ma crosses below LowerBand;
condition1.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
plot condition2 = ma crosses above UpperBand;
condition2.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
 
@ngunda Certainly possible, but since the signal doesn't appear very often, I think you're better off creating a scanner for this sort of crossover and then save it as a watchlist. Otherwise, you're going to see a blank column most of the time.
 

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