using Quantlib from Java

One of these days I’m going to give an overview of all the excellent open-source software I use on a daily basis. Until that day comes, I’ll observe that finance remains one of the big areas where open-source software has made relatively limited inroads.
Two production-quality packages fight that unhappy state: QuantLib - a comprehensive framework for quantitative finance - and QuickFix - a full-featured FIX engine. Both are C++ libraries and both provide very nice interfaces to facilitate integration with other languages, including Java. QuantLib is a big and complicated library and integrating it with Java is not totally obvious. Below, I’ll describe how to build and use QuantLib from Java.
These instructions are based on a unix installation. I’m not really a windows developer and don’t have all the shiny tools that windows developers use, so it’s not an area of focus for me. That said, I have managed to build QuantLib under windows by using MinGW+MSYS but it wasn’t terribly easy and I don’t currently have a working installation, so I won’t cover that here. If this is your aim, don’t be dismayed as it is possible and it had all the functionality I enjoy under linux.
Using QuantLib from Java (on linux)
- Build QuantLib
- Requires a working version of Boost. This may prove to be the hardest step of all and you’ll need to use the ample documentation provided by the Boost team.
- Once you have a working copy of Boost, building QuantLib should require little more than
sh autogen.sh
./configuremake
sudo make install - Build QuantLib-SWIG
- Requires a working copy of SWIG. Again, look to the SWIG instructions, but it should be easy.
- Once SWIG is available, building the QuantLib/SWIG interfaces should only require:
sh autogen.sh
./configure \–with-jdk-include=${JAVA_HOME}/include \
–with-jdk-system-include=${JAVA_HOME}/include/linux
make -C Java
sudo make install
- Now you’ll have a Jar file with all of the SWIG/JNI stubs in it available in /usr/local/lib/QuantLib.jar. Add this to your classpath.
- Programs which call QuantLib functionality will need to have the
LD_LIBRARY_PATHset. This can be done by invoking the vm with something like:
-Djava.library.path=/usr/local/lib
- Programs which call QuantLib functionality will also need to explicitly load the QuantLib libraries. This can be done with something like the following static block appearing before your main method:
- That’s it. Now test your configuration by running the examples in Quantlib-SWIG/Java/examples.
static { // Load QuantLib
try { System.loadLibrary("QuantLibJNI"); }
catch (RuntimeException e) { e.printStackTrace(); }
}
It’s worth understanding how Quantlib is being used from java. SWIG is creating a JNI interface into those methods within Quantlib which have been exposed through their declaration in the swig *.i files. These files are found in Quantlib-SWIG/SWIG and they determine what functionality from Quantlib will be available to you. You’ll likely need to get familiar with a subset of those files that you care about. If you find that some functionality you care about isn’t exposed in those files, you may need to expose it yourself.
There’s a learning curve, but it’s worth traversing so you can get at all the rich functionality so many smart people have put together.
FIX Protocol, monte-carlo methods, open-source software, options pricing, technology
This week we spent a few hours at the tradetech conference to see what people in the industry are up to and see some demos. Their “certified” logo inevitably reminded me of an old colleague who would from time to time mimic an old saturday night live 