Find Outperformers vs Any Benchmark
Is Apple outperforming the NASDAQ? How does Tesla compare to the S&P 500? Is Microsoft beating its sector over the past 30 days?
Stop guessing and start measuring with this simple but powerful comparison tool:
- Compare any stock vs any benchmark (indexes, sectors, individual stocks)
- Choose between lookback bars (30 days) or specific date comparison
- Instant visual feedback: green for outperform, yellow for underperform
- Toggle between simple labels or detailed percentage breakdowns
Build it from scratch and understand exactly how relative performance analysis works.
Relative Performance Analysis for Better Stock Selection
Absolute returns tell only half the story. A stock up 15% sounds great until you discover the sector is up 25%. That 15% “winner” is actually underperforming by 10 percentage points.
Relative performance analysis reveals the complete picture by comparing individual stock returns against benchmarks like the S&P 500, sector ETFs, or peer stocks. This identifies true outperformers and helps avoid stocks that look good in isolation but are actually lagging their comparisons.
Professional portfolio managers use relative performance as a core selection criterion. Individual investors often skip this step, leading to suboptimal picks that underperform relevant benchmarks.
The Relative Performance indicator solves this with flexible comparison capabilities and instant visual feedback. No mental math required – just load any stock and immediately see if it’s winning or losing versus your chosen benchmark.
Start with the input configuration that gives users control:
input benchmarkType = {default bars, date};
input lookbackPeriod = 30;
input date = 20220217;
input benchmark = "SPY";
input detailedLabels = yes;
This creates a flexible system where users can compare performance either over a set number of lookback bars (like the past 30 trading days) or from a specific date. The benchmark can be any symbol – SPY for market comparison, QQQ for tech stocks, XLF for financials, or even individual stocks for peer analysis.
The core logic uses a switch statement to handle both comparison methods:
def benchmarkClose = close(symbol=benchmark);
def benchmarkPrevClose;
def currPrevClose;
switch(benchmarkType){
case bars:
benchmarkPrevClose = close(symbol=benchmark)[lookbackPeriod];
currPrevClose = close[lookbackPeriod];
case date:
benchmarkPrevClose = if getYYYYMMDD() == date then close(symbol=benchmark) else benchmarkPrevClose[1];
currPrevClose = if getYYYYMMDD() == date then close else currPrevClose[1];
}
For the bars method, it simply looks back the specified number of periods. For the date method, it finds the exact date specified and captures both the current stock and benchmark closing prices from that day, then carries those values forward through all subsequent bars.
Performance calculation uses the standard percentage change formula:
def benchmarkPerformance = (benchmarkClose - benchmarkPrevClose)/benchmarkPrevClose;
def currPerformance = (close - currPrevClose)/currPrevClose;
This calculates the percentage return for both the current stock and the benchmark over the same time period, making them directly comparable regardless of their absolute price levels.
Smart Labeling System and Practical Applications
The indicator provides two label options for different use cases:
# Simple label for quick assessment
AddLabel(yes, if currPerformance > benchmarkPerformance then "Outperform" else "Underperform",
if currPerformance > benchmarkPerformance then color.green else color.yellow);
# Detailed label with exact percentages
AddLabel(detailedLabels, "Curr: " + AsPercent(currPerformance) + " | " + benchmark + ": " + AsPercent(benchmarkPerformance),
if currPerformance > benchmarkPerformance then color.green else color.yellow);
The simple label shows just “Outperform” or “Underperform” with color coding – perfect for quick scanning across multiple charts. The detailed label adds exact percentage returns for both the stock and benchmark, useful when you need specific numbers.
Color coding provides instant visual feedback: green indicates the stock is beating its benchmark, yellow means it’s lagging. This eliminates the mental math of comparing percentage returns.
Real-world applications demonstrate the indicator’s versatility:
Stock vs Market: Load TSLA with SPY as benchmark to see if Tesla is outperforming the broad market. Switch to QQQ benchmark for tech-specific comparison.
Sector Rotation: Compare XLK (technology) vs XLF (financials) to identify which sector has better momentum. Use this for sector ETF rotation strategies.
Peer Analysis: Set Lowe’s chart with Home Depot as benchmark to see which home improvement retailer is performing better over your chosen timeframe.
Asset Class Comparison: Compare GLD (gold) vs BTC-USD to see which alternative asset is outperforming over various time periods.
The flexibility between bars and date comparison serves different analytical needs:
Bars Method: Best for rolling performance comparisons. “How has this stock done vs its benchmark over the past X trading days?” Updates daily as new data comes in.
Date Method: Best for event-based analysis. “How has this stock performed since earnings?” or “What’s the performance since the Fed meeting?” Locked to specific starting points.
Professional traders often use 30-day relative performance for momentum screens, 90-day for intermediate trends, and 252-day (one year) for longer-term relative strength analysis. The indicator accommodates all these timeframes.
Common screening applications:
- Momentum Screens: Find stocks outperforming SPY over the past 30 days
- Sector Leaders: Identify which stocks are beating their sector ETFs
- Relative Strength: Compare similar stocks to find the strongest performer
- Market Timing: See if defensive stocks are outperforming growth during market stress
The indicator works on any timeframe – daily for swing trading analysis, weekly for longer-term trends, or even intraday for short-term relative momentum plays.
Remember that relative performance can diverge significantly from absolute performance. A stock down 5% might be outperforming if its benchmark is down 10%. Conversely, a stock up 8% might be underperforming if its benchmark is up 15%. This context is crucial for proper stock evaluation.
Combine this with other technical analysis for complete picture. Relative outperformance plus strong technical setup often provides higher-probability trade opportunities than either signal alone.
#TOS Indicators
#Home of the Volatility Box
#Indicator: Relative Performance
#Code written in 2022
#Full Tutorial: https://www.tosindicators.com/indicators/relative-performance
input benchmarkType = {default bars, date};
input lookbackPeriod = 30;
input date = 20220217;
input benchmark = "SPY";
// ... 23 more lines ...Here are some resources that you may find useful: