Financial Analysis in Python
Pricing Assets in 20 Lines of Code
In today’s financial landscape, data-driven decision making has become imperative for investors, analysts, and financial professionals. In an effort to increase their profitability, many turn to financial analysis software like Bloomberg terminal.
Though many tools exist to help investors make more informed decisions, they can be quite costly. Bloomberg terminal, for example, is one of the most expensive options with an annual cost of over $20,000 per user.
With just a few lines of Python code and the ‘QuantLib’ library, we can create our own simple financial analysis program. While there are numerous models the library offers, in this article, we will learn how to use it to calculate the net present value (NPV) of a bond.
The Program
The Python library QuantLib
provides us with the tools to calculate and predict the value of different investments. First, we will install and import the library.
pip install QuantLib
import QuantLib as ql
Then, we need to define the parameters of the bond, so that we can calculate its NPV. The parameters include the bond’s face value; coupon rate, which is the expected annual income of the bond; settlement date, maturity date, and issue date.
face_value = 1000
coupon_rate = 0.05
settlement_date = ql.Date(15, 1, 2023)
maturity_date = ql.Date(15, 1, 2030)
bond_issue_date = ql.Date(15, 1, 2020)
Now, we can create two QuantLib
objects to account for US holidays and to determine the number of days elapsed between two dates to correctly calculate interest. After this, we will create a FixedRateBond
object to act as the bond we will be using.
calendar = ql.UnitedStates(ql.UnitedStates.GovernmentBond)
day_count = ql.Thirty360(ql.Thirty360.BondBasis)
bond = ql.FixedRateBond(2, calendar, face_value, bond_issue_date, maturity_date, ql.Period('6M'), [coupon_rate], day_count)
Finally, we can calculate the bond’s value using functions included in QuantLib
.
discount_curve = ql.FlatForward(settlement_date, ql.QuoteHandle(ql.SimpleQuote(0.03)), day_count)
engine = ql.DiscountingBondEngine(ql.YieldTermStructureHandle(discount_curve))
bond.setPricingEngine(engine)
present_value = bond.NPV()
Printing out the present value of the bond, we should see an output of $1148.92.
Full Code — 18 Lines!
import QuantLib as ql
face_value = 1000
coupon_rate = 0.05
settlement_date = ql.Date(15, 1, 2023)
maturity_date = ql.Date(15, 1, 2030)
bond_issue_date = ql.Date(15, 1, 2020)
calendar = ql.UnitedStates(ql.UnitedStates.GovernmentBond)
day_count = ql.Thirty360(ql.Thirty360.BondBasis)
bond = ql.FixedRateBond(2, calendar, face_value, bond_issue_date, maturity_date, ql.Period('6M'), [coupon_rate], day_count)
discount_curve = ql.FlatForward(settlement_date, ql.QuoteHandle(ql.SimpleQuote(0.03)), day_count)
engine = ql.DiscountingBondEngine(ql.YieldTermStructureHandle(discount_curve))
bond.setPricingEngine(engine)
present_value = bond.NPV()
print(f"Present Value of the Bond: {present_value:.2f}")
This was just one example of how we can use Python, the QuantLib
library, and a few lines of code to create a financial analysis program that equips us to make more informed investing decisions.
What other ways do you think computer science proves useful in finance? Please feel free to comment if you have any questions or if you enjoyed this article!
20 Lines or Less
Thank you for being a part of the community! Before you head out:
- Clap for this article and follow the writer!
- Follow 20 Lines or Less to read more content!