# stacked_avg_widening_upper_01
# add check for down
# upper study
# https://usethinkscript.com/threads/stacked-diverging-moving-averages-for-n-consecutive-bars.11474/
# 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);
# spreading
#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();
AddVerticalLine( up_stack_widen, "-" , Color.GREEN);
AddVerticalLine( dwn_stack_widen, "-" , Color.REd);
addchartbubble(0, low,
gap1chg + "\n" +
gap2chg
, color.yellow, no);
#