Thanks

You are using an out of date browser. It may not display this or other websites correctly.

You should upgrade or use an alternative browser.

You should upgrade or use an alternative browser.

- Thread starter Craighaber71
- Start date

Thanks

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:

Awesome...Will that work in a watch list

Can this be done?

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);
```

Tried this today and it works really well, thanks a lot @horserider

@horserider you are a valuable help on this site. I appreciate the work you do. Thanks! Markos

Code:

```
def lookback = 0;
def EMA = ExpAverage(close, 9);
def vwapValue = VWAP()[lookback];
plot scan = EMA crosses vwapValue;
```

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.

@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

@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:

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

plot vwap = vwap();

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

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);
```

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);
```

Thread starter | Similar threads | Forum | Replies | Date |
---|---|---|---|---|

EMA-SMA-VWAP Stacked Indicator for ThinkorSwim | Questions | 17 | ||

O | Histogram of difference between VWAP and EMA? | Questions | 11 | |

J | Halfway between the 9 and 21 EMA | Questions | 4 | |

G | Plot two consecutive closes above 9 EMA | Questions | 5 | |

C | Filter for 8 EMA / 20 SMA Crossover Signals | Questions | 25 |