input price = close;
input displace = 0;
input length = 20;
input Num_Dev_Dn = -1.0;
input Num_Dev_up = 1.0;
input averageType = AverageType.simple;
def sDev = stdev(data = price[-displace], length = length);
def MidLine = MovingAverage(averageType, data = price[-displace], length = length);
def LowerBand = MidLine + num_Dev_Dn * sDev;
plot UpperBand = MidLine + num_Dev_Up * sDev;
def aboveUpperBand = close > UpperBand;
def almostToUpperBand = close < UpperBand and close > UpperBand ;
UpperBand.AssignValueColor(if aboveUpperBand or almostToUpperBand then Color.BLACK else Color.WHITE);
AssignBackgroundColor(if aboveUpperBand then Color.GREEN else if almostToUpperBand then Color.YELLOW else Color.BLACK);