This post will walk through some of the basics of QuantLib Python library.

Visit here for other QuantLib Python examples. If you found these posts useful, please take a minute by providing some feedback.

I installed the latest version of QuantLib (V1.5) and the python wrapper to QuantLib. My experiments lately have been to get a feel for the QuantLib API. The library itself is so extensive, that it is rather hard for a new comer to get going. In this post we will look into some of the basic classes and functionality in QuantLib.

Let us import QuantLib as:

import QuantLib as ql

Time SubModule

The ql/time sub-folder implements various time related classes. Lets take a look at the Date object which can be constructed as Date(date, month, year).

>>> date = ql.Date(31, 3, 2015) # 31 March, 2015
>>> print date
March 31st, 2015
>>> date.dayOfMonth()
>>> date.month()
>>> date.year()
>>> date.weekday() == ql.Tuesday

# arithmetic with dates
>>> date + 1  # add a day
>>> date - 1  # subtract a day
>>> date + ql.Period(1, ql.Months)
>>> date + ql.Period(1, ql.Weeks)
>>> date + ql.Period(1, ql.Years)

# logical operations
>>> ql.Date(31, 3, 2015) > ql.Date(1, 3, 2015)

The Schedule object can be used to construct a list of dates such as coupon payments. Lets look at some examples.

>>> date1 = ql.Date(1, 1, 2015)
>>> date2 = ql.Date(1, 1, 2016)
>>> tenor = ql.Period(ql.Monthly)
>>> calendar = ql.UnitedStates()
>>> schedule = ql.Schedule(date1, date2, tenor, calendar, ql.Following,
                           ql.Following, ql.DateGeneration.Forward, False)
>>> list(schedule)

Here we have generated a Schedule object that will contain dates between date1 and date2 with the tenor specifying the Period to be every Month. The calendar object is used for determining holidays. The two arguments following the calendar in the Schedule constructor are the BussinessDayConvention. Here we chose the convention to be the day following holidays. That is why we see that holidays are excluded in the list of dates.

Interest Rate

The InterestRate class can be used to store the interest rate with the compounding type, day count and the frequency of compounding. Below we show how to create an interest rate of 5.0% compounded annually, using Actual/Actual day count convention.

>>> annualRate = 0.05
>>> dayCount = ql.ActualActual()
>>> compoundType = ql.Compounded
>>> frequency = ql.Annual
>>> interestRate = ql.InterestRate(annualRate, dayCount, compoundType, frequency)

Lets say if you invest a dollar at the interest rate described by interestRate, the compoundFactor method gives you how much your investment will be worth after t years. Below we show that the value returned by compoundFactor for 2 years agrees with the expected compounding formula.

>>> interestRate.compoundFactor(2.0)
>>> (1.0 + annualRate)*(1.0 + annualRate)  # Check the above calculation

The discountFactor method returns the reciprocal of the compoundFactor method. The discount factor is useful while calculating the present value of future cashflows.

>>> interestRate.discountFactor(2.0)
>>> 1.0 / interestRate.compoundFactor(2.0)

A given interest rate can be converted into other types using the equivalentRate method as :

>>> newFrequency = ql.Semiannual
>>> effectiveRate = interestRate.equivalentRate(compoundType, newFrequency, 1)
>>> effectiveRate.rate()

The InterestRate class also has an impliedRate method. The impliedRate method takes compound factor to return the implied rate. The impliedRate method is a static method in the InterestRate class and can be used without an instance of InterestRate. Internally the equivalentRate method invokes the impliedRate method in its calculations.

Here we have converted into a semi-annual compounding type. A 4.939% of semi-annual compounding is equivalent to 5.0% annual compounding. This should mean, that both should give identical discount factors. Lets check that:

>>> interestRate.discountFactor(1.0)
>>> effectiveRate.discountFactor(1.0)

So this means that pricing bonds using either interest rate convention should give the same net present value (barring some precision).


In this post we looked at the basics of QuantLib:

  • We learnt how to use Date and Schedule classes from the time sub-module
  • we learnt how to use the InterestRate class

   python   finance   quantlib  

Related Post

I am Goutham Balaraman, and I explore topics in quantitative finance, programming, and data science. You can follow me @gsbalaraman.

Checkout my book

Updated posts from this blog and transcripts of Luigi's screencasts on YouTube is compiled into QuantLib Python Cookbook .