Files
strategy-lab/to_explore/pyquantnews/102_DeltaHedging.ipynb
David Brazda e3da60c647 daily update
2024-10-21 20:57:56 +02:00

8.5 KiB

No description has been provided for this image

This code prices a European call option using QuantLib. It sets up market conditions including spot price, volatility, dividend rate, and risk-free rate. The code then calculates the option price and delta using the Black-Scholes-Merton model. It also demonstrates delta hedging by adjusting the stock position in response to changes in the spot price. This is useful for option pricing and risk management in financial markets.

In [1]:
import QuantLib as ql

Set the evaluation date for the pricing model

In [2]:
today = ql.Date(15, 1, 2023)
ql.Settings.instance().evaluationDate = today

Define the option parameters including expiry date, strike price, and type

In [3]:
expiry = ql.Date(15, 7, 2023)
strike_price = 100
option_type = ql.Option.Call

Create the payoff and exercise objects for the European option

In [4]:
payoff = ql.PlainVanillaPayoff(option_type, strike_price)
exercise = ql.EuropeanExercise(expiry)
european_option = ql.VanillaOption(payoff, exercise)

Define market parameters including spot price, volatility, dividend rate, and risk-free rate

In [5]:
spot_price = 100
volatility = 0.2  # 20%
dividend_rate = 0.01  # 1%
risk_free_rate = 0.05  # 5%

Create handles for market conditions such as spot price, volatility, dividend yield, and risk-free rate

In [6]:
spot_handle = ql.QuoteHandle(ql.SimpleQuote(spot_price))
volatility_handle = ql.BlackVolTermStructureHandle(
    ql.BlackConstantVol(today, ql.NullCalendar(), ql.QuoteHandle(ql.SimpleQuote(volatility)), ql.Actual365Fixed())
)
dividend_handle = ql.YieldTermStructureHandle(
    ql.FlatForward(today, ql.QuoteHandle(ql.SimpleQuote(dividend_rate)), ql.Actual365Fixed())
)
risk_free_handle = ql.YieldTermStructureHandle(
    ql.FlatForward(today, ql.QuoteHandle(ql.SimpleQuote(risk_free_rate)), ql.Actual365Fixed())
)

Create the Black-Scholes-Merton process using the defined market conditions

In [7]:
bsm_process = ql.BlackScholesMertonProcess(spot_handle, dividend_handle, risk_free_handle, volatility_handle)

Price the option using the analytic European engine and calculate its Net Present Value (NPV)

In [8]:
european_option.setPricingEngine(ql.AnalyticEuropeanEngine(bsm_process))
option_price = european_option.NPV()
print(f"Option Price: {option_price:.2f}")
Option Price: 6.56

Calculate the option delta, which measures sensitivity to changes in the spot price

In [9]:
delta = european_option.delta()
print(f"Option Delta: {delta:.2f}")
Option Delta: 0.58

Compute the initial stock position required for delta hedging

In [10]:
stock_position = delta * spot_price
print(f"Initial Stock Position: {stock_position:.2f}")
Initial Stock Position: 58.08

Simulate a change in the spot price and update the spot handle

In [11]:
new_spot_price = 105
spot_handle = ql.QuoteHandle(ql.SimpleQuote(new_spot_price))

Recalculate the option price and delta with the new spot price

In [12]:
new_option_price = european_option.NPV()
new_delta = european_option.delta()
print(f"New Option Price: {new_option_price:.2f}")
print(f"New Option Delta: {new_delta:.2f}")
New Option Price: 6.56
New Option Delta: 0.58

Adjust the stock position for delta hedging based on the new delta

In [13]:
new_stock_position = new_delta * new_spot_price
hedge_adjustment = new_stock_position - stock_position
print(f"Adjustment in Stock Position: {hedge_adjustment:.2f}")
Adjustment in Stock Position: 2.90

PyQuant News is where finance practitioners level up with Python for quant finance, algorithmic trading, and market data analysis. Looking to get started? Check out the fastest growing, top-selling course to get started with Python for quant finance. For educational purposes. Not investment advise. Use at your own risk.

In [ ]: