Relative Strength Z-Score Histogram For ThinkOrSwim

justAnotherTrader

Well-known member
VIP
VIP Enthusiast
I built this indicator to solve a specific problem: identifying sector rotations before they become obvious. Turns out though, this thing can do a lot more than that. While price tells you where a stock is, Relative Strength (RS) Z-Scores tell you how much energy is actually behind the move compared to the rest of the market.

What started as a tool for quick rotations has evolved into a complete framework for navigating market cycles.

How it Works​

The study combines three distinct layers of signal:

  • The White Line (Z-Score): This represents the Fundamental Reality. It normalizes performance over a 6-month window to show if a sector is a true leader (Z > 0) or a laggard (Z < 0).
  • The Histogram (MACD-Style): This captures the Market Mood. When the histogram turns green, the Expectations Gap is narrowing—the market is starting to price in a turnaround even if the Z-score is still low.
  • The Cloud: This defines the Regime. A blue cloud signifies a self-reinforcing reflexive trend (Soros), where you want to be positioning rather than predicting.

Why It is Useful​

By triangulating these values, I can separate tradable excuses from fundamental shifts. It allows me to spot Mood Exhaustion at statistical extremes (Z > 2.0 or Z < -2.0) and identify the exact moment a new industry begins to lead the pack.

Let me walk you through an example of me using it to find a stock I want to trade:

Sector Outperformance: Notice in the following image in the shaded area. When the Z-Score crossed above the zero-line it indicated that XLI was consistently outperforming our benchmark (SPY)

FFtsK7a.png


Industry Outperformance: Now I go through different ETF's that represent different industries of the Industrial Sectors and I come across one called PAVE that represents the power and engineering industrials and notice the following chart. I am getting a lot of bullish trending action and the stocks are respecting the 21EMA at the shaded areas, now using XLI as my benchmark

JFdEu1p.png


Individual Company: Lastly I drill down into the specific companies of ETF PAVE (Using PAVE as my new benchmark) and I find FIX. Notice that FIX has been outperforming its own industry for quite a while, and lining up the time from the shaded area on the chart above would have been an opportune buying time.

1qNpsRI.png


Note that I have been trading FIX and have mentioned it over in the sites discord recently. It is up over 20% in the last month, while the overall market is struggling to keep up. I hope you find this indicator useful.

Cheers!

Code:
# RS Z-Score MACD-Style with Z-Line & Signal
# Fixed Color Constants for ThinkScript

declare lower;

input benchmark = "SPY";
input zLength = 126;  
input fastLen = 10;
input slowLen = 30;
input signalLen = 9;

# --- Relative Strength Logic ---
def benchClose = close(symbol = benchmark);
def rs = if !IsNaN(benchClose) and benchClose > 0 then close / benchClose else Double.NaN;

# --- RS Z-Score (The "Reality" Line) ---
def mean = Average(rs, zLength);
def sd   = StDev(rs, zLength);
plot RSZ_Line = if sd != 0 then (rs - mean) / sd else 0;

# --- MACD Components ---
def rsZ_fast = Average(RSZ_Line, fastLen);
def rsZ_slow = Average(RSZ_Line, slowLen);
plot RSZ_Hist = rsZ_fast - rsZ_slow;
plot Signal = ExpAverage(RSZ_Hist, signalLen);

# --- Thresholds ---
plot ZeroLine = 0;
plot UpperExtreme = 2.0;
plot LowerExtreme = -2.0;

# --- Styling & Clouds ---
RSZ_Line.SetDefaultColor(Color.WHITE);
RSZ_Line.SetLineWeight(2);

RSZ_Hist.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
RSZ_Hist.AssignValueColor(
    if RSZ_Hist > Signal and RSZ_Hist > 0 then Color.GREEN
    else if RSZ_Hist < Signal and RSZ_Hist > 0 then Color.DARK_GREEN
    else if RSZ_Hist < Signal and RSZ_Hist < 0 then Color.RED
    else Color.DARK_RED
);

# Fixed Cloud: Using DefineColor or standard Blue
AddCloud(RSZ_Line, ZeroLine, Color.BLUE, Color.DARK_GRAY);

Signal.SetDefaultColor(Color.YELLOW);
Signal.SetStyle(Curve.SHORT_DASH);
ZeroLine.SetDefaultColor(Color.GRAY);
UpperExtreme.SetDefaultColor(Color.RED);
LowerExtreme.SetDefaultColor(Color.CYAN);

Also in order to track trends for sector rotation I use watchlist columns like the one pictured below:

IGrjwMw.png


Here is the code for that:

Code:
input benchmark = "SPY";
input zLength = 126;     # ~6 months
input weeksAgo = 0;      # set per column: 1, 3, 5, 8, 13

def barsPerWeek = 5;
def offsetBars = weeksAgo * barsPerWeek;

def rs = close / close(benchmark);
def mean = Average(rs, zLength);
def sd = StDev(rs, zLength);

def rsZ =
    if sd != 0
    then (rs - mean) / sd
    else 0;

plot RS_Z_AsOf = rsZ[offsetBars];

RS_Z_AsOf.AssignValueColor(
    if RS_Z_AsOf > 1 then Color.GREEN
    else if RS_Z_AsOf < -1 then Color.RED
    else Color.GRAY
);

RS_Z_AsOf.SetLineWeight(2);
 
Last edited:

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
916 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