########
# RelativeStrength_MultiTimeFrame
#
#
# 04.15.20 Version 1 RmS59
#
#
# This indicator calculates the Relative Strength of the underlying security to an alternate (SPX default)
# Allows user to select Aggregation Period for calculation
#
declare lower;
def price = FundamentalType.CLOSE;
input AggPeriod = AggregationPeriod.week;
input RelativeToSecurity = "SPX";
input Lookback = 50;
input smoothingPeriod1 = 5;
input smoothingPeriod2 = 8;
input smoothingPeriod3 = 34;
input showRSI3 = no;
input displayThresholds = yes;
input Threshold = 0.050;
input displayRSCrossOvers = no;
input showMvgAvgCross = yes;
input showTrendLabel = yes;
input hideLabel = no;
def price1 = close(Period = aggPeriod);
def price2 = Fundamental(price, RelativeToSecurity,AggPeriod);
def UnderlyingStrength = (price1 - price1[Lookback]) / price1[Lookback];
def BaseStrength = (price2 - price2[Lookback]) / price2[Lookback];
plot RSI = Round((1 + UnderlyingStrength) / (1 + BaseStrength), 3);
RSI.DefineColor("UpTrending", Color.GREEN);
RSI.DefineColor("UpNormal", Color.DARK_GREEN);
RSI.DefineColor("DnTrending", Color.RED);
RSI.DefineColor("DnNormal", Color.DARK_RED);
RSI.DefineColor("Flat", Color.WHITE);
RSI.AssignValueColor(
if RSI >= 1 + Threshold then RSI.Color("UpTrending")
else if RSI >= 1 then RSI.Color("UpNormal")
else if RSI <= 1 - Threshold then RSI.Color("DnTrending")
else if RSI < 1 then RSI.Color("DnNormal")
else RSI.Color("Flat"));
plot SmRSI1 = SimpleMovingAvg(RSI, smoothingPeriod1);
plot SmRSI2 = SimpleMovingAvg(RSI, smoothingPeriod2);
plot SmRSI3 = if showRSI3 then SimpleMovingAvg(RSI, smoothingPeriod3) else Double.NaN;
SmRSI1.DefineColor("Normal", Color.MAGENTA);
SmRSI1.AssignValueColor(RSI.TakeValueColor());#colorizeRSI1
SmRSI1.SetLineWeight(2);
SmRSI2.SetDefaultColor(Color.white);
#SmRSI1.AssignValueColor(RSI.TakeValueColor());
SmRSI2.SetLineWeight(1);
plot Base = if !IsNaN(close) then 1.00 else Double.NaN;
Base.SetDefaultColor(Color.WHITE);
plot PosCross = if RSI crosses above SmRSI1 and displayRSCrossOvers then SmRSI1 else Double.NaN;
PosCross.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
PosCross.setdefaultColor(color.green);
plot NegCross = if RSI crosses below SmRSI1 and displayRSCrossOvers then SmRSI1 else Double.NaN;
NegCross.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
NegCross.setdefaultColor(color.red);
plot BullishThreshold = if displayThresholds and !IsNaN(close) then 1 + Threshold else Double.NaN;
BullishThreshold.SetDefaultColor(Color.DARK_GREEN);
plot BearishThreshold = if displayThresholds and !IsNaN(close) then 1 - Threshold else Double.NaN;
BearishThreshold.SetDefaultColor(Color.DARK_RED);
BullishThreshold.setStyle(curve.short_dash);
BullishThreshold.setdefaultColor(color.dark_green);
BearishThreshold.setStyle(curve.short_dash);
BearishThreshold.setdefaultColor(color.dark_red);
AddLabel(!hideLabel, Lookback + " RS / " + RelativeToSecurity + ": " + RSI, RSI.TakeValueColor());
plot LongCross = if showMvgAvgCross and SmRSI1 crosses above SmRSI2 then SmRSI2 - 0.5*ticksize() else Double.NaN;
LongCross.setpaintingStrategy(paintingStrategy.ARROW_UP);
LongCross.setdefaultColor(color.green);
plot ShortCross = if showMvgAvgCross and SmRSI1 crosses below SmRSI2 then SmRSI1 + 0.5*ticksize() else Double.NaN;
ShortCross.setpaintingStrategy(paintingStrategy.ARROW_DOWN);
ShortCross.setdefaultColor(color.red);
plot Trend = if SmRSI1 > SmRSI2 then 1 else -1;
Trend.hide();
##############
Trend.DefineColor("UpTrending", Color.GREEN);
Trend.DefineColor("DnTrending", Color.RED);
Trend.AssignValueColor(
if Trend == 1 then Trend.Color("UpTrending")
else Trend.Color("DnTrending"));
Addlabel(showTrendLabel, "Trend = "+ if Trend == 1 then "Bullish" else "Bearish", Trend.takeValueColor());
SmRSI1.AssignValueColor(RSI.TakeValueColor());