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:

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

@Patrick_Hu Yes, you can use the code below. Got it from @markos in comment #3.

Code:
# MA Distance Percentage
# Paris
# 4.13.2018
# Computes percentage the current close is above/below the 200 DSMA
# Displays on a lower chart

input length = 200;

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

plot dist = pct * 100;
 
Need script code for scan to load into watchlist columns of price on 60 min that is 5% or greater over 50 SMA also same for 30 min SMA ,,another column for 5% or greater over 20 SMA on 60 min and 30 min,,,,my intent is to identify prices extended from 50sma where rsi 3 day show overbought range of 90 or oversold of 20 ,,,thankyou
 
@Slickss Just add the column and in the column define your timeframe. Price ix XYZ Percent away from XYZ Moving Average.

Code:
Price ix XYZ Percent away from XYZ MovingAverage
#Define Percent In Decimal
def percentvalue = .05;
#Define Moving Average Desired
def ma = simpleMovingAvg(close,50);
def s = ma*percentvalue;
plot scan = absvalue(close - ma) is greater than or equal to s;
 
Here's another way to do it in a label:
Code:
# 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);
how do you get this to work on the 9 ema?
 
I was wondering if someone could apply the same logic from this thread. How can I measure the distance from the upper bollinger band and plot that value as % in a watchlist column?

I would be forever grateful to the person who has the skill to update my code to show % distance from the upper bollinger band.

Hoping someone can assist.

Thank you whoever you may be.

Code:
input price = close;

input displace = 0;

input length = 10;

input Num_Dev_Dn = -1.5;

input Num_Dev_up = 1.5;

input averageType = AverageType.simple;



def sDev = stdev(data = price[-displace], length = length);



def MidLine = MovingAverage(averageType, data = price[-displace], length = length);

def LowerBand = MidLine + num_Dev_Dn * sDev;

plot UpperBand = MidLine + num_Dev_Up * sDev;

def aboveUpperBand = close > UpperBand;

def almostToUpperBand = close < UpperBand and close > UpperBand;



UpperBand.AssignValueColor(if aboveUpperBand or almostToUpperBand then Color.BLACK else Color.WHITE);

AssignBackgroundColor(if aboveUpperBand then Color.GREEN else if almostToUpperBand then Color.YELLOW else Color.BLACK);
 
I figured it out. Wasn't too bad. Posted here if someone could use this.

Cheers!

Code:
input price = OPEN;
input displace = 0;
input length = 10;
input Num_Dev_Dn = -1.5;
input Num_Dev_up = 1.5;
input averageType = AverageType.simple;


def sDev = stdev(data = price[-displace], length = length);

def MidLine = MovingAverage(averageType, data = price[-displace], length = length);
def LowerBand = MidLine + num_Dev_Dn * sDev;
def UpperBand = MidLine + num_Dev_Up * sDev;
plot calc = ((price - UpperBand)/ UpperBand * 100);
def aboveUpperBand = OPEN > UpperBand;
def almostToUpperBand = OPEN < UpperBand and OPEN > UpperBand ;
 
How do I scan for stocks where the close price is within 6% of 20 SMA value (above or below). I couldn't find it from the study. They have only for 52W high or low but not for moving average value.
 
@lowtrade Here is what you requested. Remember to thumbs up if you find this post useful.

Price is within X% max of the 20 SMA

Code:
#Price is within X% max of the 20 SMA
#### Change percentvalue to percent % in decimal format
###  Example .02 is 2%  and .025 is 2.5%
def percentvalue = .01;
#Price is with X of X EMA
def ma = simpleMovingAvg(close,20);
def s3 = ma*percentvalue;
plot scan = absvalue(close - ma) is less than or equal to s3;
 
Last edited:
I have seen many posts regarding how to scan for when price is within a certain percentage of a particular moving average.

is there a scan for finding how close one moving average is to another?

For instance, how close (% wise) the 5 EMA currently is to the 10 EMA?

Any help is greatly appreciated!
 
I have seen many posts regarding how to scan for when price is within a certain percentage of a particular moving average.

is there a scan for finding how close one moving average is to another?

For instance, how close (% wise) the 5 EMA currently is to the 10 EMA?

Any help is greatly appreciated!

post the formula for finding % distance of two lines and it can be coded.
 
I figured it out. Wasn't too bad. Posted here if someone could use this.

Cheers!

Code:
input price = OPEN;
input displace = 0;
input length = 10;
input Num_Dev_Dn = -1.5;
input Num_Dev_up = 1.5;
input averageType = AverageType.simple;


def sDev = stdev(data = price[-displace], length = length);

def MidLine = MovingAverage(averageType, data = price[-displace], length = length);
def LowerBand = MidLine + num_Dev_Dn * sDev;
def UpperBand = MidLine + num_Dev_Up * sDev;
plot calc = ((price - UpperBand)/ UpperBand * 100);
def aboveUpperBand = OPEN > UpperBand;
def almostToUpperBand = OPEN < UpperBand and OPEN > UpperBand ;
@perseverance_trading1-Can you explain little more about how you are using this code? are you creating as a study & then creating a watchlist ?
If you can share some screenshot will be helpful
 
I am looking for how far price gets above the upper bollinger band
you stated you figured it out in your earlier post... are you trying to scan for something different? can you give more detail? can you give example of what you mean? how far in what increment? and upper band compared to the price or the price compared to the upper band?
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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