def bond_mod_duration(price, par, T, coup, freq, dy=0.01): ytm = bond_ytm(price, par, T, coup, freq) ytm_minus = ytm - dy price_minus = bond_price(par, T, ytm_minus, coup, freq) ytm_plus = ytm + dy price_plus = bond_price(par, T, ytm_plus, coup, freq) duration = (price_minus - price_plus) / (2 * price * dy) return duration
def bond_convexity(price, par, T, coup, freq, dy=0.01): ytm = bond_ytm(price, par, T, coup, freq) ytm_minus = ytm - dy price_minus = bond_price(par, T, ytm_minus, coup, freq) ytm_plus = ytm + dy price_plus = bond_price(par, T, ytm_plus, coup, freq) convexity = (price_minus + price_plus - 2 * price) / (price * dy ** 2) return convexity
def test_bond_ytm(self): ytm = bond_ytm(95.0428, 100, 1.5, 5.75, 2) print("ytm=%s" % ytm) self.assertAlmostEqual(ytm, 0.0936915534524, places=13)