# Stacked & Diverging Moving Averages for [n] Consecutive Bars For ThinkOrSwim

#### BiggySmall

##### New member
Tried cobbling and editing a bunch of scripts together but I'm not having any luck getting a good scan.
Can someone help or direct me to some code for a scan for 3 moving averages where...
The 9/21/34 ema's are stacked higher than each other, AND ALSO
the 9ema is widening/spreading/diverging from the 21ema, and the 21ema is widening/spreading/diverging from the 34ema.
All of this happening for the past 5 consecutive bars.
Thanks!

Tried cobbling and editing a bunch of scripts together but I'm not having any luck getting a good scan.
Can someone help or direct me to some code for a scan for 3 moving averages where...
The 9/21/34 ema's are stacked higher than each other, AND ALSO
the 9ema is widening/spreading/diverging from the 21ema, and the 21ema is widening/spreading/diverging from the 34ema.
All of this happening for the past 5 consecutive bars.
Thanks!

you made a good list of what you want to have happen.
next thing is to think about the steps that the code will follow.

start by making an upper chart study. this way you will have something to look at and visually verify your formulas are working.
when that works, change the plots to work for a column and a scan.

you want stacked emas and increasing line gaps.
if gaps are checked for multiple previous bars, the stack only needs to be checked once.

to check if 2 lines are getting farther apart, compare their current gap to the gap on previous bar. you have 3 lines, so there will be 2 gaps to check. make the bar to bar gap comparison be a boolean, true/false formula. this way later, you can sum a group of those values and compare that to a number, the quantity of bars back.

upper chart test study

Ruby:
``````# stacked_avg_widening_upper_0

# upper study

# make an upper to test
# 3 emas , 9 / 21 / 34
# this assumes ma1 has the shortest length, and ma3 has the longest.

def bn = barnumber();
def na = double.nan;

input price = close;
input avg_type =  AverageType.EXPONENTIAL;

input avg1_len = 9;
def ma1 = MovingAverage(avg_type, price, avg1_len);
input avg2_len = 21;
def ma2 = MovingAverage(avg_type, price, avg2_len);
input avg3_len = 34;
def ma3 = MovingAverage(avg_type, price, avg3_len);

def stackedup = (ma1 > ma2 and ma2 > ma3);

# line gaps
def gap1 = (ma1 - ma2);
def gap2 = (ma2 - ma3);

# are the gaps increasing?  true or false, 1 or 0
def gap1chg = (gap1 > gap1[1]);
def gap2chg = (gap2 > gap2[1]);

input bars_increasing = 5;
def barz = bars_increasing - 1;

# add up some previous gap changes. are all of them true?
# add up 4 boolean values. if = 4, then all are true
def gap1x = (Sum(gap1chg, barz) == barz);
def gap2x = (Sum(gap2chg, barz) == barz);

def up_stack_widen = (stackedup and gap1x and gap2x);
def up_stack_widenbn = if bn == 1 then 0 else if up_stack_widen then bn else up_stack_widenbn[1];
def up_sw_diff = bn - up_stack_widenbn;

# -----------------------
# outputs

addlabel(1, "bars ago: " + up_sw_diff, color.yellow);

input show_lines = yes;
plot z1 = if show_lines then ma1 else na;
z1.SetDefaultColor(Color.CYAN);
z1.HideBubble();
plot z2 = if show_lines then ma2 else na;
z2.SetDefaultColor(Color.YELLOW);
z2.HideBubble();
plot z3 = if show_lines then ma3 else na;
z3.SetDefaultColor(Color.green);
z3.HideBubble();

#``````

upper study, draw a vertical line on each true condition
lower/scan study below

-------------------------------------

column study
zstk_wide
http://tos.mx/WaxFmib

lists how many bars ago was the latest true condition, widening stack.
it adds leading 0's to the numbers so the text column sorts correctly

Ruby:
``````# zstk_wide

# 15min , column
# stacked_avg_widening_upper_0

#3 emas , 9 / 21 / 34
def bn = barnumber();
def na = double.nan;

input price = close;
input avg_type =  AverageType.EXPONENTIAL;

input avg1_len = 9;
def ma1 = MovingAverage(avg_type, price, avg1_len);
input avg2_len = 21;
def ma2 = MovingAverage(avg_type, price, avg2_len);
input avg3_len = 34;
def ma3 = MovingAverage(avg_type, price, avg3_len);

def stackedup = (ma1 > ma2 and ma2 > ma3);

def gap1 = (ma1 - ma2);
def gap2 = (ma2 - ma3);

# are the gaps increasing?  true or false, 1 or 0
def gap1chg = (gap1 > gap1[1]);
def gap2chg = (gap2 > gap2[1]);

input bars_increasing = 5;
def barz = bars_increasing - 1;

# add up some previous gap changes. are all of them true?
# add up 4 boolean values. if = 4, then all are true
def gap1x = (Sum(gap1chg, barz) == barz);
def gap2x = (Sum(gap2chg, barz) == barz);

def up_stack_widen = (stackedup and gap1x and gap2x);
def up_stack_widenbn = if bn == 1 then 0 else if up_stack_widen then bn else up_stack_widenbn[1];
def up_sw_diff = bn - up_stack_widenbn;

# -----------------------
# outputs

def x = up_sw_diff;

( if x < 10 then "00" else if x < 100 then "0" else "") + x
, color.white);

addlabel(0, "bars ago: " + up_sw_diff, color.yellow);
#``````

lists how many bars ago was a true condition

-------------------------------------

scan / lower study
1 true/false plot

Ruby:
``````# stacked_avg_widening_scan_0

# scan/lower study
declare lower;

#3 emas , 9 / 21 / 34

def bn = barnumber();
def na = double.nan;

input price = close;
input avg_type =  AverageType.EXPONENTIAL;

input avg1_len = 9;
def ma1 = MovingAverage(avg_type, price, avg1_len);
input avg2_len = 21;
def ma2 = MovingAverage(avg_type, price, avg2_len);
input avg3_len = 34;
def ma3 = MovingAverage(avg_type, price, avg3_len);

def stackedup = (ma1 > ma2 and ma2 > ma3);

def gap1 = (ma1 - ma2);
def gap2 = (ma2 - ma3);

# are the gaps increasing?  true or false, 1 or 0
def gap1chg = (gap1 > gap1[1]);
def gap2chg = (gap2 > gap2[1]);

input bars_increasing = 5;
def barz = bars_increasing - 1;

# add up some previous gap changes. are all of them true?
# add up 4 boolean values. if = 4, then all are true
def gap1x = (Sum(gap1chg, barz) == barz);
def gap2x = (Sum(gap2chg, barz) == barz);

def up_stack_widen = (stackedup and gap1x and gap2x);
#def up_stack_widenbn = if bn == 1 then 0 else if up_stack_widen then bn else up_stack_widenbn[1];
#def up_sw_diff = bn - up_stack_widenbn;

plot z = up_stack_widen;
#``````

Spectacular! Scan results are exactly what I was looking for!
Thanks for taking the time to fully explain, and write the code. You went far above the answer I was hoping for.

@halcyonguy
Can someone turn this into a Points Painting Strategy? I want to use this as a lower chart study.

Last edited by a moderator:
@halcyonguy
this is great, how can we add same principles t the short side?

@halcyonguy
Can someone turn this into a Points Painting Strategy? I want to use this as a lower chart study.

@halcyonguy
this is great, how can we add same principles t the short side?

here is an upper that shows red lines for down moves

Code:
``````# stacked_avg_widening_upper_01

# upper study

# stacked & Diverging Moving Averages for [n] Consecutive Bars
# BiggySmall  6/3

#Tried cobbling and editing a bunch of scripts together but I'm not having any luck getting a good scan.
#Can someone help or direct me to some code for a scan for 3 moving averages where...
#The 9/21/34 ema's are stacked higher than each other, AND ALSO
#the 9ema is widening/spreading/diverging from the 21ema, and the 21ema is widening/spreading/diverging from the 34ema.
#All of this happening for the past 5 consecutive bars.

# start by making an upper chart study.  this way you will have something to look at and visually verify your formulas are working.   when that works, change the plots to work for a scan.

#you did well in listing out what you want to have happen.
#next thing is to think about the steps that the code will follow.

#you want stacked emas and increasing line gaps.  if gaps are checked for multiple previous bars, the stack only needs to checked once.

#to check if 2 lines are getting farther apart, compare their current gap to the previous bar gap. you have 3 lines, so there will be 2 gaps to check. make the bar to bar  gap comparison be a boolean, true/false formula.  this way later, you can sum a group of those values and compare that to 5 (bars back).

# make an upper to test
#3 emas , 9 / 21 / 34

def bn = barnumber();
def na = double.nan;

input price = close;
input avg_type =  AverageType.EXPONENTIAL;

input avg1_len = 9;
def ma1 = MovingAverage(avg_type, price, avg1_len);
input avg2_len = 21;
def ma2 = MovingAverage(avg_type, price, avg2_len);
input avg3_len = 34;
def ma3 = MovingAverage(avg_type, price, avg3_len);

def stackedup = (ma1 > ma2 and ma2 > ma3);
def stackeddwn = (ma1 < ma2 and ma2 < ma3);

#def gap1 = (ma1 - ma2);
#def gap2 = (ma2 - ma3);
def gap1 = absvalue(ma1 - ma2);
def gap2 = absvalue(ma2 - ma3);

# are the gaps increasing?  true or false, 1 or 0
def gap1chg = (gap1 > gap1[1]);
def gap2chg = (gap2 > gap2[1]);

#input bars_increasing = 5;
input bars_increasing = 2;
def barz = bars_increasing - 1;

# add up some previous gap changes. are all of them true?
# add up 4 boolean values. if = 4, then all are true
def gap1x = (Sum(gap1chg, barz) == barz);
def gap2x = (Sum(gap2chg, barz) == barz);

def up_stack_widen = (stackedup and gap1x and gap2x);
def up_stack_widenbn = if bn == 1 then 0 else if up_stack_widen then bn else up_stack_widenbn[1];
def up_sw_diff = bn - up_stack_widenbn;

def dwn_stack_widen = (stackeddwn and gap1x and gap2x);
def dwn_stack_widenbn = if bn == 1 then 0 else if dwn_stack_widen then bn else dwn_stack_widenbn[1];
def dwn_sw_diff = bn - dwn_stack_widenbn;

# -----------------------
# outputs

addlabel(1, "UP- bars ago: " + up_sw_diff, color.yellow);
addlabel(1, "Down- bars ago: " + dwn_sw_diff, color.yellow);

input show_lines = yes;
plot z1 = if show_lines then ma1 else na;
z1.SetDefaultColor(Color.CYAN);
z1.HideBubble();
plot z2 = if show_lines then ma2 else na;
z2.SetDefaultColor(Color.YELLOW);
z2.HideBubble();
plot z3 = if show_lines then ma3 else na;
z3.SetDefaultColor(Color.green);
z3.HideBubble();

gap1chg + "\n" +
gap2chg
, color.yellow, no);

#``````

87k+ Posts
403 Online

## The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
• Exclusive indicators
• Proven strategies & setups
• Private Discord community
• Exclusive members-only content
• 1 full year of unlimited support

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?