BLOG

Behind the numbers.

Code examples, market analysis, and data quality deep-dives.

Is AI Capex Paying Back Fast Enough? Revenue Hurdle Forecasting in Python
Could Shorter AI Asset Lives Hit Earnings? Depreciation Stress Test in Python
How Much AI Capex Risk Can a Portfolio Remove? Constrained Optimization in Python
Is the AI Capex Trade Crowded? Rolling Volatility and Sector Rotation in Python
Did the AI Boom Come From Existing S&P 500 Members? Point-in-Time Momentum Test in Python
Is AI Revenue Circular? Customer-Vendor Capex Loop Analysis in Python
Is the AI Trade Connected to Private Credit? Rolling Correlation Network in Python
Is Apollo More Balance-Sheet Sensitive Than Peers? Leverage Screen in Python
Are AI Earnings Supported by Cash Flow? Accrual and Capex Screen in Python
Can Defensive Stocks Hedge AI Drawdowns? Basket Regime Test in Python
How Fast Does the Market Price In Fed Decisions? FOMC Event Study in Python
How Much Are Options Sellers Overpaid? The Variance Risk Premium in Python
Which Companies Have the Worst Earnings Quality? Sloan Accrual Screen with Geographic Revenue Data in Python
Does the Oil-to-Gold Ratio Signal Recessions? XLE/GLD Backtest in Python
Is AI Spending Crowding Out Free Cash Flow? Capex Sustainability Across the Mag 7 in Python
Does a Long Energy / Short Bonds Portfolio Capture Inflation Surprises? Factor Construction in Python
Can a Hidden Markov Model Detect Oil Market Regimes? HMM Analysis in Python
Do Grain Prices Predict Food Inflation? Granger Causality Test in Python
Does the Corporate Credit Spread Predict Stock Market Crashes? BAA-AAA Spread Analysis in Python
Do Oil Stocks Hedge Inflation? Rolling Beta Analysis in Python
Which Stocks Are Most Rate-Sensitive? Equity Duration via Bond Beta in Python
Which Companies Have the Highest Accrual Ratios? Earnings Quality Screening in Python
Is Alpha Persistent or Decaying? Rolling Sharpe Ratio Analysis in Python
Are Markets Trending or Mean-Reverting? Hurst Exponent Analysis in Python
Is Consumer Discretionary vs Staples a Leading Indicator? XLY/XLP Ratio Analysis in Python
Does Heavy Capex Predict Future Stock Returns? Capital Expenditure Analysis in Python
How to Estimate Cost of Equity Using CAPM in Python
Is Volatility Predictable? Testing for Volatility Clustering in Python
Which Industrials Are Overleveraged? Net Debt to EBITDA Screening in Python
GM Before and After Bankruptcy: Why Entity Resolution Matters for Financial Data
What Is Adjusted Beta? Merrill Lynch Beta Shrinkage in Python
How Good Is a Stock Pick? Information Ratio and Tracking Error in Python
Do Stock Returns Follow a Normal Distribution? Testing for Fat Tails in Python
Which Large Caps Have the Highest Free Cash Flow Yield? FCF Screening in Python
Which Sectors Won Over 5 Years? Sector Rotation Analysis in Python
How to Forecast Stock Volatility with GARCH Models in Python
Are Stock Prices Mean-Reverting? Augmented Dickey-Fuller Test in Python
How to Calculate CAPM Alpha and Beta with Regression in Python
How to Compare Sector Sharpe Ratios and Sortino Ratios in Python
DELL: Why Stitching Historical Price Data Together Is Wrong
How to Analyze Drawdown and Recovery for Bank Stocks in Python
How to Screen SaaS Stocks by Revenue Growth and Cash Flow in Python
How to Screen REITs by Dividend Yield and Valuation in Python
How Correlated Are the Magnificent 7? Intra-Group Correlation in Python
AAPL vs XOM: Do Individual Stocks Have Seasonal Patterns?
How to Rank Large-Cap Stocks by Momentum in Python
How to Build a Multi-Endpoint Financial Dashboard in Python
How to Compare Volatility Across Energy Stocks in Python
How to Screen Healthcare Stocks by Valuation in Python
How to Build a Sector Correlation Matrix for Portfolio Diversification in Python
How to Find Oversold and Overbought Stocks Using Z-Scores in Python
How to Measure Earnings Quality: Cash Flow vs Net Income in Python
How to Build a Multi-Factor Stock Screen in Python (Value + Momentum + Quality)
How to Build a Simple DCF Model for Any Stock in Python
How to Screen Tech Stocks by Revenue Growth in Python
How to Screen Stocks by Balance Sheet Health in Python
Is "Sell in May" Real? SPY Monthly Seasonality Over 10 Years
How to Compare Sector Performance YTD Using Python
How to Track S&P 500 Additions and Removals Over Time in Python
How to Screen Dividend Stocks by Yield and Quality in Python
How to Calculate Max Drawdown and Recovery Time for Any Stock in Python
How to Compare Profitability Across Mega-Cap Tech Stocks in Python
Why Ticker Symbols Are Unreliable: The Recycling Problem Every Quant Should Know
How to Calculate and Compare Stock Volatility in Python
How to Screen Blue-Chip Stocks by P/E Ratio in Python
How to Track Companies Through Ticker Changes, Bankruptcies, and Renames in Python
S&P 500 Turnover: How Much the Index Has Changed Since 2010
How to Calculate Stock Beta and Correlation in Python
← All articles

How to Compare Sector Sharpe Ratios and Sortino Ratios in Python

What’s the question?

Raw returns are misleading as a measure of investment quality because they ignore the risk required to achieve them. A sector returning 30% with 30% volatility is worse on a risk-adjusted basis than one returning 15% with 10% volatility. The Sharpe ratio normalizes return by total volatility, answering the question: how much excess return did this sector generate per unit of total risk? The Sortino ratio refines this further by penalizing only downside volatility — deviation below the risk-free rate — on the premise that upside volatility is desirable rather than penalizable. Which U.S. equity sectors delivered the best risk-adjusted returns, and does the distinction between total volatility and downside volatility change the ranking?

The approach

Eight SPDR sector ETFs are evaluated over one year of daily price data: Technology (XLK), Energy (XLE), Industrials (XLI), Utilities (XLU), Healthcare (XLV), Consumer Discretionary (XLY), Consumer Staples (XLP), and Financials (XLF). For each sector, four metrics are computed. The Sharpe ratio is calculated as (annualized return minus risk-free rate) divided by annualized volatility, where the risk-free rate is set at 5% to reflect prevailing Treasury yields. The Sortino ratio replaces total volatility in the denominator with downside deviation — the root mean square of negative excess daily returns, annualized by multiplying by the square root of 252. Maximum drawdown (the largest peak-to-trough decline) provides additional context on tail risk. Sectors are ranked by Sharpe ratio.

import xfinlink as xfl
import pandas as pd
import numpy as np

xfl.set_api_key("your_key")  # free at https://xfinlink.com/signup

RF_ANNUAL = 0.05
tickers = ["XLK","XLF","XLV","XLE","XLY","XLP","XLI","XLU"]
labels = {"XLK":"Technology","XLF":"Financials","XLV":"Healthcare","XLE":"Energy",
          "XLY":"ConsDisc","XLP":"ConsStaples","XLI":"Industrials","XLU":"Utilities"}
df = xfl.prices(tickers, period="1y", fields=["return_daily"])

for ticker in tickers:
    t = df[df["ticker"] == ticker].sort_values("date")
    r = t["return_daily"].dropna()
    ann_ret = (1 + r.mean()) ** 252 - 1
    ann_vol = r.std() * np.sqrt(252)
    sharpe = (ann_ret - RF_ANNUAL) / ann_vol
    sortino_den = r[r < 0].std() * np.sqrt(252)
    sortino = (ann_ret - RF_ANNUAL) / sortino_den if sortino_den > 0 else 0
    print(f"{labels[ticker]:14s} return={ann_ret:+.1%} vol={ann_vol:.1%} sharpe={sharpe:+.2f} sortino={sortino:+.2f}")

Output:

=== Sector Risk-Adjusted Returns: Sharpe & Sortino (1Y, Rf=5%) ===

  Technology     (XLK)  return=+55.5%  vol=20.3%  sharpe=+2.48  sortino=+3.62  maxDD=-16.2%
  Energy         (XLE)  return=+37.3%  vol=20.1%  sharpe=+1.60  sortino=+2.44  maxDD=-12.1%
  Industrials    (XLI)  return=+24.4%  vol=15.3%  sharpe=+1.27  sortino=+2.14  maxDD=-12.5%
  Utilities      (XLU)  return=+13.4%  vol=14.3%  sharpe=+0.58  sortino=+0.88  maxDD= -9.9%
  Healthcare     (XLV)  return=+12.6%  vol=15.0%  sharpe=+0.50  sortino=+0.87  maxDD=-10.8%
  ConsDisc       (XLY)  return=+12.1%  vol=18.0%  sharpe=+0.40  sortino=+0.63  maxDD=-15.1%
  ConsStaples    (XLP)  return= +6.9%  vol=12.7%  sharpe=+0.15  sortino=+0.24  maxDD= -9.9%
  Financials     (XLF)  return= +0.9%  vol=14.4%  sharpe=-0.29  sortino=-0.39  maxDD=-15.2%

What this tells us

Technology dominates risk-adjusted returns with a Sharpe ratio of 2.48 — a level that is exceptional by historical standards, as sustained Sharpe ratios above 2.0 over a full year are rare for any asset class. Despite a maximum drawdown of -16.2% (the second deepest in the group), the 55.5% return more than compensated for the volatility incurred.

The gap between Technology’s Sharpe (2.48) and Sortino (3.62) reveals an important characteristic of the return distribution. The Sortino ratio is higher than the Sharpe ratio when a disproportionate share of the total volatility comes from upside moves. In Technology’s case, the large gap (3.62 vs. 2.48) indicates that most of the sector’s daily return variance was on the positive side — exactly the type of volatility investors welcome.

Energy ranks second on both measures (Sharpe 1.60, Sortino 2.44), delivering comparable volatility to Technology (20.1% vs. 20.3%) but lower absolute returns. The similar Sharpe-to-Sortino ratio gap suggests Energy also exhibited more upside than downside volatility during this period.

Financials is the only sector with a negative Sharpe ratio (-0.29), meaning the 5% risk-free Treasury rate outperformed XLF on a risk-adjusted basis. The Sortino ratio (-0.39) is more negative than the Sharpe ratio, which is the inverse of the Technology pattern: Financials experienced more downside volatility than upside, and its 15.2% maximum drawdown was the deepest in the group despite generating only 0.9% in returns.

Consumer Staples and Utilities, while posting positive Sharpe ratios, barely exceeded the risk-free rate. Their low volatility (12.7% and 14.3%) keeps the ratios positive, but the near-zero excess returns raise the question of whether the equity risk premium justified the allocation relative to Treasury bills.

So what?

Risk-adjusted metrics should be the primary basis for sector comparison and allocation decisions, not raw returns. A sector with moderate returns and low downside volatility (high Sortino) may be a better allocation choice than one with higher returns but symmetric or negatively skewed volatility. The Sharpe-to-Sortino ratio gap is itself informative: sectors where Sortino significantly exceeds Sharpe have favorable return distributions (more upside than downside), while sectors where Sortino is worse than Sharpe have unfavorable distributions. Use both ratios together to distinguish between total risk and the risk that actually matters — losses.

Built with xfinlink — free financial data API for Python. pip install xfinlink
← All articles