Moving Average Crossover Watchlist Column for ThinkorSwim

T

tomsk

Well-known member
VIP
Funny ... but it is hard to admit that I simply cannot combine these characters to get the desired result even though the answers are provided here in plain English.

This simple script is not changing to SMA regardless of the variations I throw at it. Please help again.

declare upper;
input price = close;
input displace = 0;

input EMALength1 = 9;
input EMALength2 = 20;

plot upper = ExpAverage(data = price[-displace], length = EMALength1);
upper.SetDefaultColor(Color.RED);
plot lower = ExpAverage(data = price[-displace], length = EMALength2);
lower.SetDefaultColor(Color.BLUE);
AddCloud(upper,lower);

Thank you,
Doug

@dougn Since you're still stuck I MODIFIED your code to a generic moving average type so that it can be selected to SIMPLE, EXPONENTIAL, WILDERS, HULL etc via the user interface. I have made it so that SIMPLE moving average has been selected. Your earlier code used the call ExpAverage() which is to calculate Exponential Moving Average and so won't work for a Simple Moving Average.

Compare this code with your earlier code, so that you can use this as a learning opportunity. You were also missing a few parameters in your AddCloud() statement - that was also fixed

Code:
# Moving Average Dual
# tomsk
# 1.2.2020

input price = close;

input averageType = AverageType.SIMPLE;
input displace = 0;
input Length1 = 9;
input Length2 = 20;

plot fast = MovingAverage(averageType, price[-displace], length1);
fast.SetDefaultColor(Color.RED);

plot slow = MovingAverage(averageType, price[-displace], length2);
slow.SetDefaultColor(Color.BLUE);

AddCloud(fast,slow, color.green, color.red);
# End Moving Average Dual
 
T

tomsk

Well-known member
VIP
You're very welcome, it's a very simple starter script for those wishing to learn/dabble in ThinkScript
 
T

tradeking313

New member
Hey. I was seeing if there was any way to build a custom watchlist column of a 9ema and 26 ema line..to where when 9 crosses over 26...the column turns green and vice versa on the one minute aggregation period. thanks!!
 
T

tomsk

Well-known member
VIP
@tradeking313 See if this watchlist works for you. It's after trading hours now so I can't test in a live environment, but from minimal testing on my end, it should work

Code:
# EMA Crossover Watchlist
# tomsk
# 1.11.2020

def ema1 = ExpAverage(close,9);
def ema2 = ExpAverage(close,26);

def crossOver = if ema1 crosses above ema2 then 1 else if ema1 crosses below ema2 then -1 else 0;
AddLabel(1, if crossOver == 1 then "Up" else if crossOver == -1 then "Down" else "  ", Color.BLACK);
AssignBackgroundColor(if crossOver == 1 then Color.GREEN else if crossOver == -1 then Color.RED else Color.BLACK);
# EMA Crossover Watchlist
 
S

SoChicago

New member
Hello, this a great column and I am going to attempt to reverse hack it and substitute the Simple Moving Average (SMA) for the EMA.
Ben (or can anyone) comment on what this part I quoted means. Its the only part of the logic I don't follow. What are you looking at to see what is the "highest" of between the crosses and lookback for?

def bull_lookback = highest(bull_cross, lookback); def bear_lookback = highest(bear_cross, lookback);
 
R

rokeyaalamr

New member
Hi Ben,
Thanks for the script! This is very useful. I also modified it to see close price crossing VWAP on 1-minute chart within last 2 candle stick. But it seems to picking up crossing even 7-8 sticks before. I did't select the 1 min, as you have described above. Here is the script, what am I doing wrong?
Code:
# Modifying it for close price crosses the VWAP
# https://usethinkscript.com/d/119-moving-average-crossover-watchlist-column-for-thinkorswim
declare lower;
input lookback = 2;
def bull_cross =close crosses above VWAP;
def bear_cross = close crosses below VWAP;
def bull_lookback = highest(bull_cross, lookback);
def bear_lookback = highest(bear_cross, lookback);
plot signal = if bull_lookback then 2 else if bear_lookback then 1 else 0;
signal.AssignValueColor(if signal == 2 then Color.MAGENTA else if signal == 1 then Color.VIOLET else Color.WHITE );
AssignBackgroundCOlor(if signal == 2 then Color.MAGENTA else if signal == 1 then Color.VIOLET  else Color.WHITE);
 
BenTen

BenTen

Administrative
Staff
VIP
@rokeyaalamr Can you share a screenshot of your watchlist setting? The screen where you paste the script above into.
 
R

rokeyaalamr

New member
Thanks BenTen! you are really active. Here's the link of the screen shot.



Also is there script or study for automatic support and resistances lines drawing? And drawing for pre-market highs and lows?
 
Last edited by a moderator:
S

saisharan511

New member
Hi Benten can we write EMA alert for above code that cross 20 in last 2 minutes? Can we write a code to give different alert that crossed EMA cross over in last 2 minutes. Because that helps from not catching the already running trades.
 
J

jaykay

New member
VIP
Hi @BenTen, I was trying to create a query in TOS, but don't see watch changing colors. I know script has been more than year, have you made any new enhancements to it
 
S

stockalerts72

New member
Hi,
I am looking for time of cross on watch list. when ever WAD crosses above SMA and below SMA , display time in watchlist
 
T

Tdnation12

New member
Hi @BenTen . Is it possible to alter your code so that it will notify me when the Ema line crosses over the SMA line. I changed your code to include SMA 2 instead of ema 2. Thanks
 
Last edited:
BenTen

BenTen

Administrative
Staff
VIP
@Tdnation12 Sure, you can modify the existing script. MovAvgExponential will call for EMA while SimpleMovingAvg will call for SMA.
 
T

Tdnation12

New member
@BenTen perfect thanks. For the line: input averageType = AverageType.EXPONENTIAL; do I leave this as is, or change to include both exponential and simple? Thanks once again!
 
BenTen

BenTen

Administrative
Staff
VIP
@Tdnation12 That is not being used in the script so you can either remove it or leave it as is.
 
Top