How to Calculate the Bond Price using Python

In this short guide, you’ll see how to calculate the bond price using Python.

You’ll also observe how to create the tool below to calculate bond prices:

Tool 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.

Tool to Calculate the Bond Price

Step 1: Copy the code

Copy the code below into Python:

import tkinter as tk

root= tk.Tk()

canvas1 = tk.Canvas(root, width = 470, height = 480)
canvas1.pack()

label1 = tk.Label(root, text='Calculate Bond Price')
label1.config(font=('helvetica', 14))
canvas1.create_window(235, 40, window=label1)

entry1 = tk.Entry (root) 
canvas1.create_window(330, 100, window=entry1)

entry2 = tk.Entry (root) 
canvas1.create_window(330, 140, window=entry2)

entry3 = tk.Entry (root) 
canvas1.create_window(330, 180, window=entry3)

entry4 = tk.Entry (root) 
canvas1.create_window(330, 220, window=entry4)

entry5 = tk.Entry (root) 
canvas1.create_window(330, 260, window=entry5)

entry6 = tk.Entry (root) 
canvas1.create_window(240, 380, window=entry6)


label1 = tk.Label(root, text='Number of Payments Per Period: ')
label1.config(font=('helvetica', 10))
canvas1.create_window(160, 100, window=label1)

label2 = tk.Label(root, text='Number of Years to Maturity:       ')
label2.config(font=('helvetica', 10))
canvas1.create_window(160, 140, window=label2)

label3 = tk.Label(root, text='Yield to Maturity (YTM):              ')
label3.config(font=('helvetica', 10))
canvas1.create_window(160, 180, window=label3)

label4 = tk.Label(root, text='Face Value:                               ')
label4.config(font=('helvetica', 10))
canvas1.create_window(160, 220, window=label4)

label5 = tk.Label(root, text='Coupon Rate:                             ')
label5.config(font=('helvetica', 10))
canvas1.create_window(160, 260, window=label5)


def calcBondPrice ():  
    m = float(entry1.get())
    t = float(entry2.get())
    ytm = float(entry3.get())/100
    fv = float(entry4.get())
    c = float(entry5.get())/100
    
    bondPrice = ((fv*c/m*(1-(1+ytm/m)**(-m*t)))/(ytm/m)) + fv*(1+(ytm/m))**(-m*t)
    
    label6 = tk.Label(root, text= bondPrice,font=('helvetica', 10, 'bold'),bg='white')
    canvas1.create_window(240, 380, window=label6)

    
button1 = tk.Button(text='Calculate Bond Price', command=calcBondPrice, bg='green', fg='white', font=('helvetica', 9, 'bold'))
canvas1.create_window(240, 330, window=button1)

root.mainloop()

Step 2: Run the code

Run the code in Python. You’ll then get this tool that can be used to calculate the bond price:

Tool to Calculate the Bond Price using Python

Step 3: Calculate the bond price

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

To get the bond price using the tool, simply type the values in the entry boxes:

Bond Price using Python

Once you’re done entering the values, click on the green button to calculate the bond price. You’ll then get the bond price of 873.4:

Tool to Calculate the Bond Price using Python

You can use the tool at any point in time to derive the price of your bond. You may also want to check the following source that explains the actual calculations that were used to derive the bond price.