# sym1_macd_rsi_00
# https://usethinkscript.com/threads/add-label-to-display-macd-rsi-values-of-a-different-symbol.12695/
# Add Label to display MACD, RSI values of a different symbol
declare lower;
def na = double.nan;
def bn = barnumber();
input symbol1 = "SPY";
def price1 = close(symbol1);
input show_plots = no;
#-------------------------------------
# macd
# TD Ameritrade IP Company, Inc. (c) 2007-2022
input fastLength = 12;
input slowLength = 26;
input MACDLength = 9;
input macd_averageType = AverageType.EXPONENTIAL;
input showBreakoutSignals = no;
#plot Value = MovingAverage(averageType, close, fastLength) - MovingAverage(averageType, close, slowLength);
def Value = MovingAverage(macd_averageType, price1, fastLength) - MovingAverage(macd_averageType, price1, slowLength);
def Avg = MovingAverage(macd_averageType, Value, MACDLength);
plot ZValue = value;
plot ZAvg = avg;
plot Diff = Value - Avg;
plot ZeroLine = 0;
plot UpSignal = if Diff crosses above ZeroLine then ZeroLine else Double.NaN;
plot DownSignal = if Diff crosses below ZeroLine then ZeroLine else Double.NaN;
zvalue.sethiding(!show_plots);
zavg.sethiding(!show_plots);
diff.sethiding(!show_plots);
zeroline.sethiding(!show_plots);
upsignal.sethiding(!show_plots);
downsignal.sethiding(!show_plots);
UpSignal.SetHiding(!showBreakoutSignals);
DownSignal.SetHiding(!showBreakoutSignals);
zValue.SetDefaultColor(GetColor(1));
zAvg.SetDefaultColor(GetColor(8));
Diff.SetDefaultColor(GetColor(5));
Diff.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
Diff.SetLineWeight(3);
Diff.DefineColor("Positive and Up", Color.GREEN);
Diff.DefineColor("Positive and Down", Color.DARK_GREEN);
Diff.DefineColor("Negative and Down", Color.RED);
Diff.DefineColor("Negative and Up", Color.DARK_RED);
Diff.AssignValueColor(if Diff >= 0 then if Diff > Diff[1] then Diff.color("Positive and Up") else Diff.color("Positive and Down") else if Diff < Diff[1] then Diff.color("Negative and Down") else Diff.color("Negative and Up"));
ZeroLine.SetDefaultColor(GetColor(0));
UpSignal.SetDefaultColor(Color.UPTICK);
UpSignal.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
DownSignal.SetDefaultColor(Color.DOWNTICK);
DownSignal.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
#-----------------------------
# rsi
# TD Ameritrade IP Company, Inc. (c) 2007-2022
#declare lower;
input length = 14;
input over_Bought = 70;
input over_Sold = 30;
input price = close;
input rsi_averageType = AverageType.WILDERS;
input rsi_showBreakoutSignals = no;
def NetChgAvg = MovingAverage(rsi_averageType, price - price[1], length);
def TotChgAvg = MovingAverage(rsi_averageType, AbsValue(price - price[1]), length);
def ChgRatio = if TotChgAvg != 0 then NetChgAvg / TotChgAvg else 0;
def RSI = 50 * (ChgRatio + 1);
plot zRSI = rsi;
plot OverSold = over_Sold;
plot OverBought = over_Bought;
plot rUpSignal = if RSI crosses above OverSold then OverSold else Double.NaN;
plot rDownSignal = if RSI crosses below OverBought then OverBought else Double.NaN;
zrsi.sethiding(!show_plots);
oversold.sethiding(!show_plots);
overbought.sethiding(!show_plots);
rUpSignal.sethiding(!show_plots);
rDownSignal.sethiding(!show_plots);
rUpSignal.SetHiding(!showBreakoutSignals);
rDownSignal.SetHiding(!showBreakoutSignals);
zRSI.DefineColor("OverBought", GetColor(5));
zRSI.DefineColor("Normal", GetColor(7));
zRSI.DefineColor("OverSold", GetColor(1));
zRSI.AssignValueColor(if RSI > over_Bought then zRSI.color("OverBought") else if RSI < over_Sold then zRSI.color("OverSold") else zRSI.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);
#----------------------
addlabel(1, symbol1, color.yellow);
addlabel(1, "MACD " + round(value,2), color.yellow);
addlabel(1, "RSI " + round(rsi,2), color.yellow);
#