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:

dsohi11

New member
VIP
How can I write I want the difference between the VWAP and 34 adaptive to be only 5 points? condition I'm writng stragety where the price between VWAP and 34 moving average is five points or I can make it variable


Thank you
 
Last edited:

jktrader37

New member
VIP
Dear All,
Since there are so many generous people here willing to share, I wanted to throw my two cents in..
This is my first ThinkScript indicator from scratch - it is super simple and very easy to modify
if you wanted to. I still need to learn about colors, but they are all manually configurable.

It simply plots a line that indicates the % deviation that price is making in real time from
a selected exponential moving average.

; this script calculates then plots a line that shows how
; far price has moved away from a moving average
input ema_length = 21;
input low1_refLine = -1;
input high1_refLine = 1;
input low2_refLine = -2;
input high2_refLine = 2;
input mid_refLine = 0;

def my_ema = movavgexponential(Close, ema_length, 0, no);
def close_dev = ((close - my_ema)/close) * 1000;
Plot data = close_dev;
Plot low1 = low1_refLine;
Plot high1 = high1_refLine;
Plot low2 = low2_refLine;
Plot high2 = high2_refLine;
Plot midline = mid_refLine;
 

Redpot

New member
VIP
Since I have used this site for my reference for several months, I have decided to become a VIP member, looking forward to seeing much depth contents.



Currently I am using a custom watchlist code based on price distance (%) from EMAs between current price and 200 EMAs in Five min chart or 80 EMA in Hourly Chart, 200 EMA in daily Charts. By doing so I can quickly check the current status of the trend in short term, mid term, long term for a specific stock, future).

Below is the current code I use

input length = 200;

def data = close;
def expavg = expAverage(data, length);
def pct = (data-expavg)/data;

plot dist = pct;
plot zero = 0;

zero.SetDefaultColor(Color.WHITE);
zero.SetLineWeight(2);

AddLabel(1, AsPercent(pct), if pct > 0 then Color.cyan else Color.red);


The improvment that I would like to add a dynamic lable showing the text and distance(%) is as below.

if pct >5% then "Storng" + Pct%,
if 5% > pct >0 then Bullish + pct%,
if 0 > pct -5% then "Bearish" + pct%,
if pct <-5% then "selloff" + pct%

Please help me out on this.
 

MerryDay

Administrative
Staff member
Staff
VIP
Lifetime
Currently I am using a custom watchlist code based on price distance (%) from EMAs between current price and 200 EMAs in Five min chart or 80 EMA in Hourly Chart, 200 EMA in daily Charts.
Shared Watchlist Column Code link: http://tos.mx/IKH3k3l Click here for --> Easiest way to load shared links
IMtiROA.png

Ruby:
input length = 200;
def data = close;
def expavg = expAverage(data, length);
def pct = (data-expavg)/data;

AddLabel(1, 
if pct > .05 then "Storng "   +AsPercent(Pct) else
if pct > 0   then "Bullish "  +AsPercent(Pct) else
if pct <-.05 then "selloff "  +AsPercent(Pct) else
                  "Bearish "  +AsPercent(Pct));

AssignBackgroundColor(
if pct > 0 then color.cyan else color.red);
 

djgLXXII

New member
Is it possible to create a study that allows one to compare one moving average with another (or more) to see if they are within a defined percentage of each other? For example: compare the 20MA, 50MA, 200MA and show me all stocks where those MAs are within 1% of each other.
 

MerryDay

Administrative
Staff member
Staff
VIP
Lifetime
One thing i don't see is distance between EMA and a bollinger band, for example. is that even possible?
Choose any script that you like in these 8 pages and substitute the BollingerBand definition for one of the plots:
Code:
plot TopBand = reference BollingerBands()."UpperBand" ;
plot BotBand = reference BollingerBands()."LowerBand" ;
 

duke-oofington

New member
Hello. I hope I'm asking this in the right place. This is not concerning crossovers but instead I want to create a filter in TOS scanner for X MA X% over or under X MA

For example, 50 SMA 30% above 200 SMA

I can't find it as a pre-made filter and would like to try to build it myself so I can start learning script. Thanks
 

henry1224

Active member
VIP
Hello. I hope I'm asking this in the right place. This is not concerning crossovers but instead I want to create a filter in TOS scanner for X MA X% over or under X MA

For example, 50 SMA 30% above 200 SMA

I can't find it as a pre-made filter and would like to try to build it myself so I can start learning script. Thanks
Code:
Def A = Average(Close,50);
Def B = Average(Close,200);
Def P = A/B;
Plot Result = If p >= 1.3 then 1 else 0;
 

shankarp2752

New member
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);

The script below can be used if you want to scan for price trading between two different moving averages.

Code:
# Stock price trading between two moving averages

input price = close;
input length1 = 50;
input length2 = 200;
input displace = 0;

def AvgExp1 = ExpAverage(price[-displace], length1);
def AvgExp2 = ExpAverage(price[-displace], length2);

def between_ma = price > AvgExp1 and price < AvgExp2;
plot scan = if between_ma then 1 else 0;
@markos @Joshua Hello, Can you please modify my label script? It's shows green color when reach pct above 20.

Here is I am looking for:

If Pct in between 0 and 20 then color green
If pct above 20 then color red
else color White

AddLabel(1, "Percentage from " + length2 + " SMA: " + As Percent pct), if (pct < 20 and pct >0 ) then Color.GREEN else if pct > 20 then Color.RED else Color.white);
 

Joshua

Well-known member
VIP
Code:
declare lower;

input length = 200;

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

plot zero = 0;
zero.SetDefaultColor(Color.WHITE);
zero.SetLineWeight(2);

plot dist = pct;
dist.assignValueColor(
    if dist > 20 then color.red
    else if dist < 0 then color.white
    else color.green
);

AddLabel(
    yes,
    "Percentage from " + length + " MA: " + round(pct,2) + "%",
    if pct > 20 then Color.RED
    else if pct < 0 then color.white
    else Color.green
);
 

shankarp2752

New member
Code:
declare lower;

input length = 200;

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

plot zero = 0;
zero.SetDefaultColor(Color.WHITE);
zero.SetLineWeight(2);

plot dist = pct;
dist.assignValueColor(
    if dist > 20 then color.red
    else if dist < 0 then color.white
    else color.green
);

AddLabel(
    yes,
    "Percentage from " + length + " MA: " + round(pct,2) + "%",
    if pct > 20 then Color.RED
    else if pct < 0 then color.white
    else Color.green
);
@Joshua Thank you!
 

Cribbage

Member
Has anyone written a code that shows what percent a fast EMA is over a slow EMA?

I've scoured some of the related material to no avail. Any direction would be great. Thanks!
 

StockGuru71

New member
I am looking for a scanner that allows you to search stocks by choosing a percent away from the 50 or 200 day average. I believe it can be useful when looking for bounces in the markets for trading maneuvers. Thanks to anyone that can help.
 

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.
Top