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) bondPrice = ((fv*c/m*(1-(1+ytm/m)**(-m*t)))/(ytm/m)) + fv*(1+(ytm/m))**(-m*t) print (bondPrice)
For example, let’s suppose that you have a bond, where the:
- Coupon rate is 6% with semiannually 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 semiannually 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 bondPrice = ((fv*c/m*(1-(1+ytm/m)**(-m*t)))/(ytm/m)) + fv*(1+(ytm/m))**(-m*t) print (bondPrice)
Run the code in Python and you’ll get the Bond price of: 873.4