In a previous post in this series, I introduced the concept of ‘present value’. In this post, I will cover some examples of calculating present value using QuantLib’s CashFlow and CashFlows classes. Though certainly a simplification, it is not too much of a stretch to claim that all financial instrument pricing boils down to estimating the present value of a future cash flow or series of cash flows. A cash flow can be the repayment of a loan on its maturity date, which we learned how to calculate in my last post. Or a future cash flow could be a stock dividend, an interest payment, an annuity payment, the payoff from an option or a bond coupon.
So let’s start with the example from my last post – a loan with a single future cash flow, the amount due on a $100 one-year loan with an interest rate of 5% compounded annually. The amount due at the end of the loan period is $105, which is the sum of the principal and the accrued interest. What is the ‘present value’ of the loan assuming that the loan was issued today? The code below computes the answer:
The output of this code is:
Net Present Value (NPV) of cash flows is: 100
The first line of the test instantiates a Leg object. Leg is defined in cashflow.hpp as:
typedef std::vector<boost::shared_ptr<CashFlow> > Leg,
which is a sequence of cash flows. The loan repayment amount is modeled as a single cash flow, which is subsquently added to the Leg. The static
Cashflows::npv function is used to calculate the present value of the cash flow given the specified interest rate, day count convention, simple compounding rule and annual compounding period.
In my next post, we’ll apply what we’ve learned about present value to price a bond with annual coupons. See you then and thanks for reading my blog!