Moving Average Crossover Watchlist Column for ThinkorSwim

I have tried and the code works in the watch list it just puts a 1 when true but the color helps and so does the alert. I am using the trend reversal indicator from ben but when I am scalping I like the confirmation of the close being above the 200ema on the 3min chart (seems to have worked well for me)
 

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

That's two separate pieces of code because you can't fire Alerts from a Watchlist Column...

Edited to add: I didn't think that Study could be used in a Watchlist Column...
 
@pga0008 Not tested but should work...

Ruby:
plot crossUp = if close crosses above ExpAverage("data" = CLOSE, "length" = 200) then 1 else 0;
crossUp.AssignValueColor(if crossUp then Color.GREEN else Color.CURRENT);
AssignBackgroundColor(if crossUp then Color.GREEN else Color.CURRENT);
 
Last edited:
Hi BenTen, hope you've been doing well in the markets! Quick question: how could I switch the following scanner column study to an sma8/sma20 cross instead of ema8/sma20 cross it currently is:

Code:
declare lower;

input lookback = 5;
input ema1_len = 8;
input sma1_len = 20;
input averageType = AverageType.EXPONENTIAL;

def ema1 = MovAvgExponential(length=ema1_len);
def sma1 = simpleMovingAvg(length=sma1_len);

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

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

I tried by doing the following and the results seemed a bit off:

Code:
declare lower;

input lookback = 5;
input sma1_len = 8;
input sma2_len = 20;

def sma1 = simpleMovingAvg(length=sma1_len);
def sma2 = simpleMovingAvg(length=sma2_len);

def bull_cross = sma1 crosses above sma2;
def bear_cross = sma1 crosses below sma2;

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

I have a hunch it had to do with removing the "input averageType = AverageType.EXPONENTIAL;" line of code as I was trying to do something similar on a chart study and it gave an error about removing a constant or something.

Let me know if there are some changes I could make.

Thanks!
 
Take a look at this example:

Code:
def ema1 = MovAvgExponential(length=ema1_len);
def sma1 = simpleMovingAvg(length=sma1_len);

MovAvgExponential = EMA
simpleMovingAvg = SMA

From the second script you posted, you have:

Code:
def sma1 = simpleMovingAvg(length=sma1_len);
def sma2 = simpleMovingAvg(length=sma2_len);

Both are using simple moving averages. Let's say you want sma1 to become an exponential moving average.

Code:
def sma1 = MovAvgExponential(length=sma1_len);

I hope that helps.
 
@Andygray8

sma8/sma20 cross instead of ema8/sma20

Oops, I did not see that. Use the code on the first page of this thread but change the following:

Code:
input averageType = AverageType.EXPONENTIAL;

to

Code:
input averageType = AverageType.SIMPLE;
 
I noticed on the mobile app where I have Bollinger Bands on the chart where midline Bollinger is the 20SMA all i had to do was add in an SMA study and make it 8 and on the chart it looked like exactly what I need. Now I am trying to get that same thing into scanner column format. I figured just using two SMAs would work but maybe it isn't that easy.
 
Oh wow I think that worked. Sounds like I was overthinking it. How can it be that just changing the first input to SIMPLE and leaving the ema references later on can work?

And as always, thanks BenTen you're the man!

Edit: Similar issue with the Chart Study but I don't think the fix is the same. I can post the error it gives me if you want:
 
@Vinny1993 Try this

Code:
# EMA Crossover Label
# BenTen at UseThinkScript.com

input price = close;
input length1 = 8;
input length2 = 34;
input displace = 0;

def AvgExp1 = ExpAverage(price[-displace], length1);
def AvgExp2 = ExpAverage(price[-displace], length2);
def UpSignal = AvgExp1 crosses above AvgExp2;
def DownSignal = AvgExp1 crosses below AvgExp2;

AddLabel(yes, if UpSignal then "Bullish Crossover" else if DownSignal then "Bearish Crossover" else "N/A");
 
Hi guys, I need a watchlist script for 9 SMA crosses 21 SMA with Multi-Time Frame 15 m, 30 m, 1h and 4h. When the indicators crossover in all time frame and it's bullish, the background color is green and when the indicators crossover and it's bearish, the background color is red.
Can someone help me? ... thanks!
 
Hey all, I’m having trouble with a code. I want to create a watchlist column for the close price in relation to a moving average. I’ve already created one for the 150 SMA but having trouble with the 40 EMA. It should be something along the lines of (CLOSE/40 EMA)*100, (times 100 to covert to percentage) but in TOS this is not working as easy for the exponential moving average.

ps, extra brownie points if you can color code for below 1 red above 1 green.
 
@Sticks-N-Bones As @rad14733 stated,
  • provide your code that you have that works for the 150 SMA so we can see exactly what you are looking for.
  • provide a screenshot of that watchlist column. (how to provide a screenshot)
  • And your attempted code for the 40 EMA.
It shouldn't be a problem finding what the issue is and we will make sure to get them color-coded also :)
 
Thank you guys for your responses. Hope this helps.

Code for 150 that works:
Code:
CLOSE/SimpleMovingAvg("length" = 150)."SMA"

Screenshot: 150 Screenshot Example

40 code attempt that will not work:
Code:
CLOSE/AvgExp = ExpAverage(price[-displace], length 40);

Ideally I would like them to be represented as "colors only" like the orb watchlist column (<1 RED, >1 GREEN)
 
@Sticks-N-Bones So..... You are looking for something like one of these columns?
3O3rKSo.png

Ruby:
# Code for the 1st column

def signal = close / MovAvgExponential("length" = 40)."AvgExp" *100 ;
AddLabel(yes, " ");
AssignBackgroundColor(if signal > 100 then color.green else color.red);

Ruby:
# code for the 2nd column

plot signal = close / MovAvgExponential("length" = 40)."AvgExp" *100 ;
AssignBackgroundColor(if signal > 100 then color.green else color.red);
HTH
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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