Percent difference between 2 moving averages scan

Ictheeuss

New member
Hi everyone,

I've been scouring the threads trying to find a scan that will allow me to scan for a certain percent difference between two moving averages. For example, 8 EMA is 15% away from the 20 SMA. I've attached a screenshot for clarity. Can anyone point me in the right direction? Thank you in advance.
 

Attachments

  • Screenshot 2024-11-01 235842.png
    Screenshot 2024-11-01 235842.png
    5.8 KB · Views: 20
Solution
Hi everyone,

I've been scouring the threads trying to find a scan that will allow me to scan for a certain percent difference between two moving averages. For example, 8 EMA is 15% away from the 20 SMA. I've attached a screenshot for clarity. Can anyone point me in the right direction? Thank you in advance.

here is a lower study that should work in a scan
can pick a percentage for the difference. default is 7% (i didn't find any hits at 15%). % is calc'd from the longer average.
can pick a tolerance % , default is 0.2%

Code:
# avg_diff_per_lower
#https://usethinkscript.com/threads/percent-difference-between-2-moving-averages-scan.19922/
#Percent difference between 2 moving averages scan

declare lower;

def na = double.nan;
def...
Hi everyone,

I've been scouring the threads trying to find a scan that will allow me to scan for a certain percent difference between two moving averages. For example, 8 EMA is 15% away from the 20 SMA. I've attached a screenshot for clarity. Can anyone point me in the right direction? Thank you in advance.

here is a lower study that should work in a scan
can pick a percentage for the difference. default is 7% (i didn't find any hits at 15%). % is calc'd from the longer average.
can pick a tolerance % , default is 0.2%

Code:
# avg_diff_per_lower
#https://usethinkscript.com/threads/percent-difference-between-2-moving-averages-scan.19922/
#Percent difference between 2 moving averages scan

declare lower;

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

input avg1_type = AverageType.exponential;
input avg1_length = 8;
def avg1 = MovingAverage(avg1_type, data, avg1_length );

input avg2_type = AverageType.Simple;
input avg2_length = 20;
def avg2 = MovingAverage(avg2_type, data, avg2_length );

def avgdiffper = round(100* absvalue(avg1 - avg2)/avg2,2);
def avgdiff = round(absvalue(avg1 - avg2),2);

# % diff from avg2 , bigger avg
input average_percent_difference = 7.0;
input difference_tolerance_per = 0.2;

def diffpr = round(avg2*average_percent_difference/100,2);
def tolpr = round(avg2*difference_tolerance_per/100,2);

def near =  avgdiff < (diffpr + tolpr) and avgdiff > (diffpr - tolpr);

plot z = near;
#


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

this is the upper study i created first, to test it

Code:
# avg_diff_per
#https://usethinkscript.com/threads/percent-difference-between-2-moving-averages-scan.19922/
#Percent difference between 2 moving averages scan

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

input avg1_type = AverageType.exponential;
input avg1_length = 8;
def avg1 = MovingAverage(avg1_type, data, avg1_length );

input avg2_type = AverageType.Simple;
input avg2_length = 20;
def avg2 = MovingAverage(avg2_type, data, avg2_length );

def avgdiffper = round(100* absvalue(avg1 - avg2)/avg2,2);
def avgdiff = round(absvalue(avg1 - avg2),2);

# % diff from avg2 , bigger avg
input average_percent_difference = 7.0;
input difference_tolerance_per = 0.2;

def diffpr = round(avg2*average_percent_difference/100,2);
def tolpr = round(avg2*difference_tolerance_per/100,2);

def near =  avgdiff < (diffpr + tolpr) and avgdiff > (diffpr - tolpr);


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

addlabel(1, "Gap: " + average_percent_difference + " %" , color.yellow);
addlabel(1, "Tolerance: " + difference_tolerance_per + " %", color.yellow);


input show_clouds = yes;
def top = if (near[1] or near or near[-1]) then max(avg1,avg2) else na;

addcloud(top, min(avg1,avg2), color.cyan);


input test_bubble = no;
addchartbubble(test_bubble,low,
diffpr + " d\n" +
tolpr + " t\n" +
avgdiff + " dif\n" +
avgdiffper + " %"
, (if near then color.yellow else color.gray), no);


input show_avg_lines = yes;
plot zavg1 = if show_avg_lines then avg1 else na;
plot zavg2 = if show_avg_lines then avg2 else na;
zavg1.SetDefaultColor(Color.cyan);
zavg1.setlineweight(1);
zavg1.hidebubble();
zavg2.SetDefaultColor(Color.yellow);
zavg2.setlineweight(1);
zavg2.hidebubble();
#
 

Attachments

  • img2.JPG
    img2.JPG
    69.9 KB · Views: 10
Solution

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

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
265 Online
Create Post

Similar threads

Similar threads

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