# rs_normalize_compare_00
#https://usethinkscript.com/threads/normalized-scale-for-relative-strength-rs.12072/
# How can I normalize the stocks' RS ratios to compare them?
#==========================================
# compare_8stocks_rel_str_00
#https://usethinkscript.com/threads/a-chart-that-compares-relative-strength-against-spy.12637/
#UnAnswered A chart that compares relative strength against spy
declare lower;
def na = double.nan;
#=======================================
script normalizePlot {
input data = close;
input newRngMin = 0;
input newRngMax = 100;
def HHData = HighestAll(data);
def LLData = LowestAll(data);
plot nr = (((newRngMax - newRngMin)*(data - LLData))/(HHData - LLData)) + newRngMin;
}
#def a = normalizePlot(data, newmin, newmax);
#=======================================
input CorrelationWithSecurity = "SPY";
def cls_corr = close(CorrelationWithSecurity);
#plot RS = if close_corr == 0 then 0 else close / close_corr;
#RS.SetDefaultColor(GetColor(6));
#def sr = CompoundValue("historical data" = RS, "visible data" = if IsNaN(sr[1]) then RS else sr[1]);
#plot SRatio = sr;
#SRatio.SetDefaultColor(GetColor(5));
#----------------
# symbol1 is the main stock.
# all others will be scaled to stock1 data
# use symbol ZZ for non entries
input Symbol1_primary = "AAPL";
#hint Symbol1_primary: This stock determines the min and max boundries. the other stocks will be scaled to be within the limits of this stock. This symbol needs to be entered.
def cls1 = close(Symbol1_primary);
def rs1 = if isnan(cls1) then na else if cls_corr == 0 then na else cls1/cls_corr;
def n1 = rs1;
plot z1 = n1;
z1.hidebubble();
def s1hi = highestall(rs1);
def s1lo = lowestall(rs1);
def s1rng = s1hi - s1lo;
input show_upper_lower_lines = yes;
plot s1top = if show_upper_lower_lines and !isnan(close) then s1hi else na;
plot s1bot = if show_upper_lower_lines and !isnan(close) then s1lo else na;
s1top.setdefaultcolor(color.gray);
s1bot.setdefaultcolor(color.gray);
s1top.hidebubble();
s1bot.hidebubble();
#---------------------------
#def a = normalizePlot(data, newmin, newmax);
input Symbol2 = "MSFT";
#hint symbol2: Enter a stock symbol for any of the symbols 2 to 8. \nIf you don't want to enter a stock, use ZZ.
def cls2 = close(Symbol2);
#def rs2 = if cls_corr == 0 then 0 else cls2/cls_corr;
def rs2 = if isnan(cls2) then na else if cls_corr == 0 then na else cls2/cls_corr;
def n2 = normalizePlot(rs2, s1lo, s1hi);
plot z2 = n2;
z2.hidebubble();
input Symbol3 = "WMT";
def cls3 = close(Symbol3);
def rs3 = if isnan(cls3) then na else if cls_corr == 0 then na else cls3/cls_corr;
def n3 = normalizePlot(rs3, s1lo, s1hi);
plot z3 = n3;
z3.hidebubble();
input Symbol4 = "ZZ";
def cls4 = close(Symbol4);
def rs4 = if isnan(cls4) then na else if cls_corr == 0 then na else cls4/cls_corr;
def n4 = normalizePlot(rs4, s1lo, s1hi);
plot z4 = n4;
z4.hidebubble();
input Symbol5 = "ZZ";
def cls5 = close(Symbol5);
def rs5 = if isnan(cls5) then na else if cls_corr == 0 then na else cls5/cls_corr;
def n5 = normalizePlot(rs5, s1lo, s1hi);
plot z5 = n5;
z5.hidebubble();
input Symbol6 = "ZZ";
def cls6 = close(Symbol6);
def rs6 = if isnan(cls6) then na else if cls_corr == 0 then na else cls6/cls_corr;
def n6 = normalizePlot(rs6, s1lo, s1hi);
plot z6 = n6;
z6.hidebubble();
input Symbol7 = "ZZ";
def cls7 = close(Symbol7);
def rs7 = if isnan(cls7) then na else if cls_corr == 0 then na else cls7/cls_corr;
def n7 = normalizePlot(rs7, s1lo, s1hi);
plot z7 = n7;
z7.hidebubble();
input Symbol8 = "ZZ";
def cls8 = close(Symbol8);
def rs8 = if isnan(cls8) then na else if cls_corr == 0 then na else cls8/cls_corr;
def n8 = normalizePlot(rs8, s1lo, s1hi);
plot z8 = n8;
z8.hidebubble();
#------------------------------
# draw bubbles , to the right of last bar, with symbol name
input show_name_bubbles = yes;
def off = 5;
def x = show_name_bubbles and (!isnan(close[off]) and isnan(close[off-1]) );
addchartbubble(x, rs1[off], Symbol1_primary, z1.TakeValueColor(), yes);
addchartbubble(x, n2[off], Symbol2, z2.TakeValueColor(), yes);
addchartbubble(x, n3[off], Symbol3, z3.TakeValueColor(), yes);
addchartbubble(x, n4[off], Symbol4, z4.TakeValueColor(), yes);
addchartbubble(x, n5[off], Symbol5, z5.TakeValueColor(), yes);
addchartbubble(x, n6[off], Symbol6, z6.TakeValueColor(), yes);
addchartbubble(x, n7[off], Symbol7, z7.TakeValueColor(), yes);
addchartbubble(x, n8[off], Symbol8, z8.TakeValueColor(), yes);
#-----------------------------
input show_labels = yes;
addlabel(show_labels, " ", color.black);
addlabel(show_labels and !isnan(rs1), Symbol1_primary, z1.TakeValueColor());
addlabel(show_labels and !isnan(rs2), Symbol2, z2.TakeValueColor());
addlabel(show_labels and !isnan(rs3), Symbol3, z3.TakeValueColor());
addlabel(show_labels and !isnan(rs4), Symbol4, z4.TakeValueColor());
addlabel(show_labels and !isnan(rs5), Symbol5, z5.TakeValueColor());
addlabel(show_labels and !isnan(rs6), Symbol6, z6.TakeValueColor());
addlabel(show_labels and !isnan(rs7), Symbol7, z7.TakeValueColor());
addlabel(show_labels and !isnan(rs8), Symbol8, z8.TakeValueColor());
addlabel(show_labels, " ", color.black);
addlabel(show_labels, "RS of " + CorrelationWithSecurity, color.yellow);
def offc = off + 30;
def xc = (!isnan(close[offc]) and isnan(close[offc-1]) );
#def minc = min(rs1,min(rs2,min(rs3,min(rs4,min(rs5,min(rs6,min(rs7,rs8)))))));
# doesnt work, some values are na, so result is na
#def minc = min(n1,min(n2,min(n3,min(n4,min(n5,min(n6,min(n7,n8)))))));
input show_compare_stock_bubble = yes;
addchartbubble(show_compare_stock_bubble and xc, s1lo,
"Relative\nStrength\nof stocks\ncompared\nto " +
CorrelationWithSecurity + "\n" +
"chart is scaled\nto Stock1\n" + Symbol1_primary
, Color.yellow, yes);
#
input shrink_the_plots = yes;
def zoom_per = 0.9;
def ztop = s1hi * ( 1 + (zoom_per/100));
def zbot = s1lo * ( 1 - (zoom_per/100));
plot zzt = if shrink_the_plots then ztop else na;
plot zzb = if shrink_the_plots then zbot else na;
zzt.setdefaultcolor(color.black);
zzb.setdefaultcolor(color.black);
zzt.hidebubble();
zzb.hidebubble();
addchartbubble(0,0,
n1 + "\n" +
n2 + "\n" +
n3 + "\n" +
n4 + "\n" +
n5 + "\n" +
n6 + "\n" +
n7 + "\n" +
n8
, color.yellow, no);
#