Delta Hedging Backtest
Calculate the realized profit and loss of selling a European option on each ticker and delta hedging it on the ticker’s own prices until it expires days trading days later: how much the hedged seller gained or lost when the volatility the stock realized differed from the one the option was sold at (Hull, 2022, chapter 19).
The option is sold at its Black-Scholes value, the hedge holds its delta in shares, pays a proportional cost on every trade, receives the dividends on the shares held and keeps the rest in cash at the risk free rate:
\[\text{Option Value} = \text{Black-Scholes value at the volatility}\; \sigma \;\text{on the start date}\] \[\text{Cash}_{i+1} = \text{Cash}_{i} e^{r \Delta t} + \Delta_{i} D_{i+1} - (\Delta_{i+1} - \Delta_{i}) S_{i+1} - k \left|\Delta_{i+1} - \Delta_{i}\right| S_{i+1}\] \[\text{Profit and Loss} = e^{- rT}\left[\text{Cash}_{T} + \Delta_{T} S_{T} - \operatorname{Payoff}(S_{T})\right]\]The profit and loss shows what the volatility the option was sold at earned against the volatility the stock then realized: the seller gains about ½ \(\Gamma S^{2} (\sigma^{2} - \sigma_{\text{realized}}^{2})\) per unit of time (Wilmott, 2006), so a positive result means the stock moved less than the option priced, less the costs of hedging (Leland, 1985).
Also known as: delta hedging P&L, hedged option P&L, gamma scalping backtest, realized hedging error.
Example · AAPL, MSFT · 2023 to 2025
Install the Finance Toolkit with pip install financetoolkit -U, then call get_delta_hedging_backtest:
from financetoolkit import Toolkit
toolkit = Toolkit(
["AAPL", "MSFT"], api_key="FINANCIAL_MODELING_PREP_KEY", start_date="2023-01-01", end_date="2025-12-31"
)
toolkit.options.get_delta_hedging_backtest(
start_date="2025-06-02",
transaction_cost=0.0005,
show_columns=["Volatility", "Realized Volatility", "Option Value", "Payoff", "Transaction Costs", "Profit and Loss"],
)
Which returns:
| Item | Volatility | Realized Volatility | Option Value | Payoff | Transaction Costs | Profit and Loss |
|---|---|---|---|---|---|---|
| AAPL | 0.3304 | 0.2141 | 14.2016 | 28.02 | 0.2146 | 2.9596 |
| MSFT | 0.2574 | 0.1516 | 25.7490 | 43.15 | 0.3291 | 5.2926 |
2 rows
Parameters
get_delta_hedging_backtest accepts the following parameters.
| Parameter | Type | Description | Example |
|---|---|---|---|
days | int | The trading days from the sale to expiration, at least 1. Defaults to 63, about three months. | 360 |
start_date | str | None | The date the option is sold, or the last trading day before it. Defaults to None, days trading days before the end of the historical data, so the option expires on its last date. | "2021-01-01" |
moneyness | float | The strike as a fraction of the stock price on the start date. Defaults to 1.0, at the money. | |
volatility | float | None | The volatility the option is sold and hedged at, such as an implied volatility, for every ticker, 0 or above (at 0 the option is sold at its discounted intrinsic value and the delta is 0 or 1). Defaults to None, the volatility of the year of daily returns up to the start date, which is known on that date. | 0.008 |
put_option | bool | Whether the option is a put. Defaults to False. | True |
transaction_cost | float | The cost of a trade as a fraction of its value (0.001 is 10 basis points), 0 or above. Defaults to 0.0. | 0.0 |
rebalancing_days | int | The trading days between rebalances. Defaults to 1, daily. | 1, daily |
rounding | int | None | The number of decimals to round the results to. Defaults to None, which uses the Toolkit’s rounding. | 2 |
show_columns | str | list[str] | None | The columns to keep, a name or a list of names, matched on every column level, or the rows when no column has those names (such as the line items of a financial statement). Names that do not exist are logged, and when none exist the result is returned in full. Defaults to None, which keeps every column. | None, which keeps every column |
References
- Paper Hull, J. C. (2022). “Options, Futures, and Other Derivatives.” 11th edition, chapter 19.
- Paper Leland, H. E. (1985). “Option Pricing and Replication with Transactions Costs.” The Journal of Finance, 40(5), 1283-1301.
- Paper Wilmott, P. (2006). “Paul Wilmott on Quantitative Finance.” 2nd edition, chapter 12.