TreasuryBondProcessor.java

  1. package org.drip.service.json;

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

  77. /**
  78.  * <i>TreasuryBondProcessor</i> Sets Up and Executes a JSON Based In/Out Processing Service for Treasury
  79.  * Bonds.
  80.  *
  81.  * <br><br>
  82.  *  <ul>
  83.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ComputationalCore.md">Computational Core Module</a></li>
  84.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ComputationSupportLibrary.md">Computation Support</a></li>
  85.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/service/README.md">Environment, Product/Definition Containers, and Scenario/State Manipulation APIs</a></li>
  86.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/service/json/README.md">JSON Based Valuation Request Service</a></li>
  87.  *  </ul>
  88.  * <br><br>
  89.  *
  90.  * @author Lakshmi Krishnamurthy
  91.  */

  92. public class TreasuryBondProcessor {

  93.     /**
  94.      * JSON Based in/out Treasury Bond Secular Metrics Thunker
  95.      *
  96.      * @param jsonParameter JSON Treasury Bond Request Parameters
  97.      *
  98.      * @return JSON Treasury Bond Secular Metrics Response
  99.      */

  100.     @SuppressWarnings ("unchecked") static final org.drip.json.simple.JSONObject SecularMetrics (
  101.         final org.drip.json.simple.JSONObject jsonParameter)
  102.     {
  103.         java.lang.String strTreasuryCode = org.drip.json.parser.Converter.StringEntry (jsonParameter,
  104.             "TreasuryCode");

  105.         org.drip.analytics.date.JulianDate dtEffective = org.drip.json.parser.Converter.DateEntry
  106.             (jsonParameter, "EffectiveDate");

  107.         if (null == dtEffective) return null;

  108.         int iEffectiveDate = dtEffective.julian();

  109.         org.drip.analytics.date.JulianDate dtMaturity = org.drip.json.parser.Converter.DateEntry
  110.             (jsonParameter, "MaturityDate");

  111.         if (null == dtMaturity) return null;

  112.         org.drip.analytics.date.JulianDate dtSettle = org.drip.json.parser.Converter.DateEntry
  113.             (jsonParameter, "SettleDate");

  114.         if (null == dtSettle) return null;

  115.         int iSettleDate = dtSettle.julian();

  116.         double dblYield = java.lang.Double.NaN;
  117.         double dblCoupon = java.lang.Double.NaN;
  118.         double dblNotional = java.lang.Double.NaN;
  119.         double dblCleanPrice = java.lang.Double.NaN;

  120.         try {
  121.             dblCoupon = org.drip.json.parser.Converter.DoubleEntry (jsonParameter, "Coupon");

  122.             dblNotional = org.drip.json.parser.Converter.DoubleEntry (jsonParameter, "Notional");

  123.             dblCleanPrice = org.drip.json.parser.Converter.DoubleEntry (jsonParameter, "CleanPrice");
  124.         } catch (java.lang.Exception e) {
  125.             e.printStackTrace();

  126.             return null;
  127.         }

  128.         org.drip.market.issue.TreasurySetting ts =
  129.             org.drip.market.issue.TreasurySettingContainer.TreasurySetting (strTreasuryCode);

  130.         if (null == ts) return null;

  131.         int iFrequency = ts.frequency();

  132.         java.lang.String strDayCount = ts.dayCount();

  133.         org.drip.product.definition.Bond tsy = org.drip.product.creator.BondBuilder.Treasury (ts.code(),
  134.             dtEffective, dtMaturity, ts.currency(), dblCoupon, iFrequency, strDayCount);

  135.         if (null == tsy || tsy.maturityDate().julian() <= iSettleDate) return null;

  136.         java.lang.String strCurrency = tsy.payCurrency();

  137.         org.drip.param.valuation.ValuationParams valParams = org.drip.param.valuation.ValuationParams.Spot
  138.             (iSettleDate);

  139.         try {
  140.             dblYield = tsy.yield01FromPrice (valParams, null, null, dblCleanPrice);
  141.         } catch (java.lang.Exception e) {
  142.             e.printStackTrace();

  143.             return null;
  144.         }

  145.         org.drip.json.simple.JSONObject jsonResponse = new org.drip.json.simple.JSONObject();

  146.         org.drip.json.simple.JSONArray jsonCouponFlowArray = new org.drip.json.simple.JSONArray();

  147.         for (org.drip.analytics.cashflow.CompositePeriod cp : tsy.couponPeriods()) {
  148.             org.drip.json.simple.JSONObject jsonCouponFlow = new org.drip.json.simple.JSONObject();

  149.             org.drip.analytics.output.CompositePeriodCouponMetrics cpcm = cp.couponMetrics (iEffectiveDate,
  150.                 null);

  151.             if (null == cpcm) return null;

  152.             double dblPeriodCouponRate = cpcm.rate();

  153.             jsonCouponFlow.put ("CouponRate", dblPeriodCouponRate);

  154.             try {
  155.                 int iPeriodStartDate = cp.startDate();

  156.                 int iPeriodEndDate = cp.endDate();

  157.                 double dblYieldDF = org.drip.analytics.support.Helper.Yield2DF (iFrequency, dblYield,
  158.                     org.drip.analytics.daycount.Convention.YearFraction (iEffectiveDate, iPeriodEndDate,
  159.                         strDayCount, false, null, strCurrency));

  160.                 jsonCouponFlow.put ("StartDate", new org.drip.analytics.date.JulianDate
  161.                     (iPeriodStartDate).toString());

  162.                 jsonCouponFlow.put ("EndDate", new org.drip.analytics.date.JulianDate
  163.                     (iPeriodEndDate).toString());

  164.                 jsonCouponFlow.put ("PayDate", new org.drip.analytics.date.JulianDate
  165.                     (cp.payDate()).toString());

  166.                 double dblCouponDCF = cp.couponDCF();

  167.                 jsonCouponFlow.put ("AccrualDays", org.drip.analytics.daycount.Convention.DaysAccrued
  168.                     (iPeriodStartDate, iPeriodEndDate, strDayCount, false, null, strCurrency));

  169.                 jsonCouponFlow.put ("AccrualFraction", dblCouponDCF);

  170.                 double dblCouponAmount = dblPeriodCouponRate * dblCouponDCF * dblNotional;

  171.                 jsonCouponFlow.put ("CouponAmount", dblCouponAmount);

  172.                 jsonCouponFlow.put ("YieldDF", dblYieldDF);
  173.             } catch (java.lang.Exception e) {
  174.                 e.printStackTrace();

  175.                 return null;
  176.             }

  177.             jsonCouponFlowArray.add (jsonCouponFlow);
  178.         }

  179.         jsonResponse.put ("CouponFlow", jsonCouponFlowArray);

  180.         jsonResponse.put ("CleanPrice", 100. * dblCleanPrice);

  181.         jsonResponse.put ("Face", dblNotional);

  182.         org.drip.analytics.date.JulianDate dtPreviousCouponDate = tsy.previousCouponDate (dtSettle);

  183.         if (null != dtPreviousCouponDate) {
  184.             jsonResponse.put ("AccrualDays", iSettleDate - dtPreviousCouponDate.julian());

  185.             jsonResponse.put ("PreviousCouponDate", dtPreviousCouponDate.toString());
  186.         }

  187.         try {
  188.             double dblAccruedAmount = tsy.accrued (iSettleDate, null) * dblNotional;

  189.             double dblCurrentPrincipal = tsy.notional (dtPreviousCouponDate.julian()) * dblNotional;

  190.             double dblRisk = tsy.yield01FromPrice (valParams, null, null, dblCleanPrice);

  191.             jsonResponse.put ("Accrued", dblAccruedAmount);

  192.             jsonResponse.put ("Convexity", tsy.convexityFromPrice (valParams, null, null, dblCleanPrice));

  193.             jsonResponse.put ("CurrentPrincipal", dblCurrentPrincipal);

  194.             jsonResponse.put ("DV01", dblRisk * dblNotional);

  195.             jsonResponse.put ("ModifiedDuration", tsy.modifiedDurationFromPrice (valParams, null, null,
  196.                 dblCleanPrice));

  197.             jsonResponse.put ("Notional", dblNotional);

  198.             jsonResponse.put ("NPV", dblCurrentPrincipal + dblAccruedAmount);

  199.             jsonResponse.put ("Risk", dblRisk);

  200.             jsonResponse.put ("SettleDate", dtSettle.toString());

  201.             jsonResponse.put ("Total", dblCurrentPrincipal + dblAccruedAmount);

  202.             jsonResponse.put ("Yield", tsy.yieldFromPrice (valParams, null, null, dblCleanPrice));
  203.         } catch (java.lang.Exception e) {
  204.             e.printStackTrace();

  205.             return null;
  206.         }

  207.         return jsonResponse;
  208.     }
  209. }