Python QuantLib tutorial
Compilation and/or installation
- 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\setup.py as follows
- python setup.py 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 setup.py test
- finally, you install it
- python setup.py install
- And now you can type on python
import QuantLib. Try it out!!!
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
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( effectiveDate, terminationDate, Period(Semiannual), UnitedStates(UnitedStates.GovernmentBond), ModifiedFollowing, ModifiedFollowing, DateGeneration.Backward, False ) # 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( settlementDays, faceAmount, s, # Schedule object ( rate, ), ActualActual(), Following, redemption, todayDate ) # The Bond's Yield to Maturity f.bondYield( ActualActual(), Annual, Semiannual )