Moving Average Crossover Watchlist Column for ThinkorSwim

@wilmanv - The EMA is already in the code, written as "expaverage()". Unless you mean something else? To change the length of the EMA, adjust the input value. To display the watchlist column on different timeframes at the same time, it will be necessary to save multiple columns and change the aggregation of each to your choice using the built-in options.
 
Thanks! I was not able to get "plot" to work but I used "addlabel" and I now have what I need.
Could you share the final code please. I tried to modify but didn't seem to work this is what I had
Thanks! I was not able to get "plot" to work but I used "addlabel" and I now have what I need.
Could you please share the final code or show what I have wrong. I tried making the change to "addlabel" as well but did not work
Code:
#Watchlist Column
#displays green when in an uptrend, red when in a down trend and black when no trend apparent.



addlabel condition_up =ExpAverage(close[20],34) < ExpAverage(close[15],34)
and
ExpAverage(close[15],34) < ExpAverage(close[10],34)
and
ExpAverage(close[10],34) < ExpAverage(close[5],34)
and
ExpAverage(close[3],34) < ExpAverage(close,34);

addlabel condition_down =ExpAverage(close[20],34) > ExpAverage(close[15],34)
and
ExpAverage(close[15],34)> ExpAverage(close[10],34)
and
ExpAverage(close[10],34) > ExpAverage(close[5],34)
and
ExpAverage(close[3],34) > ExpAverage(close,34);

AssignBackgroundColor(if condition_up then color.green
                      else if condition_down then color.red
                      else color.gray);
# end code
 
@Andygray8 Your code works fine... Did you create a Custom Column in your Watchlist and add it to the Watchlist...??? Check out column test2...

1hXvIWx.jpg
Hi how can I make it so I can get color background for the RSI value like you have? Thanks
 
Hi how can I make it so I can get color background for the RSI value like you have? Thanks

Here you go...

Ruby:
def length = 14;
def ob = 70;
def os = 30;
def price = close;

def rsi = RSI("length" = length, "over bought" = ob, "over sold" = os, "price" = price, "average type" = AverageType.WILDERS)."RSI";

plot rsiValue = Round(rsi, 0);

AssignBackgroundColor(if rsiValue < os then color.dark_green else if rsiValue > ob then color.dark_red else color.dark_gray);
 
When on a specific time frame and lets say I choose 7 as my lookback and on the 7th bar back it crossed up but on the 2nd bar it's crossed down I want it to give me the results for the newest bar not the oldest bar but I still want to be able to look further back than 2 days. How can I write the code to do that? I want the latest results with no matter how far I look back. The way this code is written it gives the furthest results back first
 
Hi - amazing script, it does a lot for people, thank you for it and all you're doing to help people. I was intersted in asking a question. I promise I read the whole thread before posting.


Like several others I experienced the issue where the Column didn't show the "last" crossover and tended to stay Orange. I've made basic adjustments to the Lookback time but none seem to really pick up the last cross over.

the only modifications I made were:

input lookback = 96
input ema1_len = 13
input ema1_len = 25

but in the image displayed for MSFT you see the bar is green but the 13 EMA had crossed below the 25 EMA some time in the past (33 bars ago). I did read that the columns only update every 3 - 7 minutes. But I am wondering if I did something wrong here. Any help would be greatly appreciated.

The code:

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

declare lower;

input lookback = 96;
input ema1_len = 13;
input ema2_len = 25;
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);


zxR1ENw.jpg
 
@DelrayDad Why are you looking back 96 bars on a 15m chart...??? That's about 24 hours or just shy of 4 days worth of trading...
Apologies for my poor communication.

I moved it that far back trying to test it and see where I was going wrong. Why it wasnt changing color due to the most recent crossover. If the 13EMA crossed above the 25EMA 45 minutes ago, I want it to be green, until the 13EMA crosses back below the 25EMA.


Basically what I am seeing is the lookback goes all the way to the lookback number and uses that for its value, it doesnt see to use the most recent crossover. I'd like it to look back but use the most recent crossover as the value.
 
Last edited:
Hello traders. I have been looking for a while for a code that would indicate if a stock is above, below or inside the Ichimoku cloud for any time frame and tick charts serving as a watchlist column. If any of you guys know the code for it, or could point me in the right direction, it would be greatly appreciated it. Also, if there is no such code, would any of you guys be willing to take on this task? I think it would be a great tool to have. Thank you.
 
Last edited:
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);
Hi Ben,
For this script:

  • how can I replace the color with the price of the crossover of the ema?
  • For the lookback, it's looking back 5 days? or if I am on the 5 minute chart it's looking back the last 5 candles?
  • I can change input to EMA 13 and EMA 48, but it alot of the columns were orange.
  • I know it's impossible to to show when the emas crossover ahead of time. But is there a criteria that comes close like maybe look to the ema 12xema47?
 
Last edited by a moderator:
Hi Ben,
For this script:
  1. how can I replace the color with the price of the crossover of the ema?
  2. For the lookback, it's looking back 5 days? or if I am on the 5 minute chart it's looking back the last 5 candles?
  3. I can change input to EMA 13 and EMA 48, but it alot of the columns were orange.
  4. I know it's impossible to to show when the emas crossover ahead of time. But is there a criteria that comes close like maybe look to the ema 12xema47?
  1. The below script displays price where there is a crossover
  2. The script triggers if there is a crossover within the last 5 bars on the specified timeframe
  3. Yes, EMA10 will cross EMA20 more often then EMA13 / 48
  4. Feel free to play with the lengths in the never-ending quest to predict crossings. Report back your findings.
    0NVNSl6.png
Ruby:
# WalkingBallista EMA Lookback Cross
# https://usethinkscript.com/d/119-moving-average-crossover-watchlist-column-for-thinkorswim
# modified to display price @merryday 4/2022

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 within lookback bars;
def bear_cross = ema1 crosses below ema2 within lookback bars ;

plot price = if bull_cross or bear_cross then round(close,2) else 0;
AssignBackgroundCOlor(if bull_cross then Color.Green else if bear_cross then Color.Red else Color.Orange);
 
@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
Any way to change the 9 moving average to a weighted moving average while keeping the 50 as a simple moving average? I find that the weighted moving average crosses the 50 a bar or two earlier allowing a better entry.
 
is there a way to do a 3 ema cross(8,21,34) if all align to the upside green box if all align to the downside then red box???? thanks so much in advance
 
I use this in the MarketWatch on ToS
I am trying to add the following:
when the EMA8 crosses above the EMA 13 and EMA13 > EMA21 and EMA21 > EMA34; for Bullish
or
When the EMA8 crosses Below the EMA 13 and EMA13 < EMA21 and EMA21 < EMA34; for Bearish

Market-Watch.png

Code:
def EMA8 = ExpAverage(close, 8);
def EMA13 = ExpAverage(close, 13);
def EMA21 = ExpAverage(close, 21);
def EMA34 = ExpAverage(close, 34);

def bullish = EMA8 > EMA13 and EMA13 > EMA21 and EMA21 > EMA34;
def bearish = EMA8 < EMA13 and EMA13 < EMA21 and EMA21 < EMA34;

AddLabel(bullish, "Bullish Stacked MAs", color.black);
AddLabel(bearish, "Bearish StackedMAs", color.black);
AddLabel(!bullish and !bearish, " ", color.black);

AssignBackgroundColor(if bullish then color.green else if bearish then color.red else color.black);
 
I use this in the MarketWatch on ToS
I am trying to add the following:
when the EMA8 crosses above the EMA 13 and EMA13 > EMA21 and EMA21 > EMA34; for Bullish
or
When the EMA8 crosses Below the EMA 13 and EMA13 < EMA21 and EMA21 < EMA34; for Bearish
Ruby:
def EMA8 = ExpAverage(close, 8);
def EMA13 = ExpAverage(close, 13);
def EMA21 = ExpAverage(close, 21);
def EMA34 = ExpAverage(close, 34);

def bullish = EMA8 crosses above the EMA 13 and EMA13 > EMA21 and EMA21 > EMA34;
def bearish = EMA8 crosses Below the EMA 13 and EMA13 < EMA21 and EMA21 < EMA34;

AddLabel(bullish, "Bullish", color.black);
AddLabel(bearish, "Bearish", color.black);
AddLabel(!bullish and !bearish, " ", color.black);

AssignBackgroundColor(if bullish then color.green else if bearish then color.red else color.black);
RDDe8o0.png

n4tPd9V.png
 
Is there a way to add an alert to notify me when this is triggered?
I am using this in the MarketWatch of ToS.
Thank you for the Guidance. I had to make some slight changes to the code you posted, and it works great now. I used the following code:

Code:
def EMA8 = ExpAverage(close, 8);
def EMA13 = ExpAverage(close, 13);
def EMA21 = ExpAverage(close, 21);
def EMA34 = ExpAverage(close, 34);
def EMA55 = ExpAverage(close, 55);
def EMA89 = ExpAverage(close, 89);

def bullish = EMA8 CROSSES ABOVE EMA13 and EMA13 > EMA21 and EMA21 > EMA34 and EMA34 > EMA55 and EMA55 > EMA89;
def bearish = EMA8 CROSSES BELOW EMA13 and EMA13 < EMA21 and EMA21 < EMA34 and EMA34 < EMA55 and EMA55 < EMA89;
 

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