# Yield To Maturity calculation using a Python script

When interested in buying bonds a term that often comes up is the definition of Yield To Maturity (YTM):

*Yield to maturity (YTM) is the total return anticipated on a bond if the bond is held until the end of its lifetime. Yield to maturity is considered a long-term bond yield, but is expressed as an annual rate. In other words, it is the internal rate of return of an investment in a bond if the investor holds the bond until maturity and if all payments are made as scheduled.* – Investopedia

Let us consider a bond with a par value of and a coupon rate of with the bond maturing in years time. The current bond price is . We are interested in the bond’s YTM.

Knowing that we get annual cashflow () we will have the following cashflow for the next years. Notice in the last year we will receive an additional which is the bond’s principle:

Let us assume that we want to value these future cashflows and determine how much they are worth today. What we need to do, is to *present value* all of these future cashflows back to today and add up those present values . This sum would be the price of the bond.

A *present value* is the amount of money one would need to invest today in order to have amount of money in years if he is earning (annual)interest on his investment:

For example, if Bob wants to have one year from now at a rate of , he needs to invest today ().

Back to our bond’s present value of future cashflows the resulting formula would look as follows:

with being the yield to maturity we are looking for.

For our particular bond the formula will look like this:

So how can we go about finding ? Well, above we have learned that sum of present values of all future cash flows is our bond price. Since we already know the today’s bond price which is , the formula becomes like this:

All we need to do now is to solve the above equation for so that it holds true. We will do this through trial-and-error.

Solving the equation by hand requires an understanding of the relationship between a bond’s price and its yield, as well as of the different types of bond pricings. Bonds can be priced at a discount, at par and at a premium. When the bond is priced at par, the bond’s interest rate is equal to its coupon rate. A bond priced above par (called a premium bond) has a coupon rate higher than the interest rate, and a bond priced below par (called a discount bond) has a coupon rate lower than the interest rate. So if an investor were calculating YTM on a bond priced below par, he or she would solve the equation by plugging in various annual interest rates that were higher than the coupon rate until finding a bond price close to the price of the bond in question (*quote from:* Investopedia).

You can verify this quickly with a simple example. Assume a bond with current price and a par value of that matures in year. The coupon rate is and interest rate is :

==>

==>

If then . Similarly for the other cases.

Back to our bond. We had the following equation:

Now we must solve for the interest rate which is where things start to get difficult. Yet, we do not have to start simply guessing random numbers if we stop for a moment to consider the relationship between bond price and yield. As was mentioned above, when a bond is priced at a discount from par, its interest rate will be greater than the coupon rate. In this example, the par value of the bond is , but it is priced below the par value at , meaning that the bond is priced at a discount. As such, the annual interest rate we are seeking must necessarily be greater than the coupon rate of .

With this information we can now calculate and test a number of bond prices by plugging various annual interest rates that are higher than into the formula above. Using a few different interest rates above , one would come up with the following bond prices:

```
...
ytm:3.61% price: 89.06
ytm:3.61% price: 89.04
ytm:3.61% price: 89.02
ytm:3.62% price: 89.01
ytm:3.62% price: 88.99
```

I have used increments and as we can see with an interest rate of we can approximate our current price of quite good enough. So would be our Yield To Maturity.

```
$ python3 yield.py -p 139.87 -f 99.94 -r 6.250 -y 12 -s
```

**Full code**

import argparse import math def total_present_value(face_value, coupon, periods, rate): total_pv = 0 for n in range(1, periods+1): total_pv += coupon / math.pow((1 + rate), n) total_pv += face_value / math.pow((1 + rate), periods) return total_pv def main(): parser = argparse.ArgumentParser() parser.add_argument('-p', type=float, help='specifies the current price') parser.add_argument('-f', type=float, default=1000, help='specifies the face Value') parser.add_argument('-r', type=float, default=5, help='specifies the annual coupon rate in %') parser.add_argument('-y', type=int, default=15, help='specifies the number of years remaining to maturity') parser.add_argument('-s', action='store_true', default=False, help='coupon is a semi-annual coupon. Default is annual') args = parser.parse_args() coupon_rate = args.r / 100.0 coupon = args.f * coupon_rate factor = 2 if args.s else 1 print("------------------------------------------------") print("Price: %s" % args.p) print("Face Value: %s" % args.f) print("Annual coupon rate: %.2f%%" % args.r) print("Coupon: %s" % coupon) print("Semi-annual coupon: %s" % args.s) print("Years remaining: %s" % args.y) print("\n") ytm = coupon_rate condition = True while condition: if (args.p < args.f): ytm += 0.00001 else: ytm -= 0.00001 total_pv = total_present_value(args.f, coupon/factor, args.y*factor, ytm/factor) if (args.p < args.f): condition = total_pv > args.p else: condition = total_pv < args.p print("Yield to Maturity: %.2f%%" % (ytm*100.0)) if __name__ == '__main__': main()

Result:

```
------------------------------------------------
Price: 139.87
Face Value: 99.94
Annual coupon rate: 6.25%
Coupon: 6.24625
Semi-annual coupon: True
Years remaining: 12
Yield to Maturity: 2.40%
```

**References**

Yield To Maturity (YTM) – Investopedia