Plot distance from moving average in thinkscript?

dj45

New member
I'm guessing someone has built this wheel before. Basically looking to plot the distance in % that close price is from a moving average. Since this plot will essentially oscillate, plotting horizontal lines to mark x% above and x% below makes it easy to see if price is overextended in either direction. So user defined inputs would be:
1. length of MA
2. simple or exponential
3. values for horizontal lines
Edit: here the code I found:

Code:
``````# MovingAvgDistance
# DREWGRIFFITH5 (C) 2015

declare lower;

input price = close;
input averageType = {default Exponential, Simple, Weighted, Wilders, Hull};
input length = 300;

def avg;

switch (averageType) {

case Exponential:
avg = ExpAverage(price, length);
case Simple:
avg = Average(price, length);
case Weighted:
avg = wma(price, length);
case Wilders:
avg = WildersAverage(price, length);
case Hull:
avg = HullMovingAvg(price, length);

}

plot dist = ((price - avg) / ((price + avg) / 2)) * 100;
dist.SETLINEWEIGHT(3);

plot prev_high = Highest(dist, LENGTH = length)[1];
plot prev_low = Lowest(dist, LENGTH = length)[1];
prev_high.hide();
prev_low.hide();

dist.SETDEFAULTCOLOR(Color.cyan);
dist.AssignValueColor(if dist > prev_high then Color.RED else if dist < prev_low then Color.GREEN else Color.gray);

plot centerline = 0;
centerline.SETDEFAULTCOLOR(Color.yellow);

AddLabel(dist, dist, if dist > prev_high then Color.RED else if dist < prev_low then Color.GREEN else Color.gray);``````

Last edited by a moderator:

horserider

Well-known member
VIP
Code:
``````declare lower;

input price = close;
input length = 200;
input AverageType = {default Simple, Exponential};
input pdl = 3;
input ndl = -3;
def MA = MovingAverage(AverageType, ( price[0]), length);
plot DEV = (price / MA - 1 ) * 1000;

plot pdll = pdl;
plot ndll = ndl;
plot zero = 0;

DEV.SetDefaultColor(GetColor(1));

AddLabel(1, "Percentage from " + length + " MA: " + AsPercent(DEV * .01), if DEV > 0 then Color.GREEN else Color.PINK);``````

Last edited:

markos

Well-known member
VIP
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;``````

horserider

Well-known member
VIP
Markos stole the label idea and added it to the previous study.

dj45

New member
Thanks guys, this looks great! And adding it to the label is a nice touch for quick scanning.

Johnny Cash

Member
VIP
[type = stock]
AND [country = US]
AND [Daily SMA(20,Daily Volume) > 500000]
AND [Daily SMA(50,Daily Close) > 10]
AND [PctChange(250,close) < -30]
AND [PctChange(32,close) > 10]

zeek

Active member
2019 Donor
As my titles says, i need a script that alerts me with audio notification and also a chart label when the price is close to 200EMA. And if it`s possible, i only need the alert to trigger if the price is in a downward direction towards the EMA.

I would like the alert to trigger 10 cents before hitting the EMA and the label should be active as long as price is within a 20 cents range from the EMA price. So for example, lets say 200EMA price is at 10,40 and then label should be active on chart between 10,30 - 10,50.

Can anyone help me code this?

Eli

New member
I'm looking for a script for finding stocks that the 10 EMA is above 3% from the 50 EMA in 5min chart.
TOS only has above or below.

luicius

New member
I am looking for a scan similar to this image where the overall security is trending downward and it's down by at least 20% from 200 periods ago - daily.

I thought something like this would work but it produces some results where the overall security is breaking the downward trend
Code:
``````input length = 200;
plot scan = Average(close, length) < Average(close, length)[length];``````

My goal is to have the overall security us trending down and it's down at least 20% 200 periods ago.
I would also be interested in one that is trending upward and up by at least 20% 200 periods ago.

Any help on how to achieve these desired results will greatly be appreciated.

111catalina

New member
I am looking for a scrip that can give me a signal when current price is 2% from EMA, Do you know where I can get it?

New member
VIP
@luicius I would be interested in this as well. Let me know once you get this coded.

Ace78

New member
Good Afternoon.
I'm looking for a 20 or 21 SMA that has the ability to change colors based on the candles % extended from the 20 SMA.
My Trading strategy, game plan, is to buy pullbacks to the 20 SMA and sell when over extended. To have a 20 SMA to change colors to alert me when I'm in a selling zone or buying zone would be great instead of using the measuring tool on ToS every time.

Idea of colors on the 20 SMA: Less than or equal to 4% from SMA is green; greater than 4% up to 8% orange, above 8% Red. Normal size width of the Line would be preferred, just like a regular SMA from ToS.
This would apply for both sides of the SMA. I would love to have this indicator if it does exist and would like to see if other people would be interested in it if it doesn't.
Any assistance would be greatly appreciated.

cswu1211

Member
VIP
Could anyone help using thinkscript to Identify clustering of several moving averages (5,20,50) converging at the beginning of downward or upward trend

Last edited:

benjaminjames11

New member
can anyone help me write a scan in think or swim for a stock that OPENED a minimum distance from the 9ema - but is now coming close to touching it? would love to find something to help me know when a stock on my watchlist is about to touch the 9ema. i trade a lot of gap stocks - so typically at open the stock is not super close to the 9ema - but i look for a bounce off that level when it comes close after trying to close the gap. PLEASE HELP THANK YOU

ProfessorAR15

Member
Hello.

Is it possible on TOS to be able to know how far apart the EMA 3 and EMA 9 are away from each other at a given time?

and If so, could this value be converted into a chart label AND a column on a watchlist?

I am really REALLY wanting to figure this out, so anyone who can help would be a hero! Thank you!

Well-known member
VIP
Hello.

Is it possible on TOS to be able to know how far apart the EMA 3 and EMA 9 are away from each other at a given time?

and If so, could this value be converted into a chart label AND a column on a watchlist?

I am really REALLY wanting to figure this out, so anyone who can help would be a hero! Thank you!
There is a way and it's called subtraction... The resulting amount will either be positive or negative...

ProfessorAR15

Member
There is a way and it's called subtraction... The resulting amount will either be positive or negative...
This isn’t helpful. Was the sarcasm intentional or on accident?

I realize it’s a elementary idea, but I struggle with the coding portion.

Well-known member
VIP
This isn’t helpful. Was the sarcasm intentional or on accident?

I realize it’s a elementary idea, but I struggle with the coding portion.
Not being sarcastic at all... Merely pointing out how easy it can be... Subtract one moving average from the other... We should all learn how to do some coding at some point instead of expecting others to do our work for us... I started self-teaching myself in 1984 when I bought a computer to do stock market analysis, having never touched a computer before... I rarely ever ask for help but, rather, dig in and figure things out - and that goes for everything from electrical to plumbing to anything else that comes along... I'm willing to help point people in the right direction but refuse to just write code for free when it's part of what I do for a living... Take this however you like but you're never too old to learn... Not to mention that it's gratifying to know you can do things yourself...

You asked if it could be done and I told you how it could...

ProfessorAR15

Member
@rad14733 You didn’t tell me how though, you restated the obvious. We all know what subtraction is. I thought this was a place where people HELP people. What I’m asking for isn’t the key to riches or anything.

Ive asked questions like this before and have gotten scripts sent to me Bc people were willing to help and/or enjoy coding. I’m not trying to take advantage of anyone or anything. But now that I’ve experienced your wonderful personality, I look at this site much differently.

Unfortunately, I work full time so don’t have a lot of time to sit and try to teach myself, but I have been able to get basic things done when I can. If I had all the time in the world, or I did this for a living, chances are I wouldn’t need to come here for help, ya know?