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();
AddVerticalLine( up_stack_widen, "-" , Color.GREEN);
#
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);
# spreading
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;
addlabel(1,
( 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);
# spreading
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;
#