Skip to content

Functions: ODDFPRICE, ODDFYIELD, ODDLPRICE, ODDLYIELD (#275) - #276

Merged
h-a-n-a-s-h-i merged 1 commit into
storytold:mainfrom
vangel76:feat/odd-bond-functions
Oct 11, 2026
Merged

h-a-n-a-s-h-i merged 1 commit into
storytold:mainfrom
vangel76:feat/odd-bond-functions

Conversation

@vangel76

Copy link
Copy Markdown
Contributor

Fixes #275

What was added

The four odd-period bond functions, in crates/functions/src/financial.rs next to PRICE/YIELD. Financial is now 55 / 55.

  • ODDFPRICE: the odd first period (short or long) is split into quasi-coupon periods counted back from first_coupon; price = redemption and regular coupons discounted over N + Nq + DSC/NL periods, plus the odd first coupon 100·rate/freq · Σ DCi/NLi discounted over Nq + DSC/NL, minus accrued interest 100·rate/freq · Σ Ai/NLi. With one quasi-period this is the documented odd-short formula (A, DFC, DSC, E, N).
  • ODDFYIELD: bounded bisection on ODDFPRICE (price falls as yield rises), #NUM! when no bracket is found. Microsoft's page says Excel uses Newton; the root is the same.
  • ODDLPRICE / ODDLYIELD: quasi-coupon periods counted forward from last_interest; (red + C·ΣDCi/NLi) / (1 + ΣDSCi/NLi · yld/freq) − C·ΣAi/NLi and its closed-form inverse.
  • Day counts reuse the existing basis helpers (30/360 US and European, actual); NL is COUPDAYS' normal period length.
  • Argument checks: dates truncated, date order (maturity > first_coupon > settlement > issue, maturity > settlement > last_interest), rate < 0, yld < 0, pr ≤ 0, redemption ≤ 0, frequency not 1/2/4, basis outside 0–4 → #NUM!; non-numeric → #VALUE!. Every quasi-period walk is capped (40,000 periods, more than the date range allows).

Source: the ODDFPRICE, ODDFYIELD, ODDLPRICE and ODDLYIELD pages on support.microsoft.com (syntax, remarks, variable definitions, worked examples). No GPL code was consulted.

Tests

financial::tests::odd_periods and odd_periods_survive_hostile_input:

  • Microsoft's worked examples, against the results printed there: ODDFPRICE 113.60 (ours 113.5977), ODDFYIELD 7.72% (ours 0.077246), ODDLPRICE 99.88 (ours 99.8783), ODDLYIELD 0.04519 (ours 0.045192).
  • Yield → price → yield round trips: short and long odd first periods (bases 0, 1, 3) and a long odd last period (quarterly, basis 1).
  • Errors: bad frequency, bad basis, negative yield, zero price, settlement after first coupon, settlement before last interest, text rate.
  • Hostile input: ~32,000 quarterly quasi-periods across the date range, rates and yields of 1e300, out-of-range dates. Each returns a value or an error with no panic or hang, and the long walk still round-trips.

Commands run:

cargo fmt -p gridcraft-functions -- --check
cargo clippy -p gridcraft-functions --all-targets -- -D warnings
cargo test -p gridcraft-functions          # 98 passed

Docs

docs/function-parity.md (Financial 55 / 55; 505 / 523 by name, 496 working; scope table and revision row), docs/gaps.md §15, docs/target-app-parity.md, ROADMAP.md (numbers and a progress-log row) and the count in README.md.

Not done / unverified

  • Microsoft's pages print only 2–4 digits, so tests use tolerances at that precision. The full-precision values above need confirmation in Excel.
  • Bases 0 and 4 compute DSC directly with 30/360 from settlement to the next quasi-coupon date, not as E − A. Both give the same results for the documented examples, but they can differ at month ends. This needs confirmation in Excel.
  • A first_coupon that is not on maturity's coupon schedule is accepted: N is counted on maturity's schedule. Excel's behaviour there is unverified.
  • An issue date in the first quasi-period of 1900 returns #NUM!, because the walk cannot step before 1900-01-01.
  • Out-of-range dates return #NUM! here (the crate's existing convention for PRICE, YIELD and the rest). Microsoft documents #VALUE! for invalid dates.

🤖 Generated with Claude Code

Odd first and last coupon periods via quasi-coupon periods, from the
formulas on Microsoft's public function pages. ODDLYIELD is closed form;
ODDFYIELD bisects ODDFPRICE (bounded). Financial is now 55 / 55.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@echelon

echelon commented Oct 11, 2026

Copy link
Copy Markdown
Member

Merged — thank you for the contribution!

If you haven't already, come join us on the ArtCraft Discord: https://discord.gg/artcraft

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Parity: Formulas › Financial › ODDFPRICE, ODDFYIELD, ODDLPRICE, ODDLYIELD

3 participants