this is an upper study for testing
(below is a lower/scan)
based on these requests
https://usethinkscript.com/threads/3-moving-averages-coiled-tight-together.14180/
3 avgs near ea other
https://usethinkscript.com/threads/scan-for-2-moving-averages-close-together.14056/
2 avgs near together
find when 2 to 5 averages are close together, within x%.
can pick a duration, how many previous bars the lines need to be close together.
default averages are EMA , 9, 21, 50, 88, 111
default x% is 0.09%
4 EMA averages , 9, 21, 50, 88
all lines within 0.09%
look back at 1 bar
============================
lower / scan
3 EMA averages , 9, 21, 50
all lines within 0.09%
look back at 2 bars
(below is a lower/scan)
based on these requests
https://usethinkscript.com/threads/3-moving-averages-coiled-tight-together.14180/
3 avgs near ea other
https://usethinkscript.com/threads/scan-for-2-moving-averages-close-together.14056/
2 avgs near together
find when 2 to 5 averages are close together, within x%.
can pick a duration, how many previous bars the lines need to be close together.
default averages are EMA , 9, 21, 50, 88, 111
default x% is 0.09%
Code:
# avgs_near_ea_other_00
#https://usethinkscript.com/threads/3-moving-averages-coiled-tight-together.14180/
# 3 avgs near ea other
#https://usethinkscript.com/threads/scan-for-2-moving-averages-close-together.14056/
# 2 avgs near together
#-------------------------
def na = double.nan;
def bn = barnumber();
def lastbar = !isnan(close[0]) and isnan(close[-1]);
input duration_bars = 5;
input averages = 2;
def avgs = if averages < 2 then 2
else if averages > 5 then 5
else averages;
def price = close;
input ma1_len = 9;
input ma1_type = AverageType.EXPONENTIAL;
def ma1 = MovingAverage(ma1_type, price, ma1_len);
input ma2_len = 21;
input ma2_type = AverageType.EXPONENTIAL;
def ma2 = MovingAverage(ma2_type, price, ma2_len);
input ma3_len = 50;
input ma3_type = AverageType.EXPONENTIAL;
def ma3 = MovingAverage(ma3_type, price, ma3_len);
input ma4_len = 88;
input ma4_type = AverageType.EXPONENTIAL;
def ma4 = MovingAverage(ma4_type, price, ma4_len);
input ma5_len = 111;
input ma5_type = AverageType.EXPONENTIAL;
def ma5 = MovingAverage(ma5_type, price, ma5_len);
#-----------------------
def s = 0;
def hma1 = if avgs >= 1 then ma1 else s;
def hma2 = if avgs >= 2 then ma2 else s;
def hma3 = if avgs >= 3 then ma3 else s;
def hma4 = if avgs >= 4 then ma4 else s;
def hma5 = if avgs >= 5 then ma5 else s;
def maxz = max(hma1, max(hma2, max(hma3, max(hma4, hma5))));
def b = 99999;
def lma1 = if avgs >= 1 then ma1 else b;
def lma2 = if avgs >= 2 then ma2 else b;
def lma3 = if avgs >= 3 then ma3 else b;
def lma4 = if avgs >= 4 then ma4 else b;
def lma5 = if avgs >= 5 then ma5 else b;
def minz = min(lma1, min(lma2, min(lma3, min( lma4, lma5))));
# ---------------------
def rng = maxz - minz;
def hirng = highest(rng, duration_bars);
def per = 100*hirng/minz;
input minper = 0.09;
def en = per <= minper;
input show_vertical_lines = no;
addverticalline(show_vertical_lines and en, "-", color.light_gray);
input show_cloud = yes;
def top = if en then maxz + (4 * rng) else na;
def bot = if en then minz - (4 * rng) else na;
addcloud(top, bot, color.yellow);
#------------------
input show_average_lines = yes;
plot z1 = if show_average_lines and avgs >= 1 then ma1 else na;
z1.setdefaultcolor(getcolor(1));
#z1.setlineweight(1);
z1.hidebubble();
plot z2 = if show_average_lines and avgs >= 2 then ma2 else na;
z2.setdefaultcolor(getcolor(2));
#z2.setlineweight(1);
z2.hidebubble();
plot z3 = if show_average_lines and avgs >= 3 then ma3 else na;
z3.setdefaultcolor(getcolor(2));
#z3.setlineweight(1);
z3.hidebubble();
plot z4 = if show_average_lines and avgs >= 4 then ma4 else na;
z4.setdefaultcolor(getcolor(2));
#z4.setlineweight(1);
z4.hidebubble();
plot z5 = if show_average_lines and avgs >= 5 then ma5 else na;
z5.setdefaultcolor(getcolor(2));
#z5.setlineweight(1);
z5.hidebubble();
#
4 EMA averages , 9, 21, 50, 88
all lines within 0.09%
look back at 1 bar
============================
lower / scan
Code:
# avgs_near_ea_other_00_lower
#https://usethinkscript.com/threads/3-moving-averages-coiled-tight-together.14180/
# 3 avgs near ea other
#https://usethinkscript.com/threads/scan-for-2-moving-averages-close-together.14056/#post-117502
# 2 avgs near together
#-------------------------
declare lower;
def na = double.nan;
def bn = barnumber();
def lastbar = !isnan(close[0]) and isnan(close[-1]);
input duration_bars = 2;
input averages = 3;
def avgs = if averages < 2 then 2
else if averages > 5 then 5
else averages;
def price = close;
input ma1_len = 9;
input ma1_type = AverageType.EXPONENTIAL;
def ma1 = MovingAverage(ma1_type, price, ma1_len);
input ma2_len = 21;
input ma2_type = AverageType.EXPONENTIAL;
def ma2 = MovingAverage(ma2_type, price, ma2_len);
input ma3_len = 50;
input ma3_type = AverageType.EXPONENTIAL;
def ma3 = MovingAverage(ma3_type, price, ma3_len);
input ma4_len = 88;
input ma4_type = AverageType.EXPONENTIAL;
def ma4 = MovingAverage(ma4_type, price, ma4_len);
input ma5_len = 111;
input ma5_type = AverageType.EXPONENTIAL;
def ma5 = MovingAverage(ma5_type, price, ma5_len);
#-----------------------
def s = 0;
def hma1 = if avgs >= 1 then ma1 else s;
def hma2 = if avgs >= 2 then ma2 else s;
def hma3 = if avgs >= 3 then ma3 else s;
def hma4 = if avgs >= 4 then ma4 else s;
def hma5 = if avgs >= 5 then ma5 else s;
def maxz = max(hma1, max(hma2, max(hma3, max(hma4, hma5))));
def b = 99999;
def lma1 = if avgs >= 1 then ma1 else b;
def lma2 = if avgs >= 2 then ma2 else b;
def lma3 = if avgs >= 3 then ma3 else b;
def lma4 = if avgs >= 4 then ma4 else b;
def lma5 = if avgs >= 5 then ma5 else b;
def minz = min(lma1, min(lma2, min(lma3, min( lma4, lma5))));
# ---------------------
def rng = maxz - minz;
def hirng = highest(rng, duration_bars);
def per = 100*hirng/minz;
input minper = 0.09;
def en = per <= minper;
plot z = en;
#input show_vertical_lines = no;
#addverticalline(show_vertical_lines and en, "-", color.light_gray);
#input show_cloud = yes;
#def top = if en then maxz + (4 * rng) else na;
#def bot = if en then minz - (4 * rng) else na;
#addcloud(top, bot, color.yellow);
#------------------
#input show_average_lines = no;
#plot z1 = if show_average_lines and avgs >= 1 then ma1 else na;
#z1.setdefaultcolor(getcolor(1));
##z1.setlineweight(1);
#z1.hidebubble();
#plot z2 = if show_average_lines and avgs >= 2 then ma2 else na;
#z2.setdefaultcolor(getcolor(2));
##z2.setlineweight(1);
#z2.hidebubble();
#plot z3 = if show_average_lines and avgs >= 3 then ma3 else na;
#z3.setdefaultcolor(getcolor(2));
##z3.setlineweight(1);
#z3.hidebubble();
#plot z4 = if show_average_lines and avgs >= 4 then ma4 else na;
#z4.setdefaultcolor(getcolor(2));
##z4.setlineweight(1);
#z4.hidebubble();
#plot z5 = if show_average_lines and avgs >= 5 then ma5 else na;
#z5.setdefaultcolor(getcolor(2));
##z5.setlineweight(1);
#z5.hidebubble();
#
3 EMA averages , 9, 21, 50
all lines within 0.09%
look back at 2 bars