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:
This code provides a couple of options.
The default == no plots, just a label. You can drag it to display on any lower or upper chart
input higherchart = yes, a label and upper chart moving averages plot colored as specified in your post.
input lowerchart = yes, drag to the lower chart, label and a histogram plot.

shared chart link: http://tos.mx/BAWfraw Click here for --> Easiest way to load shared links
8oI7MBM.png

Ruby:
# MA Distance Percentage

declare real_size ;

input HigherChart = no ;
input LowerChart = no ;
input len1 = 13;
input len2 = 48;

def data = close;
def avg1 = expAverage(data, len1);
def avg2 = expAverage(data, len2);
def pct = (avg1 - avg2) / avg1 ;

plot ma1 = if HigherChart then avg1 else double.NaN;
     ma1.AssignValueColor(
if pct > 0 and pct > .05 then color.plum else
if pct >=0  then color.white else
if pct < 0 and pct > -.05 then color.blue else
color.yellow) ;
plot ma2 = if HigherChart then avg2 else double.NaN;
     ma2.SetStyle(curve.MEDIUM_DASH);

plot dist = if LowerChart then pct else double.NaN;
     dist.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
     dist.SetLineWeight(5);
     dist.AssignValueColor(
if pct > 0 and pct > .05 then color.plum else
if pct >=0  then color.white else
if pct < 0 and pct > -.05 then color.blue else
color.yellow) ;

plot zero = if LowerChart then 0 else double.NaN;;

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

AddLabel(yes, "Percentage from " + len2 + "ema: " + AsPercent(pct),
if pct > 0 and pct > .05 then color.plum else
if pct >=0  then color.white else
if pct < 0 and pct > -.05 then color.blue else
color.yellow) ;
can this script do the distance from open and high rather then the two ema's?
 
can this script do the distance from open and high rather then the two ema's?

This might work? Not at my computer to test it. Probably some redundancy also…

Code:
# MA Distance Percentage / OH Distance Percentage
# Added distance percent from open to current high

declare real_size ;

Input Show_O_to_H = yes;
Input O_to_High_Label = yes;

Input Show_Ma = no;
Input Ma_Label = no;

input Reset_Time = 0930; # Time to record open
def start_time = 0930; # Start time to find high

def Start = (SecondsTillTime(Reset_Time)[1] > 0 and SecondsTillTime(Reset_Time) <= 0);
def Start_2 = (SecondsTillTime(Start_Time)[1] < 0 and SecondsTillTime(Start_Time) >= 0);

def SessionOpen;
if Start {
SessionOpen = open;
} else {
SessionOpen = SessionOpen[1];
}

def SessionHigh;
if Start_2 {
SessionHigh = High;
} else {
SessionHigh = SessionHigh[1];
}

input HigherChart = no ;
input LowerChart = no ;
input len1 = 13;
input len2 = 48;

def data = close;
def avg1 = expAverage(data, len1);
def avg2 = expAverage(data, len2);

def pct = (avg1 - avg2) / avg1 ;
def pct_OH = (sessionopen - sessionhigh) / sessionopen ;

plot ma1 = if HigherChart and Show_Ma then avg1 else double.NaN;
ma1.AssignValueColor(
if pct > 0 and pct > .05 then color.plum else
if pct >=0  then color.white else
if pct < 0 and pct > -.05 then color.blue else
color.yellow) ;

plot ma2 = if HigherChart and Show_Ma then avg2 else double.NaN;
ma2.SetStyle(curve.MEDIUM_DASH);

plot ma3 = if HigherChart and Show_O_to_H then sessionopen else double.NaN;
ma1.AssignValueColor(
if pct_OH > 0 and pct_OH > .05 then color.plum else
if pct_OH >=0  then color.white else
if pct_OH < 0 and pct_OH > -.05 then color.blue else color.yellow) ;

plot ma4 = if HigherChart and Show_O_to_H then sessionhigh else double.NaN;
ma4.SetStyle(curve.MEDIUM_DASH);

plot dist = if LowerChart and Show_Ma then pct else double.NaN;
dist.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
dist.SetLineWeight(5);
dist.AssignValueColor(
if pct > 0 and pct > .05 then color.plum else
if pct >=0  then color.white else
if pct < 0 and pct > -.05 then color.blue else
color.yellow) ;

plot dist2 = if LowerChart and Show_O_to_H then pct_OH else double.NaN;
dist2.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
dist2.SetLineWeight(5);
dist2.AssignValueColor(
if pct_OH > 0 and pct > .05 then color.plum else
if pct_OH >=0  then color.white else
if pct_OH < 0 and pct > -.05 then color.blue else
color.yellow) ;

plot zero = if LowerChart then 0 else double.NaN;;
zero.SetDefaultColor(Color.WHITE);
zero.SetLineWeight(2);

AddLabel(Ma_Label, "Percentage from " + len2 + "ema: " + AsPercent(pct),
if pct > 0 and pct > .05 then color.plum else
if pct >=0  then color.white else
if pct < 0 and pct > -.05 then color.blue else
color.yellow) ;

AddLabel(O_to_High_Label, "Percentage from Open to High of Day  | "  + AsPercent(pct_OH),
if pct_OH > 0 and pct_OH > .05 then color.plum else
if pct_OH >=0  then color.white else
if pct_OH < 0 and pct_OH > -.05 then color.blue else color.yellow) ;
 
Last edited by a moderator:
This might work? Not at my computer to test it. Probably some redundancy also…

Code:
# MA Distance Percentage / OH Distance Percentage
# Added distance percent from open to current high

declare real_size ;

Input Show_O_to_H = yes;
Input O_to_High_Label = yes;

Input Show_Ma = no;
Input Ma_Label = no;

input Reset_Time = 0930; # Time to record open
def start_time = 0930; # Start time to find high

def Start = (SecondsTillTime(Reset_Time)[1] > 0 and SecondsTillTime(Reset_Time) <= 0);
def Start_2 = (SecondsTillTime(Start_Time)[1] < 0 and SecondsTillTime(Start_Time) >= 0);

def SessionOpen;
if Start {
SessionOpen = open;
} else {
SessionOpen = SessionOpen[1];
}

def SessionHigh;
if Start_2 {
SessionHigh = High;
} else {
SessionHigh = SessionHigh[1];
}

input HigherChart = no ;
input LowerChart = no ;
input len1 = 13;
input len2 = 48;

def data = close;
def avg1 = expAverage(data, len1);
def avg2 = expAverage(data, len2);

def pct = (avg1 - avg2) / avg1 ;
def pct_OH = (sessionopen - sessionhigh) / sessionopen ;

plot ma1 = if HigherChart and Show_Ma then avg1 else double.NaN;
ma1.AssignValueColor(
if pct > 0 and pct > .05 then color.plum else
if pct >=0  then color.white else
if pct < 0 and pct > -.05 then color.blue else
color.yellow) ;

plot ma2 = if HigherChart and Show_Ma then avg2 else double.NaN;
ma2.SetStyle(curve.MEDIUM_DASH);

plot ma3 = if HigherChart and Show_O_to_H then sessionopen else double.NaN;
ma1.AssignValueColor(
if pct_OH > 0 and pct_OH > .05 then color.plum else
if pct_OH >=0  then color.white else
if pct_OH < 0 and pct_OH > -.05 then color.blue else color.yellow) ;

plot ma4 = if HigherChart and Show_O_to_H then sessionhigh else double.NaN;
ma4.SetStyle(curve.MEDIUM_DASH);

plot dist = if LowerChart and Show_Ma then pct else double.NaN;
dist.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
dist.SetLineWeight(5);
dist.AssignValueColor(
if pct > 0 and pct > .05 then color.plum else
if pct >=0  then color.white else
if pct < 0 and pct > -.05 then color.blue else
color.yellow) ;

plot dist2 = if LowerChart and Show_O_to_H then pct_OH else double.NaN;
dist2.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
dist2.SetLineWeight(5);
dist2.AssignValueColor(
if pct_OH > 0 and pct > .05 then color.plum else
if pct_OH >=0  then color.white else
if pct_OH < 0 and pct > -.05 then color.blue else
color.yellow) ;

plot zero = if LowerChart then 0 else double.NaN;;
zero.SetDefaultColor(Color.WHITE);
zero.SetLineWeight(2);

AddLabel(Ma_Label, "Percentage from " + len2 + "ema: " + AsPercent(pct),
if pct > 0 and pct > .05 then color.plum else
if pct >=0  then color.white else
if pct < 0 and pct > -.05 then color.blue else
color.yellow) ;

AddLabel(O_to_High_Label, "Percentage from Open to High of Day  | "  + AsPercent(pct_OH),
if pct_OH > 0 and pct pct_OH .05 then color.plum else
if pct_OH >=0  then color.white else
if pct_OH < 0 and pct_OH > -.05 then color.blue else color.yellow) ;
getting an error
 
getting an error
Damn… haven’t been able to look at it yet… wonder if it has something to do with “declare real size”?
@Piper2808t
"getting an error" is not enough information for anyone to help you.
In the future please provide the whole error message.

In this case, it said that line 93 was missing its greater than sign.
The code here:
https://usethinkscript.com/threads/...lots-for-thinkorswim.1345/page-10#post-125160
has been corrected. The greater than sign was put back in.
 
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
View attachment 9966

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.
how do I add a topline and a bottomline of 1.5 and -1.5? I added the following but it doesn't plot the distance %. Added the # until this is fixed:

#plot topline = 1.5;
#plot bottomline = -1.5;

#topline.setdefaultColor(color.DARK_RED);
#topline.setlineWeight(2);
#bottomline.setDefaultColor(color.DARK_RED);
#bottomline.setlineWeight(2);
 

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

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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