CDSBuilder.java

  1. package org.drip.product.creator;

  2. /*
  3.  * -*- mode: java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  4.  */

  5. /*!
  6.  * Copyright (C) 2020 Lakshmi Krishnamurthy
  7.  * Copyright (C) 2019 Lakshmi Krishnamurthy
  8.  * Copyright (C) 2018 Lakshmi Krishnamurthy
  9.  * Copyright (C) 2017 Lakshmi Krishnamurthy
  10.  * Copyright (C) 2016 Lakshmi Krishnamurthy
  11.  * Copyright (C) 2015 Lakshmi Krishnamurthy
  12.  * Copyright (C) 2014 Lakshmi Krishnamurthy
  13.  * Copyright (C) 2013 Lakshmi Krishnamurthy
  14.  * Copyright (C) 2012 Lakshmi Krishnamurthy
  15.  * Copyright (C) 2011 Lakshmi Krishnamurthy
  16.  *
  17.  *  This file is part of DROP, an open-source library targeting analytics/risk, transaction cost analytics,
  18.  *      asset liability management analytics, capital, exposure, and margin analytics, valuation adjustment
  19.  *      analytics, and portfolio construction analytics within and across fixed income, credit, commodity,
  20.  *      equity, FX, and structured products. It also includes auxiliary libraries for algorithm support,
  21.  *      numerical analysis, numerical optimization, spline builder, model validation, statistical learning,
  22.  *      and computational support.
  23.  *  
  24.  *      https://lakshmidrip.github.io/DROP/
  25.  *  
  26.  *  DROP is composed of three modules:
  27.  *  
  28.  *  - DROP Product Core - https://lakshmidrip.github.io/DROP-Product-Core/
  29.  *  - DROP Portfolio Core - https://lakshmidrip.github.io/DROP-Portfolio-Core/
  30.  *  - DROP Computational Core - https://lakshmidrip.github.io/DROP-Computational-Core/
  31.  *
  32.  *  DROP Product Core implements libraries for the following:
  33.  *  - Fixed Income Analytics
  34.  *  - Loan Analytics
  35.  *  - Transaction Cost Analytics
  36.  *
  37.  *  DROP Portfolio Core implements libraries for the following:
  38.  *  - Asset Allocation Analytics
  39.  *  - Asset Liability Management Analytics
  40.  *  - Capital Estimation Analytics
  41.  *  - Exposure Analytics
  42.  *  - Margin Analytics
  43.  *  - XVA Analytics
  44.  *
  45.  *  DROP Computational Core implements libraries for the following:
  46.  *  - Algorithm Support
  47.  *  - Computation Support
  48.  *  - Function Analysis
  49.  *  - Model Validation
  50.  *  - Numerical Analysis
  51.  *  - Numerical Optimizer
  52.  *  - Spline Builder
  53.  *  - Statistical Learning
  54.  *
  55.  *  Documentation for DROP is Spread Over:
  56.  *
  57.  *  - Main                     => https://lakshmidrip.github.io/DROP/
  58.  *  - Wiki                     => https://github.com/lakshmiDRIP/DROP/wiki
  59.  *  - GitHub                   => https://github.com/lakshmiDRIP/DROP
  60.  *  - Repo Layout Taxonomy     => https://github.com/lakshmiDRIP/DROP/blob/master/Taxonomy.md
  61.  *  - Javadoc                  => https://lakshmidrip.github.io/DROP/Javadoc/index.html
  62.  *  - Technical Specifications => https://github.com/lakshmiDRIP/DROP/tree/master/Docs/Internal
  63.  *  - Release Versions         => https://lakshmidrip.github.io/DROP/version.html
  64.  *  - Community Credits        => https://lakshmidrip.github.io/DROP/credits.html
  65.  *  - Issues Catalog           => https://github.com/lakshmiDRIP/DROP/issues
  66.  *  - JUnit                    => https://lakshmidrip.github.io/DROP/junit/index.html
  67.  *  - Jacoco                   => https://lakshmidrip.github.io/DROP/jacoco/index.html
  68.  *
  69.  *  Licensed under the Apache License, Version 2.0 (the "License");
  70.  *      you may not use this file except in compliance with the License.
  71.  *  
  72.  *  You may obtain a copy of the License at
  73.  *      http://www.apache.org/licenses/LICENSE-2.0
  74.  *  
  75.  *  Unless required by applicable law or agreed to in writing, software
  76.  *      distributed under the License is distributed on an "AS IS" BASIS,
  77.  *      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  78.  *  
  79.  *  See the License for the specific language governing permissions and
  80.  *      limitations under the License.
  81.  */

  82. /**
  83.  * <i>CDSBuilder</i> contains the suite of helper functions for creating the CreditDefaultSwap product from
  84.  * the parameters/byte array streams. It also creates the standard EU, NA, ASIA contracts, CDS with
  85.  * amortization schedules, and custom CDS from product codes/tenors.
  86.  *
  87.  *  <br><br>
  88.  *  <ul>
  89.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ProductCore.md">Product Core Module</a></li>
  90.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/FixedIncomeAnalyticsLibrary.md">Fixed Income Analytics</a></li>
  91.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/product/README.md">Product Components/Baskets for Credit, FRA, FX, Govvie, Rates, and Option AssetClasses</a></li>
  92.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/product/creator/README.md">Streams and Products Construction Utilities</a></li>
  93.  *  </ul>
  94.  * <br><br>
  95.  *
  96.  * @author Lakshmi Krishnamurthy
  97.  */

  98. public class CDSBuilder {

  99.     /**
  100.      * Create the credit default swap from the effective/maturity dates, coupon, IR curve name, and
  101.      *  component credit valuation parameters.
  102.      *
  103.      * @param dtEffective JulianDate effective
  104.      * @param dtMaturity JulianDate maturity
  105.      * @param dblCoupon Coupon
  106.      * @param strCurrency Currency
  107.      * @param cs Credit Setting Parameters
  108.      * @param strCalendar Optional Holiday Calendar for Accrual calculation
  109.      * @param bAdjustDates Roll using the FWD mode for the period end dates and the pay dates
  110.      *
  111.      * @return CreditDefaultSwap product
  112.      */

  113.     public static final org.drip.product.definition.CreditDefaultSwap CreateCDS (
  114.         final org.drip.analytics.date.JulianDate dtEffective,
  115.         final org.drip.analytics.date.JulianDate dtMaturity,
  116.         final double dblCoupon,
  117.         final java.lang.String strCurrency,
  118.         final org.drip.product.params.CreditSetting cs,
  119.         final java.lang.String strCalendar,
  120.         final boolean bAdjustDates)
  121.     {
  122.         if (null == dtEffective || null == dtMaturity || null == strCurrency || strCurrency.isEmpty() || null
  123.             == cs || !org.drip.numerical.common.NumberUtil.IsValid (dblCoupon))
  124.             return null;

  125.         try {
  126.             org.drip.analytics.daycount.DateAdjustParams dap = bAdjustDates ? new
  127.                 org.drip.analytics.daycount.DateAdjustParams
  128.                     (org.drip.analytics.daycount.Convention.DATE_ROLL_FOLLOWING, 1, strCalendar) : null;

  129.             org.drip.product.definition.CreditDefaultSwap cds = new org.drip.product.credit.CDSComponent
  130.                 (dtEffective.julian(), dtMaturity.julian(), dblCoupon, 4, "Act/360", "Act/360", "", false,
  131.                     null, null, null, dap, dap, dap, dap, null, null, 1., strCurrency, cs, strCalendar);

  132.             cds.setPrimaryCode ("CDS." + dtMaturity.toString() + "." + cs.creditCurveName());

  133.             return cds;
  134.         } catch (java.lang.Exception e) {
  135.             e.printStackTrace();
  136.         }

  137.         return null;
  138.     }

  139.     /**
  140.      * Create the credit default swap from the effective/maturity dates, coupon, IR curve name, and
  141.      *  credit curve.
  142.      *
  143.      * @param dtEffective JulianDate effective
  144.      * @param dtMaturity JulianDate maturity
  145.      * @param dblCoupon Coupon
  146.      * @param strCurrency Currency
  147.      * @param dblRecovery Recovery Rate
  148.      * @param strCredit Credit curve name
  149.      * @param strCalendar Optional Holiday Calendar for Accrual calculation
  150.      * @param bAdjustDates Roll using the FWD mode for the period end dates and the pay dates
  151.      *
  152.      * @return CreditDefaultSwap product
  153.      */

  154.     public static final org.drip.product.definition.CreditDefaultSwap CreateCDS (
  155.         final org.drip.analytics.date.JulianDate dtEffective,
  156.         final org.drip.analytics.date.JulianDate dtMaturity,
  157.         final double dblCoupon,
  158.         final java.lang.String strCurrency,
  159.         final double dblRecovery,
  160.         final java.lang.String strCredit,
  161.         final java.lang.String strCalendar,
  162.         final boolean bAdjustDates)
  163.     {
  164.         if (null == dtEffective || null == dtMaturity || null == strCurrency || strCurrency.isEmpty() || null
  165.             == strCredit || strCredit.isEmpty() || !org.drip.numerical.common.NumberUtil.IsValid (dblCoupon))
  166.             return null;

  167.         org.drip.product.params.CreditSetting cs = new org.drip.product.params.CreditSetting (30,
  168.             dblRecovery, true, strCredit, true);

  169.         return cs.validate() ? CreateCDS (dtEffective, dtMaturity, dblCoupon, strCurrency, cs, strCalendar,
  170.             bAdjustDates) : null;
  171.     }

  172.     /**
  173.      * Create the credit default swap from the effective date, tenor, coupon, IR curve name, and component
  174.      *  credit valuation parameters.
  175.      *
  176.      * @param dtEffective JulianDate effective
  177.      * @param strTenor String tenor
  178.      * @param dblCoupon Coupon
  179.      * @param strCurrency Currency
  180.      * @param cs Credit Setting Parameters
  181.      * @param strCalendar Optional Holiday Calendar for Accrual calculation
  182.      *
  183.      * @return CreditDefaultSwap product
  184.      */

  185.     public static final org.drip.product.definition.CreditDefaultSwap CreateCDS (
  186.         final org.drip.analytics.date.JulianDate dtEffective,
  187.         final java.lang.String strTenor,
  188.         final double dblCoupon,
  189.         final java.lang.String strCurrency,
  190.         final org.drip.product.params.CreditSetting cs,
  191.         final java.lang.String strCalendar)
  192.     {
  193.         if (null == dtEffective || null == strTenor || strTenor.isEmpty() || null == strCurrency ||
  194.             strCurrency.isEmpty() || null == cs || !org.drip.numerical.common.NumberUtil.IsValid (dblCoupon))
  195.             return null;

  196.         try {
  197.             org.drip.product.definition.CreditDefaultSwap cds = new org.drip.product.credit.CDSComponent
  198.                 (dtEffective.julian(), dtEffective.addTenor (strTenor).julian(), dblCoupon, 4, "30/360",
  199.                     "30/360", "", true, null, null, null, null, null, null, null, null, null, 100.,
  200.                         strCurrency, cs, strCalendar);

  201.             cds.setPrimaryCode ("CDS." + strTenor + "." + cs.creditCurveName());

  202.             return cds;
  203.         } catch (java.lang.Exception e) {
  204.             e.printStackTrace();
  205.         }

  206.         return null;
  207.     }

  208.     /**
  209.      * Create the credit default swap from the effective/maturity dates, coupon, IR curve name, and credit
  210.      *  curve.
  211.      *
  212.      * @param dtEffective JulianDate effective
  213.      * @param strTenor String tenor
  214.      * @param dblCoupon Coupon
  215.      * @param strCurrency Currency
  216.      * @param strCredit Credit curve name
  217.      * @param strCalendar Optional Holiday Calendar for accrual calculation
  218.      *
  219.      * @return CreditDefaultSwap product
  220.      */

  221.     public static final org.drip.product.definition.CreditDefaultSwap CreateCDS (
  222.         final org.drip.analytics.date.JulianDate dtEffective,
  223.         final java.lang.String strTenor,
  224.         final double dblCoupon,
  225.         final java.lang.String strCurrency,
  226.         final java.lang.String strCredit,
  227.         final java.lang.String strCalendar)
  228.     {
  229.         if (null == dtEffective || null == strTenor || strTenor.isEmpty() || null == strCurrency ||
  230.             strCurrency.isEmpty() || null == strCredit || strCredit.isEmpty() ||
  231.                 !org.drip.numerical.common.NumberUtil.IsValid (dblCoupon))
  232.             return null;

  233.         org.drip.product.params.CreditSetting cs = new org.drip.product.params.CreditSetting (30,
  234.             java.lang.Double.NaN, true, strCredit, true);

  235.         return cs.validate() ? CreateCDS (dtEffective, strTenor, dblCoupon, strCurrency, cs, strCalendar) :
  236.             null;
  237.     }

  238.     /**
  239.      * Create an SNAC style CDS contract with full first stub
  240.      *
  241.      * @param dtEffective CDS Effective date
  242.      * @param strTenor CDS Tenor
  243.      * @param dblCoupon SNAC strike coupon
  244.      * @param strCurrency Currency
  245.      * @param strCredit Credit Curve name
  246.      * @param strCalendar Holiday Calendar
  247.      *
  248.      * @return CDS instance object
  249.      */

  250.     public static final org.drip.product.definition.CreditDefaultSwap CreateSNAC (
  251.         final org.drip.analytics.date.JulianDate dtEffective,
  252.         final java.lang.String strTenor,
  253.         final double dblCoupon,
  254.         final java.lang.String strCurrency,
  255.         final java.lang.String strCredit,
  256.         final java.lang.String strCalendar)
  257.     {
  258.         if (null == dtEffective || null == strTenor || strTenor.isEmpty()) return null;

  259.         org.drip.analytics.date.JulianDate dtFirstCoupon = dtEffective.nextCreditIMM (3);

  260.         if (null == dtFirstCoupon) return null;

  261.         org.drip.product.definition.CreditDefaultSwap cds = CreateCDS (dtFirstCoupon.subtractTenor ("3M"),
  262.             dtFirstCoupon.addTenor (strTenor), dblCoupon, strCurrency, 0.40, strCredit, strCalendar, true);

  263.         if (null == cds) return null;

  264.         cds.setPrimaryCode ("CDS." + strTenor + "." + strCredit);

  265.         return cds;
  266.     }

  267.     /**
  268.      * Create an SNAC style CDS contract with full first stub
  269.      *
  270.      * @param dtEffective CDS Effective date
  271.      * @param strTenor CDS Tenor
  272.      * @param dblCoupon SNAC strike coupon
  273.      * @param strCredit Credit Curve name
  274.      *
  275.      * @return CDS instance object
  276.      */

  277.     public static final org.drip.product.definition.CreditDefaultSwap CreateSNAC (
  278.         final org.drip.analytics.date.JulianDate dtEffective,
  279.         final java.lang.String strTenor,
  280.         final double dblCoupon,
  281.         final java.lang.String strCredit)
  282.     {
  283.         return CreateSNAC (dtEffective, strTenor, dblCoupon, "USD", strCredit, "USD");
  284.     }

  285.     /**
  286.      * Create an Standard EU CDS contract with full first stub
  287.      *
  288.      * @param dtEffective CDS Effective date
  289.      * @param strTenor CDS Tenor
  290.      * @param dblCoupon Strike coupon
  291.      * @param strCredit Credit Curve name
  292.      *
  293.      * @return CDS instance object
  294.      */

  295.     public static final org.drip.product.definition.CreditDefaultSwap CreateSTEU (
  296.         final org.drip.analytics.date.JulianDate dtEffective,
  297.         final java.lang.String strTenor,
  298.         final double dblCoupon,
  299.         final java.lang.String strCredit)
  300.     {
  301.         if (null == dtEffective || null == strTenor || strTenor.isEmpty()) return null;

  302.         org.drip.analytics.date.JulianDate dtFirstCoupon = dtEffective.nextCreditIMM (3);

  303.         if (null == dtFirstCoupon) return null;

  304.         org.drip.product.definition.CreditDefaultSwap cds = CreateCDS (dtFirstCoupon.subtractTenor ("3M"),
  305.             dtFirstCoupon.addTenor (strTenor), dblCoupon, "EUR", 0.40, strCredit, "EUR", true);

  306.         cds.setPrimaryCode ("CDS." + strTenor + "." + strCredit);

  307.         return cds;
  308.     }

  309.     /**
  310.      * Create an Standard Asia Pacific CDS contract with full first stub
  311.      *
  312.      * @param dtEffective CDS Effective date
  313.      * @param strTenor CDS Tenor
  314.      * @param dblCoupon Strike coupon
  315.      * @param strCredit Credit Curve name
  316.      *
  317.      * @return CDS instance object
  318.      */

  319.     public static final org.drip.product.definition.CreditDefaultSwap CreateSAPC (
  320.         final org.drip.analytics.date.JulianDate dtEffective,
  321.         final java.lang.String strTenor,
  322.         final double dblCoupon,
  323.         final java.lang.String strCredit)
  324.     {
  325.         if (null == dtEffective || null == strTenor || strTenor.isEmpty()) return null;

  326.         org.drip.analytics.date.JulianDate dtFirstCoupon = dtEffective.nextCreditIMM (3);

  327.         if (null == dtFirstCoupon) return null;

  328.         org.drip.product.definition.CreditDefaultSwap cds = CreateCDS (dtFirstCoupon.subtractTenor ("3M"),
  329.             dtFirstCoupon.addTenor (strTenor), dblCoupon, "HKD", 0.40, strCredit, "HKD", true);

  330.         cds.setPrimaryCode ("CDS." + strTenor + "." + strCredit);

  331.         return cds;
  332.     }

  333.     /**
  334.      * Create an Standard Emerging Market CDS contract with full first stub
  335.      *
  336.      * @param dtEffective CDS Effective date
  337.      * @param strTenor CDS Tenor
  338.      * @param dblCoupon Strike coupon
  339.      * @param strCredit Credit Curve name
  340.      * @param strLocation Location
  341.      *
  342.      * @return CDS instance object
  343.      */

  344.     public static final org.drip.product.definition.CreditDefaultSwap CreateSTEM (
  345.         final org.drip.analytics.date.JulianDate dtEffective,
  346.         final java.lang.String strTenor,
  347.         final double dblCoupon,
  348.         final java.lang.String strCredit,
  349.         final java.lang.String strLocation)
  350.     {
  351.         if (null == dtEffective || null == strTenor || strTenor.isEmpty()) return null;

  352.         org.drip.analytics.date.JulianDate dtFirstCoupon = dtEffective.nextCreditIMM (3);

  353.         if (null == dtFirstCoupon) return null;

  354.         org.drip.product.definition.CreditDefaultSwap cds = CreateCDS (dtFirstCoupon.subtractTenor ("3M"),
  355.             dtFirstCoupon.addTenor (strTenor), dblCoupon, strLocation, 0.25, strCredit, strLocation, true);

  356.         cds.setPrimaryCode ("CDS." + strTenor + "." + strCredit);

  357.         return cds;
  358.     }
  359. }