Saturday, February 9, 2008

QuantLibXL - Discovering Enumerations

It took me a while to figure out how I could "guess" what inputs are expected from a certain function. For example when QuantLib expects and Option Type as an argument, I guessed that it could be "Call" or "Put".

Now, guessing is not what the authors intended their users to do. This is why they documented these "string constants" in the enumerations section. The "string constants" are not really text constants, instead QuantLib uses a "Registration Manager" to translate these string constants into QuantLib Objects.

In the exampe above, if we try to create an option object in Excel and we pass the string "Put" as an argument to the constructor of this object, a "put option"-object is created.

All of these enumerations are nicely documented on http://quantlib.org/quantlibxl/auto/enums.html#enum_type_16.

But they can also be queried at runtime, which I found to be very useful.

Here is how you do it:
# Select in a column about 30 rows
# Type "ohListEnumeratedTypes ()"
# Press CTRL + Shift + Enter (to enter an array function)

QuantLib responds by filling your array with 27 enumerated types:


class QuantLib::Calendar
class QuantLib::Currency
class QuantLib::DayCounter
enum QuantLib::Average::Type
enumQuantLib::Barrier::Type
enum QuantLib::BusinessDayConvention
enum QuantLib::CapFloor::Type
enum QuantLib::CmsMarketCalibration::CalibrationType
enum QuantLib::Compounding
enum QuantLib::CubicSplineInterpolation::BoundaryCondition
enum QuantLib::DateGeneration::Rule
enum QuantLib::Duration::Type
enum QuantLib::EndCriteria::Type
enum QuantLib::Frequency
enum QuantLib::GFunctionFactory::ModelOfYieldCurve
enum QuantLib::Month
enum QuantLib::Option::Type
enum QuantLib::Position::Type
enum QuantLib::PriceType
enum QuantLib::Replication::Type
enum QuantLib::SalvagingAlgorithm::Type
enum QuantLib::Settlement::Type
enum QuantLib::TimeUnit
enum QuantLib::VanillaSwap::Type
enum QuantLib::Weekday
enum QuantLibAddin::RateHelper::DepoInclusionCriteria
enum QuantLibAddin::Token::Interpolator
enum QuantLibAddin::Token::Traits
Each one of those enumeration types can now be queried.

# Select two rows in your worksheet
# Type in "ohEnumeratedType (enum QuantLib::Option::Type)"
# Press CTRL + Shift + Enter

QuantLib responds by filling your array with the valid strings for "Option Type" that you can use as parameters when creating an object that requires an "Option Type" argument:

Call
Put

With other enumerations it works in the same way. In order to get all valid Calendars:

# Select 51 rows in your worksheet
# Type in "ohEnumeratedType (class QuantLib::Calendar)"
# Press CTRL + Shift + Enter

QuantLib responds with:

Argentina::Merval
Australia
Brazil::Exchange
Brazil::Settlement
Canada::Settlement
Canada::TSX
China
CzechRepublic::PSE
Denmark
EUR
Finland
GBP
Germany::Eurex
Germany::FrankfurtStockExchange
Germany::Settlement
Germany::Xetra
HongKong::HKEx
Hungary
Iceland::ICEX
India::NSE
Indonesia::BEJ
Indonesia::JSX
Italy::Exchange
Italy::Settlement
JOINHOLIDAYS(ITALY::EXCHANGE,TARGET,UNITEDKINGDOM::EXCHANGE,UNITEDSTATES::NYSE)
Japan
LONDON
Mexico::BMV
NewZealand
Norway
Null
NullCalendar
Poland
SaudiArabia::Tadawul
Singapore::SGX
Slovakia::BSSE
SouthAfrica
SouthKorea::KRX
Sweden
Switzerland
TARGET
Taiwan::TSEC
Turkey
Ukraine::USE
UnitedKingdom::Exchange
UnitedKingdom::Metals
UnitedKingdom::Settlement
UnitedStates::GovernmentBond
UnitedStates::NERC
UnitedStates::NYSE
UnitedStates::Settlement

These are all the valid calendars that can be passed as parameters whenever an object requires a calendar.

Besides enumerated types there are also enumerated classes. The difference between them is that enumerated types are stateless while enumerated classes are stateful.

The enumerated classes are returned by =ohListEnumeratedClasses() are:

class QuantLib::IborCouponPricer
class QuantLib::Interpolation
class
QuantLib::Interpolation2D
class QuantLib::Payoff
class
boost::shared_ptr (__cdecl*)(class
boost::shared_ptr const
&)
class boost::shared_ptr
(__cdecl*)(class boost::shared_ptrQuantLib::GeneralizedBlackScholesProcess> const &,long const &)

The most important enumerated class is the PayOff class.

When we select several rows in a worksheet an type =ohEnumeratedClass(class QuantLib::PayOff) and press CTRL + Shift + Enter we get:

AssetOrNothing
CashOrNothing
Gap
PercentageStrike
PlainVanilla
SuperFund
SuperShare
Vanilla

These are mandatory arguments when creating PayOff object in QuantLib and QuantLibXL.

QuantLibXL - The First Day

I only discovered QuantLib yesterday. The potential of the initiative is fantastic and the quality of the work is outstanding. What is missing is documentation that can be understood by beginners in quantitative finance and people who have no experience in using such libraries.
I therefore decided to record my own humble beginnings, including my mistakes and errors, in order to help others to progress a bit faster in the future. Perhaps in the near future I can turn this blog into proper documentation in the form of a WIKI or a book - Quantlib for Beginners.

I will start to explore the library using a port for Excel called QuantLibXL assuming that Excel would provide a simple user interface to the library functions.

So let's start off by exploring QuantLibXL in Excel.

I installed QuantLibXL within minutes following the instructions. On my machine the installed created the QuantLibXL directory in c:\Program Files\ and I could install the .xll file in Excel 2003 without any problems.

After the Add-In was successfully loaded I tested the availability of QuantLib by typing
=qlVersion()

in a worksheet cell and pressing ENTER.
Quantlib responded with
0.9.0


I then tried
=qlXLVersion()

QuantLib responded with
QuantLibXL 0.9.0

QuantLib was avaliable from Excel!

I then proceeded with trying out random functions and modules listed on the QuantLibXL website: http://quantlib.org/quantlibxl/auto/all_functions.html

The best way to discover the required inputs for a function is to key in the function name and then click on the Function Wizard (the little fx button on the Formular Bar). It lists the required arguments for a function.

Difficulties arise when QuantLib required other objects as arguments or certain constants. At this point the dependencies between various classes are not clear to me. Also I could not see which constants were expected by certain functions, so I tried to guess or get some of them by the sample workbooks which are provided with QuantLibXL.

A class diagram wor QuantLib would be helpful. It seems like Luigi Ballabio, one of the founders of Quantlib made the beginning by writing a book called Implementing QuantLib which is an excellent introduction. Unfortunately only three chapters of the book are available.