Stacked Moving Averages For ThinkOrSwim

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.
I moved your post here. You will find 5 pages discussing your query.
 
Hey everyone,

Trying to build a scan for stocks that are between 0.5%-4% above the from the 8EMA. I created this script, but I am bit confused on how to plot. I provided some context below to see if it can be of some help. The goal is to see if the stock is trending with 8 & 21 EMA(the closer the better). Trying to post a picture, but it is not letting me.

Thanks in advance!!

def p = absvalue((close-movAvgExponential(close, 8))/movavgExponential(close, 8));
@BenTen tried creating this script, but might be off here. Let me know if I am on the right track. Thanks
 
Last edited by a moderator:
Scan For Stacked Moving Averages Within x% of Close
This might resemble a member's request. I am not sure of it's value. Take it for a test ride and lets us know if it is useful.
Shared Chart Link: http://tos.mx/5HUgXnQ Click here for --> Easiest way to load shared links
5E7wPuJ.png

Shared Scanner Link: http://tos.mx/64MByE0
ixAMrWM.png

Script:
Ruby:
#Stacked MA scan
input diff = .004 ;
input length1 = 8;
input length2 = 21;
def avg1 = movAvgExponential(close, length1);
def avg2 = movAvgExponential(close, length2);
def stacked = avg1 >= avg2 ;
def pct1 = (close/avg1) - 1;
def pct2 = (close/avg2) - 1;

AssignPriceColor(
if stacked and between(pct1, -diff, diff) and between(pct2, -diff, diff) then color.violet else color.light_gray);

plot scan = stacked and between(pct1, -diff, diff) and between(pct2, -diff, diff) ;


@BenTen tried creating this script, but might be off here. Let me know if I am on the right track. Thanks
I actually have no idea of what you are asking but I took all the variables that you mentioned and put them in a script. Now that they are defined you can modify them as you wish.
 
Last edited:
@Partner I think you'll find that if you are going to get a big move that the averages will be properly stacked in order... I have been running an EMA Stack for a week or two and virtually all major moves have stacked MA's... Let's not complicate things needlessly...

Here is my EMA_Stack... Watch it and see if it will get you most of the way to where you want to be... The only thing I want to add is to know when the averages are squeezed/converged but I'm running my TTM_Squeeze_Clouds for that presently...

Ruby:
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";


AddLabel(yes, " Stacked ", if stackedUp then Color.GREEN else if stackedDn then Color.RED else Color.GRAY);

8|21|34|55|89 EMA Stack

ReGNbia.jpg
I might have set something wrong, but these dont look stacked to me.

 
I might have set something wrong, but these dont look stacked to me.

They are 'stacked' by the definition of the script. The script does not call for ALL ema to be greater. Just that immediate preceding one is greater. BTW, this is the industry standard definition of stacked.

You would need to add the additional dozen lines of statements for each ema to be greater than each of ALL the other EMAs.
 
They are 'stacked' by the definition of the script. The script does not call for ALL ema to be greater. Just that immediate preceding one is greater. BTW, this is the industry standard definition of stacked.

You would need to add the additional dozen lines of statements for each ema to be greater than each of ALL the other EMAs.
I have the staked ema label is there a war to aggregate the to show ema stacked on just the weekly time frame?
 
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);
On line 12 there is a " missing.
def stackedDn = MovAvgExponential("length" = 8)."AvgExp is less than MovAvgExponential("length" = 21)."AvgExp"
Should be:
def stackedDn = MovAvgExponential("length" = 8)."AvgExp" is less than MovAvgExponential("length" = 21)."AvgExp"
 
trying to get the addcloud function to plot cloud inside the ema's any help? thank you


input price = close;
input length1 = 8;
input length2 = 21;
input length3 = 34;
input length4 = 55;
input length5 = 89;
input displace = 0;
input showBreakoutSignals = no;

plot EMA8 = ExpAverage(price[-displace], length1);
plot EMA21 = ExpAverage(price[-displace], length2);
plot EMA34 = ExpAverage(price[-displace], length3);
plot EMA55 = ExpAverage(price[-displace], length4);
plot EMA89 = ExpAverage(price[-displace], length5);

EMA8.SetDefaultColor(GetColor(9));
EMA21.SetDefaultColor(GetColor(9));
EMA34.SetDefaultColor(GetColor(9));
EMA55.SetDefaultColor(GetColor(9));
EMA89.SetDefaultColor(GetColor(9));

EMA8.hide();
EMA34.hide();
EMA55.hide();
EMA89.hide();

#stacked EMA's


def trend = (if (EMA8 > EMA21 and EMA21 > EMA34 and EMA34 > EMA55 and EMA55 > EMA89) then 1 else
if (EMA8 < EMA21 and EMA21 < EMA34 and EMA34 < EMA55 and EMA55 < EMA89) then 3 else 0);

EMA8.AssignValueColor (if trend == 3 then color.red else if trend == 1 then color.green else color.current);
EMA21.AssignValueColor (if trend == 3 then color.red else if trend == 1 then color.green else color.yellow);
EMA34.AssignValueColor (if trend == 3 then color.red else if trend == 1 then color.green else color.current);
EMA55.AssignValueColor (if trend == 3 then color.red else if trend == 1 then color.green else color.current);
EMA89.AssignValueColor (if trend == 3 then color.red else if trend == 1 then color.green else color.current);


AddCloud( (EMA8 > EMA21 and EMA21 > EMA34 and EMA34 > EMA55 and EMA55 > EMA89), EMA8 < EMA21 and EMA21 < EMA34 and EMA34 < EMA55 and EMA55 < EMA89, Color.GREEN, Color.RED);
 
Last edited by a moderator:
trying to get the addcloud function to plot cloud inside the ema's any help? thank you

Maybe this is what you are looking for. I was just editing a code I found to do the same thing, and stumbled upon your post.

mp6OEq1.png


Code:
input price = close;
input length1 = 8;
input length2 = 21;
input length3 = 34;
input length4 = 55;
input length5 = 89;
input displace = 0;
input showBreakoutSignals = no;

plot EMA8 = ExpAverage(price[-displace], length1);
plot EMA21 = ExpAverage(price[-displace], length2);
plot EMA34 = ExpAverage(price[-displace], length3);
plot EMA55 = ExpAverage(price[-displace], length4);
plot EMA89 = ExpAverage(price[-displace], length5);

EMA8.SetDefaultColor(GetColor(9));
EMA21.SetDefaultColor(GetColor(9));
EMA34.SetDefaultColor(GetColor(9));
EMA55.SetDefaultColor(GetColor(9));
EMA89.SetDefaultColor(GetColor(9));

EMA8.hide();
EMA34.hide();
EMA55.hide();
EMA89.hide();

#stacked EMA's


def trend = (if (EMA8 > EMA21 and EMA21 > EMA34 and EMA34 > EMA55 and EMA55 > EMA89) then 1 else
if (EMA8 < EMA21 and EMA21 < EMA34 and EMA34 < EMA55 and EMA55 < EMA89) then 3 else 0);

EMA8.AssignValueColor (if trend == 3 then color.red else if trend == 1 then color.green else color.current);
EMA21.AssignValueColor (if trend == 3 then color.red else if trend == 1 then color.green else color.yellow);
EMA34.AssignValueColor (if trend == 3 then color.red else if trend == 1 then color.green else color.current);
EMA55.AssignValueColor (if trend == 3 then color.red else if trend == 1 then color.green else color.current);
EMA89.AssignValueColor (if trend == 3 then color.red else if trend == 1 then color.green else color.current);

AddCloud( EMA8, EMA21, Color.Green, Color.Red );
AddCloud( EMA21, EMA34, Color.Green, Color.Red );
AddCloud( EMA34, EMA55, Color.Green, Color.Red );
AddCloud( EMA55, EMA89, Color.Green, Color.red );
 
Last edited by a moderator:
Scan For Stacked Moving Averages Within x% of Close
This might resemble a member's request. I am not sure of it's value. Take it for a test ride and lets us know if it is useful.
Shared Chart Link: http://tos.mx/5HUgXnQ Click here for --> Easiest way to load shared links
5E7wPuJ.png

Shared Scanner Link: http://tos.mx/64MByE0
ixAMrWM.png

Script:
Ruby:
#Stacked MA scan
input diff = .004 ;
input length1 = 8;
input length2 = 21;
def avg1 = movAvgExponential(close, length1);
def avg2 = movAvgExponential(close, length2);
def stacked = avg1 >= avg2 ;
def pct1 = (close/avg1) - 1;
def pct2 = (close/avg2) - 1;

AssignPriceColor(
if stacked and between(pct1, -diff, diff) and between(pct2, -diff, diff) then color.violet else color.light_gray);

plot scan = stacked and between(pct1, -diff, diff) and between(pct2, -diff, diff) ;



I actually have no idea of what you are asking but I took all the variables that you mentioned and put them in a script. Now that they are defined you can modify them as you wish.
Thank you and it is really helpful. meantime, I am trying to make the code work for 3 EMAs. 9, 21 &50. When they are very close to each other then they usually explode when crossover happens. This could happen in either direction. Can you help me with scan please? Thanks in advance.
 
Thank you and it is really helpful. meantime, I am trying to make the code work for 3 EMAs. 9, 21 &50. When they are very close to each other then they usually explode when crossover happens. This could happen in either direction. Can you help me with scan please? Thanks in advance.
Sorry, can't help.
ThinkScript requires that you define "very close to each other" in mathematical terms.
 
DefineGlobalColor("StackedPositive", Color.GREEN);
DefineGlobalColor("StackedNegative", Color.RED);
DefineGlobalColor("Warning", Color.LIGHT_ORANGE);

def SP = ema8 > ema34 and ema34 > EMA144;
def SN = ema8 < ema34 and ema34 < EMA144;
def SPW = ema8 < ema34 and ema34 > EMA144 ;
def SNW = ema8 > ema34 and ema34 < EMA144;

[{if
AddLabel (yes,("BUY"), if SP then GlobalColor("StackedPositive") else if SN then GlobalColor("StackedNegative");}
{else if
AddLabel (yes,("SELL"), SN then GlobalColor("StackedNegative") else if SP then GlobalColor("StackedPositive"));
{then} =
addLabel (yes, ("Warning") if (SPW or SNW) then GlobalColor("Warning"));]

Trying to make a label that changes on the top of my screen if the emas cross in a the above ways but i don't know if this is even possible
 
DefineGlobalColor("StackedPositive", Color.GREEN);
DefineGlobalColor("StackedNegative", Color.RED);
DefineGlobalColor("Warning", Color.LIGHT_ORANGE);

def SP = ema8 > ema34 and ema34 > EMA144;
def SN = ema8 < ema34 and ema34 < EMA144;
def SPW = ema8 < ema34 and ema34 > EMA144 ;
def SNW = ema8 > ema34 and ema34 < EMA144;

[{if
AddLabel (yes,("BUY"), if SP then GlobalColor("StackedPositive") else if SN then GlobalColor("StackedNegative");}
{else if
AddLabel (yes,("SELL"), SN then GlobalColor("StackedNegative") else if SP then GlobalColor("StackedPositive"));
{then} =
addLabel (yes, ("Warning") if (SPW or SNW) then GlobalColor("Warning"));]

Trying to make a label that changes on the top of my screen if the emas cross in a the above ways but i don't know if this is even possible

can't put output functions within if then.
put if thens , inside 1 label.




Code:
DefineGlobalColor("StackedPositive", Color.GREEN);
DefineGlobalColor("StackedNegative", Color.RED);
DefineGlobalColor("Warning", Color.LIGHT_ORANGE);

def SP = ema8 > ema34 and ema34 > EMA144;
def SN = ema8 < ema34 and ema34 < EMA144;
def SPW = ema8 < ema34 and ema34 > EMA144 ;
def SNW = ema8 > ema34 and ema34 < EMA144;

AddLabel(
(sp or sn or spw or snw),

(if sp then "buy"
else if sn then "sell"
else if (SPW or SNW) then "Warning"
else "-"),

(if SP then GlobalColor("StackedPositive") 
else if SN then GlobalColor("StackedNegative")
else if (SPW or SNW) then GlobalColor("Warning")
else color.gray)
);
 
confused...which one to use out of all these code snippets? Thanks
There is no "best version" in this thread. ThinkScript allows us to customize many flavors to fit our style of trading.
# # # # #
The way to learn about each of these scripts is to apply them. Recommend you put the various versions on your chart. Nothing beats personal experience ;) The only way you will know what the plots are doing, is to observe them on your charts and how they inter-relate with your other indicators.

Most indicators need tweaking depending on the timeframe, the market, the ticker.

No one can possibly know what tweaks will work best for you. It is up to you to play with the settings and see how the indicator line up w/ your strategy and with your other indicators.

To determine if these scripts bring value, analyze them over different timeframes, across history and with multiple instruments.
# # # # #
 
I want to create a scanner where SMA50 < SMA200
SMA50 50<SMA200 where SMA50 is below SMA 200 only 5%

Can you help me with this?
 
I want to create a scanner where SMA50 < SMA200
SMA50 50<SMA200 where SMA50 is below SMA 200 only 5%

Can you help me with this?


5% of what?

do you want to search for ,
if sma50 is in between 2 prices?

to find a price 5% lower than a number, multiply it by 0.95.

is this what you want to search for?
sma50 is < sma 200 and
sma50 > (sma200 * 0.95)


Code:
input avg1_type = AverageType.SIMPLE;
input avg1_price = close;
input avg1_len = 50;
def ma1 = MovingAverage(avg1_type, avg1_price, avg1_len);

input avg2_type = AverageType.SIMPLE;
input avg2_price = close;
input avg2_len = 200;
def ma2 = MovingAverage(avg2_type, avg2_price, avg2_len);

input percent_gap = 5;

plot z = ma1 < ma2 and ma1 >
 (ma2 * (1 - (percent_gap/100)));
 

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