quantlibquantlib-swig

How to obtain the "time" values of a schedule


Assuming a fixed rate bond with the schedule shown in the sample code below.

I am able to obtain the number of days between the tenors by using the businessDaysBetween function.

Now I would like the "time value". Is there a way of doing it without creating a new function?

Here is the expected result:

May 14th, 2012          .5
November 14th, 2012     .5
May 14th, 2013          .5
November 14th, 2013     .5
May 14th, 2014          .5
November 14th, 2014     .5
May 14th, 2015          .5
November 16th, 2015     .505556
May 16th, 2016          .5
November 14th, 2016     .49444

Here is the code:

from QuantLib import *
import pandas as pd


effective_date = Date(14, 11, 2011)
termination_date = Date(14, 11, 2016)
tenor = Period(Semiannual)
calendar = UnitedStates()
business_convention = ModifiedFollowing
termination_business_convention = Following
date_generation = DateGeneration.Forward
end_of_month = False
day_count = Thirty360()
schedule = Schedule(effective_date,
                    termination_date,
                    tenor,
                    calendar,
                    business_convention,
                    termination_business_convention,
                    date_generation,
                    end_of_month)
t = []
for i, d in  enumerate(schedule):
    tmp = i+1, d,
    t.append(tmp)
df = pd.DataFrame(t,columns = ['tenorNo','tenorDate'])

nbDays = []
for x in df['tenorNo'] :
    if  x == 1:
        tmp = 0
    else:
        tmp = calendar.businessDaysBetween(df['tenorDate'][x-2],df['tenorDate'][x-1])
        nbDays.append(tmp)

    df['nbDays'] = nbDays

print df
    tenorNo            tenorDate  nbDays
0         1  November 14th, 2011       0
1         2       May 14th, 2012     125
2         3  November 14th, 2012     127
3         4       May 14th, 2013     124
4         5  November 14th, 2013     127
5         6       May 14th, 2014     124
6         7  November 14th, 2014     127
7         8       May 14th, 2015     124
8         9  November 16th, 2015     127
9        10       May 16th, 2016     125
10       11  November 14th, 2016     125

Solution

  • That's what DayCounter instances are for. The time will depend on the day-count convention you choose (for example, you seem to be using 30/360).

    Calling

    day_count.yearFraction(date1, date2)
    

    will return the time between date1 and date2.