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

# Add label
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:

suryabg2000

New member
VIP
Is there a way build scanner when SMA are close to each other 20, 50 & 200 SMA
ULZnQ4t.png
 
Last edited:

Similar threads

Top