Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 27 additions & 4 deletions financepy/products/bonds/bond.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,16 @@ def _g(oas, *args):
###############################################################################


def _check_not_maturity(bond, settle_dt, measure):
"""On the maturity date the final flows are paid at once, so the price does
not depend on a yield or spread and there is none to solve for."""
if settle_dt == bond.maturity_dt:
raise FinError(f"Cannot calculate the {measure} on the maturity date")


###############################################################################


def _validate_yield(ytm):

ytms = []
Expand Down Expand Up @@ -1177,8 +1187,8 @@ def dirty_price_from_discount_curve(self, settle_dt: Date, discount_curve: Disco
cpn_dt = self.cpn_dts[i]
pmt_dt = self.payment_dts[i]

# Only consider flows where the coupon date is in the future
if cpn_dt > settle_dt:
# Only consider flows that the buyer is owed
if self._is_owed(cpn_dt, settle_dt):

# Check for ex-dividend status on the next immediate coupon
# This is only relevant for the first future coupon encountered
Expand Down Expand Up @@ -1223,6 +1233,8 @@ def yield_to_maturity(
if settle_dt < self.issue_dt:
raise FinError("Settlement date falls before issue date")

_check_not_maturity(self, settle_dt, "yield to maturity")

clean_prices = _vectorise_price(clean_price)

self.accrued_interest(settle_dt, 1.0)
Expand Down Expand Up @@ -1297,6 +1309,15 @@ def _calc_pcd_ncd(self, settle_dt: Date):

###########################################################################

def _is_owed(self, cpn_dt: Date, settle_dt: Date):
"""Whether the buyer is owed the flow of coupon date cpn_dt. A coupon
paid on the settlement date goes to the seller, except on the maturity
date, when the final flows are still owed, as in dirty_price_from_ytm."""

return cpn_dt > settle_dt or cpn_dt == settle_dt == self.maturity_dt

###########################################################################

def accrued_interest(self, settle_dt: Date, face: float = 100.0):
"""Calculate the amount of coupon that has accrued between the
previous coupon date and the settlement date. Note that for some day
Expand Down Expand Up @@ -1406,6 +1427,7 @@ def z_spread(
"""Calculate the z-spread of the bond. The discount curve
is a Ibor curve that is passed in."""
check_curve_dt(settle_dt, discount_curve)
_check_not_maturity(self, settle_dt, "z-spread")

self.accrued_int = self.accrued_interest(settle_dt, 1.0)
accrued_amount = self.accrued_int * self.par
Expand Down Expand Up @@ -1484,7 +1506,7 @@ def dirty_price_from_oas(self, settle_dt: Date, discount_curve: DiscountCurve, o
pmt_dt = self.payment_dts[i]
cpn_dt = self.cpn_dts[i] # Use coupon date for ownership logic

if cpn_dt > settle_dt:
if self._is_owed(cpn_dt, settle_dt):

t = times_from_dates(settle_dt, pmt_dt, curve_dc_type)
t = np.maximum(t, G_SMALL)
Expand Down Expand Up @@ -1523,6 +1545,7 @@ def option_adjusted_spread(
and the discount relative to which the spread is to be computed."""

check_curve_dt(settle_dt, discount_curve)
_check_not_maturity(self, settle_dt, "OAS")

if isinstance(clean_price, float) or isinstance(clean_price, np.float64):
clean_prices = np.array([clean_price])
Expand Down Expand Up @@ -1600,7 +1623,7 @@ def dirty_price_from_survival_curve(
pmt_dt = self.payment_dts[i]
cpn_dt = self.cpn_dts[i]

if cpn_dt > settle_dt:
if self._is_owed(cpn_dt, settle_dt):
df = discount_curve.df(pmt_dt)
q = survival_curve.survival_prob(pmt_dt)

Expand Down
56 changes: 56 additions & 0 deletions tests/unit/test_bond_maturity_settlement.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import pytest

from financepy.market.curves.flat_discount_curve import FlatDiscountCurve
from financepy.products.bonds.bond import Bond, YTMCalcType
from financepy.utils.date import Date
from financepy.utils.day_count import DayCountTypes
Expand Down Expand Up @@ -60,6 +61,61 @@ def test_settlement_on_maturity(earlier_dt, ex_div_days, accrued, dirty_price):
########################################################################################


@pytest.mark.parametrize("earlier_dt", EARLIER_DTS, ids=EARLIER_IDS)
@pytest.mark.parametrize(
"ex_div_days, dirty_price",
[(0, 102.0), (7, 100.0)],
ids=["no_ex_div", "ex_div"],
)
def test_curve_prices_on_maturity(earlier_dt, ex_div_days, dirty_price):

# The flows paid on the maturity date are owed, as in dirty_price_from_ytm, and
# are discounted to the settlement date, so the anchor of the curves cancels out
discount_curve = FlatDiscountCurve(ISSUE_DT, 0.05)
survival_curve = FlatDiscountCurve(ISSUE_DT, 0.02)

def bond():
b = _bond(ex_div_days)
if earlier_dt is not None:
b.accrued_interest(earlier_dt, 100.0)
return b

dirty_prices = [
bond().dirty_price_from_discount_curve(MATURITY_DT, discount_curve),
bond().dirty_price_from_oas(MATURITY_DT, discount_curve, 0.01),
bond().dirty_price_from_survival_curve(MATURITY_DT, discount_curve, survival_curve, 0.4),
]
clean_prices = [
bond().clean_price_from_discount_curve(MATURITY_DT, discount_curve),
bond().clean_price_from_survival_curve(MATURITY_DT, discount_curve, survival_curve, 0.4),
]

assert dirty_prices == pytest.approx([dirty_price] * 3)
assert clean_prices == pytest.approx([100.0] * 2)


########################################################################################


def test_yields_and_spreads_on_maturity_raise():

# The price no longer depends on a yield or spread on the maturity date
bond = _bond()
discount_curve = FlatDiscountCurve(ISSUE_DT, 0.05)

with pytest.raises(FinError, match="on the maturity date"):
bond.yield_to_maturity(MATURITY_DT, 100.0)

with pytest.raises(FinError, match="on the maturity date"):
bond.option_adjusted_spread(MATURITY_DT, 100.0, discount_curve)

with pytest.raises(FinError, match="on the maturity date"):
bond.z_spread(MATURITY_DT, 100.0, discount_curve)


########################################################################################


@pytest.mark.parametrize("earlier_dt", EARLIER_DTS[:2], ids=EARLIER_IDS[:2])
def test_settlement_after_maturity_raises(earlier_dt):

Expand Down
Loading