#// Use this indicator to compare how security is performing in compare with prefered index (SPX by default).
#// > 0 outperforming
#// < 0 underperforming
#// Works best for weekly, but can be applied to monthly and daily charts. It will be rather useless to use it in smaller timeframes
#// Apply it to SPX, industry index, sector index or other security in similar sector
#// UPDATE 1: Added sector and industry as optional params. Leave them same as index if you don't want to use them
#study("Mansfield Relative Strength indicator")
# converted by Sam4Cok@Samer800 - 04/2024
declare lower;
input index = "SPX"; #(title="Index", type=symbol, defval="SP:SPX")
input sector = "SPX";#(title="Sector (optional)", type=symbol, defval="SP:SPX")
input industry = "SPX";#(title="Industry (optional)", type=symbol, defval="SP:SPX")
input ma_type = AverageType.SIMPLE; #="Which moving average to use?", defval="SMA", options=["SMA", "WMA", "EMA"])
input source = FundamentalType.CLOSE;
input length_daily = 200; #(title="MA length for Daily", defval=200)
input length_weekly = 52; #(title="MA length for Weekly", defval=52)
input length_monthly = 10; #(title="MA length for Monthly", defval=10)
input len_other = 52; #(title="MA length for all other periods", defval=52)
def na = Double.NaN;
def last = isNaN(close);
def pos = Double.POSITIVE_INFINITY;
def neg = Double.NEGATIVE_INFINITY;
def period = GetAggregationPeriod();
def w = AggregationPeriod.WEEK;
def m = AggregationPeriod.MONTH;
def d = AggregationPeriod.DAY;
DefineGlobalColor("up1", CreateColor(0, 139, 0));
DefineGlobalColor("up2", CreateColor(0, 100, 0));
DefineGlobalColor("up3", CreateColor(0, 61, 0));
DefineGlobalColor("dn1", CreateColor(178, 0, 0));
DefineGlobalColor("dn2", CreateColor(139, 0, 0));
DefineGlobalColor("dn3", CreateColor(100, 0, 0));
def len = if period == w then length_weekly else
if period == d then length_daily else
if period == m then length_monthly else len_other;
def srcSym = Fundamental(FundamentalType = source);
def srcIdx = Fundamental(FundamentalType = source, Symbol = index);
def srcSec = Fundamental(FundamentalType = source, Symbol = sector);
def srcInd = Fundamental(FundamentalType = source, Symbol = industry);
script calc_mrs_func {
input x = close;
input ind = close;
input length = 14;
input ma_type = AverageType.SIMPLE;
def nom = x / ind;
def dnom = MovingAverage(ma_type, nom, length);
def mrs = (nom / dnom - 1) * 10;
plot out = mrs;
}
def mrs_index = calc_mrs_func(srcSym, srcIdx, len, ma_type);
def mrs_sector = calc_mrs_func(srcSym, srcSec, len, ma_type);
def mrs_industry = calc_mrs_func(srcSym, srcInd, len, ma_type);
def c = sign(mrs_index) + sign(mrs_sector) + sign(mrs_industry);
plot mrsINDX = mrs_index; #, linewidth=3, title="MRS index")
plot mrsSector = if mrs_sector != mrs_index then mrs_sector else na; #, linewidth=2, title="MRS sector")
plot mrsIndustry = if mrs_industry != mrs_index then mrs_industry else na; #, linewidth=1, title="MRS industry")
plot zero = if last then na else 0;
mrsINDX.SetLineWeight(3);
mrsSector.SetLineWeight(3);
zero.SetDefaultColor(Color.GRAY);
mrsINDX.SetDefaultColor(Color.CYAN);
mrsSector.SetDefaultColor(Color.MAGENTA);
mrsIndustry.SetDefaultColor(Color.YELLOW);
#-- bg
def c3 = c==3 or c[1]==3;
def c2 = c==2 or c[1]==2;
def c1 = c==1 or c[1]==1;
def n3 = c==-3 or c[1]==-3;
def n2 = c==-2 or c[1]==-2;
def n1 = c==-1 or c[1]==-1;
AddCloud(if c3 then pos else na, neg, GlobalColor("up3"));
AddCloud(if c2 then pos else na, neg, GlobalColor("up2"));
AddCloud(if c1 then pos else na, neg, GlobalColor("up1"));
AddCloud(if n3 then pos else na, neg, GlobalColor("dn3"));
AddCloud(if n2 then pos else na, neg, GlobalColor("dn2"));
AddCloud(if n1 then pos else na, neg, GlobalColor("dn1"));
#-- EDN of CODE