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
65 changes: 38 additions & 27 deletions financepy/products/bonds/bond.py
Original file line number Diff line number Diff line change
Expand Up @@ -613,12 +613,14 @@ def forward_price(
accrued_settle = self.accrued_interest(settle_dt)
accrued_forward = self.accrued_interest(forward_dt)

# The seller keeps the coupons that the settlement price includes and the
# forward price does not. One kept in an ex-dividend period is paid after
# the forward date, so its time to the forward date is negative
fv_cpns = 0.0
for dt, amt in zip(self.payment_dts[1:], self.flow_amounts[1:]):
if settle_dt < dt <= forward_dt:
t_cpn_to_forward, _, _ = repo_dc.year_frac(dt, forward_dt)
g_cpn = 1.0 + repo_rate * t_cpn_to_forward
fv_cpns += amt * self.par * g_cpn
for pmt_dt, amt in self._cpns_held(settle_dt, forward_dt):
t_cpn_to_forward, _, _ = repo_dc.year_frac(pmt_dt, forward_dt)
g_cpn = 1.0 + repo_rate * t_cpn_to_forward
fv_cpns += amt * self.par * g_cpn

dirty_price = clean_price + accrued_settle
g = 1.0 + repo_rate * t_fwd
Expand Down Expand Up @@ -1658,6 +1660,36 @@ def clean_price_from_survival_curve(

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

def _cpns_held(self, begin_dt: Date, end_dt: Date):
"""Return the (payment date, amount per unit of par) of the coupons that a
holder from begin_dt to end_dt receives. As in the dirty prices, a
settlement on or after a coupon date, or after its ex-dividend date,
leaves that coupon to the seller. So the holder receives the coupons the
begin price includes and the end price does not. On the maturity date
the end price includes the final coupon unless the bond is ex-dividend."""

cal = Calendar(self.cal_type)

def is_entitled(settle_dt, cpn_dt, ex_div_dt):
return settle_dt < cpn_dt and settle_dt <= ex_div_dt

held = []
flows = zip(self.cpn_dts[1:], self.payment_dts[1:], self.flow_amounts[1:])

for cpn_dt, pmt_dt, flow in flows:
ex_div_dt = cal.add_business_days(cpn_dt, -self.ex_div_days)
in_end_price = end_dt == self.maturity_dt == cpn_dt and end_dt <= ex_div_dt
if (
is_entitled(begin_dt, cpn_dt, ex_div_dt)
and not is_entitled(end_dt, cpn_dt, ex_div_dt)
and not in_end_price
):
held.append((pmt_dt, flow))

return held

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

def calc_ror(
self,
begin_dt: Date,
Expand All @@ -1677,28 +1709,7 @@ def calc_ror(
buy_price = self.dirty_price_from_ytm(begin_dt, begin_ytm, convention)
sell_price = self.dirty_price_from_ytm(end_dt, end_ytm, convention)

# As in the dirty prices, a settlement on or after a coupon date, or after
# its ex-dividend date, leaves that coupon to the seller. So the holder
# receives the coupons the buy price includes and the sell price does
# not. On the maturity date the sell price includes the final coupon
# unless the bond is ex-dividend.
cal = Calendar(self.cal_type)

def is_entitled(settle_dt, cpn_dt, ex_div_dt):
return settle_dt < cpn_dt and settle_dt <= ex_div_dt

dts_cfs = []
flows = zip(self.cpn_dts[1:], self.payment_dts[1:], self.flow_amounts[1:])

for cpn_dt, pmt_dt, flow in flows:
ex_div_dt = cal.add_business_days(cpn_dt, -self.ex_div_days)
in_sell_price = end_dt == self.maturity_dt == cpn_dt and end_dt <= ex_div_dt
if (
is_entitled(begin_dt, cpn_dt, ex_div_dt)
and not is_entitled(end_dt, cpn_dt, ex_div_dt)
and not in_sell_price
):
dts_cfs.append((pmt_dt, flow * self.par))
dts_cfs = [(pmt_dt, flow * self.par) for pmt_dt, flow in self._cpns_held(begin_dt, end_dt)]

dts_cfs.append((begin_dt, -buy_price))
dts_cfs.append((end_dt, sell_price))
Expand Down
29 changes: 29 additions & 0 deletions tests/unit/test_bond_maturity_settlement.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,32 @@ def test_rate_of_return_coupons(ex_div_days, buy_dt, sell_dt, num_cpns):
_, _, pnl = _bond(ex_div_days).calc_ror(buy_dt, sell_dt, YTM, YTM, convention)

assert pnl == pytest.approx(2.0 * num_cpns + sell_price - buy_price)


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


@pytest.mark.parametrize("ex_div_days", [0, 7], ids=["no_ex_div", "ex_div"])
@pytest.mark.parametrize(
"first_fwd_dt, last_fwd_dt",
[
(Date(10, 7, 2029), Date(20, 7, 2029)),
(Date(28, 12, 2029), MATURITY_DT),
],
ids=["sunday_coupon_date", "final_coupon_period"],
)
def test_forward_price_moves_smoothly(ex_div_days, first_fwd_dt, last_fwd_dt):

# From one forward date to the next, the forward clean price only moves by a
# day of repo carry and accrual. Crediting the seller with a coupon that the
# forward price also includes, or not crediting one the seller keeps, would
# move it by a whole coupon of 2. The 15 Jul 2029 coupon falls on a Sunday and
# is paid on the Monday, the final coupon's ex-dividend date is 4 Jan 2030 when
# there are 7 ex-dividend days, and the final coupon is paid on the maturity date
settle_dt = Date(1, 3, 2027)

fwd_dts = [first_fwd_dt.add_days(n) for n in range(int(last_fwd_dt - first_fwd_dt) + 1)]
prices = [_bond(ex_div_days).forward_price(settle_dt, dt, 100.0, 0.03) for dt in fwd_dts]
moves = [abs(b - a) for a, b in zip(prices, prices[1:])]

assert max(moves) < 0.01
Loading