input price = close;
input displace = 0;
input length = 20;
input Num_Dev_Dn = -2.0;
input Num_Dev_up = 2.0;
input averageType = AverageType.EXPONENTIAL;
def sDev = stdev(data = price[-displace], length = length);
plot MidLine = MovingAverage(averageType, data = price[-displace], length = length);
plot LowerBand = MidLine + num_Dev_Dn * sDev;
plot UpperBand = MidLine + num_Dev_Up * sDev;
MidLine.SetPaintingStrategy(PaintingStrategy.LINE);
MidLine.SetStyle(curve.SHORT_DASH);
MidLine.SetDefaultColor(Color.GRAY);
UpperBand.SetPaintingStrategy(PaintingStrategy.LINE);
UpperBand.SetStyle(curve.LONG_DASH);
UpperBand.SetDefaultColor(Color.DARK_RED);
LowerBand.SetPaintingStrategy(PaintingStrategy.LINE);
LowerBand.SetStyle(curve.LONG_DASH);
LowerBand.SetDefaultColor(Color.DARK_GREEN);
def AvgExp = ExpAverage(price[-displace], length);
def UpSignal = if close[1] crosses below AvgExp and close crosses above AvgExp or close crosses above AvgExp then 1 else 0;
def DnSignal = if close[1] crosses above AvgExp and close crosses below AvgExp or close crosses below AvgExp then 1 else 0;
AssignPriceColor(if UpSignal or DnSignal then CreateColor(0,130,211) else Color.CURRENT);
def prange = high - low;
def phigh = high + prange * .2;
def plow = low - prange * .2;
def SignUpDn = if UpSignal or DnSignal then 1 else if SignUpDn[1] == 1 and UpSignal or DnSignal then 1 else 0;
#input debug = yes;
#plot x = if !debug then Double.NAN else SignUpDn;
plot SignUpDt = if UpSignal and signUpDn[-1] == 0 then plow else Double.NaN;
plot SignDnDt = if DnSignal and signUpDn[-1] == 0 then phigh else Double.NaN;
SignUpDt.SetDefaultColor(Color.YELLOW);
SignUpDt.SetStyle(Curve.POINTS);
SignUpDt.SetLineWeight(3);
SignDnDt.SetDefaultColor(Color.YELLOW);
SignDnDt.SetStyle(Curve.POINTS);
SignDnDt.SetLineWeight(3);