Testing the Hedge with a Shift
Move both yields by the same amount and reprice: for one basis point the hedged book barely moves, and for a large shift a small profit remains on both sides.
The question
The hedge of the last lesson was built from DV01, a slope. Does it work for a real move? Move both yields by the same amount, reprice bond A and the sold bond B with the engine, and look at the profit of the book: the change in the value of A minus the change in the value of the face of B that was sold. The bonds, yields and shifts are made-up inputs of a model, not a forecast.
Work it by hand
Take the two zero bonds of the last lesson: A with a face of 1,000,000 and 4 years, and the hedge of 3,455,350.39 of the 1-year bond B, both at a yield of 5%. Let both yields rise by one percentage point to 6%.
A falls from 822,702.47 to 792,093.66, a change of -30,608.81. The sold hedge falls from 3,290,809.90 to 3,259,764.52, a change of -31,045.38, and selling it earns that fall. The profit of the book is -30,608.81 minus -31,045.38, which is 436.57 from these rounded changes and 436.56 from the unrounded ones. With a fall of one point instead the same book gains 459.31. A hedged book that earns on both sides is not free money: the next lesson explains where it comes from.
Run it
The program builds the fixed example of lesson 3 (A of 1,000,000 face, 5%, 10 years; B 4%, 2 years) with the hedge face from DV01 (lines 4 and 5); coupons are paid once a year and yields are yields per year. It reprices both bonds for 17 shifts from -200 to +200 basis points in steps of 25 (lines 9 to 17) and plots the profit of A alone and of A with the sold hedge. A basis point is 0.01%.

Watch the steps (recorded video)
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 hedge = risk.bond_dv01_yield(a, yield_a) / risk.bond_dv01_yield(b, yield_b);
hedge = hedge * 1000000.0;
let base_a = pricing.bond_price_from_yield(a, yield_a) * face_a / 100.0;
let base_b = pricing.bond_price_from_yield(b, yield_b) * hedge / 100.0;
let bp = float_list(); let alone = float_list(); let hedged = float_list();
for (k in range(0, 17)) {
let s = 0.0025 * float(k - 8);
let pa = pricing.bond_price_from_yield(a, yield_a + s);
let pb = pricing.bond_price_from_yield(b, yield_b + s);
let da = pa * face_a / 100.0 - base_a;
let db = pb * hedge / 100.0 - base_b;
bp.add(lmf.round(s * 10000.0, 0)); alone.add(lmf.round(da, 2));
hedged.add(lmf.round(da - db, 2));
}
let one = pricing.bond_price_from_yield(a, yield_a + shift) * face_a / 100.0;
lmcode_return_put("bp", bp); lmcode_return_put("alone", alone);
lmcode_return_put("hedged", hedged);
let answer = int(math_round(lmf.round(one - base_a, 2) * 100.0));
{
"answer": -7360087,
"bp": [
-200,
-175,
-150,
-125,
-100,
-75,
-50,
-25,
0,
25,
50,
75,
100,
125,
150,
175,
200
],
"alone": [
170604.06,
147391.91,
124749.08,
102659.84,
81108.96,
60081.65,
39563.59,
19540.87,
0,
-19072.1,
-37688.13,
-55860.4,
-73600.87,
-90921.14,
-107832.45,
-124345.75,
-140471.63
],
"hedged": [
11627.74,
8795.71,
6384.99,
4381.32,
2770.87,
1540.25,
676.53,
167.16,
0,
163.3,
645.69,
1436.15,
2524.02,
3898.96,
5550.97,
7470.38,
9647.79
]
}The picture shows the profit of A alone falling by 73,600.87 for +100 basis points, while the hedged book gains 2,524.02, and gains 2,770.87 for -100. The hedged line looks flat at this scale; its values are in this paragraph and in the Recap.
Repricing the same book for a shift of 0.0001 (the 25 basis point step of the picture cut to one) gives A -771.80, the sold hedge +772.06 and a profit of 0.26: close to zero, not zero. The answer, line 21, is the profit of A alone at shift, in whole cents: -7360087 for 0.01.
You can change all inputs. A shift that takes bond A's yield to -100% or below stops line 18 with "pricing.bond_price_from_yield: yield must be above -100% per coupon period (above -100% a year for 1 period a year), got -1.45" (for a shift of -1.5).
A shift of -0.9 stays above -100% but gives a profit too large to return, and the run stops, without a line, with "lmcode return: invalid JSON value at /answer: unsafe_integer". A yield of -0.99 for A stops line 11 for the same reason, with "got -1.01". A maturity of 0 years stops line 4 with "bond maturity must be between 0 and 100 years".
A face of 10^16 or more makes the profit of A too large to return and stops the run, without a line, with the same "unsafe_integer" text; 10^15 still runs.
Your turn
The program below should return the profit of a hedged book after a shift. Several candidate programs are offered. Read them, run them on books of your own, and decide which of them is right for every book. Bond A has a face of 1,000,000 in every case, and the hedge face is given. Check runs each candidate on the book shown and on five others.

market.set_date("2025-01-02");
let a = instrument.bond(1000000.0, coupon_a, float(years_a), 1);
let b = instrument.bond(1000000.0, coupon_b, float(years_b), 1);
let da = pricing.bond_price_from_yield(a, yield_a + shift);
da = (da - pricing.bond_price_from_yield(a, yield_a)) * 10000.0;
let db = pricing.bond_price_from_yield(b, yield_b + shift);
db = (db - pricing.bond_price_from_yield(b, yield_b)) * hedge / 100.0;
lmcode_return_put("leg_a", lmf.money(da));
let answer = int(math_round(lmf.round(da + db, 2) * 100.0));
{
"answer": -15194008,
"leg_a": "-74,962.61"
}Bond A (face 1,000,000, coupon coupon_a once a year, years_a whole years, yield yield_a) is held, and the face hedge of bond B (coupon coupon_b, years_b whole years, yield yield_b) has been sold; hedge is given and need not be the exact hedge ratio. Both yields rise by shift (a decimal, negative for a fall). Return, in whole cents, the profit of the book: the change in the value of A minus the change in the value of the face hedge of B, both repriced at the shifted yields. Check runs your program on this book and on five others.
Read the result carefully
The test assumes the two yields move by the same amount, and that assumption is what the profit measures against itself. A 2-year yield and a 10-year yield rarely move together, and lesson 6 shows what happens then. The small profit on both sides is not a gain to count on: it is the difference of the curvature of the two bonds, and the model charges nothing for time passing, for borrowing bond B or for trading.
The prices come from the engine at the shifted yields, so they are exact for the model; the profit is rounded to cents because binary floats are not exact. The bonds pay for certain, and the yields and shifts are inputs.
Recap
- To test a hedge, reprice both bonds at the shifted yields and take the change of the long bond minus the change of the sold one.
- For one basis point the hedged profit is close to zero: 0.26.
- For larger shifts a small profit remains, here on both sides: 2,524.02 and 2,770.87 for 100 basis points either way.
- The test is only as good as the assumption that both yields move by the same amount.