Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Mastering Python for Finance

You're reading from   Mastering Python for Finance Understand, design, and implement state-of-the-art mathematical and statistical applications used in finance with Python

Arrow left icon
Product type Paperback
Published in Apr 2015
Publisher Packt
ISBN-13 9781784394516
Length 340 pages
Edition 1st Edition
Languages
Arrow right icon
Toc

Table of Contents (12) Chapters Close

Preface 1. Python for Financial Applications FREE CHAPTER 2. The Importance of Linearity in Finance 3. Nonlinearity in Finance 4. Numerical Procedures 5. Interest Rates and Derivatives 6. Interactive Financial Analytics with Python and VSTOXX 7. Big Data with Python 8. Algorithmic Trading 9. Backtesting 10. Excel with Python Index

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 investigated earlier. Save the code as bond_price.py:

""" Get bond price from YTM """
def bond_price(par, T, ytm, coup, freq=2):
    freq = float(freq)
    periods = T*freq
    coupon = coup/100.*par/freq
    dt = [(i+1)/freq for i in range(int(periods))]
    price = sum([coupon/(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:

>>> from bond_price import bond_price
>>> bond_price(100, 1.5, ytm, 5.75, 2)
95.0428

This gives us the same original bond price discussed in the earlier example. Using the bond_ytm and bond_price functions, we can use them for further uses in bond pricing, such as finding the bond's modified duration and convexity. These two characteristics of bonds are of importance to bond traders to help them...

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at £16.99/month. Cancel anytime