Introducing QuantLib: Calculating Future Value

Welcome back. In my last post I showed how to calculate the future value of a loan, that is the amount owed when the loan is due assuming a one year term at a 5% annual rate compounded annually. The code is repeated below:

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

BOOST_AUTO_TEST_CASE(testCalculateLoanPayment) {
typedef FixedRateCoupon Loan;
Natural lengthOfLoan = 365;
Real loanAmount = 100.0;
Rate rate = .05;
Date today = Date::todaysDate();
Date paymentDate = today + lengthOfLoan;
Loan loan(paymentDate, loanAmount, rate, ActualActual(), today, paymentDate);
Real payment = loanAmount + loan.accruedAmount(paymentDate);
std::cout << “Payment due in ” << lengthOfLoan << ” days on loan amount of $” <<
loanAmount << ” at annual rate of ” << rate*100 << “% is: $” << payment << std::endl;
}

Let’s go through this code and highlight some of the key statements. First and most important is the class that represents the loan, FixedRateCoupon.  The FixedRateCoupon class models a payment where the dollar amount due at the payment date is specified at the inception of the loan.  Our simple loan has one payment, the premium plus the interest, which is owed the lender when the loan is due.  I aliased the FixedRateCoupon class with a typedef to create a Loan abstraction that, hopefully, makes subsequent code more intuitive.

The next several lines just define the loan’s term, premium and interest rate using specialized QuantLib data types, such as Natural and Real.  In the next two lines, today’s date is established and the payment date is calculated with respect to today’s date.

In the following line, an instance of the Loan class is declared, given the loan parameters previously defined. The ActualActual() argument is the ‘day count’ convention for the loan. I’ll cover day count conventions in a future post, but suffice it to say that different types of loans are priced using different day count conventions.

Finally, the final payment is calculated by adding the interest due on the loan, obtained with the accruedAmount() method, to the loan principal. As is probably obvious, the argument to accruedAmount() is the date that the loan matures and the loan repayment is due.

I hope that the code makes sense. If you have any questions about anything that I haven’t covered, I encourage you to consult the QuantLib documentation.

Rather than jump into the calculation of present value and interest rates as I had originally planned, I will instead defer those topics to my next post. Thanks for visiting my blog and feel free to leave a comment or question.

Advertisements

About Mick Hittesdorf

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

2 Responses to Introducing QuantLib: Calculating Future Value

  1. Pingback: Pilot–QuantLib | StockRanger

  2. dbning says:

    you are a great man

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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