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

6.6 KiB

No description has been provided for this image

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.

In [ ]:
import QuantLib as ql

Set the evaluation date for the option pricing

In [ ]:
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

In [ ]:
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

In [ ]:
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

In [ ]:
dividend_ts = ql.YieldTermStructureHandle(
    ql.FlatForward(
        evaluation_date, 
        dividend_rate, 
        ql.Actual360()
    )
)
In [ ]:
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

In [ ]:
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

In [ ]:
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

In [ ]:
heston_model = ql.HestonModel(heston_process)

Define the payoff and exercise type for the American option

In [ ]:
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

In [ ]:
heston_fd_engine = ql.FdHestonVanillaEngine(heston_model)
american_option.setPricingEngine(heston_fd_engine)

Calculate and print the net present value (NPV) or option price

In [ ]:
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.