QuantLib Java binding

The bindings wrap the unmodified C++ QuantLib library using the SWIG generator and the official QuantLib SWIG binding code in a way which is similar to the way QuantLib Python and .Net bindings are produced. This is a long-established and reliable way of using C++ code in higher-level languages.

You can now try QuantLib-Java easily on binderhub as a IJava notebook: badge and open for example the Bonds notebook.

See also:

  1. Example OIS yield Curve building in Java
  2. Example Swap pricing in Java

We also commercially supply a higher level Java interface to QuantLib – contact us at webs@bnikolic.co.uk for more information.

The architecture of QuantLib-SWIG is shown below:

quantlib_java Architecture of the QuantLib Java wrapper cluster_QLBase QuantLib C++ Library cluster_QLSWig QuantLib-SWIG wrapper cluster_QLApp Your application built on QuantLib-Java QLSource QuantLib C++ Source Code QLLib QuantLib Dynamic Lib (.so, .dll) QLSource->QLLib C++ Compiler (gcc, MS Visual Studio) QLSWIGLib QuantLib-Swig Dynamic Lib (.so, .dll) QLLib->QLSWIGLib QLSwig SWIG Interface Definitions (*.i files) QLJNIC C++ JNI code (generated C++ files) QLSwig->QLJNIC SWIG QLWrap Swig Java Wrapper Classes (generated .java files) QLSwig->QLWrap SWIG QLJNIC->QLSWIGLib C++ Compiler (gcc, MS Visual Studio) QLSwigJar QuantLib.jar QLWrap->QLSwigJar javac QLSWIGLib->QLSwigJar QLAppJar Application JAR (.jar file) QLSwigJar->QLAppJar QLAppSource Application source (.java files) QLAppSource->QLAppJar javac

These bindings allow reliable and long-term use of QuantLib from within your Java project on either Linux or Windows. Typical use is as part of a pricing and risk server.

We are able to offer services related to support and extension of these bindings – please contact us at: webs@bnikolic.co.uk.

If you are looking for an alternative to Java binding to QuantLib, we offer ready-to-license wrapping of QuantLib and other libraries via REST interface, suitable for fast and durable development of applications: see following dedicated website and this blog post

Maven builds

You can get Maven-compatible QuantLib builds following instructions here https://github.com/ralfkonrad/quantlib_for_maven.

Builds of QuantLib for Java on Windows

You can run the examples as:

java -cp ".\QuantLib.jar;." examples.Bonds

First, if you haven’t done already, you need to install Java (e.g., winget install Microsoft.OpenJDK.16) and unpacking (e.g., Expand-Archive .\QuantLib-Win-J16-1.28.zip, cd .\QuantLib-Win-J16-1.28\).

QuantLib Ver QL-SWIG Ver OS Architecture Java Version Download Link
V1.32 V1.32 Win 10 X86_64 16 download
V1.29 V1.29 Win 10 X86_64 16 download
V1.28 V1.28 Win 10 X86_64 16 download
V1.27.1 V1.27.1 Win 10 X86_64 16 download
V1.27 V1.27 Win 10 X86_64 16 download
V1.26 V1.26 Win 10 X86_64 16 download
V1.25 V1.25 Win 10 X86_64 16 download
V1.24 V1.24 Win 10 X86_64 16 download
V1.23 V1.23 Win 10 X86_64 16 download
V1.23 V1.23 Win 10 X86_64 11 download
V1.22 V1.22 Win 10 X86_64 16 download
V1.22 V1.22 Win 10 X86_64 11 download
V1.21 V1.21 Win 10 X86_64 16 download

The above software is licensed by BN Algorithms Ltd to viewers of this page under the QuantLib license. Please note there is no warranty of any kind for this software.

Builds of QuantLib for Java on Linux

You can run these with for example:

LD_LIBRARY_PATH=. java -cp ".:QuantLib.jar" examples.Bonds
QuantLib Ver QL-SWIG Ver OS Architecture Java Version Download Link
V1.32 V1.32 Linux/GlibC 2.29 X86_64 17 download
V1.32 V1.32 Linux/GlibC 2.29 X86_64 11 download
V1.30 V1.30 Linux/GlibC 2.32 X86_64 17 download
V1.29 V1.29 Linux/GlibC 2.28 X86_64 11 download
V1.29 V1.29 Linux/GlibC 2.34 X86_64 17 download
V1.28 V1.28 Linux/GlibC 2.34 X86_64 17 download
V1.24 V1.24 Linux/Ubuntu 20.04 X86_64 17 download
V1.15 V1.15 Linux/GLibc 2.27 X86_64 8 download

The above software is licensed by BN Algorithms Ltd to viewers of this page under the QuantLib license. Please note there is no warranty of any kind for this software.

Example Java program

Here is an example Java for pricing of callable bonds that we contributed to some time ago:

/*
  Copyright (C) 2008 Allen Kuo
  Copyright (C) 2014 Wondersys Srl
  Copyright (C) 2017 BN Algorithms Ltd

  This file is part of QuantLib, a free-software/open-source library
  for financial quantitative analysts and developers - http://quantlib.org/

  QuantLib is free software: you can redistribute it and/or modify it
  under the terms of the QuantLib license.  You should have received a
  copy of the license along with this program; if not, please email
  <quantlib-dev@lists.sf.net>. The license is also available online at
  <http://quantlib.org/license.shtml>.

  This program is distributed in the hope that it will be useful, but WITHOUT
  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  FOR A PARTICULAR PURPOSE.  See the license for more details.
*/

package examples;

import org.quantlib.CallableFixedRateBond;
import org.quantlib.Date;
import org.quantlib.Period;
import org.quantlib.Settings;
import org.quantlib.DayCounter;
import org.quantlib.ActualActual;
import org.quantlib.Compounding;
import org.quantlib.Frequency;
import org.quantlib.FlatForward;
import org.quantlib.QuoteHandle;
import org.quantlib.Quote;
import org.quantlib.SimpleQuote;
import org.quantlib.YieldTermStructure;
import org.quantlib.YieldTermStructureHandle;
import org.quantlib.InterestRate;
import org.quantlib.Month;
import org.quantlib.Date;
import org.quantlib.CallabilitySchedule;
import org.quantlib.Schedule;
import org.quantlib.NullCalendar;
import org.quantlib.UnitedStates;
import org.quantlib.Calendar;
import org.quantlib.Callability;
import org.quantlib.BondPrice;
import org.quantlib.TimeUnit;
import org.quantlib.BusinessDayConvention;
import org.quantlib.DateGeneration;
import org.quantlib.ShortRateModel;
import org.quantlib.HullWhite;
import org.quantlib.PricingEngine;
import org.quantlib.TreeCallableFixedRateBondEngine;
import org.quantlib.DoubleVector;

public class CallableBonds {

    public static YieldTermStructure flatRate(Date today,
                                              Quote forward,
                                              DayCounter dc,
                                              Compounding compounding,
                                              Frequency frequency)
    {
        return new FlatForward(today,
                               new QuoteHandle(forward),
                               dc,
                               compounding,
                               frequency);
    }

    public static void priceCallable(double sigma,
                                     YieldTermStructureHandle termStructure,
                                     CallableFixedRateBond callableBond,
                                     Frequency frequency,
                                     DayCounter bondDayCounter)
    {
        long maxIterations = 1000;
        double accuracy = 1e-8;
        long gridIntervals = 40;
        double reversionParameter = .03;

        ShortRateModel hw0 =
            new HullWhite(termStructure,
                          reversionParameter,
                          sigma);

        PricingEngine engine0 =
            new TreeCallableFixedRateBondEngine(hw0,
                                                gridIntervals);

        callableBond.setPricingEngine(engine0);

        System.out.printf("sigma/vol (%%) =  %6.2f \n",
                          100.*sigma);

        System.out.printf("QuantLib price/yld (%%)  %10.2f  /  %10.2f \n",
                          callableBond.cleanPrice(),
                          100.0 * callableBond.yield(bondDayCounter,
                                                     Compounding.Compounded,
                                                     frequency,
                                                     accuracy,
                                                     maxIterations));

    System.out.printf("QuantLib OAS from model clean price (bp)  %10.2f \n",
              10000.0 * callableBond.OAS(callableBond.cleanPrice(),
                             termStructure,
                             bondDayCounter,
                             Compounding.Compounded,
                             frequency));


    double cp=callableBond.cleanPriceOAS(10*1e-4,
                       termStructure,
                       bondDayCounter,
                       Compounding.Compounded,
                       frequency);
    System.out.printf("QuantLib spreaded clean price with 10bp OAS %f \n",
              cp);

    System.out.printf("QuantLib OAS from spreaded clean price (bp)  %10.2f \n",
              10000.0 * callableBond.OAS(cp,
                             termStructure,
                             bondDayCounter,
                             Compounding.Compounded,
                             frequency));

    System.out.printf("QuantLib effectiveDuration / convexity for 10bp OAS %f / %f \n",
              callableBond.effectiveDuration(10*1e-4,
                             termStructure,
                             bondDayCounter,
                             Compounding.Compounded,
                             frequency),
              callableBond.effectiveConvexity(10*1e-4,
                              termStructure,
                              bondDayCounter,
                              Compounding.Compounded,
                              frequency));
    }


    public static void main(String[] args) throws Exception {
        System.out.println("Callable Bonds example:");

        Date today = new Date(16, Month.October, 2007);
        Settings.instance().setEvaluationDate(today);

        double bbCurveRate = 0.055;
        DayCounter bbDayCounter =
            new ActualActual(ActualActual.Convention.Bond);

        InterestRate bbIR =
            new InterestRate(bbCurveRate,
                             bbDayCounter,
                             Compounding.Compounded,
                             Frequency.Semiannual);

        YieldTermStructureHandle termStructure =
            new YieldTermStructureHandle(flatRate(today,
                                                  new SimpleQuote(bbIR.rate()),
                                                  bbIR.dayCounter(),
                                                  bbIR.compounding(),
                                                  bbIR.frequency()));


        // set up the call schedule
        CallabilitySchedule callSchedule =
            new CallabilitySchedule();
        double callPrice = 100.;
        long numberOfCallDates = 24;
        Date callDate =
            new Date(15, Month.September, 2006);

        Calendar nullCalendar = new NullCalendar();

        for (long i=0; i< numberOfCallDates; ++i) {

            BondPrice myPrice=
                new BondPrice(callPrice,
                              BondPrice.Type.Clean);
            callSchedule.add(new Callability(myPrice,
                                             Callability.Type.Call,
                                             callDate));
            callDate = nullCalendar.advance(callDate, 3, TimeUnit.Months);
        }

        // set up the callable bond
        Date dated =
            new Date(16, Month.September, 2004);
        Date issue = dated;
        Date maturity = new Date(15, Month.September, 2012);
        int settlementDays = 3;  // Bloomberg OAS1 settle is Oct 19, 2007
        Calendar bondCalendar = new
            UnitedStates(UnitedStates.Market.GovernmentBond);
        double coupon = .0465;
        Frequency frequency =  Frequency.Quarterly;
        double redemption = 100.0;
        double faceAmount = 100.0;

        /* The 30/360 day counter Bloomberg uses for this bond cannot
           reproduce the US Bond/ISMA (constant) cashflows used in PFC1.
           Therefore use ActAct(Bond)
        */
        DayCounter bondDayCounter =
            new ActualActual(ActualActual.Convention.Bond);

        // PFC1 shows no indication dates are being adjusted
        // for weekends/holidays for vanilla bonds
        BusinessDayConvention accrualConvention = BusinessDayConvention.Unadjusted;
        BusinessDayConvention paymentConvention = BusinessDayConvention.Unadjusted;

        Schedule sch =
            new Schedule(dated,
                         maturity,
                         new Period(frequency),
                         bondCalendar,
                         accrualConvention,
                         accrualConvention,
                         DateGeneration.Rule.Backward,
                         false);

        DoubleVector couponsVector = new DoubleVector();
        couponsVector.add(coupon);

        CallableFixedRateBond callableBond =
            new CallableFixedRateBond(settlementDays,
                                      faceAmount,
                                      sch,
                                      couponsVector,
                                      bondDayCounter,
                                      paymentConvention,
                                      redemption,
                                      issue,
                                      callSchedule);


        priceCallable(1e-20,
                      termStructure,
                      callableBond,
                      frequency,
                      bondDayCounter);

        System.out.printf("Bloomberg price/yld (%%) 96.50 / 5.47 \n");

        priceCallable(0.01,
                      termStructure,
                      callableBond,
                      frequency,
                      bondDayCounter);

        System.out.printf("Bloomberg price/yld (%%) 95.68 / 5.66 \n");

        priceCallable(0.03,
                      termStructure,
                      callableBond,
                      frequency,
                      bondDayCounter);

        System.out.printf("Bloomberg price/yld (%%) 92.34 / 6.49 \n");

        priceCallable(0.06,
                      termStructure,
                      callableBond,
                      frequency,
                      bondDayCounter);

        System.out.printf("Bloomberg price/yld (%%) 87.16 / 7.83 \n");

        priceCallable(0.12,
                      termStructure,
                      callableBond,
                      frequency,
                      bondDayCounter);

        System.out.printf("Bloomberg price/yld (%%) 77.31 / 10.65 \n");


        System.out.println("Done");
    }

}

Contact

Copyright and published by: BN Algorihtms Ltd 2024. For general information only. Not to be relied for any purpose. Not advice about investment. No warranty of any kind. No liability for any use of this information accepted. Contact: webs@bnikolic.co.uk