Weekly Relative Strength vs SPX

RmS59

Member
I am trying to create a weekly Relative_Strength indicator that compares a security to the SPX (or other)

I can not seem to get the SPX data using Aggregation Period = weekly.

Here is what I use for the Daily RS_SPX
-------------------------------


input price = FundamentalType.CLOSE;
input RelativeToSecurity = "SPX";
input Lookback = 50;
input smoothingPeriod1 = 8;
input smoothingPeriod2 = 21;

def price1 = Fundamental(price);
def price2 = Fundamental(price, RelativeToSecurity);


def UnderlyingStrength = (price1 - price1[Lookback]) / price1[Lookback];
def BaseStrength = (price2 - price2[Lookback]) / price2[Lookback];

plot RSI = Round((1 + UnderlyingStrength) / (1 + BaseStrength), 3);


-----------------------------

I then look at 2 moving averages to see crosses - defing longer term performance.

I know that I could simply use periods that are 5* the daily period, but that does not provide correct results.

Can anyone help ??
 
It seems that sometimes I just need to ask the question to get motivated.....

I had tried to do this for a while, but just stumbled upon something. This works:

input price = FundamentalType.CLOSE;
input RelativeToSecurity = "SPX";
input Lookback = 50;


def price1 = CLOSE(period = AggregationPeriod.WEEK);
def price2 = Fundamental(price, RelativeToSecurity, AggregationPeriod.WEEK);


def UnderlyingStrength = (price1 - price1[Lookback]) / price1[Lookback];
def BaseStrength = (price2 - price2[Lookback]) / price2[Lookback];

plot RSI = Round((1 + UnderlyingStrength) / (1 + BaseStrength), 3);

-------------

not using fundamental type for price1 was the key.
 
Last edited:
It seems that sometimes I just need to ask the question to get motivated.....

I had tried to do this for a while, but just stumbled upon something. This works:

input price = FundamentalType.CLOSE;
input RelativeToSecurity = "SPX";
input Lookback = 50;


def price1 = CLOSE(period = AggregationPeriod.WEEK);
def price2 = Fundamental(price, RelativeToSecurity, AggregationPeriod.WEEK);


def UnderlyingStrength = (price1 - price1[Lookback]) / price1[Lookback];
def BaseStrength = (price2 - price2[Lookback]) / price2[Lookback];

plot RSI = Round((1 + UnderlyingStrength) / (1 + BaseStrength), 3);

-------------

not using fundamental type for price1 was the key.
Hi, how do i apply these code? do you mind posting a screenshot how the indicator looks like on the chart or scan. Thank you
 
Hey man, check out the RSMK indicator on TOS. It f*ckin rules, was just added, and I find it much easier to read at a glance. You can change the time period. I think it may be exactly what you may be looking for. I THINK it may have originally been done by one of the guys on here?
 
Hey man, check out the RSMK indicator on TOS. It f*ckin rules, was just added, and I find it much easier to read at a glance. You can change the time period. I think it may be exactly what you may be looking for. I THINK it may have originally been done by one of the guys on here?
Wow..thanks alot...
 
Sorry for the VERY LATE RESPONSE!

You asked how I use it - I have incorporated it into a study that defines a stock's movement strength using a variety of measurements that include trend, momentum, relative strength (SPX), Overbought/Oversold and Accumulation/Distribution.

Here is the complete code:

Code:
########
# 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());
 
Sorry for the VERY LATE RESPONSE!

You asked how I use it - I have incorporated it into a study that defines a stock's movement strength using a variety of measurements that include trend, momentum, relative strength (SPX), Overbought/Oversold and Accumulation/Distribution.

Here is the complete code:

Code:
########
# 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());
Thank you very much.
 

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

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
529 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