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:
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?
@XeoNoX That is correct. In terms of price, how far price gets above the upper band in terms of %. It is based on the current price and current band.
 

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

@perseverance_trading1
here is what you requested.
The % that closing Price is away from the UpperBand BollingerBand as a Label
Code:
# The % that closing Price is away from the UpperBand BollingerBand as a Label
# By XeoNoX Via usethinkscript.com
input price = close;
input displace = 0;
input length = 20;
input Num_Dev_Dn = -2.0;
input Num_Dev_up = 2.0;
input averageType = AverageType.Simple;

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

plot MidLine = MovingAverage(averageType, data = price[-displace], length = length);
plot LowerBand = MidLine + num_Dev_Dn * sDev;
plot UpperBand = MidLine + num_Dev_Up * sDev;

LowerBand.SetDefaultColor(GetColor(0));
MidLine.SetDefaultColor(GetColor(1));
UpperBand.SetDefaultColor(GetColor(5));
def percent_away = (close- UpperBand) / close;
AddLabel (yes, "Percent Price Away from Upper Band BollingerBand: " + AsPercent(percent_away), if percent_away >= .001 then color.dark_green else if percent_away <= .001 then color.downTICK else color.gray);
 
@ProfessorAR15 Got a little tense in here but here's the code if you're still interested. It'll work for both chart and watchlist.

Code:
input price = close;
input length_1 = 3;
input length_2 = 9;
input AverageType = AverageType.SIMPLE;

def MA_1 = MovingAverage(AverageType, price, length_1);
def MA_2 = MovingAverage(AverageType, price, length_2);

def MA_delta = Round(MA_1 - MA_2, 2);

AddLabel(yes, MA_delta, if MA_delta > 0 then Color.GREEN else Color.RED);
Quick question. I'm looking for 9 and 45 ema distance. This is what I change, is this correct? Thanks in advance

input price = close;
input length_1 = 9;
input length_2 =45;
input AverageType = AverageType.EXPONENTIAL;

def MA_1 = MovingAverage(AverageType, price, length_1);
def MA_2 = MovingAverage(AverageType, price, length_2);

def MA_delta = Round(MA_1 - MA_2, 2);

AddLabel(yes, MA_delta, if MA_delta > 0 then Color.GREEN else Color.RED);
 
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 #4
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.

@violatedillusions HTH
Thank You very much for this I've been trying to get it to work profusely. However it gives me an error..... Invalid Statement: AddLabel. I guess the problem is located at....

AddLabel(1, "Percentage from " + length + " MA: " + AsPercent(pct), if pct > 0 then Color.GREEN else Color.PINK);[/CODE]

Any chance of knowing what the issue might be? I'm completely noob when it comes to scripting.

Thank You again, your help is tremendously appreciated; No one else could/wanted to help me.
 
Thank You very much for this I've been trying to get it to work profusely. However it gives me an error..... Invalid Statement: AddLabel. I guess the problem is located at....

AddLabel(1, "Percentage from " + length + " MA: " + AsPercent(pct), if pct > 0 then Color.GREEN else Color.PINK);[/CODE]

Any chance of knowing what the issue might be? I'm completely noob when it comes to scripting.

Thank You again, your help is tremendously appreciated; No one else could/wanted to help me.

The code @MerryDay posted above work fine form me without errors... Are you sure you Copied and Pasted the entire script...??? One thing I do is to use yes instead of 1 for the first condition parameter, but that shouldn't present an issue as it works fine for me and, apparently, @MerryDay...

qHaDcb1.png
 
The code @MerryDay posted above work fine form me without errors... Are you sure you Copied and Pasted the entire script...??? One thing I do is to use yes instead of 1 for the first condition parameter, but that shouldn't present an issue as it works fine for me and, apparently, @MerryDay...

qHaDcb1.png
Yea so, small issue was I didn't add the ; at the end of the entire script. I guess that semicolon eluded me. However, now the issue is Addlabel not allowed in this context. I was looking into this and am wondering if this script is for something other than a scanner. I went to scan, added a study filter and then proceeded to put the entire script in. I even tried the yes

Sorry about all this hassle, I wish I was more proficient.
 
Hi all,

I am looking for a custom watch list column that will tell me how close the price action is to the 180 period SMA line. For example if the price action is below the SMA line it would show negative $xx to SMA line and vice versa. I am using this for 1 day 1 min charts. Is there anything out there built for this already?

Thank you all

VD
 
Hi all,

I am looking for a custom watch list column that will tell me how close the price action is to the 180 period SMA line. For example if the price action is below the SMA line it would show negative $xx to SMA line and vice versa. I am using this for 1 day 1 min charts. Is there anything out there built for this already?

Thank you all

VD

Price Difference from moving average
Code:
#Price Difference from moving average
input price = close;
input length_1 = 3;
input AverageType = AverageType.SIMPLE;
def MA_1 = MovingAverage(AverageType, price, length_1);
plot scan = ma_1 - close;
 
Does a far from moving average indicator exist? One that would show when price is extended a considerable distance either above or below a moving average? Could be just the low or close of a candle that is stretched far from moving average. The indicator would show buy signal when price has fallen too far below moving average and sell signals when price has risen too high above it.
 
@Woodman78 Your concept is very subjective so unless you can determine the exact logic that such an indicator would use, that serves every circumstance, it would be almost impossible to code effectively... You're more than welcome to dabble with the concept and we'll help you along when and if you get stuck... The reality is that it's hard to determine whether a random extreme candle is simply an anomaly, caused by a specific one-time event, or an actual condition worthy of signaling the entry of a trade...
 
@rad14733 I believe what Im thinking would be something that would note when price goes below a certain amount of deviations from the moving average. But the standard deviations are slanted lines which I wish were horizontal instead, think eg.persons pivots indicator but again follows deviation values from moving average instead of pivot points.
 
@Woodman78 I've never done much with pivot points or support and resistance because they have never really fit in with my styles of trading or of those whom I have done custom statistical rules-based coding for over the years so I can't say whether or not something along those lines already exists or not... However, if it's solid rules based logic then it can be coded... Perhaps someone else will chime in with ideas...

Our posts are in the right place now... Thanks @BenTen...
 
Hello Community -

I'm looking to display labels on my charts and hoped to find someone with time and capable of helping.

Display Labels in upper corner.
1. Display whether the MACD Value line is "above or below" the MACD Average Line.
1b. For Daily Timeframe
1c. For active Intraday Timeframe

Display Label in upper corner.
2. Display the space between two averages (to determine how far apart they are).
2b. SMA(20) - SMA(50)

I hope the above was simple and concise enough for those reading.
 
Last edited:
Hello Community -

I'm looking to display labels on my charts and hoped to find someone with time and capable of helping.

Display Labels in upper corner.
1. Display whether the MACD Value line is "above or below" the MACD Average Line.
1b. For Daily Timeframe
1c. For active Intraday Timeframe

Display Label in upper corner.
2. Display the space between two averages (to determine how far apart they are).
2b. SMA(20) - SMA(50)

I hope the above was simple and concise enough for those reading.
Don't say I never did nothin for ya

Code:
declare upper;

# bsdvs23 request on 5.3.21
# wtf_dude

input fastLength = 12;
input slowLength = 26;
input MACDLength = 9;
input AverageType = AverageType.EXPONENTIAL;
input MA_Length1 = 20;
input MA_Length2 = 50;
input AverageType2 = AverageType.simple;


def agg1 = aggregationPeriod.day;
def agg2 = getaggregationPeriod();

##### MACD Day label

def ValueDay = MovingAverage(averageType, close("period" =agg1), fastLength) - MovingAverage(averageType, close("period" =agg1), slowLength);
def AvgDay = MovingAverage(averageType, ValueDay, MACDLength);

addlabel(yes, "MACD Day: " + if valueday>avgday then "Above" else "Below", if valueday>avgday then color.green else color.red);

##### MACD current timeframe label

def Value = MovingAverage(averageType, close("period"=agg2), fastLength) - MovingAverage(averageType, close("period"=agg2), slowLength);
def Avg = MovingAverage(averageType, Value, MACDLength);

addlabel(yes, "MACD: " + if value>avg then "Above" else "Below", if value>avg then color.green else color.red);

##### Moving Average Label

def MovAvg = MovingAverage(averageType2, close, MA_Length1); 
def MovAvg2 = MovingAverage(averageType2, close, MA_Length2); 

def diff = (movavg - movavg2)/Movavg2;

addlabel(yes,"MA distance: " + aspercent(diff), if diff>0 then color.green else color.red);
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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