#
# TD Ameritrade IP Company, Inc. (c) 2007-2023
#
declare lower;
input length = 14;
input over_Bought = 70;
input over_Sold = 30;
input price = close;
input averageType = AverageType.WILDERS;
input showBreakoutSignals = no;
def NetChgAvg = MovingAverage(averageType, price - price[1], length);
def TotChgAvg = MovingAverage(averageType, AbsValue(price - price[1]), length);
def ChgRatio = if TotChgAvg != 0 then NetChgAvg / TotChgAvg else 0;
plot RSI = 50 * (ChgRatio + 1);
plot OverSold = over_Sold;
plot OverBought = over_Bought;
plot UpSignal = if RSI crosses above OverSold then OverSold else Double.NaN;
plot DownSignal = if RSI crosses below OverBought then OverBought else Double.NaN;
UpSignal.SetHiding(!showBreakoutSignals);
DownSignal.SetHiding(!showBreakoutSignals);
RSI.DefineColor("OverBought", GetColor(5));
RSI.DefineColor("Normal", GetColor(7));
RSI.DefineColor("OverSold", GetColor(1));
RSI.AssignValueColor(if RSI > over_Bought then RSI.Color("OverBought") else if RSI < over_Sold then RSI.Color("OverSold") else RSI.Color("Normal"));
OverSold.SetDefaultColor(GetColor(8));
OverBought.SetDefaultColor(GetColor(8));
UpSignal.SetDefaultColor(Color.UPTICK);
UpSignal.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
DownSignal.SetDefaultColor(Color.DOWNTICK);
DownSignal.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
#### Bubble at last bar using IsNaN(RSI[-1])
AddChartBubble(IsNaN(RSI[-1]), RSI, RSI, if RSI < 30 then Color.CYAN else if RSI > 70 then Color.MAGENTA else Color.YELLOW);
#### Bubble is movable
input bubble = yes;
input bubblemover = 3;
def b = bubblemover;
def b1 = b + 1;
AddChartBubble(bubble and !IsNaN(RSI[b1]) and IsNaN(RSI[b]), RSI[b1], RSI[b1], if RSI[b1] < 30 then Color.CYAN else if RSI[b1] > 70 then Color.MAGENTA else Color.YELLOW);