find when 2 to 5 averages are close together, within x%.

halcyonguy

Moderator - Expert
VIP
Lifetime
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%


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
vqKsujB.jpg



============================


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
Z28DM5V.jpg
 
@halcyonguy Thank you again for the script you wrote, it took me a little time to modify it to only show 10, 20 and 50 moving averages but it seems to be returning many results that fit what im looking for, just curious, is there a way to create a watch list label that will show when those 3 moving averages are all within 0.09% of each other?
thank you again for your help

Here is the script that I modified; it would be awesome to be able to see these meeting results in a watch list.
Code:
#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 = 10;
input ma1_type =  AverageType.EXPONENTIAL;
def ma1 = MovingAverage(ma1_type, price, ma1_len);

input ma2_len = 20;
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(hma3, hma3))));

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( lma3, lma3))));

# ---------------------

def rng = maxz - minz;
def hirng = highest(rng, duration_bars);
def per = 100*hirng/minz;

input minper = 0.5;
def en = per <= minper;

plot z = en;
 
Last edited by a moderator:

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
287 Online
Create Post

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