Bojan Nikolic: Using and Understanding the QuantLib Addin

[website home] | [BN Algorithms]

qlPiecewiseHazardRateCurve – create a hazard-rate curve for credit default

Usage:

=qlPiecewiseHazardRateCurve(<ObjPrefix>,
                            <Helpers>, <DayCounter>,
                            <Calendar>, <InterpolatorID>
                            <Accuracy)
<ObjPrefix>

Optional prefix for names of objects created with this function

<Helpers>

Sequence of “Spred CDS Helpers” that encapsulate the market information about the default probability

<DayCounter>

Day count convention used to calculate the interest earned over a specific period of time (see Day Count(ing) Conventions). For example, “Actual/360”.

<Calendar>

The calendar used for determining the business days. For example, “TARGET” (see TARGET Calendar)

<InterpolatorID>

Type of interpolation to do between the implied rates. Possible values are BackwardFlat, ForwardFlat, Linear, LogLinear, LogParabolic, KrugerLogCubic, MonotonicLogCubicNaturalSpline. See also Interpolation in QuantLib

<Accuracy>

Optional accuracy for which the iterative bootstrap process should aim for.

QLW/Java example

Here is example usage in QLW – QuantLib-Addin like interface from Java and Python

// Copyright (C) 2019 Bojan Nikolic <bojan@bnikolic.co.uk>
//
// See "RiskyBonds.xls" spreadsheet.
//
// Requires qlw-1.2.2p2 or later


import co.uk.bnikolic.qlw.property_t;
import co.uk.bnikolic.qlw.qlw;
import co.uk.bnikolic.qlw.StringVector;
import co.uk.bnikolic.qlw.LongVector;
import co.uk.bnikolic.qlw.DoubleVector;
import co.uk.bnikolic.qlw.PropertyVector;

public class  CreditDefaultSwapExample {

    static String calendar="TARGET";
    static String dcc="Actual/365 (Fixed)";
    static String discountc;

    static int today=41358;
    

    public static void main(String[] args) throws Exception {

	// See Money Market $D2
        qlw.qlSettingsSetEvaluationDate(new property_t(today));

	discountc=simpleYC();

	StringVector helpers=new StringVector();
	// These are running spreads -- see CreditMarket!C5:K6
	helpers.push_back(mkSpread("6m", 0.01));
	helpers.push_back(mkSpread("5Y", 0.01));

	// See CreditMarket!M9:M10
	String hzCurve=qlw.qlPiecewiseHazardRateCurve("hzCurve",
						      helpers,
						      new property_t(dcc),
						      calendar,
						      "BACKWARDFLAT",
						      qlw.OH_NULL());

	// CDS Payment Schedule -- every 3 Months for 5 Years. See
	// cell E36
	String schedule=qlw.qlSchedule("schedule",
				       new property_t(calAdvance(calendar,
								 today,
								 "1D")),
				       new property_t(calAdvance(calendar,
								 today,
								 "5Y")),
				       "3M",
				       new property_t(calendar),
				       new property_t("Unadjusted"),
				       new property_t("Unadjusted"),
				       new property_t("OldCDS"),
				       qlw.OH_NULL(),
				       qlw.OH_NULL(),
				       qlw.OH_NULL()
				       );

	String cds=qlw.qlCreditDefaultSwap("cds",
					   new property_t("Buyer"),
					   1e6,
					   0.0,
					   500*1e-4,
					   schedule,
					   new property_t("Modified Following"),
					   dcc,
					   new property_t(true),
					   new property_t(true),
					   new property_t(calAdvance(calendar,
								     today,
								     "1D")),
					   qlw.OH_NULL());
	String engine=qlw.qlMidPointCdsEngine("engine",
					   hzCurve,
					   0.4,
					   discountc);
	qlw.qlInstrumentSetPricingEngine(cds, engine);
	System.out.println("NPV: "+ qlw.qlInstrumentNPV(cds));
					   
    }

    // Helpers tying default probability to observed market
    // pricing. See CreditMarket!C9:K10
    public static String mkSpread(String term, double rate)
    {
	
	return qlw.qlSpreadCdsHelper("running" + term,
				     new property_t(rate),
				     term,
				     new property_t(2),
				     calendar,
				     "Quarterly",
				     "Modified Following",
				     "OldCDS",
				     dcc,
				     0.4,
				     discountc,
				     qlw.OH_NULL(),
				     qlw.OH_NULL()
				     );
				     
    }

    // Create a simple Yield Curve for testing
    public static String simpleYC() {

        PropertyVector datesList= new PropertyVector();
        datesList.push_back(new property_t("0D"));
        datesList.push_back(new property_t("10Y"));
        DoubleVector ratesList=new DoubleVector();
        ratesList.push_back(0.01);
        ratesList.push_back(0.01);

        String curve=qlw.qlInterpolatedYieldCurve("curve",
                                                  datesList,  
                                                  ratesList,
                                                  "TARGET",
						  new property_t(dcc),
                                                  new PropertyVector(), 
                                                  new PropertyVector(),
                                                  new property_t("ZeroYield"), 
                                                  new property_t("LogLinear"),
                                                  qlw.OH_NULL(),
                                                  qlw.OH_NULL());
	return curve;

    }

    // Helper function to advance only one date at a time
    public static int calAdvance(String cal, int today, String d)
    {
	PropertyVector datesList= new PropertyVector();
	datesList.push_back(new property_t(d));
	LongVector newDates= qlw.qlCalendarAdvance(cal,
						   new property_t(today),
						   datesList,
						   qlw.OH_NULL(),
						   qlw.OH_NULL());
	return newDates.get(0);
    }



}