Plot distance from moving average in thinkscript?

D

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
 
horserider

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

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);
 
D

dj45

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

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

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?
Thanks in advance:)
 
E

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

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

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?
 
T

Trader_Vic

New member
VIP
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.

I would be interested in this as well. Let me know once you get this coded.
 
Ace78

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

cswu1211

New 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:
B

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 :)
 
P

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!
 
rad14733

rad14733

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

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

rad14733

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

ProfessorAR15

Member
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...
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?
 
rad14733

rad14733

Well-known member
VIP
So it isn't that you can't learn, you just don't want to... Maybe someone else will write your code for you... It doesn't sound like you've even tried...

As I have stated here previously, I have a problem with members of forums who just take and never give and never try to help themselves... I see far too many members taking advantage of some members here, especially @BenTen... Once @BenTen helps someone, selflessly, people just keep asking him to do more and more... That isn't the way things should work... I used to run a software forum years ago and the rule there was that we only helped those willing to help themselves... Start some code and I'm willing to help but I don't write free code on request/demand... Yet I will share code I write for myself... I'll tell you right now that I'm not the one with a skewed view of things here...
 

Similar threads

Top