SingleStreamComponentBuilder.java

  1. package org.drip.product.creator;

  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>SingleStreamComponentBuilder</i> contains the suite of helper functions for creating the Futures
  84.  * product and product pack from the parameters/codes/byte array streams. It also contains function to
  85.  * construct EDF codes and the EDF product from code.
  86.  *
  87.  *  <br><br>
  88.  *  <ul>
  89.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ProductCore.md">Product Core Module</a></li>
  90.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/FixedIncomeAnalyticsLibrary.md">Fixed Income Analytics</a></li>
  91.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/product/README.md">Product Components/Baskets for Credit, FRA, FX, Govvie, Rates, and Option AssetClasses</a></li>
  92.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/product/creator/README.md">Streams and Products Construction Utilities</a></li>
  93.  *  </ul>
  94.  * <br><br>
  95.  *
  96.  * @author Lakshmi Krishnamurthy
  97.  */

  98. public class SingleStreamComponentBuilder {

  99.     /**
  100.      * Construct the Forward Rate Futures Code given a Effective Date
  101.      *
  102.      * @param strPrefix The Forward Rate Futures Code Prefix
  103.      * @param iEffectiveDate Double representing the Effective JulianDate
  104.      *
  105.      * @return The Forward Rate Futures Code
  106.      */

  107.     public static java.lang.String ForwardRateFuturesCode (
  108.         final java.lang.String strPrefix,
  109.         final int iEffectiveDate)
  110.     {
  111.         try {
  112.             return strPrefix + org.drip.analytics.date.DateUtil.CodeFromMonth
  113.                 (org.drip.analytics.date.DateUtil.Month (iEffectiveDate)) +
  114.                     (org.drip.analytics.date.DateUtil.Year (iEffectiveDate) % 10);
  115.         } catch (java.lang.Exception e) {
  116.             e.printStackTrace();
  117.         }

  118.         return null;
  119.     }

  120.     /**
  121.      * Generate a Forward Rate Futures Pack corresponding to the Specified Number of Contracts
  122.      *
  123.      * @param dtSpot Spot Date specifying the Contract Issue
  124.      * @param iNumContract Number of Contracts
  125.      * @param strCurrency Contract Currency String
  126.      *
  127.      * @return Array of Forward Rate Futures
  128.      */

  129.     public static org.drip.product.rates.SingleStreamComponent[] ForwardRateFuturesPack (
  130.         final org.drip.analytics.date.JulianDate dtSpot,
  131.         final int iNumContract,
  132.         final java.lang.String strCurrency)
  133.     {
  134.         if (null == dtSpot || 0 >= iNumContract || null == strCurrency || strCurrency.isEmpty()) return null;

  135.         org.drip.product.rates.SingleStreamComponent[] aSSC = new
  136.             org.drip.product.rates.SingleStreamComponent[iNumContract];

  137.         try {
  138.             org.drip.param.period.ComposableFloatingUnitSetting cfus = new
  139.                 org.drip.param.period.ComposableFloatingUnitSetting ("3M",
  140.                     org.drip.analytics.support.CompositePeriodBuilder.EDGE_DATE_SEQUENCE_SINGLE, null,
  141.                         org.drip.state.identifier.ForwardLabel.Standard (strCurrency + "-3M"),
  142.                             org.drip.analytics.support.CompositePeriodBuilder.REFERENCE_PERIOD_IN_ADVANCE,
  143.                                 0.);

  144.             org.drip.param.period.CompositePeriodSetting cps = new
  145.                 org.drip.param.period.CompositePeriodSetting (4, "3M", strCurrency, null, 1., null, null,
  146.                     null, null);

  147.             org.drip.param.valuation.CashSettleParams csp = new org.drip.param.valuation.CashSettleParams (0,
  148.                 strCurrency, 0);

  149.             org.drip.analytics.date.JulianDate dtStart = dtSpot.nextRatesFuturesIMM (3);

  150.             for (int i = 0; i < iNumContract; ++i) {
  151.                 org.drip.analytics.date.JulianDate dtMaturity = dtStart.addMonths (3);

  152.                 aSSC[i] = new org.drip.product.rates.SingleStreamComponent ("FUTURE_" + i, new
  153.                     org.drip.product.rates.Stream
  154.                         (org.drip.analytics.support.CompositePeriodBuilder.FloatingCompositeUnit
  155.                             (org.drip.analytics.support.CompositePeriodBuilder.EdgePair (dtStart,
  156.                                 dtMaturity), cps, cfus)), csp);

  157.                 aSSC[i].setPrimaryCode (ForwardRateFuturesCode ("USD".equalsIgnoreCase (strCurrency) ? "ED" :
  158.                     strCurrency, dtStart.julian()));

  159.                 dtStart = dtMaturity;
  160.             }
  161.         } catch (java.lang.Exception e) {
  162.             e.printStackTrace();

  163.             return null;
  164.         }

  165.         return aSSC;
  166.     }

  167.     /**
  168.      * Create a Deposit Product from the Effective and the Maturity Dates, and the Forward Label
  169.      *
  170.      * @param dtEffective Effective date
  171.      * @param dtMaturity Maturity
  172.      * @param fri The Floating Rate Index
  173.      *
  174.      * @return Deposit product
  175.      */

  176.     public static final org.drip.product.rates.SingleStreamComponent Deposit (
  177.         final org.drip.analytics.date.JulianDate dtEffective,
  178.         final org.drip.analytics.date.JulianDate dtMaturity,
  179.         final org.drip.state.identifier.ForwardLabel fri)
  180.     {
  181.         java.lang.String strTenor = fri.tenor();

  182.         java.lang.String strCurrency = fri.currency();

  183.         boolean bIsON = "ON".equalsIgnoreCase (strTenor);

  184.         java.lang.String strCode = "DEPOSIT::" + fri.fullyQualifiedName() + "::{" + dtEffective + "->" +
  185.             dtMaturity + "}";

  186.         try {
  187.             int iFreq = bIsON ? 360 : org.drip.analytics.support.Helper.TenorToFreq (strTenor);

  188.             org.drip.param.period.ComposableFloatingUnitSetting cfus = new
  189.                 org.drip.param.period.ComposableFloatingUnitSetting (strTenor, bIsON ?
  190.                     org.drip.analytics.support.CompositePeriodBuilder.EDGE_DATE_SEQUENCE_OVERNIGHT :
  191.                         org.drip.analytics.support.CompositePeriodBuilder.EDGE_DATE_SEQUENCE_SINGLE, null,
  192.                             fri,
  193.                                 org.drip.analytics.support.CompositePeriodBuilder.REFERENCE_PERIOD_IN_ADVANCE,
  194.                 0.);

  195.             org.drip.param.period.CompositePeriodSetting cps = new
  196.                 org.drip.param.period.CompositePeriodSetting (iFreq, strTenor, strCurrency,
  197.                     fri.floaterIndex().spotLagDAPForward(), 1., null, null, null, null);

  198.             org.drip.product.rates.SingleStreamComponent sscDeposit = new
  199.                 org.drip.product.rates.SingleStreamComponent (strCode, new org.drip.product.rates.Stream
  200.                     (org.drip.analytics.support.CompositePeriodBuilder.FloatingCompositeUnit
  201.                         (org.drip.analytics.support.CompositePeriodBuilder.EdgePair (dtEffective,
  202.                             dtMaturity), cps, cfus)), new org.drip.param.valuation.CashSettleParams (0,
  203.                                 strCurrency, 0));

  204.             sscDeposit.setPrimaryCode (strCode);

  205.             return sscDeposit;
  206.         } catch (java.lang.Exception e) {
  207.             e.printStackTrace();
  208.         }

  209.         return null;
  210.     }

  211.     /**
  212.      * Create a Standard FRA from the Spot Date, the Forward Label, and the Strike
  213.      *
  214.      * @param dtForwardStart Forward Start Date
  215.      * @param forwardLabel The Floating Rate Index
  216.      * @param dblStrike Futures Strike
  217.      *
  218.      * @return The Standard FRA Instance
  219.      */

  220.     public static final org.drip.product.fra.FRAStandardComponent FRAStandard (
  221.         final org.drip.analytics.date.JulianDate dtForwardStart,
  222.         final org.drip.state.identifier.ForwardLabel forwardLabel,
  223.         final double dblStrike)
  224.     {
  225.         if (null == dtForwardStart || null == forwardLabel) return null;

  226.         java.lang.String strCurrency = forwardLabel.currency();

  227.         org.drip.analytics.date.JulianDate dtEffective = null;

  228.         org.drip.analytics.daycount.DateAdjustParams dapEffective =
  229.             forwardLabel.floaterIndex().spotLagDAPForward();

  230.         try {
  231.             dtEffective = null == dapEffective ? dtForwardStart : new org.drip.analytics.date.JulianDate
  232.                 (dapEffective.roll (dtForwardStart.julian()));
  233.         } catch (java.lang.Exception e) {
  234.             e.printStackTrace();

  235.             return null;
  236.         }

  237.         java.lang.String strTenor = forwardLabel.tenor();

  238.         boolean bIsON = "ON".equalsIgnoreCase (strTenor);

  239.         org.drip.analytics.date.JulianDate dtMaturity = dtEffective.addTenor (strTenor);
  240.        
  241.         java.lang.String strCode = (0 == dblStrike ? "FUTURES::" : "FRA::") +
  242.             forwardLabel.fullyQualifiedName() + "::{" + dtEffective + "->" + dtMaturity + "}";

  243.         try {
  244.             int iFreq = bIsON ? 360 : 12 / org.drip.analytics.support.Helper.TenorToMonths (strTenor);

  245.             org.drip.param.period.ComposableFloatingUnitSetting cfus = new
  246.                 org.drip.param.period.ComposableFloatingUnitSetting (strTenor, bIsON ?
  247.                     org.drip.analytics.support.CompositePeriodBuilder.EDGE_DATE_SEQUENCE_OVERNIGHT :
  248.                         org.drip.analytics.support.CompositePeriodBuilder.EDGE_DATE_SEQUENCE_SINGLE, null,
  249.                             forwardLabel,
  250.                                 org.drip.analytics.support.CompositePeriodBuilder.REFERENCE_PERIOD_IN_ADVANCE,
  251.                 0.);

  252.             org.drip.param.period.CompositePeriodSetting cps = new
  253.                 org.drip.param.period.CompositePeriodSetting (iFreq, strTenor, strCurrency, null, 1., null,
  254.                     null, null, null);

  255.             org.drip.product.fra.FRAStandardComponent sscDeposit = new
  256.                 org.drip.product.fra.FRAStandardComponent (strCode, new org.drip.product.rates.Stream
  257.                     (org.drip.analytics.support.CompositePeriodBuilder.FloatingCompositeUnit
  258.                         (org.drip.analytics.support.CompositePeriodBuilder.EdgePair (dtEffective,
  259.                             dtMaturity), cps, cfus)), dblStrike, new
  260.                                 org.drip.param.valuation.CashSettleParams (0, strCurrency, 0));

  261.             sscDeposit.setPrimaryCode (strCode);

  262.             return sscDeposit;
  263.         } catch (java.lang.Exception e) {
  264.             e.printStackTrace();
  265.         }

  266.         return null;
  267.     }

  268.     /**
  269.      * Create a FRA Market Component Instance from the Spot Date, the Forward Label, and the Strike
  270.      *
  271.      * @param dtForwardStart Forward Start Date
  272.      * @param forwardLabel The Floating Rate Index
  273.      * @param dblStrike Futures Strike
  274.      *
  275.      * @return The Futures Product
  276.      */

  277.     public static final org.drip.product.fra.FRAMarketComponent FRAMarket (
  278.         final org.drip.analytics.date.JulianDate dtForwardStart,
  279.         final org.drip.state.identifier.ForwardLabel forwardLabel,
  280.         final double dblStrike)
  281.     {
  282.         if (null == dtForwardStart || null == forwardLabel) return null;

  283.         org.drip.analytics.date.JulianDate dtEffective = dtForwardStart;

  284.         java.lang.String strTenor = forwardLabel.tenor();

  285.         java.lang.String strCurrency = forwardLabel.currency();

  286.         boolean bIsON = "ON".equalsIgnoreCase (strTenor);

  287.         org.drip.analytics.date.JulianDate dtMaturity = dtEffective.addTenor (strTenor);
  288.        
  289.         java.lang.String strCode = "FUTURES::" + forwardLabel.fullyQualifiedName() + "::{" + dtEffective +
  290.             "->" + dtMaturity + "}";

  291.         try {
  292.             int iFreq = org.drip.analytics.support.Helper.TenorToFreq (strTenor);

  293.             org.drip.param.period.ComposableFloatingUnitSetting cfus = new
  294.                 org.drip.param.period.ComposableFloatingUnitSetting (strTenor, bIsON ?
  295.                     org.drip.analytics.support.CompositePeriodBuilder.EDGE_DATE_SEQUENCE_OVERNIGHT :
  296.                         org.drip.analytics.support.CompositePeriodBuilder.EDGE_DATE_SEQUENCE_SINGLE, null,
  297.                             forwardLabel,
  298.                                 org.drip.analytics.support.CompositePeriodBuilder.REFERENCE_PERIOD_IN_ADVANCE,
  299.                 0.);

  300.             org.drip.param.period.CompositePeriodSetting cps = new
  301.                 org.drip.param.period.CompositePeriodSetting (iFreq, strTenor, strCurrency, null, 1., null,
  302.                     null, null, null);

  303.             org.drip.product.fra.FRAMarketComponent sscDeposit = new org.drip.product.fra.FRAMarketComponent
  304.                 (strCode, new org.drip.product.rates.Stream
  305.                     (org.drip.analytics.support.CompositePeriodBuilder.FloatingCompositeUnit
  306.                         (org.drip.analytics.support.CompositePeriodBuilder.EdgePair (dtEffective,
  307.                             dtMaturity), cps, cfus)), dblStrike, new
  308.                                 org.drip.param.valuation.CashSettleParams (0, strCurrency, 0));

  309.             sscDeposit.setPrimaryCode (strCode);

  310.             return sscDeposit;
  311.         } catch (java.lang.Exception e) {
  312.             e.printStackTrace();
  313.         }

  314.         return null;
  315.     }

  316.     /**
  317.      * Create a Forward Rate Futures Product Instance from the Spot Date and the Forward Label
  318.      *
  319.      * @param dtSpot Spot Date
  320.      * @param fri The Floating Rate Index
  321.      *
  322.      * @return The Forward Rate Futures Product Instance
  323.      */

  324.     public static final org.drip.product.fra.FRAStandardComponent ForwardRateFutures (
  325.         final org.drip.analytics.date.JulianDate dtSpot,
  326.         final org.drip.state.identifier.ForwardLabel fri)
  327.     {
  328.         return FRAStandard (dtSpot, fri, 0.);
  329.     }
  330. }