Mansfield Relative Strength For ThinkOrSwim

vcolon221

New member
Author states:
Use this indicator to compare how security is performing in compare with preferred index (SPX by default).
greater than 0 outperforming
less than 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

KYRACh6.png

@samer800 could you convert mansfield relative strength indicator from Tradingview?
https://www.tradingview.com/script/F4g647QI-Mansfield-Relative-Strength-indicator/
any help would be appreciated. unless there is a comparable indicator already available you can suggest.
 
Last edited by a moderator:

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

@samer800 could you convert mansfield relative strength indicator from Tradingview?
https://www.tradingview.com/script/F4g647QI-Mansfield-Relative-Strength-indicator/
any help would be appreciated. unless there is a comparable indicator already available you can suggest.
Ruby:
//@version=3

// 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")

index = input(title="Index", type=symbol, defval="SP:SPX")
sector = input(title="Sector (optional)", type=symbol, defval="SP:SPX")
industry = input(title="Industry (optional)", type=symbol, defval="SP:SPX")

ma_type = input(title="Which moving average to use?", defval="SMA", options=["SMA", "WMA", "EMA"])
len_daily = input(title="MA length for Daily", defval=200)
len_weekly = input(title="MA length for Weekly", defval=52)
len_monthly = input(title="MA length for Monthly", defval=10)
len_other = input(title="MA length for all other periods", defval=52)
val = close


len = period == "W" ? len_weekly : (period == "D" ? len_daily : (period == "M" ? len_monthly : len_other))

ma_func(x, length) =>
    ma_type == "WMA" ? wma(x, length) : (ma_type == "SMA" ? sma(x, length) : ema(x, length))

 
calc_mrs_func(x, ind, length) =>
    ((x / security(ind, period, x)) / ma_func((x / security(ind, period, x)), length) - 1) * 10


mrs_index = calc_mrs_func(val, index, len)
mrs_sector = calc_mrs_func(val, sector, len)
mrs_industry = calc_mrs_func(val, industry, len)
c = sign(mrs_index) + sign(mrs_sector) + sign(mrs_industry)

bgcolor(c == 3 ? color(green, 80) : c == 2 ? color(green, 75) : c == 1 ? color(green, 70) : c == -1 ? color(red, 70) : c == -2 ? color(red, 75) : c == -3 ? color(red, 80) : gray)
plot(mrs_index, linewidth=3, title="MRS index")
plot(mrs_sector != mrs_index ? mrs_sector : na, linewidth=2, title="MRS sector")
plot(mrs_industry != mrs_index ? mrs_industry : na, linewidth=1, title="MRS industry")
hline(price=0, linestyle=dashed, title="Zero baseline")
check the below

CSS:
#// 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
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
500 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top