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:
- length of MA
- simple or exponential
- values for horizontal lines
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: