Introducing QuantLib: Calculating Present Value

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:

#include <ql/quantlib.hpp>
#include <iostream>
#include <boost/test/unit_test.hpp>

Leg cashFlows;
Date date = Date::todaysDate();
cashFlows.push_back(boost::shared_ptr<CashFlow>(new SimpleCashFlow(105.0, date + 365)));
Rate rate = .05;
Real npv = CashFlows::npv(cashFlows, InterestRate(rate, ActualActual(), Compounded, Annual), true);
std::cout << “Net Present Value (NPV) of cash flows is: ” << npv << std::endl;

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!


About Mick Hittesdorf

Financial Systems Architect, Analyst and Developer
This entry was posted in QuantLib and tagged , , , , , , . Bookmark the permalink.

3 Responses to Introducing QuantLib: Calculating Present Value

  1. veda aranyam says:

    cashFlows.push_back(boost::shared_ptr(new SimpleCashFlow(105.0, date + 365)));
    should we have ………boost::shared_ptr…….. ?
    I am weak on boost lib. I am on linux, g++ compiler.

  2. veda aranyam says:

    I meant boost::shared_ptr ?

  3. Veda,

    I think you were referring to the missing template parameter in boost::shared_ptr? It got eaten up by the HTML parser. I fixed the error. Thanks for bringing it to my attention. This unfortunately is a common problem when copying and pasting C++ code into an HTML editor. I need to do a better job of proofreading. Thanks!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s