Moving Average Crossover Watchlist Column for ThinkorSwim

BenTen

Administrative
Staff member
Staff
VIP
Lifetime
If anyone uses EMA crossover as a way to identify trend or point of entry/exit, knowing when there is a moving average crossover on your watchlist column can certainly be helpful. Here is a quick snippet that let you do just that.

It includes lookback period so that you can set to X amount of when there is an EMA (exponential moving average) crossover. For example, the default is 5. The column will turn green when there is a bullish crossover within the last 5 bars and vice versa. Orange for neutral.

DQJCTgG.png


thinkScript Code

Rich (BB code):
# WalkingBallista EMA Lookback Cross
# https://usethinkscript.com/d/119-moving-average-crossover-watchlist-column-for-thinkorswim

declare lower;

input lookback = 5;
input ema1_len = 10;
input ema2_len = 20;
input averageType = AverageType.EXPONENTIAL;

def ema1 = MovAvgExponential(length=ema1_len);
def ema2 = MovAvgExponential(length=ema2_len);

def bull_cross = ema1 crosses above ema2;
def bear_cross = ema1 crosses below ema2;

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.Dark_Green else if signal == 1 then Color.Dark_Red else Color.Dark_Orange);
AssignBackgroundCOlor(if signal == 2 then Color.Dark_Green else if signal == 1 then Color.Dark_Red else Color.Dark_Orange);

The default values for EMAs are 10 and 20. You can change the input to whichever EMA you use on your chart.

In case you don't want to check for moving average crossover but price moving above or below a specific moving average, you can use the alternative code below.

This script will highlight the column green if the current stock price is above the 200 simple moving average. If the price is below the 200 SMA, the column will turn red.

Code:
input price = close;
input length = 200;
input displace = 0;

plot SMA = Average(price[-displace], length);
AssignBackgroundColor(if price > SMA then color.dark_green else color.dark_red);
 

Attachments

  • DQJCTgG.png
    DQJCTgG.png
    24.5 KB · Views: 431
Last edited:

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

@BenTen Now, that is an efficient way of using a watchlist...just scroll!

Here is a moving average crossover study for a watchlist column, from BLT

Code:
# MA Crossover Watchlist AG
# blt
# 5.19.2016

def ma_fast = average(close,9);
def ma_slow = average(close,50);
def xo_up = ma_fast crosses above ma_slow;
def xo_dn = ma_fast crosses below ma_slow;
def up = ma_fast > ma_slow;
def dn = ma_fast < ma_slow;

plot output = if xo_up OR xo_dn then 1 else 0;
output.assignvaluecolor(if up then color.green else if dn then color.red else color.yellow);

assignbackgroundcolor(if up then color.green else if dn then color.red else color.black);

# End Study
 
Last edited:
Hi BenTen. This is a great script. How do I customize it to use it for day trading? I added to my WL, but it is working for a daily timeframe. I would like to watch if there is a MA cross over at 5 minute interval.
 
Last edited:
@jan_angel When adding the script to your watchlist, you should be able to select to the timeframe of your liking. Daily is by default.

tZ5WOCK.png


 

Attachments

  • tZ5WOCK.png
    tZ5WOCK.png
    250.1 KB · Views: 240
Last edited:
Thank you! that did the trick. Appreciate the quick response. - I have been using your MA crossover script all this week...and it worked great. However, for some reason, today all day it kept on saying 'loading' in the WL column. Do you have any ideas why? I thought it was TOS issue, but the helpdesk guy was totally helpless. Any info that you can provide will be great. I love this script.
 
Last edited:
@jan_angel Hey, I had the same problem for other indicators that I include in the watchlist columns too. It's totally ToS issue. Nothing we can do here ☹️

 
Last edited:
I am relatively new to TOS and until now have utilized the standard studies with some basic modification. I have installed some custom scripts that have been shared, but I do not know how to program it. I am looking to create a watchlist column that will visually indicate to me when there was a recent EMA cross over. I think it should be pretty simple.

The column that I want is a EMA cross over for 5/13 EMA. Criteria:
  • cross over has occurred in last 3 bars on hourly chart.
  • 5/13 crosses above (in last 3 bars) background is green.
  • 5/13 crosses below (in last 3 bars) background is red.
  • There has been no cross over in last 3 bars background is black.

It would be nice if this script had variables in it that I can change if I want to i.e. EMA 5/13 or whatever, time frame i.e. hourly, 30 min, 15 min, etc. Background colors. This way, I could copy the script, modify and add to a new column for a different EMA cross over, etc.
 
Hi Ben,

Thanks for sharing this! I was hunting around on the web today and was led your post here.

I was looking to develop a watchlist column that is color coded very similar (if not exact) to what you have offered. But, I was looking at it from the 'MovingAverageCrossOver' variable.

I am looking to change the color of the column background when for example the 9 EMA crosses the 21. When 9 goes above 21 it is bullish. When 9 goes below 21 it is bearish. I see that I can adjust the ema inputs with your script. But how come if I set of these for 9, 21 and 2 period lookback on a one hour chart, many times the EMA has not actually crossed? It appears to only be a price cross. Am I missing something? I am trying to find a way to turn the column green when the 9/21 actually 'cross'. Bullish above, bearish below. Lookback 2 or 3 bars. Only if they actually cross. Do you have any ideas or am I doing something wrong? Thanks again for your help and sharing!

Mike
 
This is what I am trying to do in the column and have it change the background color like yours does. Green 'above', red 'below', orange neither in the last X bars.

Code:
MovingAvgCrossover("length1" = 9, "length2" = 21, "average type1" = "EXPONENTIAL", "average type2" = "EXPONENTIAL") is true within 2 bars

I'm trying to figure out how to do this, but not a programmer:

Code:
input lookback = 2;
input ema1_len = 10;
input ema2_len = 21;

MovingAvgCrossover(CLOSE,ema1_len,ema2_len,EXPONENTIAL,EXPONENTIAL,above)is true within 'lookback' bars;
green
else orange

MovingAvgCrossover(CLOSE,ema1_len,ema2_len,EXPONENTIAL,EXPONENTIAL,below)is true within 'lookback' bars;
red
else orange
 
@mkalavitz welcome to useThinkScript. If you are using a 1 hour chart and you want to know when a 9 period EMA crosses something else, you will be 9 Hours behind. Does that make sense? Maybe I'm messed up. I Dunno...
Please post a screen shot of something that you want. Also, your watchlist column.
 
Hi Guys. Simply trying to come up with code that will show this Exponential Moving Average Cross Over, 9/21 on an hourly chart. If a bullish cross over occurred in the last 2 bars the cell is green. If a bearish cross over occurred in the last 2 bars the cell is red. If no cross over has occurred in the last 2 hours, orange.

In the indicator on this chart, I simply used the
'MovingAvgCrossover' study set to (CLOSE, 5, 13, EXPONENTIAL, EXPONENTIAL, above)
'MovingAvgCrossover' study set to (CLOSE, 5, 13, EXPONENTIAL, EXPONENTIAL, below)

I just want the cell in the watchlist to go to the appropriate color if either of the above have occurred in the last two bars. Any ideas?

xT9jwRR.png


thx
Mike
 

Attachments

  • xT9jwRR.png
    xT9jwRR.png
    475.8 KB · Views: 258
Last edited by a moderator:
@mkalavitz I do not see any issues with this watchlist column. I plugged the code into the watchlist, change it to my desired EMAs (in this case, I changed it to 9 and 21 just like you wanted), and adjust the timeframe to hourly). I also changed the lookback period to 2 (since that's what you also wanted).

Here is my result. Orange = neutral because there hasn't been any crossover between the two EMAs within the last 2 bars on the hourly chart.

jXJCUn7.png
 
Hey Ben. I think this is good! I looked a little closer and maybe I am just being extra picky. Sometimes when I look at the watchlist it shows a cross color and then when I compare to the appropriate chart with indicators, the cross has not yet officially happened. Very close, but not there yet. This must just be because of math or the chart updates?

Also, I want to know exactly when a cross happens on a 1 hour chart. Is this script looking at the 'close' for that time frame or the 'current price'? What could I substitute 'crosses' with to mean 'equal or greater than' for bull_cross and 'equal or less than' for bear_cross on the current price.

Awesome man! Thanks!

MK
 
Hi BenTen - i have been using your MA crossover script all this week...and it worked great. However, for some reason, today all day it kept on saying 'loading' in the WL column. Do you have any ideas why? I thought it was TOS issue, but the helpdesk guy was totally helpless. Any info that you can provide will be great. I love this script.
I have been getting a lot of loading too. Might be the massive number people coming from Robin Hood to ThinkorSwim. It is a shame too, because a few months ago, my watch-list and scans became lightning fast, and barely any loading at all. Now it is going back to "loading", but still not as bad as it used to be.
 
Can this be made into a chart alert with sound instead of color? Maybe a bell for a bull alert and a ring for a bear alert? How's this?

Code:
# WalkingBallista EMA Lookback Cross
# https://usethinkscript.com/d/119-moving-average-crossover-watchlist-column-for-thinkorswim

declare lower;

input lookback = 5;
input ema1_len = 10;
input ema2_len = 20;
input averageType = AverageType.EXPONENTIAL;

def ema1 = MovAvgExponential(length=ema1_len);
def ema2 = MovAvgExponential(length=ema2_len);

def bull_cross = ema1 crosses above ema2;
def bear_cross = ema1 crosses below ema2;

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;

Alert(1, "Buy", Alert.BAR, Sound.BELL);
Alert(2, "Sell", Alert.BAR, Sound.RING);
 
Last edited:
Hi Ben. Do you know if there is a way to add to your script to give a 'Pre-Bullish' or a 'Pre-Bearish' alert on these crossovers? See the image below.

Many times I find that if the price action (candles body) crosses both of the EMA's, the averages generally do end up crossing several bars later. This would allow me to speculate ahead of time before the cross actually occurs. Here are the bars that I would like to get alerted on (see image below). I think it is just an addition to the script you have already created. Use 'green' which should be brighter than your dark_green setting to indicate a bullish candle stick body crossing both averages with the price above both averages. and 'red' which is brighter than the current dark_red which would indicate a pre-bearish candle stick body crossing both averages with the price being below both averages.

u5BfXyJl.jpg


Looking at the attached image, you can see that I (in the last 15 minutes) received a green cross over signal for SNAP and SHAK and a red cross over for NFLX. Do you know why the SNAP and NFLX signals came in late, while the SHAK came in great? I have lookback set at 1 for an hourly chart cross over.

xGEs5xr.png
 

Attachments

  • u5BfXyJl.jpg
    u5BfXyJl.jpg
    22.3 KB · Views: 232
  • xGEs5xr.png
    xGEs5xr.png
    121.3 KB · Views: 231
Last edited by a moderator:
@Dman There should be an option called Custom Quotes under your list of watchlist items. Select one of the custom quotes and paste the code in there.
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
246 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