Learn Quantlib - 10 Code Examples & CST Typing Practice Test
QuantLib is an open-source library for quantitative finance, providing tools for modeling, trading, and risk management in C++ with bindings for Python, R, and other languages.
Learn QUANTLIB with Real Code Examples
Updated Nov 27, 2025
Code Sample Descriptions
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.
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.
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.
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.
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.
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.
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.
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.
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.
Frequently Asked Questions about Quantlib
What is Quantlib?
QuantLib is an open-source library for quantitative finance, providing tools for modeling, trading, and risk management in C++ with bindings for Python, R, and other languages.
What are the primary use cases for Quantlib?
Pricing complex derivatives and fixed-income products. Risk management and sensitivity analysis. Portfolio modeling and scenario analysis. Developing custom quantitative finance models. Backtesting trading strategies and models
What are the strengths of Quantlib?
Open-source and actively maintained. High performance C++ core. Python and other language bindings. Extensible for custom instruments and models. Widely recognized in financial industry and academia
What are the limitations of Quantlib?
Steep learning curve for beginners. Requires understanding of quantitative finance. Documentation can be sparse for advanced models. Python bindings sometimes lag behind C++ updates. No built-in database connectivity or GUI
How can I practice Quantlib typing speed?
CodeSpeedTest offers 10+ real Quantlib code examples for typing practice. You can measure your WPM, track accuracy, and improve your coding speed with guided exercises.