Tool to Calculate Bond Duration using Python

In this post, I’ll share with you the code to calculate bond duration using Python. I’ll also review a simple example to show you how to use the tool.

You’ll be able to use the code right away. If you are new to Python, you may want to check the following guide that explains how to run a code in Python from scratch.

By the end of this post, you’ll be able to use following tool to calculate your bond duration using Python:

Tool to Calculate Bond Duration using Python

Tool to Calculate Bond Duration using Python

Step 1: Copy the code

To start, 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 Duration')
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)

entry6 = tk.Entry (root) 
canvas1.create_window(240, 420, 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)

label6 = tk.Label(root, text='Macaulay Duration:')
label6.config(font=('helvetica', 10))
canvas1.create_window(115, 380, window=label6)

label7 = tk.Label(root, text='Modified Duration:  ')
label7.config(font=('helvetica', 10))
canvas1.create_window(115, 420, window=label7)

def calcBondPrice ():  
    m = float(entry1.get())
    t = float(entry2.get())
    ytm = float(entry3.get())/100
    fv = float(entry4.get())
    c = float(entry5.get())/100
    
    pv = ((fv*c/m*(1-(1+ytm/m)**(-m*t)))/(ytm/m)) + fv*(1+(ytm/m))**(-m*t)
    final_year = (t*fv)/(pv*(1+(ytm/m))**(m*t))
    
    sum = 0
    x = 1

    while x <= m*t:
        tn = x/m
        x = x + 1
        dur = (fv*c*tn)/(pv*m*(1+(ytm/m))**(m*tn))
        sum = sum + dur
    
    #macaulayDuration
    label8 = tk.Label(root, text= sum+final_year,font=('helvetica', 10, 'bold'),bg='white')
    canvas1.create_window(240, 380, window=label8)
     
    #modifiedDuration
    label9 = tk.Label(root, text= (sum+final_year)/(1+ytm/m),font=('helvetica', 10, 'bold'),bg='white')
    canvas1.create_window(240, 420, window=label9)
        
button1 = tk.Button(text='Calculate Bond Duration', command=calcBondPrice, bg='green', fg='white', font=('helvetica', 9, 'bold'))
canvas1.create_window(240, 320, window=button1)

root.mainloop()

Step 2: Run the code

Next, run the code in Python (you can press F5 to run the code in Python), and you would see the following display:

Calculate Bond Duration using Python

In order to demonstrate how to use the tool, let’s review a simple example.

Example of Calculating the Bond Duration using Python

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

(1) What is the bond’s Macaulay duration?
(2) What is the bond’s Modified duration?

Since we are dealing with semiannually payments each year, then the number of payments per period (i.e., per year) is 2.

To get the bond duration using the tool, simply type the values in the entry boxes. Then click on the green button (the “Calculate Bond Duration” button):

Bond Duration using Python

You’ll then see that for our bond, the:

(1) Macaulay duration = 6.924
(2) Modified duration = 6.658

Tool to Calculate Bond Duration using Python

If you prefer to apply the calculations manually, you may then want to check the following source that includes the formulas used to calculate the bond duration.