curatedapi

Excel DURATION function: Macaulay duration of a bond

The Macaulay duration of a bond with periodic coupons, as Excel's DURATION: the average time to its payments, weighted by their present value.

Settlement date (when the buyer gets the bond).
Maturity date.
Annual coupon rate as a decimal. A ratio of two decimals such as "0.08/12" is accepted.
Annual yield as a decimal. A ratio of two decimals such as "0.08/12" is accepted.
Coupons per year: 1, 2 or 4.
Day count basis: 0 US (NASD) 30/360; 1 Actual/actual; 2 Actual/360; 3 Actual/365; 4 European 30/360.
More options: rounding and decimals
Precision and rounding
Rounding mode applied once to each result. Default half_even.
Round results to this many digits after the decimal point.
Round results to this many significant digits.

Examples

Result: Microsoft's example: 8% coupon, 9% yield, 2018 to 2048, actual/actual

Duration
Macaulay duration in years.
10.9191453 rounded
Coupons remaining
Coupons from settlement to maturity (N).
59

Rounding: Half even, 7 decimal places. Calculator excel/duration@1.

How it is calculated

Σ t_k·CF_k/(1 + yld/f)^t_k ÷ Σ CF_k/(1 + yld/f)^t_k ÷ f, t_k = k − 1 + DSC/E

DURATION(settlement, maturity, coupon, yld, frequency, [basis]): the Macaulay duration in years for an assumed par value of 100. It is the weighted average of the times at which the bond pays, each weighted by the present value of that payment at the given yield, and measures how sensitive the price is to the yield.

  • Between coupon dates, the time to each coupon is counted as in PRICE: the days from settlement to the next coupon divided by the days in the coupon period, plus whole periods. Microsoft does not publish this detail. On a coupon date it is the textbook formula and gives Microsoft's documented examples exactly.
  • Arguments are named and ordered as in Excel. Excel computes in binary floating point; this API computes the exact value of the same formula and rounds it once, so it agrees with Excel to the digits Excel can show, and where Excel's own rounding error reaches the 15th digit this answer is the correct one.
  • Dates are ISO 8601 (YYYY-MM-DD) from 1900-03-01 to 9999-12-31, so Excel's fictional 29 February 1900 never comes into play. Each date result also gives Excel's serial number (1900 date system).

References

Results follow each calculator's documented rules and the sources listed. They are not financial, tax or legal advice. Check that the rules fit your case before you rely on a result. See the terms.

Use it from your code

POST /v1/calc/excel/durationVersion 1
curl https://api.curatedapi.com/v1/calc/excel/duration \
  -H "Authorization: Bearer $CURATEDAPI_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "settlement": "2018-07-01",
    "maturity": "2048-01-01",
    "coupon": "0.08",
    "yld": "0.09",
    "frequency": 2,
    "basis": 1,
    "options": {"decimals": 7}
  }'
The same call in Python
import os

import requests

payload = {
    "settlement": "2018-07-01",
    "maturity": "2048-01-01",
    "coupon": "0.08",
    "yld": "0.09",
    "frequency": 2,
    "basis": 1,
    "options": {
        "decimals": 7,
    },
}
response = requests.post(
    "https://api.curatedapi.com/v1/calc/excel/duration",
    headers={"Authorization": f"Bearer {os.environ['CURATEDAPI_KEY']}"},
    json=payload,
    timeout=10,
)
print(response.json()["result"])
The same call in JavaScript
const payload = {
    "settlement": "2018-07-01",
    "maturity": "2048-01-01",
    "coupon": "0.08",
    "yld": "0.09",
    "frequency": 2,
    "basis": 1,
    "options": {
        "decimals": 7,
    },
};
const response = await fetch("https://api.curatedapi.com/v1/calc/excel/duration", {
  method: "POST",
  headers: {
    Authorization: `Bearer ${process.env.CURATEDAPI_KEY}`,
    "Content-Type": "application/json",
  },
  body: JSON.stringify(payload),
});
console.log((await response.json()).result);

MCP tool calc_excel_duration at https://mcp.curatedapi.com. Inputs, outputs and errors in the API reference.

Put it on your website

<div data-curatedapi-widget="excel/duration" data-key="capi_public_…"></div>
<script src="https://widgets.curatedapi.com/v1/widget.js" async></script>

Use a public key registered for your site. Calculations are billed like API calls. How widgets work.

Related calculators

Get Excel's answer in your own code

Moving a spreadsheet model into an app? The API returns what Excel returns for the same inputs, so your users see the same numbers in both.

Free for up to 1,000 calculations a month. 50,000 calculations a month cost $9.00 on the Starter plan.