DateManipulationClient.java

  1. package org.drip.sample.service;

  2. import org.drip.analytics.date.*;
  3. import org.drip.json.parser.Converter;
  4. import org.drip.json.simple.*;
  5. import org.drip.service.env.EnvManager;
  6. import org.drip.service.json.KeyHoleSkeleton;

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

  10. /*!
  11.  * Copyright (C) 2018 Lakshmi Krishnamurthy
  12.  * Copyright (C) 2017 Lakshmi Krishnamurthy
  13.  * Copyright (C) 2016 Lakshmi Krishnamurthy
  14.  *
  15.  *  This file is part of DRIP, a free-software/open-source library for buy/side financial/trading model
  16.  *      libraries targeting analysts and developers
  17.  *      https://lakshmidrip.github.io/DRIP/
  18.  *  
  19.  *  DRIP is composed of four main libraries:
  20.  *  
  21.  *  - DRIP Fixed Income - https://lakshmidrip.github.io/DRIP-Fixed-Income/
  22.  *  - DRIP Asset Allocation - https://lakshmidrip.github.io/DRIP-Asset-Allocation/
  23.  *  - DRIP Numerical Optimizer - https://lakshmidrip.github.io/DRIP-Numerical-Optimizer/
  24.  *  - DRIP Statistical Learning - https://lakshmidrip.github.io/DRIP-Statistical-Learning/
  25.  *
  26.  *  - DRIP Fixed Income: Library for Instrument/Trading Conventions, Treasury Futures/Options,
  27.  *      Funding/Forward/Overnight Curves, Multi-Curve Construction/Valuation, Collateral Valuation and XVA
  28.  *      Metric Generation, Calibration and Hedge Attributions, Statistical Curve Construction, Bond RV
  29.  *      Metrics, Stochastic Evolution and Option Pricing, Interest Rate Dynamics and Option Pricing, LMM
  30.  *      Extensions/Calibrations/Greeks, Algorithmic Differentiation, and Asset Backed Models and Analytics.
  31.  *
  32.  *  - DRIP Asset Allocation: Library for model libraries for MPT framework, Black Litterman Strategy
  33.  *      Incorporator, Holdings Constraint, and Transaction Costs.
  34.  *
  35.  *  - DRIP Numerical Optimizer: Library for Numerical Optimization and Spline Functionality.
  36.  *
  37.  *  - DRIP Statistical Learning: Library for Statistical Evaluation and Machine Learning.
  38.  *
  39.  *  Licensed under the Apache License, Version 2.0 (the "License");
  40.  *      you may not use this file except in compliance with the License.
  41.  *  
  42.  *  You may obtain a copy of the License at
  43.  *      http://www.apache.org/licenses/LICENSE-2.0
  44.  *  
  45.  *  Unless required by applicable law or agreed to in writing, software
  46.  *      distributed under the License is distributed on an "AS IS" BASIS,
  47.  *      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  48.  *  
  49.  *  See the License for the specific language governing permissions and
  50.  *      limitations under the License.
  51.  */

  52. /**
  53.  * DateManipulationClient demonstrates the Invocation and Examination of the JSON-based Date Manipulation
  54.  *  Service Client.
  55.  *
  56.  * @author Lakshmi Krishnamurthy
  57.  */

  58. public class DateManipulationClient {

  59.     @SuppressWarnings ("unchecked") static String IsHoliday (
  60.         final JulianDate dt,
  61.         final String strCalendar)
  62.     {
  63.         JSONObject jsonParameters = new JSONObject();

  64.         jsonParameters.put ("Date", dt.toString());

  65.         jsonParameters.put ("Calendar", strCalendar);

  66.         JSONObject jsonRequest = new JSONObject();

  67.         jsonRequest.put ("API", "DATE::ISHOLIDAY");

  68.         jsonRequest.put ("Parameters", jsonParameters);

  69.         return KeyHoleSkeleton.Thunker (jsonRequest.toJSONString());
  70.     }

  71.     @SuppressWarnings ("unchecked") static String Adjust (
  72.         final JulianDate dt,
  73.         final String strCalendar,
  74.         final int iNumDaysToAdjust)
  75.     {
  76.         JSONObject jsonParameters = new JSONObject();

  77.         jsonParameters.put ("Date", dt.toString());

  78.         jsonParameters.put ("Calendar", strCalendar);

  79.         jsonParameters.put ("DaysToAdjust", iNumDaysToAdjust);

  80.         JSONObject jsonRequest = new JSONObject();

  81.         jsonRequest.put ("API", "DATE::ADJUSTBUSINESSDAYS");

  82.         jsonRequest.put ("Parameters", jsonParameters);

  83.         return KeyHoleSkeleton.Thunker (jsonRequest.toJSONString());
  84.     }

  85.     @SuppressWarnings ("unchecked") static String Add (
  86.         final JulianDate dt,
  87.         final int iNumDaysToAdd)
  88.     {
  89.         JSONObject jsonParameters = new JSONObject();

  90.         jsonParameters.put ("Date", dt.toString());

  91.         jsonParameters.put ("DaysToAdd", iNumDaysToAdd);

  92.         JSONObject jsonRequest = new JSONObject();

  93.         jsonRequest.put ("API", "DATE::ADDDAYS");

  94.         jsonRequest.put ("Parameters", jsonParameters);

  95.         return KeyHoleSkeleton.Thunker (jsonRequest.toJSONString());
  96.     }

  97.     public static final void main (
  98.         final String[] astrArgs)
  99.         throws Exception
  100.     {
  101.         EnvManager.InitEnv ("");

  102.         JulianDate dtSpot = DateUtil.CreateFromYMD (
  103.             2016,
  104.             DateUtil.MARCH,
  105.             27
  106.         );

  107.         int iNumDays = 10;
  108.         String strCalendar = "MXN";

  109.         System.out.println ("\n\t|-----------------------------------------|");

  110.         for (int i = 0; i < iNumDays; ++i) {
  111.             JSONObject jsonResponse = (JSONObject) JSONValue.parse (
  112.                 Adjust (
  113.                     dtSpot,
  114.                     strCalendar,
  115.                     i
  116.                 )
  117.             );

  118.             System.out.println (
  119.                 "\t| Adjusted[" + dtSpot + " + " + i + "] = " +
  120.                 Converter.DateEntry (
  121.                     jsonResponse,
  122.                     "DateOut"
  123.                 ) + " |"
  124.             );
  125.         }

  126.         System.out.println ("\t|-----------------------------------------|");

  127.         System.out.println ("\n\n\t|-------------------------------------------|");

  128.         for (int i = 0; i < iNumDays; ++i) {
  129.             JSONObject jsonResponse = (JSONObject) JSONValue.parse (
  130.                 Add (
  131.                     dtSpot,
  132.                     i
  133.                 )
  134.             );

  135.             System.out.println (
  136.                 "\t| Unadjusted[" + dtSpot + " + " + i + "] = " +
  137.                 Converter.DateEntry (
  138.                     jsonResponse,
  139.                     "DateOut"
  140.                 ) + " |"
  141.             );
  142.         }

  143.         System.out.println ("\t|-------------------------------------------|");

  144.         System.out.println ("\n\n\t|---------------------------------|");

  145.         for (int i = 0; i < iNumDays; ++i) {
  146.             JulianDate dt = dtSpot.addDays (i);

  147.             JSONObject jsonResponse = (JSONObject) JSONValue.parse (
  148.                 IsHoliday (
  149.                     dt,
  150.                     strCalendar
  151.                 )
  152.             );

  153.             System.out.println (
  154.                 "\t| Is " + dt + " a Holiday? " +
  155.                 Converter.BooleanEntry (
  156.                     jsonResponse,
  157.                     "IsHoliday"
  158.                 ) + " |"
  159.             );
  160.         }

  161.         System.out.println ("\t|---------------------------------|");
  162.     }
  163. }