OPEN-SOURCE SCRIPT
Stock vs. Sector Relative Strength

**Stock vs. Sector Relative Strength**
**What it does**
Plots how the stock on the chart has performed relative to *its own sector ETF* over a lookback window (default 20 bars), and how unusual that relative performance is compared to the stock's own recent history (z-score over 60 bars). The sector ETF is detected automatically from TradingView's sector classification and mapped to the matching SPDR sector fund (XLK, XLF, XLV, …). If the sector is unknown — non-US listings, ETFs, crypto — the script falls back to a benchmark you choose (default SPY). You can also set the benchmark manually.
**What you see**
- Z-score line with ±1 bands (default view), or the raw relative return in % as columns.
- Background shading when the z-score is beyond the band: teal = outperforming the sector by more than usual, red = underperforming.
- A small table with the detected sector, the benchmark actually used, the relative return, the z-score and the current state.
- Alerts on state changes: relative return crossing zero, z-score entering the upper or lower band.
**How it works**
`relative return = (close / close[n] − 1) − (benchmark / benchmark[n] − 1)`. The z-score is the relative return minus its 60-bar mean, divided by its 60-bar standard deviation. The benchmark is requested on the chart's timeframe without lookahead, so the value on any bar only uses that bar's closes.
**Why it is different from existing sector-strength scripts**
Most published sector tools compare a *sector ETF against SPY*. This one compares the *stock against its sector*, which answers a different question: is this stock doing better than the peers it is normally traded with?
**Why I built it — the measurement behind it**
This is a state, not a signal. The reason it exists is a filter test. I took 47,013 daily bullish moving-average crossover events (EMA 9 crossing above DEMA 200) on 1,758 US stocks from 2014 to 2026, including names that were later delisted, and asked which conditions at the crossover bar separated better outcomes from worse ones. For the 18,280 events where a sector ETF could be assigned:
| Condition at the crossover bar | Share of events kept | Share positive after 20 bars | after 60 bars |
|---|---:|---:|---:|
| all events with a sector ETF | 100 % | 56.9 % | 59.7 % |
| relative return vs. sector > 0 | 28 % | 57.5 % (vs. 53.7 % for the rest) | 60.2 % (vs. 56.2 %) |
| z-score vs. sector > +1 | 17 % | 57.3 % (vs. 54.2 %) | 59.8 % (vs. 56.8 %) |
Both versions held in the second half of the sample (from September 2020) and were the only conditions in that test that improved *every* measure I looked at, including the outcome of the trades themselves under a trailing exit (+3.2 percentage points). Comparing against SPY instead of the sector gave almost the same 20-bar effect but nothing at 60 bars — the sector benchmark is where the longer-horizon difference comes from.
**Limitations — please read**
- The measurement is conditional: it says that crossovers with positive relative strength were followed by positive returns more often than crossovers without it. It is not a strategy return and says nothing about future performance.
- The sector-mapped subset consisted of today's ~1,000 largest US stocks, so it carries survivorship bias. The comparison *within* that subset (kept vs. removed) is what the numbers above show; the absolute levels are flattered.
- TradingView's sector taxonomy is not GICS. The mapping to SPDR ETFs is approximate (retail, media and REITs are the usual edge cases). Check the table and switch to a manual benchmark if it looks wrong.
- Tested on daily bars only. On intraday timeframes the lookback of 20 bars means something else.
- `syminfo.sector` is only populated for stocks. Everything else uses the fallback benchmark.
No buy or sell signals are generated, and none are implied.
**What it does**
Plots how the stock on the chart has performed relative to *its own sector ETF* over a lookback window (default 20 bars), and how unusual that relative performance is compared to the stock's own recent history (z-score over 60 bars). The sector ETF is detected automatically from TradingView's sector classification and mapped to the matching SPDR sector fund (XLK, XLF, XLV, …). If the sector is unknown — non-US listings, ETFs, crypto — the script falls back to a benchmark you choose (default SPY). You can also set the benchmark manually.
**What you see**
- Z-score line with ±1 bands (default view), or the raw relative return in % as columns.
- Background shading when the z-score is beyond the band: teal = outperforming the sector by more than usual, red = underperforming.
- A small table with the detected sector, the benchmark actually used, the relative return, the z-score and the current state.
- Alerts on state changes: relative return crossing zero, z-score entering the upper or lower band.
**How it works**
`relative return = (close / close[n] − 1) − (benchmark / benchmark[n] − 1)`. The z-score is the relative return minus its 60-bar mean, divided by its 60-bar standard deviation. The benchmark is requested on the chart's timeframe without lookahead, so the value on any bar only uses that bar's closes.
**Why it is different from existing sector-strength scripts**
Most published sector tools compare a *sector ETF against SPY*. This one compares the *stock against its sector*, which answers a different question: is this stock doing better than the peers it is normally traded with?
**Why I built it — the measurement behind it**
This is a state, not a signal. The reason it exists is a filter test. I took 47,013 daily bullish moving-average crossover events (EMA 9 crossing above DEMA 200) on 1,758 US stocks from 2014 to 2026, including names that were later delisted, and asked which conditions at the crossover bar separated better outcomes from worse ones. For the 18,280 events where a sector ETF could be assigned:
| Condition at the crossover bar | Share of events kept | Share positive after 20 bars | after 60 bars |
|---|---:|---:|---:|
| all events with a sector ETF | 100 % | 56.9 % | 59.7 % |
| relative return vs. sector > 0 | 28 % | 57.5 % (vs. 53.7 % for the rest) | 60.2 % (vs. 56.2 %) |
| z-score vs. sector > +1 | 17 % | 57.3 % (vs. 54.2 %) | 59.8 % (vs. 56.8 %) |
Both versions held in the second half of the sample (from September 2020) and were the only conditions in that test that improved *every* measure I looked at, including the outcome of the trades themselves under a trailing exit (+3.2 percentage points). Comparing against SPY instead of the sector gave almost the same 20-bar effect but nothing at 60 bars — the sector benchmark is where the longer-horizon difference comes from.
**Limitations — please read**
- The measurement is conditional: it says that crossovers with positive relative strength were followed by positive returns more often than crossovers without it. It is not a strategy return and says nothing about future performance.
- The sector-mapped subset consisted of today's ~1,000 largest US stocks, so it carries survivorship bias. The comparison *within* that subset (kept vs. removed) is what the numbers above show; the absolute levels are flattered.
- TradingView's sector taxonomy is not GICS. The mapping to SPDR ETFs is approximate (retail, media and REITs are the usual edge cases). Check the table and switch to a manual benchmark if it looks wrong.
- Tested on daily bars only. On intraday timeframes the lookback of 20 bars means something else.
- `syminfo.sector` is only populated for stocks. Everything else uses the fallback benchmark.
No buy or sell signals are generated, and none are implied.
開源腳本
秉持TradingView一貫精神,這個腳本的創作者將其設為開源,以便交易者檢視並驗證其功能。向作者致敬!您可以免費使用此腳本,但請注意,重新發佈代碼需遵守我們的社群規範。
免責聲明
這些資訊和出版物並非旨在提供,也不構成TradingView提供或認可的任何形式的財務、投資、交易或其他類型的建議或推薦。請閱讀使用條款以了解更多資訊。
開源腳本
秉持TradingView一貫精神,這個腳本的創作者將其設為開源,以便交易者檢視並驗證其功能。向作者致敬!您可以免費使用此腳本,但請注意,重新發佈代碼需遵守我們的社群規範。
免責聲明
這些資訊和出版物並非旨在提供,也不構成TradingView提供或認可的任何形式的財務、投資、交易或其他類型的建議或推薦。請閱讀使用條款以了解更多資訊。