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

  76. /**
  77.  * <i>BondProcessor</i> Sets Up and Executes a JSON Based In/Out Bond Valuation Processor.
  78.  *
  79.  * <br><br>
  80.  *  <ul>
  81.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ComputationalCore.md">Computational Core Module</a></li>
  82.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ComputationSupportLibrary.md">Computation Support</a></li>
  83.  *      <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>
  84.  *      <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>
  85.  *  </ul>
  86.  * <br><br>
  87.  *
  88.  * @author Lakshmi Krishnamurthy
  89.  */

  90. public class BondProcessor {

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

  98.     @SuppressWarnings ("unchecked") static final org.drip.json.simple.JSONObject SecularMetrics (
  99.         final org.drip.json.simple.JSONObject jsonParameter)
  100.     {
  101.         org.drip.state.discount.MergedDiscountForwardCurve dcFunding =
  102.             org.drip.service.json.LatentStateProcessor.FundingCurve (jsonParameter);

  103.         if (null == dcFunding) return null;

  104.         org.drip.param.market.CurveSurfaceQuoteContainer csqc = new
  105.             org.drip.param.market.CurveSurfaceQuoteContainer();

  106.         if (!csqc.setFundingState (dcFunding)) return null;

  107.         double dblCleanPrice = java.lang.Double.NaN;
  108.         org.drip.product.credit.BondComponent bond = null;

  109.         try {
  110.             if (null == (bond = org.drip.product.creator.BondBuilder.CreateSimpleFixed
  111.                 (org.drip.json.parser.Converter.StringEntry (jsonParameter, "BondName"),
  112.                     dcFunding.currency(), "", org.drip.json.parser.Converter.DoubleEntry (jsonParameter,
  113.                         "BondCoupon"), org.drip.json.parser.Converter.IntegerEntry (jsonParameter,
  114.                             "BondFrequency"), org.drip.json.parser.Converter.StringEntry (jsonParameter,
  115.                                 "BondDayCount"), org.drip.json.parser.Converter.DateEntry (jsonParameter,
  116.                                     "BondEffectiveDate"), org.drip.json.parser.Converter.DateEntry
  117.                                         (jsonParameter, "BondMaturityDate"), null, null)))
  118.                 return null;

  119.             dblCleanPrice = org.drip.json.parser.Converter.DoubleEntry (jsonParameter, "BondCleanPrice");
  120.         } catch (java.lang.Exception e) {
  121.             e.printStackTrace();

  122.             return null;
  123.         }

  124.         org.drip.param.valuation.ValuationParams valParams = org.drip.param.valuation.ValuationParams.Spot
  125.             (dcFunding.epoch().julian());

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

  127.         jsonResponse.put ("BondName", bond.name());

  128.         jsonResponse.put ("BondEffectiveDate", bond.effectiveDate().toString());

  129.         jsonResponse.put ("BondMaturityDate", bond.maturityDate().toString());

  130.         jsonResponse.put ("BondFirstCouponDate", bond.firstCouponDate().toString());

  131.         jsonResponse.put ("BondCleanPrice", dblCleanPrice);

  132.         try {
  133.             double dblYield01 = bond.yield01FromPrice (valParams, csqc, null, dblCleanPrice);

  134.             double accrued = bond.accrued (valParams.valueDate(), csqc);

  135.             jsonResponse.put ("BondAccrued", accrued);

  136.             jsonResponse.put ("BondDirtyPrice", dblCleanPrice + accrued);

  137.             jsonResponse.put ("BondYield", bond.yieldFromPrice (valParams, csqc, null, dblCleanPrice));

  138.             jsonResponse.put ("BondMacaulayDuration", bond.macaulayDurationFromPrice (valParams, csqc, null,
  139.                 dblCleanPrice));

  140.             jsonResponse.put ("BondModifiedDuration", 10000. * bond.modifiedDurationFromPrice (valParams,
  141.                 csqc, null, dblCleanPrice));

  142.             jsonResponse.put ("BondYield01", 10000. * dblYield01);

  143.             jsonResponse.put ("BondDV01", 10000. * dblYield01);

  144.             jsonResponse.put ("BondConvexity", 1000000. * bond.convexityFromPrice (valParams, csqc, null,
  145.                 dblCleanPrice));

  146.             jsonResponse.put ("BondBasis", 10000. * bond.bondBasisFromPrice (valParams, csqc, null,
  147.                 dblCleanPrice));
  148.         } catch (java.lang.Exception e) {
  149.             e.printStackTrace();

  150.             return null;
  151.         }

  152.         return jsonResponse;
  153.     }

  154.     /**
  155.      * JSON Based in/out Bond Curve Metrics Thunker
  156.      *
  157.      * @param jsonParameter JSON Bond Request Parameters
  158.      *
  159.      * @return JSON Bond Curve Metrics Response
  160.      */

  161.     @SuppressWarnings ("unchecked") static final org.drip.json.simple.JSONObject CurveMetrics (
  162.         final org.drip.json.simple.JSONObject jsonParameter)
  163.     {
  164.         org.drip.state.discount.MergedDiscountForwardCurve dcFunding =
  165.             org.drip.service.json.LatentStateProcessor.FundingCurve (jsonParameter);

  166.         if (null == dcFunding) return null;

  167.         org.drip.param.market.CurveSurfaceQuoteContainer csqc = new
  168.             org.drip.param.market.CurveSurfaceQuoteContainer();

  169.         if (!csqc.setFundingState (dcFunding) || !csqc.setGovvieState
  170.             (org.drip.service.json.LatentStateProcessor.TreasuryCurve (jsonParameter)))
  171.             return null;

  172.         double dblCleanPrice = java.lang.Double.NaN;
  173.         org.drip.product.credit.BondComponent bond = null;

  174.         org.drip.analytics.date.JulianDate dtMaturity = org.drip.json.parser.Converter.DateEntry
  175.             (jsonParameter, "BondMaturityDate");

  176.         try {
  177.             if (null == (bond = org.drip.product.creator.BondBuilder.CreateSimpleFixed
  178.                 (org.drip.json.parser.Converter.StringEntry (jsonParameter, "BondName"),
  179.                     dcFunding.currency(), "", org.drip.json.parser.Converter.DoubleEntry (jsonParameter,
  180.                         "BondCoupon"), org.drip.json.parser.Converter.IntegerEntry (jsonParameter,
  181.                             "BondFrequency"), org.drip.json.parser.Converter.StringEntry (jsonParameter,
  182.                                 "BondDayCount"), org.drip.json.parser.Converter.DateEntry (jsonParameter,
  183.                                     "BondEffectiveDate"), dtMaturity, null, null)))
  184.                 return null;

  185.             dblCleanPrice = org.drip.json.parser.Converter.DoubleEntry (jsonParameter, "BondCleanPrice");
  186.         } catch (java.lang.Exception e) {
  187.             e.printStackTrace();

  188.             return null;
  189.         }

  190.         int iSpotDate = dcFunding.epoch().julian();

  191.         org.drip.param.valuation.ValuationParams valParams = org.drip.param.valuation.ValuationParams.Spot
  192.             (iSpotDate);

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

  194.         jsonResponse.put ("BondName", bond.name());

  195.         jsonResponse.put ("BondEffectiveDate", bond.effectiveDate().toString());

  196.         jsonResponse.put ("BondMaturityDate", dtMaturity.toString());

  197.         jsonResponse.put ("BondFirstCouponDate", bond.firstCouponDate().toString());

  198.         jsonResponse.put ("BondCleanPrice", dblCleanPrice);

  199.         try {
  200.             jsonResponse.put ("BondASW", bond.aswFromPrice (valParams, csqc, null, dblCleanPrice));
  201.         } catch (java.lang.Exception e) {
  202.             e.printStackTrace();
  203.         }

  204.         try {
  205.             jsonResponse.put ("BondGSpread", bond.gSpreadFromPrice (valParams, csqc, null, dblCleanPrice));
  206.         } catch (java.lang.Exception e) {
  207.             e.printStackTrace();
  208.         }

  209.         try {
  210.             jsonResponse.put ("BondISpread", bond.iSpreadFromPrice (valParams, csqc, null, dblCleanPrice));
  211.         } catch (java.lang.Exception e) {
  212.             e.printStackTrace();
  213.         }

  214.         try {
  215.             jsonResponse.put ("BondOAS", bond.oasFromPrice (valParams, csqc, null, dblCleanPrice));
  216.         } catch (java.lang.Exception e) {
  217.             e.printStackTrace();
  218.         }

  219.         try {
  220.             jsonResponse.put ("BondTreasurySpread", bond.tsySpreadFromPrice (valParams, csqc, null,
  221.                 dblCleanPrice));
  222.         } catch (java.lang.Exception e) {
  223.             e.printStackTrace();
  224.         }

  225.         jsonResponse.put ("BondTreasuryBenchmark", org.drip.analytics.support.Helper.BaseTsyBmk
  226.             (iSpotDate, dtMaturity.julian()));

  227.         try {
  228.             jsonResponse.put ("BondZSpread", bond.zSpreadFromPrice (valParams, csqc, null, dblCleanPrice));
  229.         } catch (java.lang.Exception e) {
  230.             e.printStackTrace();
  231.         }

  232.         return jsonResponse;
  233.     }

  234.     /**
  235.      * JSON Based in/out Bond Curve Cash Flow Thunker
  236.      *
  237.      * @param jsonParameter JSON Bond Cash Flow Parameters
  238.      *
  239.      * @return JSON Bond Cash Flow Response
  240.      */

  241.     @SuppressWarnings ("unchecked") static final org.drip.json.simple.JSONObject CashFlows (
  242.         final org.drip.json.simple.JSONObject jsonParameter)
  243.     {
  244.         org.drip.state.discount.MergedDiscountForwardCurve dcFunding =
  245.             org.drip.service.json.LatentStateProcessor.FundingCurve (jsonParameter);

  246.         if (null == dcFunding) return null;

  247.         org.drip.param.market.CurveSurfaceQuoteContainer csqc = new
  248.             org.drip.param.market.CurveSurfaceQuoteContainer();

  249.         if (!csqc.setFundingState (dcFunding)) return null;

  250.         org.drip.state.credit.CreditCurve cc = org.drip.service.json.LatentStateProcessor.CreditCurve
  251.             (jsonParameter, dcFunding);

  252.         csqc.setCreditState (cc);

  253.         double dblValueNotional = 1.;
  254.         org.drip.product.credit.BondComponent bond = null;

  255.         org.drip.analytics.date.JulianDate dtMaturity = org.drip.json.parser.Converter.DateEntry
  256.             (jsonParameter, "BondMaturityDate");

  257.         try {
  258.             if (null == (bond = org.drip.product.creator.BondBuilder.CreateSimpleFixed
  259.                 (org.drip.json.parser.Converter.StringEntry (jsonParameter, "BondName"),
  260.                     dcFunding.currency(), "", org.drip.json.parser.Converter.DoubleEntry (jsonParameter,
  261.                         "BondCoupon"), org.drip.json.parser.Converter.IntegerEntry (jsonParameter,
  262.                             "BondFrequency"), org.drip.json.parser.Converter.StringEntry (jsonParameter,
  263.                                 "BondDayCount"), org.drip.json.parser.Converter.DateEntry (jsonParameter,
  264.                                     "BondEffectiveDate"), dtMaturity, null, null)))
  265.                 return null;

  266.             dblValueNotional = org.drip.json.parser.Converter.DoubleEntry (jsonParameter,
  267.                 "BondValueNotional");
  268.         } catch (java.lang.Exception e) {
  269.             e.printStackTrace();

  270.             return null;
  271.         }

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

  273.         jsonResponse.put ("BondName", bond.name());

  274.         java.util.List<org.drip.analytics.cashflow.CompositePeriod> lsCP = bond.couponPeriods();

  275.         if (null == lsCP || 0 == lsCP.size()) return null;

  276.         org.drip.json.simple.JSONArray jsonCashFlowArray = new org.drip.json.simple.JSONArray();

  277.         for (org.drip.analytics.cashflow.CompositePeriod cp : lsCP) {
  278.             if (null == cp) return null;

  279.             org.drip.json.simple.JSONObject jsonCashFlow = new org.drip.json.simple.JSONObject();

  280.             jsonCashFlow.put ("StartDate", new org.drip.analytics.date.JulianDate
  281.                 (cp.startDate()).toString());

  282.             jsonCashFlow.put ("EndDate", new org.drip.analytics.date.JulianDate (cp.endDate()).toString());

  283.             jsonCashFlow.put ("PayDate", new org.drip.analytics.date.JulianDate (cp.payDate()).toString());

  284.             try {
  285.                 double dblCouponRate = cp.periods().get (0).baseRate (csqc);

  286.                 jsonCashFlow.put ("FixingDate", new org.drip.analytics.date.JulianDate
  287.                     (cp.fxFixingDate()).toString());

  288.                 jsonCashFlow.put ("CouponDCF", cp.couponDCF());

  289.                 jsonCashFlow.put ("CouponRate", dblCouponRate);

  290.                 if (null != cc) jsonCashFlow.put ("SurvivalFactor", cc.survival (cp.payDate()));

  291.                 jsonCashFlow.put ("PayDiscountFactor", cp.df (csqc));

  292.                 jsonCashFlow.put ("CouponAmount", dblCouponRate * dblValueNotional);
  293.             } catch (java.lang.Exception e) {
  294.                 e.printStackTrace();

  295.                 return null;
  296.             }

  297.             jsonCashFlow.put ("BaseNotional", cp.baseNotional() * dblValueNotional);

  298.             jsonCashFlowArray.add (jsonCashFlow);
  299.         }

  300.         jsonResponse.put ("CashFlow", jsonCashFlowArray);

  301.         return jsonResponse;
  302.     }
  303. }