σμimplied vol · KKmax(S−K,0)udβρᵢⱼfat tailsPnL · drawdownyield · TdSₜ = μSₜ dt + σSₜ dWₜC = S·N(d₁) − K·e⁻ʳᵀ·N(d₂)σₚ² = wᵀΣwd₁ = [ln(S/K) + (r + σ²⁄2)T] ⁄ σ√T∂V/∂t + ½σ²S²·∂²V/∂S² + rS·∂V/∂S − rV = 0Sharpe = (Rₚ − R_f) ⁄ σₚVaR₉₉ = μ − 2.33σρ = Cov(X,Y) ⁄ σₓσᵧE[Rₚ] = Σ wᵢ·E[Rᵢ]φ(x) = e^(−x²/2) ⁄ √2πE[Rᵢ] = R_f + βᵢ(E[Rₘ] − R_f)dXₜ = θ(μ − Xₜ)dt + σ dWₜσ²ₜ = ω + α·ε²ₜ₋₁ + β·σ²ₜ₋₁C − P = S − K·e⁻ʳᵀΔ = ∂V/∂S Γ = ∂²V/∂S²p = (e^(rΔt) − d) ⁄ (u − d)rₜ = ln(Pₜ ⁄ Pₜ₋₁)E[Xₜ₊₁ | ℱₜ] = Xₜf* = (bp − q) ⁄ bz = (x − μ) ⁄ σP(N=k) = λᵏe^(−λ) ⁄ k!Θ = −∂V/∂Tν = S·φ(d₁)·√Tmin wᵀΣw s.t. wᵀμ = μ*∫Σ∂∞λ
uantCraft AcademyRoadmapsSkills
Ctrl K
AO
+1.2k Learners
LoginJoin ↗
RoadmapsSkills
AO
+1.2k Learners
Login
  • English
  • Français
  • Español
  • العربية
  • Português
  • Deutsch
  • 日本語
  • 中文
σμimplied vol · KKmax(S−K,0)udβρᵢⱼfat tailsPnL · drawdownyield · TdSₜ = μSₜ dt + σSₜ dWₜC = S·N(d₁) − K·e⁻ʳᵀ·N(d₂)σₚ² = wᵀΣwd₁ = [ln(S/K) + (r + σ²⁄2)T] ⁄ σ√T∂V/∂t + ½σ²S²·∂²V/∂S² + rS·∂V/∂S − rV = 0Sharpe = (Rₚ − R_f) ⁄ σₚVaR₉₉ = μ − 2.33σρ = Cov(X,Y) ⁄ σₓσᵧE[Rₚ] = Σ wᵢ·E[Rᵢ]φ(x) = e^(−x²/2) ⁄ √2πE[Rᵢ] = R_f + βᵢ(E[Rₘ] − R_f)dXₜ = θ(μ − Xₜ)dt + σ dWₜσ²ₜ = ω + α·ε²ₜ₋₁ + β·σ²ₜ₋₁C − P = S − K·e⁻ʳᵀΔ = ∂V/∂S Γ = ∂²V/∂S²p = (e^(rΔt) − d) ⁄ (u − d)rₜ = ln(Pₜ ⁄ Pₜ₋₁)E[Xₜ₊₁ | ℱₜ] = Xₜf* = (bp − q) ⁄ bz = (x − μ) ⁄ σP(N=k) = λᵏe^(−λ) ⁄ k!Θ = −∂V/∂Tν = S·φ(d₁)·√Tmin wᵀΣw s.t. wᵀμ = μ*∫Σ∂∞λ
uantCraft AcademyRoadmapsSkills
Ctrl K
AO
+1.2k Learners
LoginJoin ↗
RoadmapsSkills
AO
+1.2k Learners
Login
  • English
  • Français
  • Español
  • العربية
  • Português
  • Deutsch
  • 日本語
  • 中文
QuantLib skill›Module 3 · Curves, instruments and engines›Topic 3.3·~1 hr 5 minAdvanced

Swaps and curve building

Skill positionModule 3 of 5 · Topic 3 of 4
3.3 Swaps and curve building
—Lab score— / 100Total XP—Streak—
Lecture0% read

10 min readCode examples: 5

  1. 01Quotes in, curve out
  2. 02Swaps: two legs and a fair rate
  3. 03A curve that rebuilds itself
  4. 04A swap on the curve
  5. 05Curves and a swap on real data
  6. 06What you will build

On 9 October 2026 a 5-year payer swap struck at the 5-year quote of 5.02% is worth 0 on a curve built from nine quotes, and the curve's own 5-year zero rate is 4.91%. Now move every swap quote up by one basis point: the swap gains 433.85 on a notional of a million, the curve rebuilt itself once even though six quotes had changed, and reading the price again cost no rebuild at all. A curve that is built from the market and follows it, and a swap priced on it, are the two things a trading desk uses all day. This topic builds both on the observer design of the last topic.

01

Quotes in, curve out

Where we are. In topic 6 you bootstrapped a curve from par yields, and in topic 7 you built quotes, lazy objects and engines. Now join them. A real curve is built from quotes of different instruments: deposits (money lent for months at a simple interest rate) cover the short end, and swaps (par rates for whole years) cover the long end. Each instrument gives the discount factors of its own dates. A deposit of term t and simple rate r pays 1 + r t at the end, so its discount factor is 1 ÷ (1 + r t). The swaps are bootstrapped exactly as the par bonds of topic 6, since a par swap rate is a par coupon.

D(t) = 1 ÷ (1 + r t) for a deposit, z = −ln D(t) ÷ t for every node(1)D is the discount factor of a node at t years, r the simple deposit rate and z the continuously compounded zero rate that the curve stores

The curve keeps its nodes as zero rates, so that curve_discount of topic 6 can interpolate them: the deposit terms come first, then the years 1, 2, 3 and so on. Whatever the instrument, the curve has to reproduce the market: each deposit must be worth its interest, and each swap must have its quoted rate as its fair rate.

Worked example 1. *Problem:* build a curve from a 6-month deposit at 4% and swaps of 1, 2 and 3 years at 4.5%, 5% and 5.2%. Show the times, the zero rates and the discount factors. *Plan:* use build_curve and the discount factor of each node. *Steps:* below.

main.pyPython
times, zeros = build_curve([(0.5, 0.04)], [(1, 0.045), (2, 0.05), (3, 0.052)])1print([round(t, 4) for t in times])print([round(z, 5) for z in zeros])2print([round(math.exp(-z * t), 5) for t, z in zip(times, zeros)])3
[0.5, 1.0, 2.0, 3.0]
[0.03961, 0.04402, 0.04891, 0.05088]
[0.98039, 0.95694, 0.90681, 0.85845]
  1. 1One deposit and three swaps.
  2. 2The zero rate of every node.
  3. 3The discount factors.

*Check:* the times are 0.5, 1.0, 2.0 and 3.0, and the zero rates 0.03961, 0.04402, 0.04891 and 0.05088. The discount factors are 0.98039, 0.95694, 0.90681 and 0.85845; the first is 1 ÷ 1.02, the interest of a 6-month deposit at 4% being 2%. *What to notice:* the zero rate of the deposit is 0.03961, a little below its 4% simple rate, because it is the same 2% of interest over six months written as a continuously compounded rate, which is always a little lower than the simple one.

02

Swaps: two legs and a fair rate

A swap exchanges two streams of payments. In a payer swap you pay a fixed rate once a year on a notional, and you receive the floating rate (the short-term rate of each period). You never exchange the notional. The fixed leg is a simple sum: the fixed rate times the notional, discounted for each year. This sum without the rate is the annuity, the sum of the discount factors of the years. The floating leg needs one remark. A floating-rate note that pays the market rate each period is always worth its notional, so the floating payments plus a final notional are worth the notional today, and the floating payments alone are worth 1 − D(n) per unit of notional.

fair rate = ( 1 − D(n) ) ÷ annuity(n), annuity(n) = D(1) + D(2) + … + D(n), payer NPV = notional × ( ( 1 − D(n) ) − fixed rate × annuity(n) )(2)D(n) is the discount factor of the last year n of the swap; the fair rate makes the payer NPV zero

The fair swap rate is the fixed rate that makes the swap worth nothing: it is the quoted swap rate of the market. If you pay a fixed rate above the fair rate, you lose; below it, you gain. Notice that the formula is the par_rate of topic 6 written for a curve: the same idea, applied to a curve you can read at any date.

Worked example 2. *Problem:* on the curve of example 1, find the annuity and the fair rate of the 3-year and the 2-year swap. Then value payer swaps of a million over 3 years with fixed rates of 4%, 5.2% and 6%. *Plan:* use annuity, fair_swap_rate and swap_npv. *Steps:* below.

main.pyPython
times, zeros = build_curve([(0.5, 0.04)], [(1, 0.045), (2, 0.05), (3, 0.052)])print(round(annuity(times, zeros, 3), 5), round(fair_swap_rate(times, zeros, 3), 5), round(fair_swap_rate(times, zeros, 2), 5))1print([round(swap_npv(times, zeros, rate, 3, 1000000.0), 2) for rate in (0.04, 0.052, 0.06)])2
2.7222 0.052 0.05
[32666.35, -0.0, -21777.57]
  1. 1The annuity and two fair rates.
  2. 2Three fixed rates: below, at and above the fair rate.

*Check:* the annuity is 2.7222, the fair rate of the 3-year swap is 0.052 and that of the 2-year swap 0.05: the curve gives back the quotes it was built from. The payer swaps are worth 32666.35, -0.0 and -21777.57: you gain when you pay 4%, lose when you pay 6%, and the swap at 5.2% is worth nothing. *What to notice:* the NPV is a straight line in the fixed rate, with slope -notional × annuity; the fair rate is where the line crosses zero.

03

A curve that rebuilds itself

A desk has a few dozen quotes that tick all day, and many curves and swaps depend on them. The curve must not be rebuilt at each tick, and it must never be stale. The answer is the lazy object of topic 7. A piecewise curve registers with every quote. When a quote changes, the curve is marked out of date and tells its observers once. When somebody asks for the curve, it rebuilds from the present values of all the quotes, however many have changed. QuantLib's PiecewiseYieldCurve does exactly this, with a bootstrap helper for each quote.

Worked example 3. *Problem:* make quotes for the curve of example 1. Ask the curve for the 2-year and 3-year discount factors and count the rebuilds. Then change the 3-year and the 2-year quotes, without asking, and count again. Finally ask for the 3-year discount factor and the 2-year one, and count. *Plan:* use PiecewiseCurve with SimpleQuotes. *Steps:* below.

main.pyPython
deposits = [(0.5, SimpleQuote(0.04))]swaps = [(1, SimpleQuote(0.045)), (2, SimpleQuote(0.05)), (3, SimpleQuote(0.052))]curve = PiecewiseCurve(deposits, swaps)print(curve.rebuilds, round(curve.discount(2.0), 5), curve.rebuilds, round(curve.discount(3.0), 5), curve.rebuilds)1swaps[2][1].set_value(0.055)2swaps[1][1].set_value(0.052)3print(curve.rebuilds)4print(round(curve.discount(3.0), 5), curve.rebuilds)5print(round(curve.discount(2.0), 5), curve.rebuilds)6
0 0.90681 1 0.85845 1
1
0.85089 2
0.90327 2
  1. 1Nothing is built until the first question; then one build for both.
  2. 2A quote changes.
  3. 3Another quote changes.
  4. 4Still one build: nobody has asked.
  5. 5The first question after the changes rebuilds.
  6. 6The second question reuses the new curve.

*Check:* the first line is 0 0.90681 1 0.85845 1: zero rebuilds before the first question, one after it, and the same one for the second. After the two changes the count is still 1. The 3-year factor is then 0.85089 with 2 rebuilds, and the 2-year factor 0.90327 still with 2. *What to notice:* two changed quotes cost one rebuild, done only when the curve is read.

04

A swap on the curve

The last piece is the engine. The SwapEngine registers with the curve, and reads the curve when it prices. The swap registers with the engine. A change in any quote now travels: quote, curve, engine, swap, and each link only marks the next as out of date. When the price is read, the swap asks the engine, the engine asks the curve, and the curve rebuilds. This is the same chain as in topic 7, with the curve now built from many quotes.

Worked example 4. *Problem:* price a 3-year payer swap with a fixed rate of 5% on a million, on the curve of example 1. Then change the 3-year quote to 5.25% and price again. Read the price a second time. Finally change the deposit quote to 5% and price again. Count the engine's calculations and the curve's rebuilds each time. *Plan:* use Swap, SwapEngine and PiecewiseCurve. *Steps:* below.

main.pyPython
deposits = [(0.5, SimpleQuote(0.04))]swaps = [(1, SimpleQuote(0.045)), (2, SimpleQuote(0.05)), (3, SimpleQuote(0.052))]curve = PiecewiseCurve(deposits, swaps)swap, engine = Swap(0.05, 3, 1000000.0), SwapEngine(curve)swap.set_pricing_engine(engine)print(round(swap.NPV(), 2), engine.count, curve.rebuilds)1swaps[2][1].set_value(0.0525)print(round(swap.NPV(), 2), engine.count, curve.rebuilds)2swap.NPV()print(engine.count)3deposits[0][1].set_value(0.05)print(round(swap.NPV(), 2), engine.count, curve.rebuilds)4
5444.39 1 1
6802.26 2 2
2
6802.26 3 3
  1. 1The first price builds the curve.
  2. 2The 3-year quote moved: the swap gains.
  3. 3A second read: no calculation.
  4. 4A deposit quote moved.

*Check:* the swap is worth 5444.39 after 1 calculation and 1 rebuild. When the 3-year quote rises to 5.25% the swap is worth 6802.26, after 2 calculations and 2 rebuilds. The second read leaves the count at 2. After the deposit quote moves, the price is still 6802.26, but the counts are 3 and 3. *What to notice:* the 6-month deposit does not enter the price of a swap that pays at the end of years 1, 2 and 3, yet the swap is recalculated: a notification says that something it depends on changed, not what changed. That is the price of keeping the design simple.

05

Curves and a swap on real data

Worked example 5. *Problem:* build the curve from the real Treasury yields of 1, 3 and 6 months (as deposits) and of 1, 2, 3, 5, 7 and 10 years (as swaps) on three days, 2 July 2007, 3 August 2020 and the last day of the table. For each day price a 5-year payer swap on a million, struck at the 5-year quote. Find the zero rates at 1, 5 and 10 years, the fair 5-year rate and the NPV, then bump every swap quote by one basis point (0.0001) and find the new NPV, the change of the NPV (the DV01, the value of a basis point), and the number of rebuilds. Finally read everything again and set the same quotes again. *Plan:* study combines the classes and the functions. *Steps:* below.

main.pyPython
out = study(load_data())for date, row in out.items():    print(date, round(row["zero_1"], 4), round(row["zero_5"], 4), round(row["zero_10"], 4), round(row["fair_5"], 4), round(row["par_npv"], 2), round(row["dv01"], 2), row["rebuilds"], row["repeat_rebuilds"])1
2007-07-02 0.0485 0.0479 0.049 0.049 0.0 434.2 2 0
2020-08-03 0.0012 0.0022 0.0056 0.0022 -0.0 497.33 2 0
2026-10-09 0.0437 0.0491 0.0515 0.0502 0.0 433.85 2 0
  1. 1One row per day.

*Check:* the zero rates at 1, 5 and 10 years are 0.0485, 0.0479 and 0.049 on 2 July 2007, 0.0012, 0.0022 and 0.0056 on 3 August 2020, and 0.0437, 0.0491 and 0.0515 on 9 October 2026. The fair 5-year rates are 0.049, 0.0022 and 0.0502, the quotes themselves, so the NPV at the quote is 0 every day. The DV01 is 434.2, 497.33 and 433.85: a basis point on the rates adds that many dollars to the payer on a million. Each day shows 2 rebuilds, one at the start and one after six quotes had changed, and 0 repeated. *What to notice:* the DV01 is largest in 2020, when rates were close to zero: the annuity, the sum of the discount factors, is then close to 5; in 2026 discounting at 5% shrinks it to about 4.3.

Treasury yields are not swap rates

The course has no swap data, so Treasury yields stand in for deposit and swap quotes, and their conventions are simplified: Treasury yields are for bonds that pay twice a year, not for annual swaps. The single curve used here for both the projection of the floating rate and the discounting is also a simplification: since 2008 swaps are priced with one curve for each tenor of the floating rate and another for discounting. QuantLib has helpers for each instrument, with its own calendar and conventions.

  • NameQuestion it answers
  • build_curve(deposits, swaps)Which zero rates reproduce the quotes?uses bootstrapQuantLib: PiecewiseYieldCurve with rate helpers
  • annuity(times, zeros, n)What is a unit paid each year worth?uses curve_discountQuantLib: the fixed leg's BPS
  • fair_swap_rate(times, zeros, n)Which fixed rate makes the swap worth nothing?uses annuityQuantLib: fairRate()
  • swap_npv(times, zeros, fixed_rate, n, notional)What is a payer swap worth?uses annuityQuantLib: VanillaSwap.NPV()
  • PiecewiseCurve(deposits, swaps)A curve that follows its quotesis a LazyObjectQuantLib: PiecewiseYieldCurve
  • SwapEngine(curve)Prices a swap on a curveuses swap_npvQuantLib: DiscountingSwapEngine
What the real data saysA 5-year payer swap on a million, struck at the 5-year Treasury yield, on a curve built from nine Treasury yields
Fair 5-year rate, 9 Oct 20265.02%5-year zero rate 4.91%
DV01 of the swap, 9 Oct 2026433.85$497.33 on 3 August 2020
NPV at the quoted rate0on all three days
Rebuilds after six quotes changed12 in all, with the first build

Key takeaways

  • A curve is built from the quotes of many instruments. Deposits cover the short end with simple interest, swaps cover the long end by bootstrap, and the result must give back every quote.
  • A swap is a fixed leg against a floating leg. The floating leg is worth 1 − D(n) per unit; the fair rate is that over the annuity, and the payer's value is linear in the fixed rate.
  • A curve can be lazy too. It registers with all its quotes, is marked out of date by any of them, and rebuilds once, when asked.
  • A notification does not say what changed. A swap is recalculated for a quote that does not enter its price; the saving is that nobody tracks the dependencies by hand.
Quick check
Your turn. A payer swap is struck at its fair rate, so it is worth 0. Every swap quote then rises by one basis point. What is the payer's NPV after the curve is rebuilt?
Pick an answer.
06

What you will build

The lab follows the lecture. Task 1 builds a curve of zero rates from deposits and swaps. Task 2 is the annuity, task 3 the fair swap rate and task 4 the value of a payer swap. Task 5 is the piecewise curve, a lazy object that follows its quotes, and task 6 the engine that prices a swap on it. Task 7 builds curves from real Treasury yields on three days, prices a swap and bumps the quotes.

Next topic: Monte Carlo and finite-difference engines. So far every engine was a formula or a sum. Many instruments have no formula, so the next engines price by simulation and by solving the pricing equation on a grid. The instrument stays the same and you swap the engine, exactly as you swapped the bond's engine in topic 7, and then compare the answers with the formula of topic 5.

Lab · what's graded

7 tasks · 100 pts
  • Building the curve22 pts
  • The annuity6 pts
  • The fair swap rate8 pts
  • The value of a swap10 pts
  • The piecewise curve22 pts
  • The swap engine12 pts
  • Curves from Treasuries20 pts
Explorer · lab-sqt-08
▾ lab-sqt-08
▾ data
…
bash · in-browser
learner@quantcraft:~/lab-sqt-08$
Python not loaded yetPythonLn 1, Col 1Spaces: 4UTF-8

Reset this topic?

Everything in lab-sqt-08 goes back to the starter files and the terminal is cleared. Your current solution will be lost — this can't be undone.

Will be reset
  • main.py → starter code
  • Terminal history and test output
  • Unsubmitted changes
Will be kept
  • Best lab score
  • Your XP and streak
  • Lecture progress and notes