Python QuantLib tutorial

From Quantlib
Jump to: navigation, search

QuantLib can be compiled for Python using SWIG.

Compilation and/or installation[edit]

On Windows[edit]

  • Download and install Visual C++ Express 9.0 (Visual C++ 10.0 does not work) and follow the steps found on the QuantLib website to install QuantLib and Boost.
  • After you have compiled QuantLib, download the QuantLib-SWIG bindings from SourceForge (use SWIG for QuantLib 1.2 even if you have compiled QuantLib 1.2.1). QuantLib-SWIG can be found here: QuantLib-SWIG sourceforge
  • set an environment variable QL_DIR which points to the base directory of QuantLib. (For example: D:\QuantLib-1.2.1)
  • make sure the Boost include directories can be found by creating an environment variable named INCLUDE and by setting it equal to where you have installed Boost. (For example: D:\boost_1_52_0.)
  • Run the python script located in QuantLib-SWIG\Python\ as follows
    • python build
    • you need to have Visual C++ Express 9.0 (wxDev-C++ is not supported), this script runs the C++ compiler in such a way as to create a DLL which will be Python's module on Windows, the DLL's extension is .pyd
    • you can specify an alternate installation directory by passing in a --prefix=D:\Test parameter
  • Then you can run the tests
    • python test
  • finally, you install it
    • python install
  • And now you can type on python import QuantLib. Try it out!!!

On Ubuntu[edit]

There's already a package quantlib-python.


There are already some examples of usage in the QuantLib-SWIG package from SourceForge.

Very Easy: Find Yield to Maturity of a Bond from its Price[edit]

from QuantLib import *

effectiveDate = Date(30, 1, 1990)
terminationDate = Date(30, 1, 2000)

# The Schedule object determines the dates on which transactions occur.
s = Schedule( 

# FixedRateBond object's parameters
settlementDays = 3
faceAmount = 97.54
rate = 0.04 # this means 4%
redemption = 100.0 # this means 100% of the initial value.
todayDate = Date(24, 9, 1995) 

# Construct the object 
f = FixedRateBond( 
   s, # Schedule object
   ( rate, ), 

# The Bond's Yield to Maturity
f.bondYield( ActualActual(), Annual, Semiannual )