Volatility Targeting in Python: The One-Day Shift That Matters

Volatility targeting, which scales exposure inversely to recently realized volatility, is a simple risk-budgeting technique whose credibility in Python hinges on one detail: shifting your rolling volatility estimate by one day so that yesterday’s risk scales today’s position, never the other way around. This guide covers the published evidence, the pandas and yfinance details that keep the calculation honest, and a code sketch of the construction.
What volatility targeting actually is
Volatility targeting holds a position sized inversely to its recent volatility, so exposure falls when volatility rises and rises when it falls. The practitioner form targets a fixed annualized volatility budget. Moreira and Muir study a closely related idea, scaling monthly returns by the inverse of their previous month’s realized variance (Moreira & Muir, NBER w22208).
The academic evidence: Moreira & Muir
Moreira and Muir’s April 2016 working paper reports that volatility-managed factor portfolios raise Sharpe ratios by “50% to 100% of the original factor Sharpe ratios,” and find lifetime-utility gains of 50–90% for a mean-variance investor (Moreira & Muir, working paper PDF). The factors covered are the market, value, momentum, profitability, return on equity, and investment factors, plus the currency carry trade. These are the working-paper figures. The 2017 Journal of Finance version may differ, so check it before quoting the numbers.
What the paper does and does not claim
The paper describes its strategies as “simple to implement in real time” and says they survive transaction costs, which it tests in its Table 8 (Moreira & Muir, working paper PDF). Its evidence runs over a 1926–2015 sample, so it is a historical study. It is not a forecast, and it does not claim the strategy will work in your market or over your sample.
From monthly factors to a daily Python problem
Moving the idea to daily data means three steps: compute a rolling variance of returns, convert it into a position size, and apply that size to the next day’s return. Each step has a timing choice that can silently leak future information (pandas rolling documentation).
The look-ahead trap, stated precisely
pandas rolling labels each window at its right edge by default, so the window stamped at day t includes day t’s own return. If you use that value to size day t’s exposure and multiply it by day t’s return, the position depends on the outcome it is meant to be sized before. An explicit .shift(1) breaks the dependence (pandas rolling documentation).
The code: rolling volatility, scaled exposure, the one-day shift
The sketch below builds a volatility-scaled exposure and applies the one-day shift. It is illustrative only, and it computes no performance metrics.
# Illustrative only — no performance figures are claimed or implied.
import yfinance as yf
WINDOW = 21 # design choice: trading days in the variance window
TARGET_VOL = 0.10 # design choice: annualized volatility budget
prices = yf.download("SPY", start="2020-01-01", end="2024-01-01",
auto_adjust=True, interval="1d", progress=False)
close = prices["Close"].squeeze() # guard: may come back as a one-column DataFrame
ret = close.pct_change()
realized_var = ret.rolling(WINDOW).var() # window at day t INCLUDES day t's return
realized_vol = (realized_var * 252) ** 0.5 # annualize daily variance
weight = (TARGET_VOL / realized_vol).clip(upper=1.0) # leverage cap is a design choice
weight = weight.shift(1) # the one-day shift: day t uses data through t-1
strategy_ret = weight * ret # strategy return on the same index
With an integer window, min_periods defaults to the window size, so the first WINDOW variance values are NaN and the weight series starts late (pandas rolling documentation).
Getting the data right with yfinance
yfinance.download applies auto_adjust=True by default, which adjusts OHLC prices for splits and dividends. Its end argument is exclusive, so end="2024-01-01" stops at 2023-12-29’s trading day (yfinance download documentation). The default interval is '1d'. These settings determine the return series that feeds the variance estimate, so check them before you compute anything.
Design choices that change the answer
The window length, the annualization convention, the leverage cap, and the warm-up handling all change the weight series. None has a universally correct setting. Document each one and test how sensitive your results are to it.
FAQ
Does a volatility-targeting rule need a backtest to be valid?
A backtest is how you check a specific implementation against your data. The Moreira and Muir evidence covers monthly factor portfolios over 1926–2015. It does not establish that the same daily construction works for your instrument or period (Moreira & Muir, working paper PDF).
Why does the one-day shift matter so much in pandas?
pandas rolling labels each window at its right edge, so a value stamped at day t includes day t’s return. Using it to size day t’s position is look-ahead. Shifting the weight by one day makes day t’s exposure depend only on data through day t-1 (pandas rolling documentation).
Which yfinance settings should I use for daily volatility targeting?
Use auto_adjust=True so prices reflect splits and dividends, remember that end is exclusive, and keep the default '1d' interval for daily data (yfinance download documentation). Confirm that Close is a Series before you call pct_change().
How This Guide Was Built
This post is based on the Moreira & Muir volatility-managed-portfolios paper, its working-paper PDF, and the pandas and yfinance documentation. No hands-on backtest was run for this post, and it reports no performance figures of its own. All Sharpe and utility figures come from the paper.
The Bottom Line
Volatility targeting sizes exposure inversely to realized volatility and rests on the published evidence of Moreira and Muir. In Python, the .shift(1) is what separates a valid risk rule from a look-ahead bug. Validate any implementation on your own data before relying on it.


