Book Image

Mastering Python for Finance - Second Edition

By : James Ma Weiming
Book Image

Mastering Python for Finance - Second Edition

By: James Ma Weiming

Overview of this book

The second edition of Mastering Python for Finance will guide you through carrying out complex financial calculations practiced in the industry of finance by using next-generation methodologies. You will master the Python ecosystem by leveraging publicly available tools to successfully perform research studies and modeling, and learn to manage risks with the help of advanced examples. You will start by setting up your Jupyter notebook to implement the tasks throughout the book. You will learn to make efficient and powerful data-driven financial decisions using popular libraries such as TensorFlow, Keras, Numpy, SciPy, and scikit-learn. You will also learn how to build financial applications by mastering concepts such as stocks, options, interest rates and their derivatives, and risk analytics using computational methods. With these foundations, you will learn to apply statistical analysis to time series data, and understand how time series data is useful for implementing an event-driven backtesting system and for working with high-frequency data in building an algorithmic trading platform. Finally, you will explore machine learning and deep learning techniques that are applied in finance. By the end of this book, you will be able to apply Python to different paradigms in the financial industry and perform efficient data analysis.
Table of Contents (16 chapters)
Free Chapter
1
Section 1: Getting Started with Python
3
Section 2: Financial Concepts
9
Section 3: A Hands-On Approach

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() and bond_price() functions, we can apply these for further uses in bond pricing...