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

New member
[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:)
 

Top