Skip to Content
Mastering Python for Finance - Second Edition
book

Mastering Python for Finance - Second Edition

by James Ma Weiming
April 2019
Intermediate to advanced
426 pages
11h 13m
English
Packt Publishing
Content preview from Mastering Python for Finance - Second Edition

Calculating the price of a bond

When the YTM is known, we can get back the bond price in the same way we used the pricing equation. This is implemented by the bond_price() function:

In [ ]:    def bond_price(par, T, ytm, coup, freq=2):        freq = float(freq)        periods = T*2        coupon = coup/100.*par        dt = [(i+1)/freq for i in range(int(periods))]        price = sum([coupon/freq/(1+ytm/freq)**(freq*t) for t in dt]) + \            par/(1+ytm/freq)**(freq*T)        return price

Plugging in the same values from the earlier example, we get the following result:

In [ ]:    price = bond_price(100, 1.5, ytm, 5.75, 2)    print(price)Out[ ]:       95.04279999999997

This gives us the same original bond price discussed in the earlier example, Calculating the yield to maturity. With the bond_ytm()

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Python for Finance - Second Edition

Python for Finance - Second Edition

Yuxing Yan
Python for Finance

Python for Finance

Yves Hilpisch

Publisher Resources

ISBN: 9781789346466Supplemental Content