BondRefData.java

  1. package org.drip.feed.loader;

  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>BondRefData</i> contains functionality to load a variety of Bond Product reference data and closing
  84.  * marks. It exposes the following functionality:
  85.  * <ul>
  86.  *  <li>
  87.  *      Load the bond valuation-based reference data, amortization schedule and EOS
  88.  *  </li>
  89.  *  <li>
  90.  *      Build the bond instance entities from the valuation-based reference data
  91.  *  </li>
  92.  *  <li>
  93.  *      Load the bond non-valuation-based reference data
  94.  *  </li>
  95.  * </ul>
  96.  *
  97.  * BondRefData assumes the appropriate connections are available to load the data.
  98.  *
  99.  *  <br><br>
  100.  *  <ul>
  101.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ComputationalCore.md">Computational Core Module</a></li>
  102.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ComputationSupportLibrary.md">Computation Support</a></li>
  103.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/feed/README.md">Load, Transform, and compute Target Metrics across Feeds</a></li>
  104.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/feed/loader/README.md">Reference/Market Data Feed Loader</a></li>
  105.  *  </ul>
  106.  *
  107.  * @author Lakshmi Krishnamurthy
  108.  */

  109. class BondRefData {
  110.     private static final boolean m_bBlog = false;
  111.     private static final boolean m_bDBExec = true;

  112.     private static final org.drip.product.creator.BondRefDataBuilder MakeBRDB (
  113.         final java.lang.String[] astrFODATA)
  114.     {
  115.         if (null == astrFODATA || 88 != astrFODATA.length) return null;

  116.         org.drip.product.creator.BondRefDataBuilder brdb = new org.drip.product.creator.BondRefDataBuilder();

  117.         if (!brdb.setISIN (astrFODATA[40])) {
  118.             System.out.println ("Bad ISIN " + astrFODATA[40]);

  119.             return null;
  120.         }

  121.         if (!brdb.setCUSIP (astrFODATA[42])) {
  122.             System.out.println ("Bad CUSIP " + astrFODATA[42]);

  123.             return null;
  124.         }

  125.         brdb.setBBGID (astrFODATA[1]);

  126.         brdb.setIssuerCategory (astrFODATA[2]);

  127.         brdb.setTicker (astrFODATA[3]);

  128.         brdb.setSeries (astrFODATA[7]);

  129.         brdb.setName (astrFODATA[8]);

  130.         brdb.setShortName (astrFODATA[9]);

  131.         brdb.setIssuerIndustry (astrFODATA[10]);

  132.         brdb.setCouponType (astrFODATA[13]);

  133.         brdb.setMaturityType (astrFODATA[14]);

  134.         brdb.setCalculationType (astrFODATA[15]);

  135.         brdb.setDayCountCode (astrFODATA[16]);

  136.         brdb.setMarketIssueType (astrFODATA[17]);

  137.         brdb.setIssueCountryCode (astrFODATA[18]);

  138.         brdb.setIssueCountry (astrFODATA[19]);

  139.         brdb.setCollateralType (astrFODATA[20]);

  140.         brdb.setIssueAmount (astrFODATA[21]);

  141.         brdb.setOutstandingAmount (astrFODATA[22]);

  142.         brdb.setMinimumPiece (astrFODATA[23]);

  143.         brdb.setMinimumIncrement (astrFODATA[24]);

  144.         brdb.setParAmount (astrFODATA[25]);

  145.         brdb.setLeadManager (astrFODATA[26]);

  146.         brdb.setExchangeCode (astrFODATA[27]);

  147.         brdb.setRedemptionValue (astrFODATA[28]);

  148.         brdb.setAnnounce (astrFODATA[29]);

  149.         brdb.setFirstSettle (astrFODATA[31]);

  150.         brdb.setFirstCoupon (astrFODATA[33]);

  151.         brdb.setInterestAccrualStart (astrFODATA[35]);

  152.         brdb.setIssue (astrFODATA[37]);

  153.         brdb.setIssuePrice (astrFODATA[39]);

  154.         brdb.setNextCouponDate (astrFODATA[43]);

  155.         brdb.setIsCallable (astrFODATA[45]);

  156.         brdb.setIsSinkable (astrFODATA[46]);

  157.         brdb.setIsPutable (astrFODATA[47]);

  158.         brdb.setBBGParent (astrFODATA[48]);

  159.         brdb.setCountryOfIncorporation (astrFODATA[53]);

  160.         brdb.setIndustrySector (astrFODATA[54]);

  161.         brdb.setIndustryGroup (astrFODATA[55]);

  162.         brdb.setIndustrySubgroup (astrFODATA[56]);

  163.         brdb.setCountryOfGuarantor (astrFODATA[57]);

  164.         brdb.setCountryOfDomicile (astrFODATA[58]);

  165.         brdb.setDescription (astrFODATA[59]);

  166.         brdb.setSecurityType (astrFODATA[60]);

  167.         brdb.setPrevCouponDate (astrFODATA[61]);

  168.         brdb.setBBGUniqueID (astrFODATA[63]);

  169.         brdb.setLongCompanyName (astrFODATA[64]);

  170.         brdb.setRedemptionCurrency (astrFODATA[66]);

  171.         brdb.setCouponCurrency (astrFODATA[67]);

  172.         brdb.setIsStructuredNote (astrFODATA[68]);

  173.         brdb.setIsUnitTraded (astrFODATA[69]);

  174.         brdb.setIsReversibleConvertible (astrFODATA[70]);

  175.         brdb.setTradeCurrency (astrFODATA[71]);

  176.         brdb.setIsBearer (astrFODATA[72]);

  177.         brdb.setIsRegistered (astrFODATA[73]);

  178.         brdb.setHasBeenCalled (astrFODATA[74]);

  179.         brdb.setIssuer (astrFODATA[75]);

  180.         brdb.setPenultimateCouponDate (astrFODATA[76]);

  181.         brdb.setFloatCouponConvention (astrFODATA[77]);

  182.         brdb.setCurrentCoupon (astrFODATA[78]);

  183.         brdb.setIsFloater (astrFODATA[79]);

  184.         brdb.setTradeStatus (astrFODATA[80]);

  185.         brdb.setCDRCountryCode (astrFODATA[81]);

  186.         brdb.setCDRSettleCode (astrFODATA[82]);

  187.         brdb.setFinalMaturity (astrFODATA[83]);

  188.         brdb.setIsPrivatePlacement (astrFODATA[85]);

  189.         brdb.setIsPerpetual (astrFODATA[86]);

  190.         brdb.setIsDefaulted (astrFODATA[87]);

  191.         if (!brdb.validate()) return null;

  192.         return brdb;
  193.     }

  194.     private static final org.drip.product.creator.BondProductBuilder MakeBPB (
  195.         final java.lang.String[] astrFODATA,
  196.         final org.drip.param.definition.ScenarioMarketParams mpc)
  197.     {
  198.         if (null == astrFODATA || 88 != astrFODATA.length || null == mpc) return null;

  199.         org.drip.product.creator.BondProductBuilder bpb = new org.drip.product.creator.BondProductBuilder();

  200.         if (!bpb.setISIN (astrFODATA[40])) {
  201.             System.out.println ("Bad ISIN " + astrFODATA[40]);

  202.             return null;
  203.         }

  204.         if (!bpb.setCUSIP (astrFODATA[42])) {
  205.             System.out.println ("Bad CUSIP " + astrFODATA[42]);

  206.             return null;
  207.         }

  208.         bpb.setTicker (astrFODATA[3]);

  209.         bpb.setCoupon (astrFODATA[4]);

  210.         if (!bpb.setMaturity (astrFODATA[5])) {
  211.             System.out.println ("Bad Maturity " + astrFODATA[5]);

  212.             return null;
  213.         }

  214.         if (!bpb.setCouponFreq (astrFODATA[12])) {
  215.             System.out.println ("Bad Cpn Freq " + astrFODATA[12]);

  216.             return null;
  217.         }

  218.         bpb.setCouponType (astrFODATA[13]);

  219.         bpb.setMaturityType (astrFODATA[14]);

  220.         bpb.setCalculationType (astrFODATA[15]);

  221.         if (!bpb.setDayCountCode (astrFODATA[16])) {
  222.             System.out.println ("Bad Day Count " + astrFODATA[40]);

  223.             return null;
  224.         }

  225.         if (!bpb.setRedemptionValue (astrFODATA[28])) {
  226.             System.out.println ("Bad Redemp Value " + astrFODATA[40]);

  227.             return null;
  228.         }

  229.         bpb.setAnnounce (astrFODATA[29]);

  230.         bpb.setFirstSettle (astrFODATA[31]);

  231.         bpb.setFirstCoupon (astrFODATA[33]);

  232.         bpb.setInterestAccrualStart (astrFODATA[35]);

  233.         bpb.setIssue (astrFODATA[37]);

  234.         bpb.setIsCallable (astrFODATA[45]);

  235.         bpb.setIsSinkable (astrFODATA[46]);

  236.         bpb.setIsPutable (astrFODATA[47]);

  237.         if (!bpb.setRedemptionCurrency (astrFODATA[66])) {
  238.             System.out.println ("Bad Redemp Ccy " + astrFODATA[66]);

  239.             return null;
  240.         }

  241.         if (!bpb.setCouponCurrency (astrFODATA[67])) {
  242.             System.out.println ("Bad Cpn Ccy " + astrFODATA[40]);

  243.             return null;
  244.         }

  245.         if (!bpb.setTradeCurrency (astrFODATA[71])) {
  246.             System.out.println ("Bad Trade ccy " + astrFODATA[40]);

  247.             return null;
  248.         }

  249.         bpb.setHasBeenCalled (astrFODATA[74]);

  250.         bpb.setFloatCouponConvention (astrFODATA[77]);

  251.         bpb.setCurrentCoupon (astrFODATA[78]);

  252.         bpb.setIsFloater (astrFODATA[79]);

  253.         bpb.setFinalMaturity (astrFODATA[83]);

  254.         bpb.setIsPerpetual (astrFODATA[86]);

  255.         bpb.setIsDefaulted (astrFODATA[87]);

  256.         if (!bpb.validate (mpc)) return null;

  257.         return bpb;
  258.     }

  259.     public static final void UploadBondFromFODATA (
  260.         final java.lang.String strFODATAFile,
  261.         final java.sql.Statement stmt,
  262.         final org.drip.param.definition.ScenarioMarketParams mpc)
  263.         throws java.lang.Exception
  264.     {
  265.         int iNumBonds = 0;
  266.         int iNumFloaters = 0;
  267.         int iNumFailedToLoad = 0;
  268.         java.lang.String strBondFODATALine = "";

  269.         java.io.BufferedReader inBondFODATA = new java.io.BufferedReader (new java.io.FileReader
  270.             (strFODATAFile));

  271.         while (null != (strBondFODATALine = inBondFODATA.readLine())) {
  272.             ++iNumBonds;

  273.             java.lang.String[] astrBondFODATARecord = strBondFODATALine.split (",");

  274.             org.drip.product.creator.BondRefDataBuilder brdb = MakeBRDB (astrBondFODATARecord);

  275.             if (null != brdb) {
  276.                 System.out.println ("Doing #" + iNumBonds + ": " + brdb._strCUSIP);

  277.                 java.lang.String strSQLBRDBDelete = brdb.makeSQLDelete();

  278.                 if (null != strSQLBRDBDelete) {
  279.                     if (m_bBlog) System.out.println (strSQLBRDBDelete);

  280.                     if (m_bDBExec) stmt.executeUpdate (strSQLBRDBDelete);
  281.                 }

  282.                 java.lang.String strSQLBRDBInsert = brdb.makeSQLInsert();

  283.                 if (null != strSQLBRDBInsert) {
  284.                     if (m_bBlog) System.out.println (strSQLBRDBInsert);

  285.                     if (m_bDBExec) stmt.executeUpdate (strSQLBRDBInsert);
  286.                 }
  287.             }

  288.             org.drip.product.creator.BondProductBuilder bpb = MakeBPB (astrBondFODATARecord, mpc);

  289.             if (null != bpb) {
  290.                 if (null != bpb.getFloaterParams()) ++iNumFloaters;

  291.                 java.lang.String strSQLBPBDelete = bpb.makeSQLDelete();

  292.                 if (null != strSQLBPBDelete) {
  293.                     if (m_bBlog) System.out.println (strSQLBPBDelete);

  294.                     if (m_bDBExec) stmt.executeUpdate (strSQLBPBDelete);
  295.                 }

  296.                 java.lang.String strSQLBPBInsert = bpb.makeSQLInsert();

  297.                 if (null != strSQLBPBInsert) {
  298.                     if (m_bBlog) System.out.println (strSQLBPBInsert);

  299.                     if (m_bDBExec) stmt.executeUpdate (strSQLBPBInsert);
  300.                 }
  301.             }

  302.             if (null == brdb || null == bpb) ++iNumFailedToLoad;
  303.         }

  304.         inBondFODATA.close();

  305.         System.out.println (iNumFailedToLoad + " out of " + iNumBonds + " failed to load");

  306.         System.out.println ("There were " + iNumFloaters + " floaters!");
  307.     }
  308. }