8.5 KiB
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.
import QuantLib as ql
Set the evaluation date for the pricing model
today = ql.Date(15, 1, 2023) ql.Settings.instance().evaluationDate = today
Define the option parameters including expiry date, strike price, and type
expiry = ql.Date(15, 7, 2023) strike_price = 100 option_type = ql.Option.Call
Create the payoff and exercise objects for the European option
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
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
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
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)
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
delta = european_option.delta() print(f"Option Delta: {delta:.2f}")
Option Delta: 0.58
Compute the initial stock position required for delta hedging
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
new_spot_price = 105 spot_handle = ql.QuoteHandle(ql.SimpleQuote(new_spot_price))
Recalculate the option price and delta with the new spot price
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
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.
