New Members: Be sure to confirm your email address by clicking on the link that was sent to your email inbox. You will not be able to post messages until you click that link.
Help writing scan when MA's are a certain % above each other
How do you write a scan to scan for stocks when the 8ema is 1.2% above the 21ema and 1.5% above the 50MA and the 21ema is 0.32% above the 50MA? The reason behind this is because over the years I have put together a chart book and when the ma's above where close to the criteria I presented the stock took off.
0
Answers
So, 90% is .9
110% is 1.1
10% is .10
1% is .01
One tenth of 1% is .01 x .1 or .001
You always multiply to get a per cent result.
When you want one amount below another, e.g. A is 25% below B, then you have to subtract the amount below from 1.0. In other words, 25 from 100 is 75%, or
1.0 - .25 = .75
A < B * .75
When you want one amount greater than the other, you add amount to 1.0, e.g. A is 10% greater than B
A > B * 1.1
You would use the greater than operator (>) or less than operator (<) instead of the equals operator (=) because you are unlikely to get result for an exact amount, which the = operator requires.
On the other hand, using just > or < can give you too many results that are likely to be not what you intended. For instance,
A > B * 1.1
will also return results that are twice or three times A.
So, you probably want to specify a range, e.g. 8 ema > 21ema * 1.012 and 8ema < 21 ema * 1.019
or whatever your research shows might be a reasonable range.
and [ema(8) > ema(21) * .012]
and [ema(8) < ema(21) * .015]
[type is stock]
AND[country is US]
AND [[exchange is NASDAQ]
or [ exchange is nyse]
or [ exchange is amex]]
AND [name not contains 'Fund']
AND [name not contains 'ETF']
AND [name not contains 'ETN']
AND [name not contains 'iShares']
AND [name not contains 'PowerShares']
and [ema(8) > ema(21) * .012]
and [ema(8) < ema(21) * .015]
For 21 abv 50, substitute 21 for 8, 50 for 21, and .0032 for .012 and .005 for .015
You will find "Help" here with the focus, speaking for myself, on helping and giving the building block logic that you can take and apply to suit your needs.
The last two lines on your posted scan are looking for the EMA8 to be above the EMA21 by a percentage. Don't know if that is exactly 1.2% or just greater than but, if you want EMA8 to be 1.2% above EMA21, you need to convert that 1.2% into a decimal to multiply against the EMA21.
1% above would be 1.01. 1.2% above would be 1.012.
If you want the EMA8 to be above the EMA21 * 1.012. You can decide whether it's equal or greater than or equal.
For example
EMA8 = EMA21 * 1.012 or EMA8 >= EMA21 * 1.012
You would select the EMAs from the Technical Indicators dropdown and format it to meet your needs.
Here's the last part, where the EMA21 is in the vicinity of being 0.32% above the MA50. The code below is actually looking for the EMA to be between 0.31% and 0.33% above the MA. 0.32% should be in there.
and [EMA(21,close) >= SMA(50,close) *1.0031]
and [EMA(21,close) <= SMA(50,close) *1.0033]
Try that on your chartbook names by putting a prior date in in the "Starting 0 trading days..." box for where you believe that the instance of this was and see if the scan produces a result for that.
If you get stuck, post what you have for scan code for assistance in troubleshooting.
[type is stock]
AND[country is US]
AND [[exchange is NASDAQ]
or [ exchange is nyse]
or [ exchange is amex]]
AND [name not contains 'Fund']
AND [name not contains 'ETF']
AND [name not contains 'ETN']
AND [name not contains 'iShares']
AND [name not contains 'PowerShares']
and [ema(8) > ema(21) * .012]
and [ema(8) < ema(21) * .015]
and [EMA(21,close) >= SMA(50,close) *1.0031]
and [EMA(21,close) <= SMA(50,close) *1.0033]
Also, I made an error on ema 8 vs ema 21; it should be ema 8 > ema 21 * 1.012 and ema 8 < ema * 1.015.
Above means "greater than", so when you ask for 1.2% above, you are really saying ema 8 is greater than 101.2% of ema 21. 101.2% in scan language is 1.012, not .012.
The way it reads now, it asks for ema 8 greater than 1.2% of (not "above" but "of") ema 21, which would be a very small number and likely always true.
Sorry.
//8ema is 1.2% above the 21ema
and [ema(8) >= ema(21) * 1.012]
//and 8ema is 1.5% above the 50MA
and [ema(8) >= sma(50) * 1.015]
//and the 21ema is 0.32% above the 50MA
and [EMA(21,close) >= SMA(50,close) *1.0031]
and [EMA(21,close) <= SMA(50,close) *1.0033]
There is a very high probablity that there will be no securities that meet all of these requirements on a given day. Maybe set it up as an Alert to run and notify you of a 'hit' when it occurs. The last requirement is the reason that it is a really narrow possibility.
If those 1st two requirements become = instead of >= then that will limit it down to an even narrower of narrow possibilities.
and [1 day ago close < 1 day ago ema(20,close) * .99] // close more than 1% below ema yesterday
and [close > ema(20,close) * 1.01] // close more than 1% above ema today
You can do variations on this idea
and [7 days ago close < 7 days ago ema(20,close) * .97] // 7 days ago close more than 3% below ema 7 days ago
another variation
and [2 days ago close < 2 days ago ema(20,close) * .995] // close more than a half of 1 percent below ema 2 days ago
another variation
and [close > ema(20,close) * 1.005] // close more than a half of 1 percent above ema today
@markd's way is the way that I'd do it. I'll just point out that there are a couple other options available in the Technical Indicators dropdown on the Scan Workbench.
There are Distance from Moving Average filters that display the percentage difference from a EMA or SMA.
The Distance from EMA defaults into the workbench like this:
and [DistFromEMA(200) > 0]
It's an overly simple filter, looking for the close to be above the EMA200 of the close on a percentage basis. You can use it as a Rank By statement as well.
So on your requirement, looking at
and [DistFromEMA(200) > 0] and [1 days ago DistFromEMA(200) < 0]
You'd change the 0 to be whatever percentage you would like for each side. Change the EMA to be whatever value you'd like.
Another option is the Moving Average Envelopes. These display on a chart setting lines that are X% above and below a moving average. You can also scan based on the Upper and Lower line.
The Upper EMA Envelopes default into the workbench like this:
and [Upper EMA Env(20,2.5) < close]
The filter is looking for close that is greater than 2.5% above the 20d EMA.
You can change the close to be you price type of choice. High/Low/Open/Close or other.
So on your requirement you want the price to be less than the Lower EMA Env yesterday and greater than the Upper EMA Env today
and [Upper EMA Env(20,2.5) < close]
and [1 days ago Lower EMA Env(20,2.5) > 1 days ago close]
You'd change the 2.5 to be whatever percentage you would like for each side. Change the EMA to be whatever value you'd like.
You can use the Moving Average Envelopes in a Rank By statement.