6.6 KiB
This code prices an American call option using the Heston model, a popular stochastic volatility model in quantitative finance. It sets up the necessary market data, including spot price, dividend yield, risk-free rate, and volatility. The code configures the Heston process with parameters such as initial variance, mean reversion rate, long-term variance, volatility of volatility, and correlation. It then constructs the option payoff and exercise type, sets up the finite difference pricing engine, and calculates the option price. This approach is useful for pricing options that cannot be easily handled by closed-form solutions.
import QuantLib as ql
Set the evaluation date for the option pricing
evaluation_date = ql.Date(30, 5, 2024) ql.Settings.instance().evaluationDate = evaluation_date
Define the expiry date, strike price, and option type for the American option
expiry_date = ql.Date(20, 9, 2024) strike_price = 190 option_type = ql.Option.Call
Set the spot price, dividend rate, risk-free rate, and volatility of the underlying asset
spot_price = 191.62 dividend_rate = 0.0053 risk_free_rate = 0.05 volatility = 0.2361
Define the term structures for dividends and risk-free rates as flat curves
dividend_ts = ql.YieldTermStructureHandle( ql.FlatForward( evaluation_date, dividend_rate, ql.Actual360() ) )
risk_free_ts = ql.YieldTermStructureHandle( ql.FlatForward( evaluation_date, risk_free_rate, ql.Actual360() ) )
Set up the Heston process parameters including initial variance, mean reversion, long-term variance, and correlation
v0 = volatility * volatility kappa = 2.0 theta = volatility * volatility sigma = 0.1 rho = 0.0
Initialize the Heston process using the defined term structures, spot price, and Heston parameters
heston_process = ql.HestonProcess( risk_free_ts, dividend_ts, ql.QuoteHandle( ql.SimpleQuote(spot_price) ), v0, kappa, theta, sigma, rho )
Create a Heston model instance using the Heston process
heston_model = ql.HestonModel(heston_process)
Define the payoff and exercise type for the American option
payoff = ql.PlainVanillaPayoff(option_type, strike_price) exercise = ql.AmericanExercise(evaluation_date, expiry_date) american_option = ql.VanillaOption(payoff, exercise)
Set up the finite difference engine for pricing the American option using the Heston model
heston_fd_engine = ql.FdHestonVanillaEngine(heston_model) american_option.setPricingEngine(heston_fd_engine)
Calculate and print the net present value (NPV) or option price
option_price = american_option.NPV() print(f"Option Price: {option_price:.2f}")
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.
