Learn QUANTLIB with Real Code Examples
Updated Nov 27, 2025
Code Sample Descriptions
1
Create a Flat Yield Curve
import QuantLib as ql
calendar = ql.TARGET()
date = ql.Date(1, 12, 2025)
ql.Settings.instance().evaluationDate = date
rate = 0.05
yield_curve = ql.FlatForward(date, rate, ql.Actual360())
Creates a simple flat yield curve for discounting cash flows.
2
Price a European Option
spot = 100
strike = 100
maturity = ql.Date(1,12,2026)
vol = 0.2
risk_free = 0.01
calendar = ql.TARGET()
day_count = ql.Actual360()
option_type = ql.Option.Call
payoff = ql.PlainVanillaPayoff(option_type, strike)
exercise = ql.EuropeanExercise(maturity)
option = ql.VanillaOption(payoff, exercise)
spot_handle = ql.QuoteHandle(ql.SimpleQuote(spot))
dividend_yield = ql.YieldTermStructureHandle(ql.FlatForward(date, 0.0, day_count))
risk_free_rate = ql.YieldTermStructureHandle(ql.FlatForward(date, risk_free, day_count))
volatility = ql.BlackVolTermStructureHandle(ql.BlackConstantVol(date, calendar, vol, day_count))
process = ql.BlackScholesMertonProcess(spot_handle, dividend_yield, risk_free_rate, volatility)
engine = ql.AnalyticEuropeanEngine(process)
option.setPricingEngine(engine)
npv = option.NPV()
Prices a European call option using Black-Scholes model.
3
Construct a Simple Bond
issue_date = ql.Date(1,1,2025)
maturity_date = ql.Date(1,1,2030)
coupon_rate = 0.05
schedule = ql.Schedule(issue_date, maturity_date, ql.Period(ql.Annual), ql.TARGET(), ql.Following, ql.Unadjusted, ql.DateGeneration.Backward, False)
bond = ql.FixedRateBond(3, 100, schedule, [coupon_rate], ql.Actual360())
price = ql.CleanPrice(bond, 0.05)
Creates a fixed-rate bond and computes its clean price.
5
Construct a Yield Curve from Deposits
deposits = [ql.DepositRateHelper(ql.QuoteHandle(ql.SimpleQuote(0.01)), ql.Period(1,ql.Months), 2, calendar, ql.Following, False, ql.Actual360())]
curve = ql.PiecewiseLinearZero(date, deposits, ql.Actual360())
Builds a yield curve from deposit instruments.
6
Compute Forward Rate
fwd_rate = curve.forwardRate(ql.Date(1,12,2025), ql.Date(1,12,2026), ql.Actual360(), ql.Simple).rate()
Calculates the forward rate between two dates from a yield curve.
7
Cap/Floor Pricing
start = ql.Date(1,12,2025)
maturity = ql.Date(1,12,2028)
nominals = [1000000]
strikes = [0.03]
cap = ql.Cap(ql.IborLeg([nominals],[ql.USDLibor(ql.Period(6,ql.Months))],[ql.Schedule(start,maturity,ql.Period(ql.Semiannual),ql.TARGET(),ql.Following,ql.Unadjusted,ql.DateGeneration.Forward,False)]), strikes)
engine = ql.BlackCapFloorEngine(yield_curve, volatility)
cap.setPricingEngine(engine)
npv = cap.NPV()
Prices a cap instrument using Black model.
8
Hull-White Short Rate Model
a = 0.03
sigma = 0.01
hw_model = ql.HullWhite(yield_curve, a, sigma)
Creates a Hull-White model for interest rate simulation.
9
Monte Carlo Option Pricing
time_steps = 100
num_paths = 10000
mc_engine = ql.MCEuropeanEngine(process, 'pseudo', timeSteps=time_steps, requiredSamples=num_paths)
option.setPricingEngine(mc_engine)
mc_price = option.NPV()
Prices an option using Monte Carlo simulation.
10
Swap Pricing
start = ql.Date(1,1,2026)
maturity = ql.Date(1,1,2031)
schedule_fixed = ql.Schedule(start, maturity, ql.Period(ql.Annual), ql.TARGET(), ql.Following, ql.Unadjusted, ql.DateGeneration.Backward, False)
schedule_float = ql.Schedule(start, maturity, ql.Period(ql.Semiannual), ql.TARGET(), ql.Following, ql.Unadjusted, ql.DateGeneration.Backward, False)
swap = ql.VanillaSwap(ql.VanillaSwap.Payer, 1000000, schedule_fixed, 0.02, ql.Actual360(), schedule_float, ql.USDLibor(ql.Period(6,ql.Months)), 0.0, ql.Actual360())
engine = ql.DiscountingSwapEngine(yield_curve)
swap.setPricingEngine(engine)
npv = swap.NPV()
Constructs a fixed-for-floating interest rate swap and computes NPV.