# Scan: Find when WMA is x% below EMA (within last x agg)

#### orionanomaly

##### New member
Hello all! I know this is basic but I seem to be doing something wrong. I want to scan to find stocks with one average that is BELOW another average but close (so looking for BEFORE it might 'cross over'). Exclude those ABOVE.

I am spec. looking for when WMA10 <= 0.5% EMA 40 //So when the WMA is Less Than AND within 0.5% of the EMA. I can then find stocks that have one average below another but within 1% of each other.

I have this but can't figure out why it's not working (I am getting for example results that show a < b even though I have a > b! Any help would be SO appreciated!!

Code:
``````#start code

def a = ExpAverage(close, 40);
def b = WMA(close, 10);
def percentValue = .005;

plot scan = a > b and absValue(a-b) is less than or equal to (a*percentValue);

#end code``````

Last edited:
Solution
Hello! Ok, thank you very much for referencing!

I realized that I was asking the wrong question:

1. When coding a scan filter if you use the 'template builder' you are able to specify "how many bars" are used in the scan filter; it's an option.

2. I wanted to be able to implement this since I now have a hand-coded script. I realized what I was looking for was the "within" protected word. So I can return/chart the result "within x bars".

My final (I believe to be) working scan (which looks at the last 3 bars and finds Exp40 > WMA7 by 1.5% or less and returns them looks like this (and can add the code provided above to tag the chart for inspection if want):

Code:
``````def a = ExpAverage(close, 40);
def b = WMA(close, 7);
def...``````
Hello all! I know this is basic but I seem to be doing something wrong. I want to scan to find stocks with one average that is BELOW another average but close (so looking for BEFORE it might 'cross over'). Exclude those ABOVE.

I am spec. looking for when WMA10 <= 0.5% EMA 40 //So when the WMA is Less Than AND within 0.5% of the EMA. I can then find stocks that have one average below another but within 1% of each other.

I have this but can't figure out why it's not working (I am getting for example results that show a < b even though I have a > b! Any help would be SO appreciated!!

Code:
``````#start code

def a = ExpAverage(close, 40);
def b = WMA(close, 10);
def percentValue = .005;

plot scan = a > b and absValue(a-b) is less than or equal to (a*percentValue);

#end code``````

if i get stuck, i put the code on a chart and use bubbles to display values on each bar. then see if the values are what i think they should be. look at a few stocks of the desired time.

Code:
``addchartbubble(1, low, (a > b) + "\n" + absValue(a-b) + "\n" +  (a*percentValue), color.cyan, no);``

Hello! Thank you so much this was very helpful! I added in a few other 'debug' metrics and it looks like it's doing exactly what I thought... my error was actually in the way I had the timeframes set.

*I don't think I understand how timeframes work in the scanner. When you set "D" (DAY) for the aggregation period - what does that mean? Is it performing the scan function on the most recent DAY candle?

1. If I put the scanner at 15M - does it only scan/search against the most recent 15M candle?

2. What if I want to specify in my search function whether the condition was true "in the last x number of candles" and then select the time agg period?

Hello! Ok, thank you very much for referencing!

I realized that I was asking the wrong question:

1. When coding a scan filter if you use the 'template builder' you are able to specify "how many bars" are used in the scan filter; it's an option.

2. I wanted to be able to implement this since I now have a hand-coded script. I realized what I was looking for was the "within" protected word. So I can return/chart the result "within x bars".

My final (I believe to be) working scan (which looks at the last 3 bars and finds Exp40 > WMA7 by 1.5% or less and returns them looks like this (and can add the code provided above to tag the chart for inspection if want):

Code:
``````def a = ExpAverage(close, 40);
def b = WMA(close, 7);
def percentValue = .015;

plot scan = a > b and absValue(a-b) is less than or equal to (a*percentValue) within 3 bars;``````

87k+ Posts
529 Online

## The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
• Exclusive indicators
• Proven strategies & setups
• Private Discord community
• Exclusive members-only content
• 1 full year of unlimited support

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?