DayCountAPI.java

  1. package org.drip.sample.date;

  2. /*
  3.  * Generic imports
  4.  */

  5. import org.drip.analytics.date.*;
  6. import org.drip.analytics.daycount.Convention;
  7. import org.drip.numerical.common.FormatUtil;
  8. import org.drip.service.env.EnvManager;

  9. /*
  10.  * -*- mode: java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  11.  */

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

  88. /**
  89.  * <i>DayCountAPI</i> demonstrates Day-count API Functionality. It does the following:
  90.  *  
  91.  * <br><br>
  92.  *  <ul>
  93.  *      <li>
  94.  *          Get all the holiday locations in CreditAnalytics, and all the holidays in the year according the
  95.  *              calendar set.
  96.  *      </li>
  97.  *      <li>
  98.  *          Calculate year fraction between 2 dates according to semi-annual, Act/360, and USD calendar.
  99.  *      </li>
  100.  *  </ul>
  101.  *  
  102.  * <br><br>
  103.  *  <ul>
  104.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ProductCore.md">Product Core Module</a></li>
  105.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/FixedIncomeAnalyticsLibrary.md">Fixed Income Analytics</a></li>
  106.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/sample/README.md">DROP API Construction and Usage</a></li>
  107.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/sample/date/README.md">Calendar Date Roll Day Count</a></li>
  108.  *  </ul>
  109.  * <br><br>
  110.  *
  111.  * @author Lakshmi Krishnamurthy
  112.  */

  113. public class DayCountAPI {

  114.     /**
  115.      * Sample API demonstrating the day count functionality
  116.      *
  117.      *      USE WITH CARE: This sample ignores errors and does not handle exceptions.
  118.      */

  119.     private static final void DayCountAPISample (
  120.         final JulianDate dtStart,
  121.         final JulianDate dtEnd,
  122.         final String strDayCount)
  123.         throws Exception
  124.     {
  125.         /*
  126.          * Calculate year fraction between 2 dates according to semi-annual, Act/360, and USD calendar
  127.          */

  128.         double dblYearFraction = org.drip.analytics.daycount.Convention.YearFraction (
  129.             dtStart.julian(),
  130.             dtEnd.julian(),
  131.             strDayCount,
  132.             false,
  133.             null,
  134.             "USD"
  135.         );

  136.         int iDaysAccrued = org.drip.analytics.daycount.Convention.DaysAccrued (
  137.             dtStart.julian(),
  138.             dtEnd.julian(),
  139.             strDayCount,
  140.             false,
  141.             null,
  142.             "USD"
  143.         );

  144.         System.out.println (
  145.             "\t[" + dtStart + " -> " + dtEnd + "] => " + FormatUtil.FormatDouble (dblYearFraction, 1, 4, 1.) + " | " + iDaysAccrued + " | " + strDayCount
  146.         );
  147.     }

  148.     private static final void DayCountSequence (
  149.         final JulianDate dtStart,
  150.         final JulianDate dtEnd)
  151.         throws Exception
  152.     {
  153.         DayCountAPISample (dtStart, dtEnd, "28/360");

  154.         DayCountAPISample (dtStart, dtEnd, "30/365");

  155.         DayCountAPISample (dtStart, dtEnd, "30/360");

  156.         DayCountAPISample (dtStart, dtEnd, "30E/360 ISDA");

  157.         DayCountAPISample (dtStart, dtEnd, "30E/360");

  158.         DayCountAPISample (dtStart, dtEnd, "30E+/360");

  159.         DayCountAPISample (dtStart, dtEnd, "Act/360");

  160.         DayCountAPISample (dtStart, dtEnd, "Act/364");

  161.         DayCountAPISample (dtStart, dtEnd, "Act/365");

  162.         DayCountAPISample (dtStart, dtEnd, "Act/Act ISDA");

  163.         DayCountAPISample (dtStart, dtEnd, "Act/Act");

  164.         DayCountAPISample (dtStart, dtEnd, "NL/360");

  165.         DayCountAPISample (dtStart, dtEnd, "NL/365");
  166.     }

  167.     public static final void main (
  168.         final String astrArgs[])
  169.         throws Exception
  170.     {
  171.         // String strConfig = "c:\\Lakshmi\\BondAnal\\Config.xml";

  172.         String strConfig = "";

  173.         EnvManager.InitEnv (strConfig);

  174.         /*
  175.          * List available day count
  176.          */

  177.         String strDCList = Convention.AvailableDC();

  178.         System.out.println (strDCList + "\n--------------------\n");

  179.         DayCountSequence (
  180.             DateUtil.CreateFromYMD (2013, 5, 30),
  181.             DateUtil.CreateFromYMD (2013, 6, 24)
  182.         );

  183.         System.out.println ("\n--------------------\n");

  184.         DayCountSequence (
  185.             DateUtil.CreateFromYMD (2010, 12, 30),
  186.             DateUtil.CreateFromYMD (2012, 12, 30)
  187.         );

  188.         EnvManager.TerminateEnv();
  189.     }
  190. }