Want to calculate the bond price using Python?
If so, I’ll share with you the Python code to calculate bond prices. I’ll also review a simple example to demonstrate how to apply the code in practice.
You’ll be able to use the code right away, even if you never used Python before. For instructions on how to run any code in python, please visit this simple guide that explains the steps to run a code in Python from scratch.
By the end of this post, you’ll be able to use following tool to get your bond prices:
Tool to Calculate the Bond Price using Python
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 can press F5 to run the Python code). You’ll need to save the Python file at any location of your choosing.
You’ll then get this tool that can be used to calculate the bond price:
In the next section, I’ll review a simple example to demonstrate how you can use the tool to calculate the bond price using Python.
Example of Calculating the Bond Price 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
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.
To get the bond price using the tool, simply type the values in the entry boxes:
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:
That’s it! 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.
In the last section of this post, I’ll share with you a template that you can apply in Python to get the bond prices.
Template to 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
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.