#Mntman @funwiththinkscript
# Plot longer moving average support lines
# 20211004 added option ot plot horizontal lines across chart per @usthinkscript request
input lines_across_chart = yes;
def lastBar = HighestAll(if !IsNaN(close) then BarNumber() else 0);
def mostRecentClose = HighestAll(if BarNumber() == lastBar then close else 0);
def barNumber = BarNumber();
def barCount = HighestAll(If(IsNaN(close), 0, barNumber));
input shtAvg = 50;
input medAvg = 100;
input lngAvg = 200;
input avgType = {default "simple", "exponential"};
def avg1;
def avg2;
def avg3;
switch (avgType) {
case "simple":
avg1 = Average(close, shtAvg);
avg2 = Average(close, medAvg);
avg3 = Average(close, lngAvg);
case "exponential":
avg1 = ExpAverage(close, shtAvg);
avg2 = ExpAverage(close, medAvg);
avg3 = ExpAverage(close, lngAvg);
}
input proximity = 10; #ref 0.5 = 1/2%
def value1 = GetValue(avg1, BarNumber() - HighestAll(lastBar));
def value2 = GetValue(avg2, BarNumber() - HighestAll(lastBar));
def value3 = GetValue(avg3, BarNumber() - HighestAll(lastBar));
def inRange1 = mostRecentClose > (value1 * (1 - proximity / 100)) and mostRecentClose < (value1 * (1 + proximity / 100));
def inRange2 = mostRecentClose > (value2 * (1 - proximity / 100)) and mostRecentClose < (value2 * (1 + proximity / 100));
def inRange3 = mostRecentClose > (value3 * (1 - proximity / 100)) and mostRecentClose < (value3 * (1 + proximity / 100));
def avg1Line = if lines_across_chart then highestall(value1) else if barNumber == 1 then Double.NaN else if barNumber == barCount then avg1 else if barNumber == barCount then Double.NaN else avg1Line[1];
def avg2Line = if lines_across_chart then highestall(value2) else if barNumber == 1 then Double.NaN else if barNumber == barCount then avg2 else if barNumber == barCount then Double.NaN else avg2Line[1];
def avg3Line = if lines_across_chart then highestall(value3) else if barNumber == 1 then Double.NaN else if barNumber == barCount then avg3 else if barNumber == barCount then Double.NaN else avg3Line[1];
plot sandline1 = avg1Line;
sandline1.AssignValueColor(globalColor("sand" ));
sandline1.SetLineWeight(1);
sandline1.SetHiding(!inRange1);
AddChartBubble(yes, if inRange1 and BarNumber() == HighestAll(Lastbar+3) then sandline1 else double.nan, shtAvg + (if avgType == avgType.simple then "sma" else "ema" ), globalColor("sand" ), 0);
plot sandline2 = avg2Line;
sandline2.AssignValueColor(globalColor("sand" ));
sandline2.SetLineWeight(1);
sandline2.SetHiding(!inRange2);
AddChartBubble(yes, if inRange2 and BarNumber() == HighestAll(Lastbar+3) then sandline2 else double.nan, medAvg + (if avgType == avgType.simple then "sma" else "ema" ), globalColor("sand" ), 0);
plot sandline3 = avg3Line;
sandline3.AssignValueColor(globalColor("sand" ));
sandline3.SetLineWeight(1);
sandline3.SetHiding(!inRange3);
AddChartBubble(yes, if inRange3 and BarNumber() == HighestAll(Lastbar+3) then sandline3 else double.nan, lngAvg + (if avgType == avgType.simple then "sma" else "ema" ), globalColor("sand" ), 0);
AddLabel(yes, "proximity: " + proximity + "%", color.white);
AddLabel(yes, shtAvg + (if avgType == avgType.simple then "sma: " else "ema: " ) + round(avg1,2), globalColor("sand" )); #for reference only
AddLabel(yes, medAvg + (if avgType == avgType.simple then "sma: " else "ema: " ) + round(avg2,2), globalColor("sand" )); #for reference only
AddLabel(yes, lngAvg + (if avgType == avgType.simple then "sma: " else "ema: " ) + round(avg3,2), globalColor("sand" )); #for reference only
DefineGlobalColor("sand", CreateColor(255,204,102)); #sand