Stacked Moving Averages For ThinkOrSwim

@abestinkin8 That 'looks' exactly right. That is the exact logic that I explained to you. To make a stacked EMA. The moving averages have to be daisy-chained together. Your original post was missing the averages that connected the last one w/ the next one.

The logic seems right. I am not in front of my app so I can't check for syntax errors. Give it a roll. Congrats!
 

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

I did some more work and came up with a second watchlist column that would be green/red depending on whether the EMA5 and EMA89 were within a certain percentage of one another. (Because the scripts would be stacked, I think that those would be the only 2 that matter).

The following script lights up Dark Green if EMA5 and EMA 89 are within .1 of each other. It lights up Dark Red if they are more than 1 of teacher other. It's Gray the rest of the time. I'm not exactly sure what the .1 / 1 measure, but it seems to track where the closer the number is to 0, the more consolidated the trend lines become. I'm posting this in the hopes that other people might shed light on what the numbers mean, and if there are better metrics to use than .1 / 1.

Code:
input lookback = 5; # past N minutes

Def a = MovAvgExponential(“length” = 5);
Def b = MovAvgExponential(“length” = 89);
Def d = a-b;
Plot p = (d / b) * 100;

Def Good = p <= .1 && p >= -.1;
Def Bad1 = p >= 1;
Def Bad2 = p <= -1;

AssignBackgroundColor(if Good then color.darK_GREEN
else if Bad1 then color.dark_RED else if Bad2 then color.dark_RED
else color.gray);
 
Last edited by a moderator:
here is my EMA_x5_Stack script...

Ruby:
#EMA_x5_Stack
#Created by rad14733 for usethinkscript.com
#v1.0 2021-01-02
#NOTE: EMA crossovers can be set to only paint based on long term trends.
#      When configured, EMA crossovers will only paint when filtered by longer term trends (ema1 <> ema5).
# EMA Length's
input ema1Length = 8;
input ema2Length = 21;
input ema3Length = 34;
input ema4Length = 55;
input ema5Length = 89;
input showXovers = yes;

# Display EMA's Stacked Label
input show_stacked_label = yes;
input show_ema_label = yes;

# Conditionally Plot EMA's
plot ema1 = ExpAverage(close, ema1Length);
ema1.SetPaintingStrategy(PaintingStrategy.LINE);
ema1.SetStyle(Curve.FIRM);
ema1.SetLineWeight(1);
ema1.SetDefaultColor(Color.GREEN);
plot ema2 = ExpAverage(close, ema2Length);
ema2.SetPaintingStrategy(PaintingStrategy.LINE);
ema2.SetStyle(Curve.FIRM);
ema2.SetLineWeight(1);
ema2.SetDefaultColor(Color.YELLOW);
plot ema3 = ExpAverage(close, ema3Length);
ema3.SetPaintingStrategy(PaintingStrategy.LINE);
ema3.SetStyle(Curve.FIRM);
ema3.SetLineWeight(1);
ema3.SetDefaultColor(Color.RED);
plot ema4 = ExpAverage(close, ema4Length);
ema4.SetPaintingStrategy(PaintingStrategy.LINE);
ema4.SetStyle(Curve.FIRM);
ema4.SetLineWeight(1);
ema4.SetDefaultColor(Color.MAGENTA);
plot ema5 = ExpAverage(close, ema5Length);
ema5.SetPaintingStrategy(PaintingStrategy.LINE);
ema5.SetStyle(Curve.FIRM);
ema5.SetLineWeight(1);
ema5.SetDefaultColor(Color.BLUE);

# Calculate EMA Crossovers
plot x1a2 = showXovers and ema1 crosses above ema2;
x1a2.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
x1a2.SetStyle(Curve.FIRM);
x1a2.SetLineWeight(3);
x1a2.SetDefaultColor(Color.YELLOW);
plot x1b2 = showXovers and ema1 crosses below ema2;
x1b2.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
x1b2.SetStyle(Curve.FIRM);
x1b2.SetLineWeight(3);
x1b2.SetDefaultColor(Color.DARK_ORANGE);
# Define Global Colors For Clouds
# Crosses Above
DefineGlobalCOlor("x1a2", Color.GREEN);
DefineGlobalCOlor("x2a3", Color.DARK_GREEN);
DefineGlobalCOlor("x3a4", Color.YELLOW);
DefineGlobalCOlor("x4a5", Color.WHITE);
# Crosses Below
DefineGlobalCOlor("x1b2", Color.RED);
DefineGlobalCOlor("x2b3", Color.DARK_RED);
DefineGlobalCOlor("x3b4", Color.MAGENTA);
DefineGlobalCOlor("x4b5", Color.BLUE);

# Paint Clouds
input showClouds = yes;
AddCloud(if showClouds then ema1 else Double.NaN, if showClouds then ema2 else Double.NaN, GlobalColor("x1a2"), GlobalColor("x1b2"));
AddCloud(ema2, ema3, GlobalColor("x2a3"), GlobalColor("x2b3"));
AddCloud(ema3, ema4, GlobalColor("x3a4"), GlobalColor("x3b4"));
AddCloud(ema4, ema5, GlobalColor("x4a5"), GlobalColor("x4b5"));

# Create Chart Label to signify that EMA's are Stacked
def stackedUp = ema1 > ema2 and ema2 > ema3 and ema3 > ema4 and ema4 > ema5;
def stackedDn = ema1 < ema2 and ema2 < ema3 and ema3 < ema4 and ema4 < ema5;
AddLabel(show_ema_label, "" + ema1Length + "|" + ema2Length + "|" + ema3Length + "|" + ema4Length + "|" + ema5Length + "|" + "EMAs", if stackedUp then Color.GREEN else if stackedDn then Color.RED else Color.GRAY);
AddLabel(show_stacked_label, "EMAs Stacked", if stackedUp then Color.GREEN else if stackedDn then Color.RED else Color.GRAY);
# END EMA_5_Stack
 
Last edited by a moderator:
This is a simple plot on a lower study showing the color condition of short term EMAs.
However, I do not know the code to keep it from plotting cyan dots into the expansion area.
Can someone correct it for me?
Thanks for any reply!

###
Code:
declare lower;
def EMA5 = ExpAverage(close, 5);
def EMA8 = ExpAverage(close, 8);
def EMA13 = ExpAverage(close, 13);
def cond1 = (EMA5 > EMA8) and (EMA8 > EMA13);
def cond2 = (EMA5 < EMA8) and (EMA8 < EMA13);

plot dot = 1;
dot.SetStyle(Curve.POINTS);
dot.SetLineWeight(5);
dot.AssignValueColor(if cond1 then Color.GREEN
else if cond2 then Color.RED
else color.YELLOW);
 
Last edited by a moderator:
Can someone help me with making this code into a label? The text on the label can be the number 5. The label should turn colors base on the criteria below.

input price = close;

AddLabel (yes, if ma5 >= ma5[1] and ma5>ma100
then color.Green else if ma5 < ma5[1] and ma5>ma100
then color.Blue else if ma5<= ma5[1] and ma5 <ma100
then color.red else if ma5 >= ma5[1] and ma5 < ma100
then color.Blue else color.gray);
 
@Branch Something like this...??? You were very close... Note that I didn't verify your logic...

Ruby:
def ma5 = Average(close, 5);
def ma100 = Average(close, 100);

AddLabel (yes, "MA5",
  if ma5 >= ma5[1] and ma5 > ma100 then Color.GREEN
  else if ma5 < ma5[1] and ma5 > ma100 then Color.BLUE
  else if ma5 <= ma5[1] and ma5 < ma100 then Color.RED
  else if ma5 >= ma5[1] and ma5 < ma100 then Color.BLUE
  else Color.GRAY
);
 
New to the forum, I'm looking to implement a scan to filter only instruments where the moving averages are above each other, meaning 20EMA>40EMA>50SMA... (uptrend) and use in ToS to create scans in different timeframes, 1D, 1W, 1M.....

Appreciate if someone can point me in the right direction, perhaps a similar script original script already exists and I can adapt ?....not an expert by any means but I can handle modifications

thanks!
 
New to the forum, I'm looking to implement a scan to filter only instruments where the moving averages are above each other, meaning 20EMA>40EMA>50SMA... (uptrend) and use in ToS to create scans in different timeframes, 1D, 1W, 1M.....

Appreciate if someone can point me in the right direction, perhaps a similar script original script already exists and I can adapt ?....not an expert by any means but I can handle modifications

thanks!
This should help
Ruby:
def ema20 = ExpAverage(close, 20);
def ema40 = ExpAverage(close, 40);
def sma50 = SimpleMovingAvg(close, 50);
plot scan = ema20 > ema40 and ema40 > sma50;
 
Thanks, found out the ToS stock hacker/condition wizard allows building "stacked" filters...this filters securities with all classic moving averages aligned on top of each other...

the GUI

the code
MovAvgExponential("length" = 20)."AvgExp" is greater than or equal to MovAvgExponential("length" = 40)."AvgExp" and MovAvgExponential("length" = 40)."AvgExp" is greater than or equal to SimpleMovingAvg("length" = 50)."SMA" and SimpleMovingAvg("length" = 50)."SMA" is greater than or equal to SimpleMovingAvg("length" = 100)."SMA" and SimpleMovingAvg("length" = 100)."SMA" is greater than or equal to SimpleMovingAvg("length" = 150)."SMA" and SimpleMovingAvg("length" = 150)."SMA" is greater than or equal to SimpleMovingAvg("length" = 200)."SMA"
 
I'm not a coder, (and have only been trading for 3 weeks) but this change made more sense to me... I also commented out "AddLabel(!bullish and !bearish, " ", Color.BLACK);" because I didn't know what it did (don't know the TOS syntax yet).

I assume the black color is for a neutral position. Does the "!" mean "Not"... so the code would read NOT bullish AND NOT bearish then have no text and paint the box black Seemed like too much code, so I took it out.

Again, I last coded 35 years ago, so be nice :)

PS: Thanks for everyone writing all this fantastic code. I really appreciate you guys/gals :)

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;

# Changed "Stacked MAs" to "Stacked EMAs" when they actually stacked and to "Unstacked EMAs" when they are not stacked.
# Now there is visual and written confirmation (change "Unstacked" to "Crossed" if you wish)
# Also commented out the background color change so the entire screen didn't get changed

AddLabel(bullish, “Stacked EMAs”, Color.GREEN);
AddLabel(bearish, “UnStacked EMAs”, Color.RED);

#AddLabel(!bullish and !bearish, " ", Color.BLACK); Is this needed?

#AssignbackgroundColor(if EMA8 > EMA13 and EMA13 > EMA21 and EMA21 > EMA34 then color.green else if EMA8 < EMA13 and EMA13 < EMA21 and EMA21 < EMA34 then color.red else color.black);
 
Last edited:
@Some Random Alien
  • Yes the explanation mark ! denotes NOT in Thinkscript (y)
  • Yes the last two lines of the script should be commented out if adding this study as labels to a chart
However, do not comment out the lines when using the script in a watchlist.
Adding the AssignbackgroundColor makes the labels into a stoplight that pops!
a1.png
 
Last edited:
I created this code to account for John Carter's EMA setup. NOT tested. Noob... don't know how to back test yet.

CHANGELOG:
1. Changed Plum to Yellow (easier to read and Yellow means caution, duh)
2. Added EMA 55 and 89, ala JC. Hope you find thus useful :)
3. Added padding to text displayed in boxes (I hate crowded text :) )
4. Added a catch-all in case the lines are not completely Bull or Bear (Changing EMAs)


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

#---------Below is John Carter EMA setup-------------
def EMA55 = ExpAverage(close, 55);
def EMA89 = ExpAverage(close, 89);
#----------------------------------------------------

def bullish = EMA8 > EMA13 and EMA13 > EMA21 and EMA21 > EMA34 and EMA55 > EMA89;

def bearish = EMA8 < EMA13 and EMA13 < EMA21 and EMA21 < EMA34 and EMA55 < EMA89;

AddLabel(bullish, “  Stacked EMAs  ”, Color.GREEN);
AddLabel(bearish, “  UnStacked EMAs  ”, Color.RED);
AddLabel(!bullish and !bearish, "  Changing EMAs  ", Color.YELLOW);

#AssignbackgroundColor(if EMA8 > EMA13 and EMA13 > EMA21 and EMA21 > EMA34 then color.green else if EMA8 < EMA13 and EMA13 < EMA21 and EMA21 < EMA34 then color.red else color.black);
 
Here is my actual Watchlist Column code... It's almost identical to the code above... I just use colors without text because I need the screen real estate for other things... Green = StackedUp, Red = StackedDown, Gray = NotStacked...

Ruby:
#EMA_Stack
#Used to indicate that 8, 21, 34, 55, 89  EMA's are ALL stacked trend-wise
#Created by rad14733 for usethinkscript.com
#v1.0 2021-01-02

def stackedUp = MovAvgExponential("length" = 8)."AvgExp" is greater than MovAvgExponential("length" = 21)."AvgExp"
and MovAvgExponential("length" = 21)."AvgExp" is greater than MovAvgExponential("length" = 34)."AvgExp"
and MovAvgExponential("length" = 34)."AvgExp" is greater than MovAvgExponential("length" = 55)."AvgExp"
and MovAvgExponential("length" = 55)."AvgExp" is greater than MovAvgExponential("length" = 89)."AvgExp";


def stackedDn = MovAvgExponential("length" = 8)."AvgExp is less than MovAvgExponential("length" = 21)."AvgExp"
and MovAvgExponential("length" = 21)."AvgExp" is less than MovAvgExponential("length" = 34)."AvgExp"
and MovAvgExponential("length" = 34)."AvgExp" is less than MovAvgExponential("length" = 55)."AvgExp"
and MovAvgExponential("length" = 55)."AvgExp" is less than MovAvgExponential("length" = 89)."AvgExp";

def state = if stackedUp then 1 else if stackedDn then -1 else 0;

AddLabel(yes, state, if stackedUp then Color.DARK_GREEN else if stackedDn then Color.DARK_RED else Color.DARK_GRAY);

AssignBackgroundColor(if stackedUp then Color.DARK_GREEN else if stackedDn then Color.DARK_RED else Color.DARK_GRAY);

Hello, this watchlist study is very useful . I have it plotted on 3 different time frames: T1<T2<T3 .
What lines should we add to have a single watchlist study that displays a green / red only when all 3 timeframes
are stackedup / stackeddn please ? Hope it's an usefull idea for others, too.
(optional - white when T1 and T2 have same direction ;
grey when T2 and T3 have same direction).
 
Hello, this watchlist study is very useful . I have it plotted on 3 different time frames: T1<T2<T3 .
What lines should we add to have a single watchlist study that displays a green / red only when all 3 timeframes
are stackedup / stackeddn please ? Hope it's an usefull idea for others, too.
(optional - white when T1 and T2 have same direction ;
grey when T2 and T3 have same direction).
Multiple aggregations can't be used in watchlists :(
 
okkkk...so I REDID it...turns out.. and you all can correct me if I'm wrong...but it looks like thinkscript does not like to make serial calculations...so I had to split up the comparisons of the lows as well as the moving averages...

Looks something like this

def run=movAvgExponential() is greater than MovAvgExponential(length = 50) and MovAvgExponential(length = 50) is greater than MovAvgExponential(length = 200);
def runs=open is greater than low[1] and low [1] is greater than low[2] and low[2] is greater than low[3];
def grow=open>open[8]*1.05;
plot scan=run and runs;

works pretty good!!


Actually...I feel like my other Studies have been working the other way, but now I wonder...Any input would be appreciated.
 
okkkk...so I REDID it...turns out.. and you all can correct me if I'm wrong...but it looks like thinkscript does not like to make serial calculations...so I had to split up the comparisons of the lows as well as the moving averages...

Looks something like this

def run=movAvgExponential() is greater than MovAvgExponential(length = 50) and MovAvgExponential(length = 50) is greater than MovAvgExponential(length = 200);
def runs=open is greater than low[1] and low [1] is greater than low[2] and low[2] is greater than low[3];
def grow=open>open[8]*1.05;
plot scan=run and runs;

works pretty good!!


Actually...I feel like my other Studies have been working the other way, but now I wonder...Any input would be appreciated.
looks MUCH better! I moved your post to this thread. Read through it for even more ideas.
 
Where can I find the updated version of the label "stack" for EMAs ?

Does any one have chart label of EMA (8, 21, 50, 200) of Daily chart that show UP on 5 min chart??
 
Where can I find the updated version of the label "stack" for EMAs ?

Does any one have chart label of EMA (8, 21, 50, 200) of Daily chart that show UP on 5 min chart??
I moved your post here as there are 4 pages of stacked averages examples.
  1. choose one
  2. change the lengths to meet your requirements
  3. make it a daily aggregation that will plot on a 5min chart, follow these instructions: https://usethinkscript.com/threads/...imeframe-mtf-a-tutorial-for-thinkorswim.8050/
 
Hello. I tried out the DailySMA but I need the EMA. Is there a study out there for this already? if not, should i attempt to modify the DailySMA script to be the DailyEMA? I have never written a script but I'm willing to learn. Would it be easier to modify or start from scratch?

The end goal: Daily: 20 EMA, 100 SMA & 200 SMA all plotted on my 1min, 5min & tick charts. I assume I can use/apply 2 different scripts to my charts. One being the DailySMA currently found in the studies and the other the "DailyEMA" if i can find one.

Thank You, John.
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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