Provides an example of the concept of introspection in QTK



Last couple of months, I have been working on the infrastructure of QTK. I annouced the first point release a couple of weeks back after I implemented the core internals. Recently, I implemented the workflow paradigm for qtk. I will highlight some of the core concepts here. The Template in QTK is like a blueprint for what you want to accomplish. Let us see how to create a flat forward curve using some of the introspection concepts introduced in 0.1.3 onwards.

In [1]:
from qtk import Template as T, Controller

The above statement is used to import the template. Now you can use the tab functionality in ipython to get all the different types of templates. Now if you want to get the description of how to create this template, then all you do is execute that template as shown below. This is going to produce a rather nice output for you to view with the required variables and such.

In [2]:
T.TS_YIELD_FLAT
Out[2]:

Description

A template to create a flat forward yield curve.

Required Fields

  • Template [Template]: 'TermStructure.Yield.FlatCurve'
  • ForwardRate [Float]: Forward rate from an yield curve in decimal
  • Currency [String]: Currency

Optional Fields

  • ObjectId [String]: A unique name or identifier to refer to this dictionary data
  • AsOfDate [Date]: As of date for the yield curve to create a curve fixed to a given reference date. Alternately, one can provide settlement days and settlement calendar to do relative to calculation date.
  • DiscountBasis [DayCount]: Discount Basis
  • SettlementDays [Integer]: Settlement days, and is used if asof date is not provided.
  • SettlementCalendar [Calendar]: Settlement Calendar
  • Compounding [Compounding]: Compounding
  • CompoundingFrequency [Frequency]: Compounding Frequency
  • Extrapolation [Boolean]: Enable Extrapolation

Or if you use a simple python console, then you can use the help to print the info in Markdown format. If you just need the help string, then you can use the info method.

In [3]:
T.TS_YIELD_FLAT.help()
**Description**

A template to create a flat forward yield curve.

**Required Fields**

 - `Template` [*Template*]: 'TermStructure.Yield.FlatCurve'
 - `ForwardRate` [*Float*]: Forward rate from an yield curve in decimal
 - `Currency` [*String*]: Currency

**Optional Fields**

 - `ObjectId` [*String*]: A unique name or identifier to refer to this dictionary data
 - `AsOfDate` [*Date*]: As of date for the yield curve to create a curve fixed to a given reference date. Alternately, one can provide settlement days and settlement calendar to do relative to calculation date.
 - `DiscountBasis` [*DayCount*]: Discount Basis
 - `SettlementDays` [*Integer*]: Settlement days, and is used if asof date is not provided.
 - `SettlementCalendar` [*Calendar*]: Settlement Calendar
 - `Compounding` [*Compounding*]: Compounding
 - `CompoundingFrequency` [*Frequency*]: Compounding Frequency
 - `Extrapolation` [*Boolean*]: Enable Extrapolation

In order to create a yield curve, you can use the sample_data method attached to the template to get a sample dictionary object. This is what it looks like.

In [4]:
T.TS_YIELD_FLAT.sample_data()
Out[4]:
{'AsOfDate': 'Optional (Date)',
 'Compounding': 'Optional (Compounding)',
 'CompoundingFrequency': 'Optional (Frequency)',
 'Currency': 'Required (String)',
 'DiscountBasis': 'Optional (DayCount)',
 'Extrapolation': 'Optional (Boolean)',
 'ForwardRate': 'Required (Float)',
 'ObjectId': 'Optional (String)',
 'SettlementCalendar': 'Optional (Calendar)',
 'SettlementDays': 'Optional (Integer)',
 'Template': 'TermStructure.Yield.FlatCurve'}

You can prune the optional fields to just pass the required items. It is worth pointing out that sometimes when one of two keywords need to be provided, both would be marked as optional. Looking at the documentation as listed by info or help methods will clarify the usage. For example, the fixed rate bond template INSTRUMENT_BOND_TBOND has Coupon and ListOfCoupon both marked as optional fields. But this is because you can provide either one of the two as inputs, but atleast one of the two has to be provided. This is clarified in the documentation.

In [5]:
zero_curve = {"Template": T.TS_YIELD_FLAT.id, "ForwardRate": 0.02, "Currency": "USD", "ObjectId": "FlatCurve.USD"}
data = [zero_curve]
In [6]:
c = Controller(data)
out = c.process("1/15/2016")
ts = c.object("FlatCurve.USD")
In [7]:
ts.referenceDate()
Out[7]:
Date(20,1,2016)
In [8]:
import QuantLib as ql
time_grid = range(30)
zero = [ts.zeroRate(t, ql.Compounded).rate() for t in time_grid]
In [9]:
import matplotlib.pyplot as plt
%matplotlib inline
In [10]:
plt.scatter(time_grid, zero)
plt.xlabel("Time", size=12)
plt.ylabel("Zero Rate", size=12)
Out[10]:
<matplotlib.text.Text at 0x7f1f95768690>



   qtk   python   finance  

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 .