A Cap Is a Strip of Caplets
A cap on a floating rate is one caplet for each period after the first, each priced by Black's formula with its own forward rate, expiry and discount factor, and its value is their sum; the engine prices a five-year cap on a flat curve and the program checks the sum and one caplet by hand.
The question
A borrower pays a floating rate every six months for five years, and wants a ceiling on every payment. One caplet protects one payment. Protecting all of them takes a caplet for each, with the same strike: a cap. What does a cap cost, and how many caplets does it have?
The cap is the sum of its caplets, because each period's payoff is separate. Each caplet has its own forward rate, its own time to the fixing and its own discount factor, so each is priced by Black's formula with its own numbers, and the volatility is the same for all of them.
Work it by hand
Take a cap of 5 years with semiannual periods, so 10 periods. The rate of the first period is fixed today, at the start of the loan, so nothing is uncertain about it and it has no option value: the cap has 9 caplets, one for each period from the second to the tenth. In general a cap of y whole years has 2 y minus 1 caplets.
- Caplet i has its forward rate F_i, the time T_i from today to its fixing, the discount factor D_i from today to its payment date and its accrual a_i, the length of the period.
- Its value is notional times a_i times Black's call price of F_i against the strike with volatility and expiry T_i, discounted by D_i.
- The cap's value is the sum over the nine caplets.
For the first caplet of the demo the engine's schedule gives an accrual of 183 days over 360, a fixing 182 days away and a payment 369 days away. These three day counts were found by matching the engine's price to the last digit, not read from a calendar on this page.
Run it
The program takes notional, strike, the flat continuous rate, vol and the whole number of years. Line 1 fixes the evaluation date, which must come before any curve is built. Line 3 describes the cap, and line 4 asks for the value of each caplet. The forwards on line 5 are the simple rates of the periods.
The loop on lines 8 to 13 adds the caplets up and keeps them rounded, with their forwards. Lines 14 to 16 price the first caplet by hand with the three day counts. The returned extra values show the sum of the caplets, the cap's value from pricing.capfloor_npv and the hand-priced first caplet. The program returns the number of caplets, 9.

Watch the steps (recorded video)
market.set_date("2025-01-02");
let curve = market.flat_curve(rate);
let cap = instrument.cap(notional, strike, float(years));
let caplets = pricing.capfloor_caplets(cap, curve, vol);
let forwards = pricing.capfloor_forwards(cap, curve);
let total = 0.0; let shown = float_list(); let rates = float_list();
let labels = str_list();
for (i in range(0, caplets.len())) {
total = total + caplets.at(i);
shown.add(lmf.round(caplets.at(i), 2));
rates.add(lmf.round(forwards.at(i), 6));
labels.add(str(i + 2));
}
let disc = curve.discount(369.0 / 365.0);
let first = forwards.at(0);
let unit = pricing.black76("call", first, strike, vol, 182.0 / 365.0, disc);
lmcode_return_put("labels", labels);
lmcode_return_put("caplets", shown);
lmcode_return_put("forwards", rates);
lmcode_return_put("sum_of_caplets", lmf.money(total));
lmcode_return_put("cap_npv", lmf.money(pricing.capfloor_npv(cap, curve, vol)));
lmcode_return_put("first_hand", lmf.money(notional * (183.0 / 360.0) * unit));
let answer = caplets.len();
{
"answer": 9,
"labels": [
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9",
"10"
],
"caplets": [
1061.09,
1469.17,
1798.37,
2009.95,
2237.74,
2380.92,
2577.02,
2597.09,
2791.12
],
"forwards": [
0.03985,
0.039846,
0.039853,
0.039846,
0.039853,
0.039848,
0.039857,
0.039842,
0.039855
],
"sum_of_caplets": "18,922.46",
"cap_npv": "18,922.46",
"first_hand": "1,061.09"
}The picture has one bar for each caplet, labelled by the period it protects, from the second; it shows at most the first 16, so a cap of 9 years or more is drawn without its last caplets.
You can change the inputs, with limits. years must be 1 to 100, or line 4 stops with "cap/floor maturity must be a whole number of half years, 1 to 100 years". A volatility, strike or notional of 0 stops the same line with "cap/floor volatility must be positive", "cap/floor strike must be positive" or "cap/floor notional must be positive".
A rate of 0 or below stops it with "forward + displacement (0 + 0) must be positive" (for a rate of 0) or "forward + displacement (-0.00983833 + 0) must be positive" (for -0.01): Black's formula needs a positive forward rate, so the flat rate must be above 0. A rate so large that a discount factor becomes 0, such as 150, stops the same line with "discount (0) must be positive".
Your turn
The lists forwards, discounts and expiries describe the caplets of a cap, one entry for each caplet, all of the same length. The program below is meant to return the cap's value in whole cents: the sum over the caplets of notional times accrual times Black's call price with the caplet's own forward, expiry and discount factor. Decide whether it does, and find the candidates that do. Check runs each candidate on the data shown and on five others.

let total = 0.0;
for (i in range(0, forwards.len())) {
let t = expiries.at(forwards.len() - 1); let d = discounts.at(i);
let unit = pricing.black76("call", forwards.at(i), strike, vol, t, d);
total = total + notional * accrual * unit;
}
let answer = int(math_round(lmf.round(total, 2) * 100.0));
{
"answer": 1651715
}The lists forwards, discounts and expiries hold, for each caplet of a cap, its forward rate, its discount factor from today to the payment date and its time in years to the fixing. All three lists have the same length. Return the cap's value in whole cents: the sum over the caplets of notional times accrual times Black's call price on that caplet's forward, with the given strike and vol, the caplet's expiry and its discount factor. Round the total to cents.
Read the result carefully
One flat volatility for every caplet is a quoting convention, not a belief that every period is equally uncertain. The dates follow one calendar and one day count fixed by the engine, and other conventions give other cents. The curve is one made-up flat rate, and the first caplet's hand route uses day counts specific to this evaluation date.
The lists of the exercise must have the same length. A shorter list of discounts or expiries stops the program with "FloatList.at: index out of range: 3" (the index shown is the first one missing); a shorter list of forwards does not stop it, the extra entries are silently ignored. The page does not show how a volatility is quoted for a real cap, or what the cap costs a borrower in fees. It is a description of an arithmetic model and not investment advice.
Recap
- A cap is one caplet for each period whose rate is not yet fixed: 2 y minus 1 caplets for y years of semiannual periods.
- Each caplet is Black's formula with its own forward, expiry and discount factor, times notional and accrual.
- The cap's value is the sum of its caplets.
- The engine's
pricing.capfloor_capletsreturns the caplets andpricing.capfloor_npvtheir sum.