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) mduration = (price_minus-price_plus)/(2.*price*dy) return mduration
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 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 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) mduration = (price_minus - price_plus) / (2. * price * dy) return mduration
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) # calculate the difference in price when bond moves up and down # for a given change in yield mduration = (price_minus - price_plus) / (2. * price * dy) return mduration
def bond_convexity(price, par, T, coup, freq, dy=0.01): """ Measures the change in duration due to a change in yield a convexity of 300 means a 1% change in yield will lead to a 3% (0.03) change in price resulting from convexity """ 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
""" README ====== This file contains Python codes. ====== """ """ Get bond price from YTM """ def bond_price(par, T, ytm, coup, freq=2): freq = float(freq) periods = T*freq coupon = coup/100.*par/freq dt = [(i+1)/freq for i in range(int(periods))] price = sum([coupon/(1+ytm/freq)**(freq*t) for t in dt]) + \ par/(1+ytm/freq)**(freq*T) return price if __name__ == "__main__": from bond_ytm import bond_ytm ytm = bond_ytm(95.0428, 100, 1.5, 5.75, 2) print bond_price(100, 1.5, ytm, 5.75, 2)