Delta Hedging Simulator
This project implements a Delta Hedging simulation using historical price data for a listed security (e.g., AAPL) and models the P&L of a dynamically hedged options portfolio over time. It applies quantitative finance concepts like the Black-Scholes model, Greeks, and discrete rebalancing under realistic conditions (e.g., transaction costs, finite hedging frequency).
- Delta Hedging: A strategy that neutralizes directional risk by taking offsetting positions in the underlying asset and its derivative (option), based on the option’s delta (∂Price/∂Underlying).
- Black-Scholes Pricing: A closed-form formula for pricing European options under the assumptions of log-normal returns, constant volatility, and continuous trading.
- Historical Volatility Estimation: Realized volatility is estimated from log returns of adjusted close prices and annualized via √252 scaling.
- Path-Dependent P&L: The hedging error evolves based on rebalancing frequency and timing relative to price path curvature (gamma exposure).
- Real Data Simulation: Pulls historical stock prices using
yfinance, normalizes time to[0, 1]. - Delta Rebalancing: Executes discrete hedging (e.g., every 5 steps), simulating practical rebalancing.
- Transaction Costs: Models proportional costs (default: 0.1%) per trade to mimic realistic execution friction.
- Volatility Estimation: Computes annualized historical volatility from daily log returns.
- Greeks Tracking: Logs delta values at each time step and visualizes their progression.
- Interactive Plots:
- Normalized asset price path
- Delta over time
- Hedged portfolio P&L trajectory
- Figure: Delta-Hedged Portfolio Value Over Time
- Figure: Delta Evolution Across Time
# Load historical price data (auto-adjusted)
data = yf.download("AAPL", start="2024-01-01", end="2024-12-31", auto_adjust=True)
# Estimate annualized historical volatility
sigma = np.std(np.log(data['Close'] / data['Close'].shift(1))) * np.sqrt(252)
# Normalize price path to [0, 1] time and simulate
path = normalize_price_series(data['Close'])
portfolio, deltas = simulate_delta_hedging(path, K=S0, r=0.0443, sigma=sigma)The Black-Scholes formula calculates the fair value of a European call option:
[ C = S \cdot N(d_1) - K \cdot e^{-rT} \cdot N(d_2) ]
Where:
- ( C ): Call option price
- ( S ): Current stock price
- ( K ): Strike price
- ( T ): Time to maturity (in years)
- ( r ): Risk-free interest rate
- ( \sigma ): Volatility of the underlying asset
- ( N(d) ): Cumulative distribution function of the standard normal distribution
[ d_1 = \frac{\ln(S / K) + (r + \frac{1}{2}\sigma^2)T}{\sigma \sqrt{T}}, \quad d_2 = d_1 - \sigma \sqrt{T} ]
Delta, the sensitivity of the option price to the stock price, is:
[ \Delta = N(d_1) ]
- Incorporate Gamma risk and higher-order Greeks
- Add stochastic volatility models like GARCH or SABR
- Expand to multi-asset delta-neutral portfolios
- Backtest strategies across rolling windows and cross-asset calibration
- Hull, J. (2022). Options, Futures, and Other Derivatives
- Black, F. & Scholes, M. (1973). The Pricing of Options and Corporate Liabilities
- Wilmott, P. (2006). Paul Wilmott Introduces Quantitative Finance
pip install numpy pandas matplotlib yfinance scipyMIT License. Fork, contribute, and build on it!
License
MIT License. Feel free to fork, extend, and contribute!