The Hedge Ratio Drifts
DV01 changes when yields change and when time passes, at a different speed for the two bonds, so an exact hedge drifts and has to be adjusted.
The question
The hedge ratio of lesson 3 is a number for today. Does it stay right? DV01 depends on the yield, and it depends on the time left to maturity, and the two bonds change at different speeds. After a year the 10-year bond has 9 years left and the 2-year bond only 1. This lesson shows the hedge face at other yield levels, then one year later with the yields unchanged. The bonds and yields are made-up inputs; trading to adjust costs money that the model does not count.
Work it by hand
Take two zero bonds at a yield of 5%, a 4-year bond A of 1,000,000 face and a 2-year hedge bond B. Today their DV01s are 313.41 and 172.77 for 1,000,000 each, rounded to cents, so the hedge face is 1,000,000 times their unrounded quotient, 2 / 1.05^2, which is 1,814,058.96.
One year later, with the yield unchanged, A has 3 years left and B has 1. Their DV01s are 246.81 and 90.70, rounded to cents, so the hedge face is 1,000,000 times their unrounded quotient, 3 / 1.05^2, which is 2,721,088.44. Bond A lost 21% of its DV01 and bond B 47.5% of its own, so more of B must be sold: 907,029.48 more.
Run it
The program takes the fixed example (A of 1,000,000 face, 5% coupon paid once a year, 10 years, yield 5% a year; B 4% coupon, 2 years, yield 4%). Lines 7 to 12 compute the hedge face for nine yield levels: the yield of B from 1% to 9% and the yield of A always one point higher. Lines 13 to 16 compute it one year later, with the same yields and years less one.

market.set_date("2025-01-02");
let a = instrument.bond(face_a, coupon_a, float(years_a), 1);
let b = instrument.bond(1000000.0, coupon_b, float(years_b), 1);
let today = risk.bond_dv01_yield(a, yield_a) / risk.bond_dv01_yield(b, yield_b);
today = today * 1000000.0;
let level = float_list(); let face = float_list();
for (k in range(1, 10)) {
let low = 0.01 * float(k);
let top = risk.bond_dv01_yield(a, low + 0.01);
level.add(lmf.round(low, 4));
face.add(lmf.round(top / risk.bond_dv01_yield(b, low) * 1000000.0, 0));
}
let a1 = instrument.bond(face_a, coupon_a, float(years_a - 1), 1);
let b1 = instrument.bond(1000000.0, coupon_b, float(years_b - 1), 1);
let later = risk.bond_dv01_yield(a1, yield_a);
later = later / risk.bond_dv01_yield(b1, yield_b) * 1000000.0;
lmcode_return_put("level", level); lmcode_return_put("face", face);
lmcode_return_put("today", lmf.money(today));
lmcode_return_put("a_year_later", lmf.money(later));
let answer = int(math_round(lmf.round(today, 0)));
{
"answer": 4094034,
"level": [
0.01,
0.02,
0.03,
0.04,
0.05,
0.06,
0.07,
0.08,
0.09
],
"face": [
5049717,
4704013,
4386294,
4094034,
3824955,
3577005,
3348331,
3137256,
2942266
],
"today": "4,094,033.58",
"a_year_later": "7,392,134.54"
}Today the hedge face is 4,094,033.58, and the answer on line 20 is that number in whole units, 4094034. The plot falls from 5,049,717 at a yield of 1% for B to 2,942,266 at 9%. One year later the hedge face is 7,392,134.54, which is 3,298,100.96 more.
You can change all seven inputs, but the years must be 2 or more: years_b of 1 stops line 16 with "bond maturity must be between 0 and 100 years", years_a of 1 stops line 15 with the same text, and a years_b of 0 stops line 4. A yield of -1 or lower stops line 4 with "risk.bond_dv01_yield: yield must be above -100% per coupon period (above -100% a year for 1 period a year), got -1". The nine levels do not depend on the yields you enter.
A face of 10^16 or more makes the hedge faces too large to return and stops line 17 with "lmcode return: invalid JSON value at /face/0: unsafe_integer"; 10^15 still runs.
Your turn
The program below should return how much more of bond B must be sold after one year. Several candidate programs are offered. Read them, run them on bonds of your own, and decide which of them is right for every pair. Both bonds have at least 2 years to run. Check runs each candidate on the bonds shown and on five others.

market.set_date("2025-01-02");
let a = instrument.bond(face_a, coupon_a, float(years_a), 1);
let b = instrument.bond(1000000.0, coupon_b, float(years_b), 1);
let a1 = instrument.bond(face_a, coupon_a, float(years_a - 1), 1);
let b1 = instrument.bond(1000000.0, coupon_b, float(years_b - 1), 1);
let old = risk.bond_dv01_yield(a, yield_a) / risk.bond_dv01_yield(b, yield_b);
let new = risk.bond_dv01_yield(a1, yield_a) / risk.bond_dv01_yield(b, yield_b);
old = old * 1000000.0;
new = new * 1000000.0;
lmcode_return_put("old_hedge", lmf.money(old));
let answer = int(math_round(lmf.round(new - old, 0)));
{
"answer": -282511,
"old_hedge": "2,522,670.19"
}Bond A (face face_a, coupon coupon_a once a year, years_a whole years, yield yield_a) is held against bond B (coupon coupon_b, years_b whole years, yield yield_b), with the face of B that makes the DV01 of the book zero sold today (the exact ratio). One year passes and the yields stay where they are: both bonds have one year less to run. Return, in whole units, how much more face of B must be sold to make the DV01 zero again: the new hedge face minus the old one, rounded once at the end. years_a and years_b are at least 2. Check runs your program on these bonds and on five others.
Read the result carefully
After one year the 2-year hedge bond has 51% of its DV01 left, 96.15 instead of 188.61 for 1,000,000, and the 10-year bond 92% of its own, 710.78 instead of 772.17, so in the demo the hedge face grows by 81%. A hedge has to be watched and adjusted: after a move of yields as well as after the passing of time. In the model adjusting costs nothing; in practice trading has costs, and coupons paid or received between the dates are left out here.
The model keeps the yields unchanged for the year, which no market does, and every bond pays for certain. The numbers are inputs; the hedge face is rounded to whole units with lmf.round because binary floats are never exact.
Recap
- DV01 changes with the yield: the hedge face falls from 5,049,717 to 2,942,266 as the yield of B goes from 1% to 9%.
- DV01 changes with time, and the two bonds change at different speeds.
- After one year the demo's hedge face is 7,392,134.54 instead of 4,094,033.58.
- An exact hedge has to be adjusted; the model does not charge for it.