Comprehensive Guide to QuantLib for Financial Engineering with Detailed API Examples

Introduction to QuantLib

QuantLib is an open-source library for quantitative finance, providing tools for valuation, trading, and risk management. It offers a vast array of features including pricing various financial instruments, managing portfolios, and performing quantitative risk assessment.

Key API Examples

1. Date Calculation


  from QuantLib import Date, Months, Period

  date = Date(15, 3, 2023)
  future_date = date + Period(3, Months)
  print(future_date)

2. Day Count Conventions


  from QuantLib import Actual360, Thirty360, Date

  start_date = Date(1, 1, 2023)
  end_date = Date(1, 7, 2023)
  day_count = Actual360()
  t360 = Thirty360()

  print(day_count.yearFraction(start_date, end_date))
  print(t360.yearFraction(start_date, end_date))

3. Interest Rate Curve


  from QuantLib import ZeroCurve, Date, SimpleQuote, RateHelper

  dates = [Date(1,1,2023), Date(1,1,2024), Date(1,1,2025)]
  rates = [SimpleQuote(rate) for rate in [0.01, 0.015, 0.02]]
  helpers = [RateHelper(rate, date) for rate, date in zip(rates, dates)]

  curve = ZeroCurve(dates, rates, dayCounter=Actual360())

4. Bond Pricing


  from QuantLib import Bond, Schedule, Date, SimpleQuote, FixedRateBond, ActualActual, UnitedStates
  from QuantLib import TARGET, Annual

  settlement_days = 3
  face_value = 100
  coupon_rate = 0.045
  start_date = Date(1, 1, 2020)
  maturity_date = Date(1, 1, 2030)
  calendar = TARGET()
  schedule = Schedule(start_date, maturity_date, Period(Annual), calendar, BusinessDayConvention.ModifiedFollowing, BusinessDayConvention.ModifiedFollowing, DateGeneration.Backward, False)
  day_counter = ActualActual()
  bond = FixedRateBond(settlement_days, face_value, schedule, [coupon_rate], day_counter)

  clean_price = bond.cleanPrice(0.04, day_counter, Compounding.Compounded, Frequency.Annual)
  print(clean_price)

5. Option Pricing


  from QuantLib import EuropeanOption, PlainVanillaPayoff, EuropeanExercise, QuoteHandle, SimpleQuote

  spot = QuoteHandle(SimpleQuote(100))
  strike = 100
  payoff = PlainVanillaPayoff(Option.Call, strike)
  maturity = Date(15, 6, 2023)
  exercise = EuropeanExercise(maturity)

  vanilla_option = EuropeanOption(payoff, exercise)
  print(vanilla_option.NPV())

Practical Application Example


  import QuantLib as ql
  
  # Parameters
  spot_price = 100.0
  strike_price = 100.0
  maturity = ql.Date(15, 6, 2023)
  risk_free_rate = 0.05
  dividend_yield = 0.02
  volatility = 0.20
  
  # Creating the Black-Scholes-Merton process
  spot_handle = ql.QuoteHandle(ql.SimpleQuote(spot_price))
  flat_ts = ql.YieldTermStructureHandle(ql.FlatForward(0, ql.NullCalendar(), ql.QuoteHandle(ql.SimpleQuote(risk_free_rate)), ql.Actual360()))
  dividend_yield = ql.YieldTermStructureHandle(ql.FlatForward(0, ql.NullCalendar(), ql.QuoteHandle(ql.SimpleQuote(dividend_yield)), ql.Actual360()))
  flat_vol_ts = ql.BlackVolTermStructureHandle(ql.BlackConstantVol(0, ql.NullCalendar(), ql.QuoteHandle(ql.SimpleQuote(volatility)), ql.Actual360()))
  bsm_process = ql.BlackScholesMertonProcess(spot_handle, dividend_yield, flat_ts, flat_vol_ts)

  # European Option
  payoff = ql.PlainVanillaPayoff(ql.Option.Call, strike_price)
  exercise = ql.EuropeanExercise(maturity)
  european_option = ql.EuropeanOption(payoff, exercise)
  
  # Pricing Engine
  european_option.setPricingEngine(ql.AnalyticEuropeanEngine(bsm_process))
  
  # Price
  price = european_option.NPV()
  print("The option price is:", price)

QuantLib empowers financial engineers with the capability to create sophisticated models and tools for finance. The above APIs and example serve as an introductory guide to get started with QuantLib’s diverse functionalities.

Hash: 488f39866b552602bf30b5566e3073aae062e85a81f254de11b96da6b3c8be1f

Leave a Reply

Your email address will not be published. Required fields are marked *