In this short guide, you’ll see how to calculate the bond price using Python.
Calculate the Bond Price using Python
Here is a template that you can use to calculate the bond price using Python:
m = number of payments per period (e.g., m=2 for semiannually payments) t = number of years to maturity ytm = yield to maturity (in decimals terms) fv = the bond’s face value c = coupon rate (in decimals terms) bond_price = ((fv * c / m * (1 - (1 + ytm / m) ** (-m * t))) / (ytm / m)) + fv * (1 + (ytm / m)) ** (-m * t) print(bond_price)
For example, let’s suppose that you have a bond, where the:
- Coupon rate is 6% with semiannual payments
- Yield to maturity (YTM) is 8%
- Bond matures in 9 years
- Bond’s face value is 1000
What is the price of the Bond?
Since we are dealing with semiannual payments each year, then the number of payments per period (i.e., per year) is 2.
Based on the above information, you can derive the bond price using this code:
m = 2 t = 9 ytm = 0.08 fv = 1000 c = 0.06 bond_price = ((fv * c / m * (1 - (1 + ytm / m) ** (-m * t))) / (ytm / m)) + fv * (1 + (ytm / m)) ** (-m * t) print(bond_price)
Run the code in Python and you’ll get the Bond price of: 873.4