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

6.9 KiB

No description has been provided for this image

This code calculates the implied volatility of an American call option using the QuantLib library. It sets up the necessary financial parameters, including the spot price, risk-free rate, dividend yield, volatility, days to maturity, strike price, and option price. The code establishes the yield term structure, spot price handle, payoff, exercise type, and volatility structure. It then uses the Black-Scholes-Merton process and a binomial pricing engine to compute the implied volatility. This is useful for financial analysts and traders to evaluate the market's expectation of future volatility.

In [ ]:
import QuantLib as ql

Define the financial parameters including spot price, risk-free rate, dividend yield, volatility, days to maturity, strike price, and option price

In [ ]:
spot_price = 188.64
risk_free_rate = 0.0525
dividend_yield = 0.0052
volatility = 0.20
days_to_maturity = 148
strike_price = 190
option_price = 11.05

Set up the calendar, day count convention, and evaluation date for the option pricing

In [ ]:
calendar = ql.NullCalendar()
day_count = ql.Actual360()
today = ql.Date().todaysDate()

Set the evaluation date to today's date

In [ ]:
ql.Settings.instance().evaluationDate = today

Create a flat yield term structure for the risk-free rate and dividend yield

In [ ]:
risk_free_ts = ql.YieldTermStructureHandle(
    ql.FlatForward(today, risk_free_rate, day_count)
)
In [ ]:
dividend_ts = ql.YieldTermStructureHandle(
    ql.FlatForward(today, dividend_yield, day_count)
)

Set up the handle for the underlying asset's spot price

In [ ]:
spot_handle = ql.QuoteHandle(ql.SimpleQuote(spot_price))

Define the expiration date, payoff, and exercise type for the American option

In [ ]:
expiration_date = today + ql.Period(days_to_maturity, ql.Days)
payoff = ql.PlainVanillaPayoff(ql.Option.Call, strike_price)
exercise = ql.AmericanExercise(today, expiration_date)
american_option = ql.VanillaOption(payoff, exercise)

Create the volatility handle for the Black-Scholes-Merton process using a constant volatility model

In [ ]:
volatility_handle = ql.BlackVolTermStructureHandle(
    ql.BlackConstantVol(today, calendar, volatility, day_count)
)

Set up the Black-Scholes-Merton process using the spot price, dividend yield, risk-free rate, and volatility

In [ ]:
bsm_process = ql.BlackScholesMertonProcess(
    spot_handle, dividend_ts, risk_free_ts, volatility_handle
)

Set the pricing engine to a binomial model with 1000 steps and calculate the implied volatility

In [ ]:
engine = ql.BinomialVanillaEngine(bsm_process, "crr", 1000)
american_option.setPricingEngine(engine)
In [ ]:
implied_volatility = american_option.impliedVolatility(
    option_price, bsm_process, 1e-4, 1000, 1e-8, 4.0
)
In [ ]:
print(f"Implied Volatility: {implied_volatility:.4f}")

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.