σμ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 5 · Validation and the book›Topic 5.2·~2 hrAdvanced

Capstone: pricing and risking a small book

Skill positionModule 5 of 5 · Topic 2 of 2
5.2 Capstone: pricing and risking a small book
—Lab score— / 100Total XP—Streak—
Lecture0% read

12 min readCode examples: 5

  1. 01From yields to prices
  2. 02Options and strikes
  3. 03The book
  4. 04Risk by bumping
  5. 05Checking the book
  6. 06What you will build

On 21 August 2026 a book of five positions is worth 1,061,807: a 5-year bond of face 1,000,000 at 981,039, a payer swap of notional 1,000,000 at 18,961, and three options on SPY at 61,807. The bond and the swap are exactly opposite in rates, a bond that loses when rates rise and a swap that gains, so the book's sensitivity to the curve is only −1.2 for one basis point, against 454 for the bond alone. What is left is the options: a delta of 623 for a 1% rise of SPY and a vega of 4,704 for a volatility point. If SPY falls 10% the book gains 23,092, because the puts gain more than the call loses. This capstone prices the book on the real curve, bumps the market to measure the risk, runs scenarios and checks the numbers.

01

From yields to prices

Where we are. You have written a calendar, a bond, a curve, a swap, three kinds of option engine, two calibrations and a validation. This last topic uses them together. A book (also called a portfolio) is a list of positions, each a trade with a quantity. Its value is the sum of the quantity times the value of one unit, and its risk is how that value changes when the market moves. A market here is four things: the SPY price, a volatility, a dividend yield and a zero curve.

The curve comes from the Treasury yields of the day. The six par yields (1, 2, 3, 5, 7 and 10 years) are in percent; divide by 100, bootstrap the discount factors of the whole years from 1 to 10 as in topic 6, and turn them into continuously compounded zero rates. A discount factor for any date is then read from the zero rates, interpolated linearly. A coupon bond is worth its cash flows times these factors. A swap is worth the floating leg, 1 − D(T), minus the fixed leg, the fixed rate times the sum of the factors of the payment dates.

bond = Σ coupon × face × D(tᵢ) + face × D(T) payer swap = notional × ( (1 − D(T)) − fixed × Σ D(tᵢ) )(1)D is the discount factor of the curve; the payer pays the fixed rate and receives the floating rate

Worked example 1. *Problem:* build the zero curve of the last day of the data (21 August 2026). Find its discount factor at 5 years and the fair 5-year swap rate. Then value the book's bond (4% coupon, 5 years) per 100 of face and its swap (fixed 4%, notional 1,000,000), and check that both are worth exactly their par value when their rate is the fair swap rate. *Plan:* use zero_curve, value_bond and value_swap. *Steps:* below.

main.pyPython
data = load_data()row = data.iloc[-1]curve = zero_curve(row)1print(data.index[-1].date(), float(row["spy"]), [round(z, 5) for z in curve[1]])2print(round(curve_discount(curve[0], curve[1], 5.0), 5), round(fair_swap_rate(curve, 5), 5))3bond, swap = BOOK[0], BOOK[1]print(round(value_bond(bond, curve), 4), round(value_swap(swap, curve), 2))4rate = fair_swap_rate(curve, 5)print(round(value_bond(dict(bond, coupon=rate), curve), 6), round(value_swap(dict(swap, fixed=rate), curve), 6))5
2026-08-21 765.72 [0.03951, 0.04157, 0.04226, 0.04286, 0.04347, 0.0442, 0.04495, 0.04556, 0.04618, 0.04682]
0.80466 0.0443
98.1039 18961.06
100.0 0.0
  1. 1The zero curve of the last row.
  2. 2The date, the SPY close and the ten zero rates.
  3. 3The discount factor at 5 years and the fair swap rate.
  4. 4The bond per 100 of face, and the swap.
  5. 5Both at the fair rate.

*Check:* the last date is 2026-08-21 and SPY closed at 765.72. The zero rates run from 0.03951 at one year to 0.04682 at ten, an upward curve. The 5-year discount factor is 0.80466 and the fair swap rate 0.0443. The bond is worth 98.1039 per 100 of face (a 4% coupon is below the 4.43% market rate, so it trades under par) and the swap 18,961.06: the payer pays 4%, less than the fair 4.43%, and gains. At the fair rate the bond is worth 100.0 and the swap 0.0. *What to notice:* the 4.43% is a property of the curve alone. Both identities are checks that you will use again in the last section.

02

Options and strikes

Three options are written on SPY, all with one year to expiry: a call struck 5% above the spot (European, by the Black-Scholes-Merton formula), a put struck 10% below (American, so the grid of topic 9) and a put struck 5% below (Heston, so the characteristic function of topic 12, with the parameters that topic's calibration found). The rate used is the zero rate for the expiry, −ln D(t) ÷ t. A detail that matters: the strike is fixed on the day of the trade, in money. If the book stored only 'a call at 105% of the spot' and recomputed the strike from each shocked spot, every option would be a bet on nothing, because the strike would follow the spot. The book is therefore given strikes in money by one function, once, with the spot of the day.

Worked example 2. *Problem:* build the market of the last day (the spot is SPY, the volatility the historical volatility of the 252 last returns, the dividend yield 1.3% and the curve of example 1). Fix the strikes of the book. Value the three options per share, and value the American put also as a European one with the same strike. *Plan:* use fix_strikes and value_option. *Steps:* below.

main.pyPython
spot = float(row["spy"])market = {"spot": spot, "vol": historical_vol(data["spy"].to_numpy(dtype=float)), "div": DIV, "curve": curve}book = fix_strikes(BOOK, spot)1print(round(spot, 2), round(market["vol"], 4), [round(p["strike"], 2) for p in book if p["kind"] == "option"])2for position in book[2:]:    print(position["name"], position["style"], round(value_option(position, market), 4))3print(round(value_option(dict(book[3], style="european"), market), 4))4
765.72 0.1287 [804.01, 689.15, 727.43]
call european 31.3016
put american 7.5607
heston put heston 22.9447
7.2362
  1. 1The strikes in money, fixed once.
  2. 2The spot, the volatility and the three strikes.
  3. 3One line for each option, priced by its own engine.
  4. 4The same put as a European option.

*Check:* the volatility is 0.1287 (12.9%) and the strikes 804.01, 689.15 and 727.43. The call is worth 31.3016 per share, the American put 7.5607 and the Heston put 22.9447. The same put priced as European is 7.2362, a little below the American one: the right to exercise early is worth 0.32 per share. *What to notice:* the Heston put, struck at 95%, is worth three times the American put struck at 90%, partly because it is closer to the money and partly because the Heston skew makes low strikes dearer than a flat 12.9% volatility would.

03

The book

A position is a dictionary: its kind says how to value it (bond, swap or option), its quantity says how many. The value of the book is the sum of the quantity times the value of one unit. A quantity can be negative: a short position is a sale, worth the opposite. The function returns each position's unit value, quantity and value together with the total, so that a person can read the book line by line, and so that the total can be reconciled with its parts.

Worked example 3. *Problem:* value the whole book on the market of example 2. Show each position's quantity, unit value and value, and the total. *Plan:* use book_value. *Steps:* below.

main.pyPython
valuation = book_value(book, market)1for r in valuation["rows"]:    print(r["name"], r["quantity"], round(r["unit"], 4), round(r["value"]))2print(round(valuation["total"]), round(valuation["total"] - 1000000.0))3
bond 10000 98.1039 981039
swap 1 18961.06 18961
call 1000 31.3016 31302
put 1000 7.5607 7561
heston put 1000 22.9447 22945
1061807 61807
  1. 1The valuation of the book.
  2. 2One line for each position.
  3. 3The total, and the total minus 1,000,000.

*Check:* the bond is 10000 × 98.1039 = 981,039, the swap 1 × 18,961.06, the call 1000 × 31.3016 = 31,302, the put 7,561 and the Heston put 22,945. The total is 1,061,807, which is 61,807 above 1,000,000. *What to notice:* the bond plus the payer swap is exactly 1,000,000. A bond with a 4% coupon and a payer swap at 4% together are a floating-rate note, which is always worth par at its reset date. What remains above a million is the three options.

04

Risk by bumping

The risk of a book is measured as in topic 13: bump the market, value the book again, and look at the change. A shock moves three things: the whole curve by a number of basis points (hundredths of a percent), the spot by a percentage and the volatility by a number of points. shocked returns a new market and leaves the original untouched; if it changed the original in place, each scenario would start from the previous scenario's market, a bug that gives plausible and wrong numbers. With central differences, the three first-order risks are dv01 (the gain when the curve falls by one basis point), delta (the gain for a 1% rise of the spot) and vega (the gain for a rise of one volatility point).

Worked example 4. *Problem:* give the value and the three risks of the book. Then give the risks of each position alone, and show the shocked market for a rise of the curve of 100 basis points, a fall of the spot of 10% and a rise of the volatility of 5 points. *Plan:* use risk_report and shocked. *Steps:* below.

main.pyPython
print({name: round(value, 1) for name, value in risk_report(book, market).items()})1for position in book:    alone = risk_report([position], market)2    print(position["name"], round(alone["dv01"], 1), round(alone["delta"], 1), round(alone["vega"], 1))3moved = shocked(market, rate_bp=100.0, spot_pct=-10.0, vol_pts=5.0)4print(round(moved["spot"], 2), round(moved["vol"], 4), round(moved["curve"][1][0], 5), round(market["curve"][1][0], 5))5
{'value': 1061806.9, 'dv01': -1.2, 'delta': 623.5, 'vega': 4703.7}
bond 453.7 0.0 0.0
swap -453.7 0.0 0.0
call -31.4 3452.0 2997.3
put 10.3 -1066.8 1706.4
heston put 19.9 -1761.7 0.0
689.15 0.1787 0.04951 0.03951
  1. 1The value and the three risks.
  2. 2Each position alone.
  3. 3Its dv01, delta and vega.
  4. 4A shocked market.
  5. 5The shocked spot, volatility and first zero rate, and the original zero rate.

*Check:* the book is {'value': 1061806.9, 'dv01': -1.2, 'delta': 623.5, 'vega': 4703.7}. By position, the bond has a dv01 of 453.7 and the swap −453.7, exactly opposite; the call has −31.4 (a call loses a little when rates fall, because its forward falls), the American put 10.3 and the Heston put 19.9. The delta of the call is 3,452, of the American put −1,067 and of the Heston put −1,762. The vega of the call is 2,997, of the American put 1,706 and of the Heston put 0.0. The shocked market has a spot of 689.15, a volatility of 0.1787 and a first zero rate of 0.04951, against 0.03951 before. *What to notice:* the Heston put has no vega to the flat volatility: its volatility comes from its own five parameters, not from market['vol']. A volatility risk for it would need a shock to its parameters, which is a different, and harder, question.

05

Checking the book

A book that computes is not a book that is right. Four checks, in the form of topic 13, use two routes to the same number. A swap at the fair rate is worth zero. A bond whose coupon is the fair rate is worth its face. The European call minus the European put at the same strike is the forward price minus the discounted strike: put-call parity, from the formula. And the European call of the book by the formula must agree with the same call on the grid of topic 9, to the tolerance of a grid of 100 points: 3%. A tolerance is chosen for each name, as in topic 13.

Worked example 5. *Problem:* run the four checks on the market of the last day. Show our value, the reference and the relative difference of each. Then run the scenarios: the book's P&L (profit and loss, the value after the shock minus the value before) when the curve rises 100 basis points, when the spot falls 10%, when the volatility rises 5 points, and in a crash with the curve down 50 basis points, the spot down 20% and the volatility up 10 points. Give all this together with study. *Plan:* use validation_checks and study. *Steps:* below.

main.pyPython
for r in validation_checks(book, market):1    print(r["name"], round(r["ours"], 6), round(r["reference"], 6), r["ok"], f"{r['rel']:.1e}")out = study(data)2print(round(out["total"]), {name: round(value) for name, value in out["scenarios"].items()}, out["passed"], "of", out["checks"])3
grid 31.30157 31.118089 True 5.9e-03
par_bond 100.0 100.0 True 0.0e+00
par_swap 0.0 0.0 True 0.0e+00
parity 19.773157 19.773157 True 0.0e+00
1061807 {'rates +100bp': 354, 'spot -10%': 23092, 'vol +5 pts': 24904, 'crash': 148644} 4 of 4
  1. 1The four checks.
  2. 2The whole study.
  3. 3The total, the four scenarios, and the number of checks passed.

*Check:* grid 31.30157 31.118089 True 5.9e-03, par_bond 100.0 100.0 True 0.0e+00, par_swap 0.0 0.0 True 0.0e+00 and parity 19.773157 19.773157 True 0.0e+00. The call by the formula and on the grid differ by 0.59%, within the 3% allowed. The total is 1,061,807 and the scenarios are {'rates +100bp': 354, 'spot -10%': 23092, 'vol +5 pts': 24904, 'crash': 148644}, with 4 of 4 checks passed. *What to notice:* a rise of 100 basis points moves the book by only 354, because the bond and the swap cancel. A fall of the spot of 10% gains 23,092, although the delta is +623 per percent: a delta of +623 suggests a loss of 6,235, but the options are convex (their delta changes as the spot moves), and a long option gains from large moves in either direction. That is the real risk picture of this book, and no single first-order number shows it.

Bumping is a measurement, not a model

The risks of this topic are numbers from bumping a model that you wrote. They are as good as the model, the bump size and the shocks you chose. A parallel shift of the curve is the simplest move; real curves twist. A shock to a flat volatility does not move a Heston price. Scenarios chosen after the fact look like predictions. Use bumps for the first-order picture, use scenarios to look at the second order, validate every price against an independent source, as in topic 13, and say what the numbers do not cover. In QuantLib, the same book would be instruments with pricing engines and quotes (topic 7), and the bumps would change the SimpleQuote objects that the engines observe.

  • NameQuestion it answers
  • zero_curve(row)What is the discount curve today?uses bootstrap and zero_ratesQuantLib: PiecewiseYieldCurve
  • fix_strikes(book, spot)What are the strikes in money?fixed once, on the day of the trade
  • value_bond(position, curve)What is the bond worth?uses curve_discountQuantLib: DiscountingBondEngine
  • value_swap(position, curve)What is the swap worth to the payer?uses curve_discountQuantLib: DiscountingSwapEngine
  • value_option(position, market)What is the option worth?uses bsm_price, fd_price or hestonQuantLib: the three engines of topics 5, 9 and 12
  • book_value(book, market)What is the whole book worth?uses the three value functions
  • shocked(market, rate_bp, spot_pct, vol_pts)What is the market after the shock?a new market, the old one untouched
  • risk_report(book, market)How does the value move?uses shocked and book_valueQuantLib: bump the quotes
  • validation_checks(book, market)Do the prices pass the identities?uses compare_prices
What the real data saysA book of five positions on the Treasury curve and the SPY price of 21 August 2026
Value of the book1,061,807a bond and a swap that make 1,000,000, and three options
Net dv01−1.2against 454 for the bond: the swap hedges it
Delta and vega623 and 4,704per 1% of SPY and per volatility point
The spot falls 10%+23,092the options are convex

Key takeaways

  • A book is a list of positions. Each has a kind and a quantity; its value is the sum of the quantity times the unit value, and its parts must add up to the total.
  • Fix the strikes once. A strike belongs to the trade, not to the market; recomputing it from a shocked spot makes every option worthless as a bet.
  • Shock a copy of the market. Bumping gives dv01, delta and vega by central differences; changing the original in place spoils every later scenario.
  • Check every price two ways. Par instruments, put-call parity and a second engine give checks that cannot pass by accident.
Quick check
Your turn. A book holds a 5-year bond and a payer swap with the same 4% fixed rate and the same size. The bond has a dv01 of +454 and the swap one of −454. What is the dv01 of the book, and what does it mean?
Pick an answer.
06

What you will build

The lab follows the lecture. Task 1 builds the zero curve of a day from the Treasury yields. Task 2 fixes the strikes of the options on the day of the trade. Tasks 3 and 4 value a coupon bond and a swap on the curve. Task 5 values an option by the engine of its style, and task 6 values the whole book, position by position. Task 7 shocks a market without changing it, and task 8 turns that into dv01, delta and vega by central differences. Task 9 runs four checks that the pricing must pass, in the form of topic 13. Task 10 prices and risks the book on the last real day, with four scenarios.

This is the end of the QuantLib skill. You have built, in Python, a small version of the structure of a real pricing library: dates and calendars, schedules and day counts, rates and compounding, bonds, curves and bootstrapping, instruments with engines that observe quotes, swaps, Monte Carlo and finite-difference engines, volatility surfaces, short-rate and stochastic-volatility models with their calibration, validation against a reference, and a book that is priced and risked. The next step on the track is to apply the same structure to real market data, with real quotes for options and caplets in place of the synthetic ones used here.

Lab · what's graded

10 tasks · 100 pts
  • The zero curve8 pts
  • Strikes in money4 pts
  • A coupon bond8 pts
  • An interest-rate swap8 pts
  • An option, by its engine14 pts
  • The value of the book10 pts
  • A shocked market8 pts
  • Dv01, delta and vega12 pts
  • Checks that must pass8 pts
  • The book on SPY20 pts
Explorer · lab-sqt-14
▾ lab-sqt-14
▾ data
…
bash · in-browser
learner@quantcraft:~/lab-sqt-14$
Python not loaded yetPythonLn 1, Col 1Spaces: 4UTF-8

Reset this topic?

Everything in lab-sqt-14 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