What is QuantLib?

  • Free, open source (BSD license) library for quantitative finance
  • Written in C++, with bindings to C#, Java, Python and other languages

Implements

  • financial instruments
  • yield curves
  • pricing engines
  • simulation framework
  • date and time management

Resources

Why another python wrapper?

  • Proper documentation and introspection on the Python side
  • Benefit from a decent namespace on the Python side
  • More Pythonic wrappers with better integration in 3rd party libraries like NumPy
  • Easier access to all of the C++ parts of the library with comprehensive wrappers
  • Reduce build time when adding new functionalities

How?

  • Using Cython 0.19
  • Some cpp support code
  • Tested on QuantLib 1.1 or later

Code

QuantLib C++ code

class SimpleQuote : public Quote {
  public:
    SimpleQuote(Real value = Null());
    Real value() const;
    bool isValid() const;
    Real setValue(Real value = Null());
};

Cython wrapper code

cdef class SimpleQuote(Quote):

    def __init__(self, float value=0.0):
        self._thisptr = new shared_ptr[_qt.Quote](new _qt.SimpleQuote(value))

    def __str__(self):
        return 'Simple Quote: %f' % self._thisptr.get().value()

    property value:
        def __get__(self):
            if self._thisptr.get().isValid():
                return self._thisptr.get().value()
            else:
                return None

        def __set__(self, float value):
            (<_qt.SimpleQuote*>self._thisptr.get()).setValue(value)

Comparing Quantlib python APIs (interactive)

>>> from QuantLib import SimpleQuote as SwigSimpleQuote
>>> spot = SwigSimpleQuote(50.0)
>>> spot.value()
50.0
>>> spot.setValue(75)
>>> print spot
<QuantLib.QuantLib.SimpleQuote; proxy of <Swig Object of type 'SimpleQuotePtr *' at
0x10de120c0> >

Comparing Quantlib python APIs (interactive)

>>> from QuantLib import SimpleQuote as SwigSimpleQuote
>>> spot = SwigSimpleQuote(50.0)
>>> spot.value()
50.0
>>> spot.setValue(75)
>>> print spot
<QuantLib.QuantLib.SimpleQuote; proxy of <Swig Object of type 'SimpleQuotePtr *' at
0x10de120c0> >
>>> from quantlib.quotes import SimpleQuote as PyqlSimpleQuote
>>> spot = PyqlSimpleQuote(50.0)
>>> spot.value
50.0
>>> spot.value = 75
>>> print spot
Simple Quote: 75.000000

Comparing Quantlib python APIs (cont.)

process = BlackScholesMertonProcess(QuoteHandle(underlying),
                                    YieldTermStructureHandle(dividendYield),
                                    YieldTermStructureHandle(riskFreeRate),
                                    BlackVolTermStructureHandle(volatility))
option = VanillaOption(payoff, exercise)
option.setPricingEngine(BaroneAdesiWhaleyEngine(process))
print option.NPV()

Comparing Quantlib python APIs (cont.)

process = BlackScholesMertonProcess(QuoteHandle(underlying),
                                    YieldTermStructureHandle(dividendYield),
                                    YieldTermStructureHandle(riskFreeRate),
                                    BlackVolTermStructureHandle(volatility))
option = VanillaOption(payoff, exercise)
option.setPricingEngine(BaroneAdesiWhaleyEngine(process))
print option.NPV()


process = BlackScholesMertonProcess(underlying, dividend_yield,
                                    risk_free_rate, volatility)
option = VanillaOption(payoff, exercise)
option.set_pricing_engine(BaroneAdesiWhaleyApproximationEngine(process))
print option.net_present_value

Examples

Future work

  • Increase the available functionalities
  • Simplify the API by making the code more Pythonic and less C++
  • Provide better integration for large datasets
  • Investigating Cython OpenMP support
  • Windows builds

Credits

  • Didrik Pinte (Enthought)
  • Patrick Hénaff (Université de Paris I)

<Thank You!>

contact info: