ExchangeInstrumentBuilder.java

  1. package org.drip.service.template;

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

  78. /**
  79.  * <i>ExchangeInstrumentBuilder</i> contains static Helper API to facilitate Construction of Exchange-traded
  80.  * Instruments.
  81.  *
  82.  * <br><br>
  83.  *  <ul>
  84.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ComputationalCore.md">Computational Core Module</a></li>
  85.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ComputationSupportLibrary.md">Computation Support</a></li>
  86.  *      <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>
  87.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/service/template/README.md">Curve Construction Product Builder Templates</a></li>
  88.  *  </ul>
  89.  * <br><br>
  90.  *
  91.  * @author Lakshmi Krishnamurthy
  92.  */

  93. public class ExchangeInstrumentBuilder {

  94.     /**
  95.      * Generate a Forward Rate Futures Contract corresponding to the Spot Date
  96.      *
  97.      * @param dtSpot Spot date specifying the contract issue
  98.      * @param strCurrency Contract Currency
  99.      *
  100.      * @return Forward Rate Futures Component
  101.      */

  102.     public static org.drip.product.rates.SingleStreamComponent ForwardRateFutures (
  103.         final org.drip.analytics.date.JulianDate dtSpot,
  104.         final java.lang.String strCurrency)
  105.     {
  106.         if (null == dtSpot) return null;

  107.         org.drip.product.rates.SingleStreamComponent[] aFutures =
  108.             org.drip.product.creator.SingleStreamComponentBuilder.ForwardRateFuturesPack (dtSpot.addBusDays
  109.                 (0, strCurrency), 1, strCurrency);

  110.         return null == aFutures || 1 != aFutures.length ? null : aFutures[0];
  111.     }

  112.     /**
  113.      * Generate a Forward Rate Futures Pack corresponding to the Spot Date and the Specified Number of
  114.      *  Contracts
  115.      *
  116.      * @param dtSpot Spot date specifying the contract issue
  117.      * @param iNumContract Number of contracts
  118.      * @param strCurrency Contract currency
  119.      *
  120.      * @return Array containing the Forward Rate Futures Pack
  121.      */

  122.     public static org.drip.product.rates.SingleStreamComponent[] ForwardRateFuturesPack (
  123.         final org.drip.analytics.date.JulianDate dtSpot,
  124.         final int iNumContract,
  125.         final java.lang.String strCurrency)
  126.     {
  127.         return null == dtSpot ? null :
  128.             org.drip.product.creator.SingleStreamComponentBuilder.ForwardRateFuturesPack (dtSpot.addBusDays
  129.                 (0, strCurrency), iNumContract, strCurrency);
  130.     }

  131.     /**
  132.      * Generate an Instance of Treasury Futures given the Inputs
  133.      *
  134.      * @param dtSpot The Futures Spot Date
  135.      * @param strCode The Treasury Code
  136.      * @param adtEffective Array of Effective Dates
  137.      * @param adtMaturity Array of Maturity Dates
  138.      * @param adblCoupon Array of Coupons
  139.      * @param adblConversionFactor The Bond Conversion Factor
  140.      * @param strUnderlierType The Underlier Type, e.g., TREASURY
  141.      * @param strUnderlierSubtype The Futures Underlier Sub-type, i.e., BONDS
  142.      * @param strMaturityTenor The Futures Maturity Tenor
  143.      *
  144.      * @return The Treasury Futures Instance
  145.      */

  146.     public static org.drip.product.govvie.TreasuryFutures TreasuryFutures (
  147.         final org.drip.analytics.date.JulianDate dtSpot,
  148.         final java.lang.String strCode,
  149.         final org.drip.analytics.date.JulianDate[] adtEffective,
  150.         final org.drip.analytics.date.JulianDate[] adtMaturity,
  151.         final double[] adblCoupon,
  152.         final double[] adblConversionFactor,
  153.         final java.lang.String strUnderlierType,
  154.         final java.lang.String strUnderlierSubtype,
  155.         final java.lang.String strMaturityTenor)
  156.     {
  157.         if (null == dtSpot) return null;

  158.         try {
  159.             org.drip.product.govvie.TreasuryFutures tsyFutures = new org.drip.product.govvie.TreasuryFutures
  160.                 (org.drip.service.template.TreasuryBuilder.FromCode (strCode, adtEffective, adtMaturity,
  161.                     adblCoupon), adblConversionFactor, null);

  162.             java.lang.String strCurrency = tsyFutures.basket()[0].currency();

  163.             if (!tsyFutures.setExpiry (dtSpot.addBusDays (0, strCurrency).nextBondFuturesIMM (3,
  164.                 strCurrency)))
  165.                 return null;

  166.             tsyFutures.setType (strCode);

  167.             org.drip.market.exchange.TreasuryFuturesConvention bfc =
  168.                 org.drip.market.exchange.TreasuryFuturesConventionContainer.FromJurisdictionTypeMaturity
  169.                     (strCurrency, strUnderlierType, strUnderlierSubtype, strMaturityTenor);

  170.             if (null == bfc) return tsyFutures;

  171.             double dblBasketNotional = bfc.basketNotional();

  172.             double dblMinimumPriceMovement = bfc.minimumPriceMovement();

  173.             tsyFutures.setNotionalValue (dblBasketNotional);

  174.             tsyFutures.setMinimumPriceMovement (dblMinimumPriceMovement);

  175.             tsyFutures.setTickValue (dblBasketNotional * dblMinimumPriceMovement);

  176.             org.drip.market.exchange.TreasuryFuturesEligibility bfe = bfc.eligibility();

  177.             if (null != bfe) {
  178.                 tsyFutures.setMaximumMaturity (bfe.maturityCeiling());

  179.                 tsyFutures.setMinimumMaturity (bfe.maturityFloor());
  180.             }

  181.             org.drip.market.exchange.TreasuryFuturesSettle bfs = bfc.settle();

  182.             if (null != bfs) {
  183.                 tsyFutures.setReferenceCoupon (bfs.currentReferenceYield());

  184.                 tsyFutures.setLastTradingDayLag (bfs.expiryLastTradingLag());

  185.                 tsyFutures.setDeliveryMonths (bfs.deliveryMonths());
  186.             }

  187.             return tsyFutures;
  188.         } catch (java.lang.Exception e) {
  189.             e.printStackTrace();
  190.         }

  191.         return null;
  192.     }

  193.     /**
  194.      * Generate the Treasury Futures Instance
  195.      *
  196.      * @param dtSpot The Spot Date Instance
  197.      * @param strFuturesCode The Treasury Futures Code
  198.      * @param aiFuturesComponentTreasuryEffectiveDate Array of the Treasury Futures Component Effective Date
  199.      * @param aiFuturesComponentTreasuryMaturityDate Array of the Treasury Futures Component Maturity Date
  200.      * @param adblFuturesComponentTreasuryCoupon Array of the Treasury Futures Component Coupon
  201.      * @param adblFuturesComponentConversionFactor Array of the Treasury Futures Component Conversion Factor
  202.      * @param strFuturesComponentUnderlierSubtype Treasury Futures Component Underlier SubType (BILL/BOND)
  203.      * @param strFuturesReferenceMaturityTenor Treasury Futures Component Reference Maturity Tenor
  204.      *
  205.      * @return The Treasury Futures Instance
  206.      */

  207.     public static final org.drip.product.govvie.TreasuryFutures TreasuryFutures (
  208.         final org.drip.analytics.date.JulianDate dtSpot,
  209.         final java.lang.String strFuturesCode,
  210.         final int[] aiFuturesComponentTreasuryEffectiveDate,
  211.         final int[] aiFuturesComponentTreasuryMaturityDate,
  212.         final double[] adblFuturesComponentTreasuryCoupon,
  213.         final double[] adblFuturesComponentConversionFactor,
  214.         final java.lang.String strFuturesComponentUnderlierSubtype,
  215.         final java.lang.String strFuturesReferenceMaturityTenor)
  216.     {
  217.         if (null == dtSpot || null == aiFuturesComponentTreasuryMaturityDate || null ==
  218.             aiFuturesComponentTreasuryEffectiveDate)
  219.             return null;

  220.         int iNumFuturesComponentMaturity = aiFuturesComponentTreasuryMaturityDate.length;
  221.         int iNumFuturesComponentEffective = aiFuturesComponentTreasuryEffectiveDate.length;
  222.         org.drip.analytics.date.JulianDate[] adtFuturesComponentTreasuryMaturity = null;
  223.         org.drip.analytics.date.JulianDate[] adtFuturesComponentTreasuryEffective = null;

  224.         if (0 != iNumFuturesComponentMaturity)
  225.             adtFuturesComponentTreasuryMaturity = new
  226.                 org.drip.analytics.date.JulianDate[iNumFuturesComponentMaturity];

  227.         if (0 != iNumFuturesComponentEffective)
  228.             adtFuturesComponentTreasuryEffective = new
  229.                 org.drip.analytics.date.JulianDate[iNumFuturesComponentEffective];

  230.         try {
  231.             for (int i = 0; i < iNumFuturesComponentMaturity; ++i)
  232.                 adtFuturesComponentTreasuryMaturity[i] = new org.drip.analytics.date.JulianDate
  233.                     (aiFuturesComponentTreasuryMaturityDate[i]);

  234.             for (int i = 0; i < iNumFuturesComponentEffective; ++i)
  235.                 adtFuturesComponentTreasuryEffective[i] = new org.drip.analytics.date.JulianDate
  236.                     (aiFuturesComponentTreasuryEffectiveDate[i]);
  237.         } catch (java.lang.Exception e) {
  238.             e.printStackTrace();

  239.             return null;
  240.         }

  241.         return TreasuryFutures (dtSpot, strFuturesCode, adtFuturesComponentTreasuryEffective,
  242.             adtFuturesComponentTreasuryMaturity, adblFuturesComponentTreasuryCoupon,
  243.                 adblFuturesComponentConversionFactor, "TREASURY", strFuturesComponentUnderlierSubtype,
  244.                     strFuturesReferenceMaturityTenor);
  245.     }

  246.     /**
  247.      * Generate the Treasury Futures Instance
  248.      *
  249.      * @param dtSpot The Spot Date Instance
  250.      * @param strFuturesCode The Treasury Futures Code
  251.      * @param aiFuturesComponentTreasuryEffectiveDate Array of the Treasury Futures Component Effective Date
  252.      * @param aiFuturesComponentTreasuryMaturityDate Array of the Treasury Futures Component Maturity Date
  253.      * @param adblFuturesComponentTreasuryCoupon Array of the Treasury Futures Component Coupon
  254.      * @param adblFuturesComponentConversionFactor Array of the Treasury Futures Component Conversion Factor
  255.      *
  256.      * @return The Treasury Futures Instance
  257.      */

  258.     public static final org.drip.product.govvie.TreasuryFutures TreasuryFutures (
  259.         final org.drip.analytics.date.JulianDate dtSpot,
  260.         final java.lang.String strFuturesCode,
  261.         final int[] aiFuturesComponentTreasuryEffectiveDate,
  262.         final int[] aiFuturesComponentTreasuryMaturityDate,
  263.         final double[] adblFuturesComponentTreasuryCoupon,
  264.         final double[] adblFuturesComponentConversionFactor)
  265.     {
  266.         org.drip.market.exchange.TreasuryFuturesContract tfc =
  267.             org.drip.market.exchange.TreasuryFuturesContractContainer.TreasuryFuturesContract
  268.                 (strFuturesCode);

  269.         return null == tfc ? null : TreasuryFutures (dtSpot, tfc.code(),
  270.             aiFuturesComponentTreasuryEffectiveDate, aiFuturesComponentTreasuryMaturityDate,
  271.                 adblFuturesComponentTreasuryCoupon, adblFuturesComponentConversionFactor, tfc.type(),
  272.                     tfc.tenor());
  273.     }
  274. }