A Python toolkit for computing, analysing, and modelling intraday realized volatility from 5-minute OHLCV bar data.
All scripts expect a CSV file with 5-minute OHLCV bars and the following columns:
| Column | Description |
|---|---|
Datetime |
Bar timestamp (UTC or tz-aware ISO 8601) |
Open, High, Low, Close |
Bar prices |
Volume |
Bar volume |
Dividends, Stock Splits, Capital Gains |
Corporate actions (may be 0) |
Data in this format can be downloaded directly from Yahoo Finance using the yfinance Python package:
import yfinance as yf
tk = yf.Ticker("SPY")
df = tk.history(start="2025-01-01", interval="5m")
df.to_csv("spy.csv")Twenty annualised daily volatility measures computed from bar data:
Close-to-close family
- Close-to-close, close-to-close+ (upside), close-to-close− (downside)
OHLC estimators
- Parkinson (1980), Garman–Klass (1980), Rogers–Satchell (1991), Yang–Zhang (2000)
Intraday realized measures (from 5-min bar returns)
- Realized variance (RV), bipower variation (BV), MedRV
- Realized semivariance+ and semivariance−
- Realized range
- Subsampled RV (Zhang et al. 2005)
- Two-scale realized variance / TSRV (Zhang et al. 2005)
- Realized kernel with Parzen weights (Barndorff-Nielsen et al. 2008)
Intraday periodicity adjustment (adjust.py): optionally divides each 5-min bar return by its cross-day average periodic component before computing realized measures, reducing open/close bias.
Summary statistics, autocorrelations, and cross-correlations for all 20 vol measures.
python xintraday_vol.py spy.csv
Vector autoregression of vol measures with OLS and non-negative-constrained (NNLS) estimation, restricted (single-predictor) models, and AIC/BIC model comparison across horizons h = 1, 5, 10, 21 days.
python xintraday_vol_var.py spy.csv
python xintraday_vol_var.py spy.csv --no-ols-var --horizon 1 5
Same as above but for a user-specified subset of vol columns.
python xintraday_selected_vol_var.py spy.csv --vol-cols vol_intraday_rv_ann vol_bv_ann vol_gk_ann
Compares realized vol estimators as one-step-ahead forecasts against GARCH(1,1) on log-likelihood grounds (Gaussian and Student-t), with optional affine correction (a + b·x) in vol or variance space and EWMA smoothing with MLE-estimated decay parameter.
python xintraday_garch.py spy.csv
Fits three distributed-lag volatility forecasting models:
- AR(1): baseline autoregression
- HAR-RV (Corsi 2009): heterogeneous autoregression with daily / weekly / monthly components
- Almon PDL: polynomial distributed lag — smooth weight profile across K lags estimated by OLS (degree p, default 3)
- MIDAS-RV (Ghysels et al. 2006): Beta-polynomial weights over K lags estimated by profile NLS
python xintraday_midas.py spy.csv
python xintraday_midas.py spy.csv --vol-col vol_bv_ann --K 44 --horizon 5
python xintraday_midas.py spy.csv --log --degree 2
Fits a realized SV model with leverage (Jacquier, Polson, Rossi 1994 / Barndorff-Nielsen & Shephard 2002 style) by quasi-ML via the Kalman filter (statsmodels state space).
Model:
h_{t+1} = (1−φ)μ + φ·h_t + ρ·r_t + w_t w_t ~ N(0, σ_η²)
y1_t = ln(r_t²) + 1.2704 ~ h_t + N(0, π²/2)
y2_t = ln(RV_t) ~ α + h_t + N(0, σ_ξ²)
Outputs estimated parameters, smoothed log-variance path, and correlation of smoothed SV vol with all realized estimators.
python xintraday_sv.py spy.csv
python xintraday_sv.py spy.csv --rv-measure intraday_realized_range
python xintraday_sv.py spy.csv --adjust-periodicity
Options:
--rv-measure: choose which intraday measure feeds they2observation (intraday_rv,intraday_realized_range,intraday_bv,intraday_medrv,intraday_rk,intraday_subsampled_rv,intraday_tsrv)--adjust-periodicity: deseasonalize bar returns before computing RV--no-compare: skip correlation table
Simulates T trading days from the SV model with leverage and a U-shaped intraday periodicity pattern, writes a CSV compatible with all other scripts, and optionally fits the model for a quick parameter-recovery check.
python simulate_sv.py --T 2000 --out-csv sim.csv --fit
python simulate_sv.py --T 2000 --rho-lev -0.10 --open-scale 3.0
Minimal script for Monte Carlo parameter recovery: simulates directly from the SV model equations, fits the model, and prints true vs estimated parameters side by side with standard errors and bias t-statistics.
python xsv_sim_fit.py --T 4000
python xsv_sim_fit.py --T 1000 --seed 7 --rho-lev 0.0
| Module | Purpose |
|---|---|
io_intraday.py |
Read and parse intraday CSV files |
adjust.py |
Corporate-action back-adjustment; aggregate bars to daily OHLC; intraday periodicity adjustment |
vol_estimators.py |
All volatility estimators; intraday periodicity factor estimation |
vector_autoreg.py |
VAR estimation (OLS and NNLS), regressor construction, AIC/BIC |
report.py |
Column name mappings and display labels |
statistics.py |
Summary statistics utilities |
numpy pandas scipy statsmodels arch
Install with:
pip install numpy pandas scipy statsmodels arch yfinance
- Andersen & Bollerslev (1997): Intraday periodicity and volatility persistence
- Barndorff-Nielsen & Shephard (2002): Econometric analysis of realized volatility
- Barndorff-Nielsen et al. (2008): Designing realized kernels to measure ex post variation of equity prices
- Corsi (2009): A simple approximate long-memory model of realized volatility
- Garman & Klass (1980): On the estimation of security price volatilities
- Ghysels, Santa-Clara & Valkanov (2006): Predicting volatility: getting the most out of return data sampled at different frequencies
- Parkinson (1980): The extreme value method for estimating the variance of the rate of return
- Rogers & Satchell (1991): Estimating variance from high, low and closing prices
- Yang & Zhang (2000): Drift-independent volatility estimation based on high, low, open, and close prices
- Zhang, Mykland & Aït-Sahalia (2005): A tale of two time scales