• LIMITED TIME OFFER: use coupon code PRIMEDAY at checkout to save $50 off VIP membership (down to $149 / year). Valid through Tuesday 6/22.

EMA and VWAP Crossover Indicator for ThinkorSwim

Craighaber71

Active member
2019 Donor
VIP
Hey guys I'm looking for a watchlist script for a 9EMA cross Vwap..With the background color coded to the up/downside....also plot how many bars ago this happened...I've found many online but not like this...

Thanks
 

BenTen

Administrative
Staff
VIP
Give this a try

Code:
# Price crossing above or below 9 EMA and VWAP
# 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 bullish_signal = EMA crosses above priceVWAP;
def bearish_signal = EMA crosses below priceVWAP;

# Plot Signals
plot bullish = bullish_signal;
bullish.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
bullish.SetDefaultColor(Color.CYAN);
bullish.SetLineWeight(3);

plot bearish = bearish_signal;
bearish.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
bearish.SetDefaultColor(Color.CYAN);
bearish.SetLineWeight(3);
 
Last edited:

zeek

Active member
2019 Donor
Hi guys, one of my long strategies is to look for the 5EMA to reclaim/cross the VWAP to the upside on the 5min chart and was wondering if anyone could code an indicator for this. Preferably, when the cross happens, a label lights up in a strong color and when there is no cross happening, it stays in a grey color.

Can this be done?
 

horserider

Well-known member
VIP
@zeek See if this works. You can turn off the plots if confirm it is working.

9bjqsba.png


Code:
# 5 EMA and VWAP cross. Modified 2 ToS studies and added labels for crosses. By Horserider 7/21/2019

# TD Ameritrade IP Company, Inc. (c) 2011-2019
#

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;


plot VWAP = price;


# TD Ameritrade IP Company, Inc. (c) 2017-2019
#

input price2 = close;
input length = 5;
input displace = 0;


plot AvgExp = ExpAverage(price2[-displace], length);


AddLabel(yes, "Buy ", if avgexp > vwap then Color.GREEN else color.GRAY);

AddLabel(yes, "Sell", if avgexp < vwap then Color.RED else color.GRAY);
 

Bung Bang

New member
Hi! I am trying to get a TOS scanner for EMA(9) crosses VWAP by certain lookback bars (1bar, 2 bars, 3 bars etc) for intraday trading? I am not sure how to change the codes in the watchlist above.; can anybody help ?
 

tomsk

Well-known member
VIP
@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;
 

Lees

New member
Is there a way to color code watchlist stocks with custom filed based on price Cross over vwap upper band and vwap lower band and vwap?

i saw a post to mark bullish and bearish when price move above and below vwap but i would like to leave rage that to identify vwap upper and lower band cross also with different coloring so that it will be easy for us to watch the stock when it reaches those levels.
 

PapaBear10

New member
2019 Donor
VIP
Hi

@BenTen @tomsk , or anyone else available to help, I was wondering if I could get some help to finish up this TOS code for watch-list that I was trying to complete, to show green when 9 ema crosses above vwap within last 3 bars, customizable for timeframes, and opposite as well, if 9 ema crosses below vwap within last 3 bars; Here is what I have gotten started:

Code:
input maLengthOne = 9;
input maType=AverageType, EXPONENTIAL
input timeFrame = {default DAY, WEEK, MONTH};
def vwapValue = reference VWAP(-2, 2, timeFrame)."VWAP";
def ma = MovingAverage(maType, vwapValue, maLength);
inputbarsAfterCross = 3;
def crossAbove = maLengthOne > vwap
def crossBelow = maLengthOne < vwap
diff.AssignValueColor(if ma == vwap then Color.CURRENT else Color.BLACK);
AssignBackgroundColor(if diff > 0 then Color. GREEN else if diff < 0 then Color.RED else Color.CURRENT);

Thank you

Papa

Just a follow up, as I previously did not state the issues I was having:
I am getting the messages invalid statements at the certain lines 2, 7 and 10.
If anyone could advise would be great, I am not a coder, but attempted to piece together what I could;
Thanks in advance @BenTen @tomsk @horserider @netarchitech or anyone else who can help me with this!

Code:
input maLengthOne = 9;
input maType=AverageType, EXPONENTIAL
input timeFrame = {default DAY, WEEK, MONTH};
def vwapValue = reference VWAP(-2, 2, timeFrame)."VWAP";
def ma = MovingAverage(maType, vwapValue, maLength);
inputbarsAfterCross = 3;
def crossAbove = maLengthOne > vwap
def crossBelow = maLengthOne < vwap
diff.AssignValueColor(if ma == vwap then Color.CURRENT else Color.BLACK);
AssignBackgroundColor(if diff > 0 then Color. GREEN else if diff < 0 then Color.RED else Color.CURRENT)

Invalid statement: input at 2:1
Invalid statement: def at 7:1
Invalid statement: AssignBackgroundC... at 10:1
 

tomsk

Well-known member
VIP
@PapaBear10 Your code had several syntax errors. Here is my version of a VWAP Watchlist that is painted green when 9 ema crosses above vwap within last 3 bars. It is painted red when 9 ema crosses below vwap within last 3 bars. Remember to select the aggregation period you are interested in when configuring this watchlist

Code:
# VWAP Watchlist
# tomsk
# 1.25.2020

# Watchlist that is painted green when 9 ema crosses above vwap within last 3 bars
# It is painted red when 9 ema crosses below vwap within last 3 bars

input length = 9;

def ema = ExpAverage(close, length);
def vwapValue = reference VWAP();
def crossUp = ema crosses above vwapValue within 3 bars;
def crossDn = ema crosses below vwapValue within 3 bars;
AddLabel(1, if crossUp then "X Up" else if crossDn then "X Down" else " ", Color.Black);
AssignBackgroundColor(if ema crosses above vwapValue then Color.GREEN
                      else if ema crosses below vwapValue then Color.RED
                      else Color.Gray);
# End VWAP Watchlist
 
Last edited:

scottrades

New member
Your code had several syntax errors. Here is my version of a VWAP Watchlist that is painted green when 9 ema crosses above vwap within last 3 bars.
Tom, what would I change here to track price crossing? I've got the following; how to I blend them together? I really like the 3-bars approach, so that it will disappear after the cross happens. I've tried swapping out the input length, etc.

plot vwap = vwap();
AssignBackgroundColor(if close > vwap then Color.DARK_GREEN else if close < vwap then Color.DARK_RED else Color.Dark_ORANGE);
 

mohitdas

New member
VIP
Hey @horserider thank you for writing this code, i am struggling with the code where i would like to paint an up( Buy Signal )/down( Sell Signal) arrow when the action takes place. Would you be able to guide on the same. Thank You

I used the below code to setup alerts

Code:
def Bull = avgexp > vwap;
def Bear = avgexp < vwap;

Alert(Bull, "Buy", Alert.Bar, Sound.Chimes);
Alert(Bear, "Sell", Alert.bar, Sound.Chimes);
 

horserider

Well-known member
VIP
@mohitdas

Redid original to include new label and arrows and alerts.

Code:
# EMA and VWAP cross. Modified 2 ToS studies and added  labels for crosses. By Horserider 7/21/2019
# Redid label and added cross arrows.


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 VWAP = price;

#
#
input price2 = close;
input length = 5;
input displace = 0;

def AvgExp = ExpAverage(price2[-displace], length);

AddLabel(yes, "VWAP EXP Cross", if avgexp > vwap then Color.GREEN else color.RED);

input showBreakoutSignals = no;
plot UpSignal = avgexp crosses above vwap;
plot DownSignal = avgexp crosses below vwap;

UpSignal.SetHiding(!showBreakoutSignals);
DownSignal.SetHiding(!showBreakoutSignals);


UpSignal.SetDefaultColor(Color.UPTICK);
UpSignal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
DownSignal.SetDefaultColor(Color.DOWNTICK);
DownSignal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);

Alert(UpSignal, "Buy", Alert.Bar, Sound.Chimes);
Alert(DownSignal, "Sell", Alert.bar, Sound.Chimes);
 

Similar threads

Top