Lags, schedules, and the MLK payment
Worked examples
Read the claim, then run it and check the machine agrees. One at a time — nothing here is taken on trust.
import QuantLib as ql
usgov = ql.UnitedStates(ql.UnitedStates.GovernmentBond)
trade = ql.Date(9, 7, 2026) # a Thursday
print("trade:", trade.ISO())
print("T+1: ", usgov.advance(trade, 1, ql.Days).ISO())
print("T+2: ", usgov.advance(trade, 2, ql.Days).ISO(), " <- spot crosses the weekend")
Two business days from a Thursday is Monday — the weekend doesn't count. Every date in the trade hangs off this one: deal a "2-year swap" on Thursday and its cash flows anchor to Monday the 13th, not to the trade date. (And per lesson 2.1's challenge: had this been early April, the count could have skipped a Good Friday too — with SIFMA deciding whether it counts.)
import QuantLib as ql
usgov = ql.UnitedStates(ql.UnitedStates.GovernmentBond)
spot = ql.Date(13, 7, 2026)
termination = spot + ql.Period(18, ql.Months)
sched = ql.Schedule(spot, termination, ql.Period(1, ql.Years), usgov,
ql.ModifiedFollowing, ql.ModifiedFollowing,
ql.DateGeneration.Backward, False)
dates = list(sched)
print("accrual dates: ", [d.ISO() for d in dates])
print("period lengths:", [b - a for a, b in zip(dates, dates[1:])], "days")
Backward means: stand at 13 January 2028 and step a year at a time — 13 January 2027... and the leftover 184-day piece lands against spot, at the front. Forward generation would have produced a short back stub instead — same trade description, different cash flow dates, which is why the generation rule is part of market convention (Backward is standard) and not a free choice.
import QuantLib as ql
usgov = ql.UnitedStates(ql.UnitedStates.GovernmentBond)
accrual_end = ql.Date(13, 1, 2028) # a Thursday
print("accrual ends: ", accrual_end.ISO())
print("+1 business day: ", usgov.advance(accrual_end, 1, ql.Days).ISO())
print("+2 business days: ", usgov.advance(accrual_end, 2, ql.Days).ISO())
print("Mon 17 Jan 2028 open?", usgov.isBusinessDay(ql.Date(17, 1, 2028)))
One business day is Friday the 14th. The second should be Monday the
17th — but that's Martin Luther King Day, so the count slides to
Tuesday the 18th. A trade dealt in July 2026 has its final cash
flow date decided by a January 2028 holiday. Nobody's mental
arithmetic catches this; advance on the right calendar does,
every time.
Check the concept
One question at a time. Unsure? Revisit the lecture, then answer.
The challenge
Pass the quiz to unlock the challenge — your code will still be waiting here.