April 2019
Intermediate to advanced
426 pages
11h 13m
English
When the YTM is known, we can get back the bond price in the same way we used the pricing equation. This is implemented by the bond_price() function:
In [ ]: def bond_price(par, T, ytm, coup, freq=2): freq = float(freq) periods = T*2 coupon = coup/100.*par dt = [(i+1)/freq for i in range(int(periods))] price = sum([coupon/freq/(1+ytm/freq)**(freq*t) for t in dt]) + \ par/(1+ytm/freq)**(freq*T) return price
Plugging in the same values from the earlier example, we get the following result:
In [ ]: price = bond_price(100, 1.5, ytm, 5.75, 2) print(price)Out[ ]: 95.04279999999997
This gives us the same original bond price discussed in the earlier example, Calculating the yield to maturity. With the bond_ytm()