Percent Distance Between EMAs or Any 2 Plots For ThinkOrSwim

MerryDay

Administrative
Staff member
Staff
VIP
Lifetime
Scan For Price Within A Percentage Of Moving Average
  1. Where price is below 100 ma
  2. Where price is rising (hl2 is greater than hl2 from 1 bars ago)
  3. Where price is within -2% of the 100 ma
The scan below utilizes @markos script from post #8
Ruby:
# MA Distance Percentage
# Paris
# 4.13.2018
# Computes percentage the current close is above/below the 200 DSMA
# Displays on a lower chart

declare lower;

input length = 200;

def data = close;
def avg = Average(data, length);
def pct = (data/avg) - 1;

plot dist = pct;
plot zero = 0;

zero.SetDefaultColor(Color.WHITE);
zero.SetLineWeight(2);
AddLabel(1, "Percentage from " + length + " MA: " + AsPercent(pct), if pct > 0 then Color.GREEN else Color.PINK);
Copy the above code
In Studies, click Create
Paste the above study
Name the study: Pct_From_MA
Save
Click on the scanner.

Where price is below ma
  1. Click on +Add filter
  2. Click on the pencil icon next to the filter you just added
  3. Click edit
  4. In the left column, click on the 1st pull-down window, click study
  5. Type in Pct_From_MA
  6. Under Plot, click on the pull-down window, choose dist
  7. In length change the 200 ma length to 100 ma length .... (or whatever moving average length that you want)
  8. In the middle column, choose Less than or equal to .... (if scanning for above ma change this to Greater than)
  9. In the right column, click on the pull-down window, click value
  10. Change 100 in the next box to 0
  11. Save

Where price is rising (hl2 is greater than hl2 from 1 bars ago)
  1. Click on +Add filter
  2. Click on the pencil icon next to the filter you just added
  3. Click edit
  4. In the left column, click on the 1st pull-down window, click price
  5. Click hl2 .... (or what ever representation of price that floats your boat)
  6. In the middle column, choose greater than or equal to
  7. In the right column, click on the pull-down window, click price
  8. Click hl2 .... (or what ever representation of price that floats your boat)
  9. In the box under hl2, change the 0 to 1 offset
  10. Save

Where price is within -2% of the ma
  1. Click on +Add filter
  2. Click on the pencil icon next to the filter you just added
  3. Click edit
  4. In the left column, click on the 1st pull-down window, click study
  5. Type in Pct_From_MA
  6. Under Plot, click on the pull-down window, choose dist
  7. In length change the 200 ma length to 100 ma length .... (or whatever moving average length that you want)
  8. In the middle column, choose greater than or equal to .... (or Less than if scanning for above ma)
  9. In the right column, click on the pull-down window, click value
  10. In the box under value, change the value from 100 to -0.02 .... (negative values represent under ma; positive values for above ma)
  11. Save .... (change the percentage to anything you want)

Remember: Change the aggregation of each filter to what you want
83orWtu.png


Here is a Shared Link: http://tos.mx/kXoe4Db
The shared link will only work if you copy and pasted the above study and named it: Pct_From_MA
A poster was having difficulty scanning for within a percentage of ma so I wrote this brief tutorial. I don't use moving average studies so I can not assist w/ how people are utilizing this in their strategies.
 
Last edited:
Hey,
I was looking for some help with making a scanner that could give indication of a stock approaching the 200EMA in TOS. Say within 1 to 5% of the 200EMA but also having the previous 5 candles or so out of that range and coming from above looking for a potential bounce area.
Any help is appreciated thanks.
Here are links to 60 examples of scanning for a percentage.
https://usethinkscript.com/search/584159/?q=scan&t=post&c[thread]=1345&o=date

Here are directions for how I compiled these results: Great Hack To Search Long Threads for Scan scripts, Labels, Watchlists, etc...
 

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

Does anyone know of any script to scan....for instance on a daily chart for stocks within a certain percent of a moving average......so if one is within 2% of the 200SMA for instance
 
Hello all!

I'm looking for a scan line that will make sure the price hasn't drifted too far away from the 8EMA. I need: OPEN is <.5% away from of 8EMA. Daily aggregation.

Any help would be fantastic. (y)
 
Hello all!

I'm looking for a scan line that will make sure the price hasn't drifted too far away from the 8EMA. I need: OPEN is <.5% away from of 8EMA. Daily aggregation.
I moved your post here because there are 7 pages of examples of what you are asking. You will need to adjust any example you use to the exact percentage you are looking for and for the moving average you want. Have fun!
 
I moved your post here because there are 7 pages of examples of what you are asking. You will need to adjust any example you use to the exact percentage you are looking for and for the moving average you want. Have fun!
@MerryDay Thank you for your quick response. I have it all set up, but I need to change it to EMA instead of SMA. Is this done using the original code?
 
I want indicator to show the % distance between SPY and its highs when I'm looking at any stock, not just when I'm looking at SPY. It's a simple relative strength indicator I thought of that I'm having a hard time coding, and I would appreciate any help!
 
I want indicator to show the % distance between SPY and its highs when I'm looking at any stock, not just when I'm looking at SPY. It's a simple relative strength indicator I thought of that I'm having a hard time coding, and I would appreciate any help!
Compare Close to Highest(High,63)

You can also do Close("SYMBOL") and High("SYMBOL") etc.
 
Last edited by a moderator:
Compare Close to Highest(High,63)

You can also do Close("SYMBOL") and High("SYMBOL") etc.
Thank you! But a problem with this is that the code is not specific to SPY. I only want to see the % distance of specifically SPY from its highest close in the last 63 bars no matter what stock I'm looking at. Is this possible to do for you?

Much appreciated,

Joe
 
Something like this should get you on the right track.

Ruby:
def SPY =
    Close("SPY")
;
def Cur =
    Close()
;
def SPYHigh =
    Highest(High("SPY"),63)
;
def CurHigh =
    Highest(High(),63)
;
def SPYRawDiff =
    SPYHigh - Spy
;
def CurRawDiff =
    CurHigh - Cur
;
def SPYPercDif =
    100 - #
    Round(SPYRawDiff / SPYHigh * 100,2)   
;
def CurPercDif =
    100 - #
    Round(CurRawDiff / CurHigh * 100,2)   
;
def Compare =
    CurPercDif - SpyPercDif
;
AddLabel(
    yes,
    "%" + SPYPercDif,
    Color.white
);
AddLabel(
    yes,
    "$" + SPYRawDiff,
    Color.white
);
AddLabel(
    yes,
    "%" + CurPercDif,
    Color.Light_Gray
);
AddLabel(
    yes,
    "$" + CurRawDiff,
    Color.Light_Gray
);
AddLabel(
    yes,
    "%" + Compare,
    if Compare >= 0
    then Color.Light_Green
    else Color.Light_Orange
);
 
# Display % difference between 200 dma
input lowLimit2 = -0.05;
input highLimit2 = 0;
input lowLimit1 = 0.05;
input highLimit1 = 0;
input length = 200;
def ema = ExpAverage(close, length);
def diff = (close - ema) * 100;
def diff2 = diff <= lowLimit1 and diff >= highLimit1;
def diff3 = diff >= lowLimit2 and diff <= highLimit2;
plot Between1 = if diff2 == 1 then 1 else if diff3 == 1 then -1 else 0;

AssignBackgroundColor(if diff2 == 1 then Color.dark_green else if diff3 == 1 then Color.red else Color.black);
Between1.AssignValueColor(if Between1 == 1 then Color.dark_green else if Between1 == -1 then Color.red else Color.black);
 
Last edited by a moderator:
Greetings,

what is the script/code for a scan that returns stocks where the simple moving average 9 is approaching simple moving average 20 from below.

in other words the scan returns stocks where the sma9 is below sma20, but sma9 is increasing, up towards sma20 from below.

sma9 has not crossed sma20 but is approaching from below.

thank you in advance for any and all assistance.

regards,
 
what is the script/code for a scan that returns stocks where the simple moving average 9 is approaching simple moving average 20 from below.
"Approaching" is not a function in ToS. A workaround favored by many members is when EMA9 gets within x% of EMA20.
I moved your post here because there are 7 pages of "approaching" examples. Change the lengths, plug, & play and see what floats your boat (y)
 
Last edited:
I need some assistance with two scan thinscripts:

  • high is within 10% of upper Bollinger band
  • close within 10% of open

Thank you in advance!
Here ya go:
Code:
high > BollingerBands()."UpperBand" * .90 and
high <  BollingerBands()."UpperBand" * 1.10

Code:
close > open* .90 and
close < open* 1.10
 
Last edited:
Apologies for intruding on another member's post but i felt their request somewhat resembled mine.

respectfully request code / script that identifies condition where 9 moving average has moved within 5 percent below previous day high.

i've attempted to post a screenshot as to help further explain this request but system would not accept the passed link. i posted the link below.

thank you in advance for any and all assistance regarding this request.

regards,

https%3A//i.imgur.com/p5iQQC7.png[/img]']
p5iQQC7.png
 
Apologies for intruding on another member's post but i felt their request somewhat resembled mine.

respectfully request code / script that identifies condition where 9 moving average has moved within 5 percent below previous day high.

i've attempted to post a screenshot as to help further explain this request but system would not accept the passed link. i posted the link below.

thank you in advance for any and all assistance regarding this request.
@cfire23
this should plot at 1 if the 9 period average is within or greater than 5% of yesterday's high otherwise it will plot at 0 -
Code:
declare lower;
plot condition = average(close,9) >= .95*high(period = "day")[1];
 
Last edited:

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
473 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