{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "b2530bdd",
   "metadata": {},
   "source": [
    "<div style=\"background-color:#000;\"><img src=\"pqn.png\"></img></div>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c70588cd",
   "metadata": {},
   "source": [
    "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."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2b46a1fd",
   "metadata": {},
   "outputs": [],
   "source": [
    "import QuantLib as ql"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "44750a62",
   "metadata": {},
   "source": [
    "Set the evaluation date for the option pricing"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5af4b1a7",
   "metadata": {},
   "outputs": [],
   "source": [
    "evaluation_date = ql.Date(30, 5, 2024)\n",
    "ql.Settings.instance().evaluationDate = evaluation_date"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "30b6b6a4",
   "metadata": {},
   "source": [
    "Define the expiry date, strike price, and option type for the American option"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bb0c8ad2",
   "metadata": {},
   "outputs": [],
   "source": [
    "expiry_date = ql.Date(20, 9, 2024)\n",
    "strike_price = 190\n",
    "option_type = ql.Option.Call"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d791b5ae",
   "metadata": {},
   "source": [
    "Set the spot price, dividend rate, risk-free rate, and volatility of the underlying asset"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "94c43338",
   "metadata": {},
   "outputs": [],
   "source": [
    "spot_price = 191.62\n",
    "dividend_rate = 0.0053\n",
    "risk_free_rate = 0.05\n",
    "volatility = 0.2361"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "00066811",
   "metadata": {},
   "source": [
    "Define the term structures for dividends and risk-free rates as flat curves"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cf1c5332",
   "metadata": {},
   "outputs": [],
   "source": [
    "dividend_ts = ql.YieldTermStructureHandle(\n",
    "    ql.FlatForward(\n",
    "        evaluation_date, \n",
    "        dividend_rate, \n",
    "        ql.Actual360()\n",
    "    )\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2307a2d3",
   "metadata": {},
   "outputs": [],
   "source": [
    "risk_free_ts = ql.YieldTermStructureHandle(\n",
    "    ql.FlatForward(\n",
    "        evaluation_date, \n",
    "        risk_free_rate, \n",
    "        ql.Actual360()\n",
    "    )\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "45034dc2",
   "metadata": {},
   "source": [
    "Set up the Heston process parameters including initial variance, mean reversion, long-term variance, and correlation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "79b5559d",
   "metadata": {},
   "outputs": [],
   "source": [
    "v0 = volatility * volatility\n",
    "kappa = 2.0\n",
    "theta = volatility * volatility\n",
    "sigma = 0.1\n",
    "rho = 0.0"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b669e4f5",
   "metadata": {},
   "source": [
    "Initialize the Heston process using the defined term structures, spot price, and Heston parameters"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9f20cb3b",
   "metadata": {},
   "outputs": [],
   "source": [
    "heston_process = ql.HestonProcess(\n",
    "    risk_free_ts, \n",
    "    dividend_ts, \n",
    "    ql.QuoteHandle(\n",
    "        ql.SimpleQuote(spot_price)\n",
    "    ), \n",
    "    v0, \n",
    "    kappa, \n",
    "    theta, \n",
    "    sigma, \n",
    "    rho\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1d6271b0",
   "metadata": {},
   "source": [
    "Create a Heston model instance using the Heston process"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f590bfdf",
   "metadata": {},
   "outputs": [],
   "source": [
    "heston_model = ql.HestonModel(heston_process)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c91b69f6",
   "metadata": {},
   "source": [
    "Define the payoff and exercise type for the American option"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6895b95f",
   "metadata": {},
   "outputs": [],
   "source": [
    "payoff = ql.PlainVanillaPayoff(option_type, strike_price)\n",
    "exercise = ql.AmericanExercise(evaluation_date, expiry_date)\n",
    "american_option = ql.VanillaOption(payoff, exercise)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2a1be49b",
   "metadata": {},
   "source": [
    "Set up the finite difference engine for pricing the American option using the Heston model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f3683e2c",
   "metadata": {},
   "outputs": [],
   "source": [
    "heston_fd_engine = ql.FdHestonVanillaEngine(heston_model)\n",
    "american_option.setPricingEngine(heston_fd_engine)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3c268400",
   "metadata": {},
   "source": [
    "Calculate and print the net present value (NPV) or option price"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2b01cd50",
   "metadata": {},
   "outputs": [],
   "source": [
    "option_price = american_option.NPV()\n",
    "print(f\"Option Price: {option_price:.2f}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "53c45f10",
   "metadata": {},
   "source": [
    "<a href=\"https://pyquantnews.com/\">PyQuant News</a> 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 <a href=\"https://gettingstartedwithpythonforquantfinance.com/\">get started with Python for quant finance</a>. For educational purposes. Not investment advise. Use at your own risk."
   ]
  }
 ],
 "metadata": {
  "jupytext": {
   "cell_metadata_filter": "-all",
   "main_language": "python",
   "notebook_metadata_filter": "-all"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
