DomesticCollateralizedForeignForward.java

  1. package org.drip.product.fx;

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

  79. /**
  80.  * <i>DomesticCollateralizedForeignForward</i> contains the Domestic Currency Collateralized Foreign Payout
  81.  * FX forward product contract details.
  82.  *
  83.  * <br><br>
  84.  *  <ul>
  85.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ProductCore.md">Product Core Module</a></li>
  86.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/FixedIncomeAnalyticsLibrary.md">Fixed Income Analytics</a></li>
  87.  *      <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>
  88.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/product/fx/README.md">FX Forwards, Cross Currency Swaps</a></li>
  89.  *  </ul>
  90.  * <br><br>
  91.  *  
  92.  * @author Lakshmi Krishnamurthy
  93.  */

  94. public class DomesticCollateralizedForeignForward {
  95.     private java.lang.String _strCode = "";
  96.     private org.drip.product.params.CurrencyPair _cp = null;
  97.     private int _iMaturityDate = java.lang.Integer.MIN_VALUE;
  98.     private double _dblForexForwardStrike = java.lang.Double.NaN;

  99.     /**
  100.      * Create an DomesticCollateralizedForeignForward from the currency pair, the strike, and the maturity
  101.      *  dates
  102.      *
  103.      * @param cp Currency Pair
  104.      * @param dblForexForwardStrike Forex Forward Strike
  105.      * @param dtMaturity Maturity Date
  106.      *
  107.      * @throws java.lang.Exception Thrown if the inputs are invalid
  108.      */

  109.     public DomesticCollateralizedForeignForward (
  110.         final org.drip.product.params.CurrencyPair cp,
  111.         final double dblForexForwardStrike,
  112.         final org.drip.analytics.date.JulianDate dtMaturity)
  113.         throws java.lang.Exception
  114.     {
  115.         if (null == (_cp = cp) || !org.drip.numerical.common.NumberUtil.IsValid (_dblForexForwardStrike =
  116.             dblForexForwardStrike) || null == dtMaturity)
  117.             throw new java.lang.Exception ("DomesticCollateralizedForeignForward ctr: Invalid Inputs");

  118.         _iMaturityDate = dtMaturity.julian();
  119.     }

  120.     public java.lang.String getPrimaryCode()
  121.     {
  122.         return _strCode;
  123.     }

  124.     public void setPrimaryCode (
  125.         final java.lang.String strCode)
  126.     {
  127.         _strCode = strCode;
  128.     }

  129.     public java.lang.String[] getSecondaryCode()
  130.     {
  131.         java.lang.String strPrimaryCode = getPrimaryCode();

  132.         int iNumTokens = 0;
  133.         java.lang.String astrCodeTokens[] = new java.lang.String[2];

  134.         java.util.StringTokenizer stCodeTokens = new java.util.StringTokenizer (strPrimaryCode, ".");

  135.         while (stCodeTokens.hasMoreTokens())
  136.             astrCodeTokens[iNumTokens++] = stCodeTokens.nextToken();

  137.         return new java.lang.String[] {astrCodeTokens[0]};
  138.     }

  139.     public org.drip.analytics.date.JulianDate getMaturityDate()
  140.     {
  141.         try {
  142.             return new org.drip.analytics.date.JulianDate (_iMaturityDate);
  143.         } catch (java.lang.Exception e) {
  144.             e.printStackTrace();
  145.         }

  146.         return null;
  147.     }

  148.     public org.drip.product.params.CurrencyPair getCcyPair()
  149.     {
  150.         return _cp;
  151.     }

  152.     public org.drip.analytics.support.CaseInsensitiveTreeMap<java.lang.Double> value (
  153.         final org.drip.param.valuation.ValuationParams valParams,
  154.         final org.drip.param.pricer.CreditPricerParams pricerParams,
  155.         final org.drip.param.market.CurveSurfaceQuoteContainer csqs,
  156.         final org.drip.param.valuation.ValuationCustomizationParams quotingParams)
  157.     {
  158.         if (null == valParams || null == csqs) return null;

  159.         long lStart = System.nanoTime();

  160.         int iValueDate = valParams.valueDate();

  161.         if (iValueDate > _iMaturityDate) return null;

  162.         org.drip.state.fx.FXCurve fxfc = csqs.fxState (org.drip.state.identifier.FXLabel.Standard (_cp));

  163.         if (null == fxfc) return null;

  164.         java.lang.String strDomesticCurrency = _cp.denomCcy();

  165.         org.drip.state.discount.MergedDiscountForwardCurve dcDomesticCollateral =
  166.             csqs.payCurrencyCollateralCurrencyCurve (strDomesticCurrency, strDomesticCurrency);

  167.         if (null == dcDomesticCollateral) return null;

  168.         java.lang.String strForeignCurrency = _cp.numCcy();

  169.         org.drip.state.discount.MergedDiscountForwardCurve dcForeignCurrencyDomesticCollateral =
  170.             csqs.payCurrencyCollateralCurrencyCurve (strForeignCurrency, strDomesticCurrency);

  171.         if (null == dcForeignCurrencyDomesticCollateral) return null;

  172.         double dblPrice = java.lang.Double.NaN;
  173.         double dblSpotFX = java.lang.Double.NaN;
  174.         double dblParForward = java.lang.Double.NaN;
  175.         double dblDomesticCollateralDF = java.lang.Double.NaN;
  176.         double dblForeignCurrencyDomesticCollateralDF = java.lang.Double.NaN;

  177.         try {
  178.             dblPrice = (dblForeignCurrencyDomesticCollateralDF = dcForeignCurrencyDomesticCollateral.df
  179.                 (_iMaturityDate)) - (_dblForexForwardStrike * (dblDomesticCollateralDF =
  180.                     dcDomesticCollateral.df (_iMaturityDate)) / (dblSpotFX = fxfc.fx (iValueDate)));

  181.             dblParForward = dblSpotFX * dblForeignCurrencyDomesticCollateralDF / dblDomesticCollateralDF;
  182.         } catch (java.lang.Exception e) {
  183.             e.printStackTrace();

  184.             return null;
  185.         }

  186.         org.drip.analytics.support.CaseInsensitiveTreeMap<java.lang.Double> mapResult = new
  187.             org.drip.analytics.support.CaseInsensitiveTreeMap<java.lang.Double>();

  188.         mapResult.put ("CalcTime", (System.nanoTime() - lStart) * 1.e-09);

  189.         mapResult.put ("DomesticCollateralDF", dblDomesticCollateralDF);

  190.         mapResult.put ("ForeignCurrencyDomesticCollateralDF", dblForeignCurrencyDomesticCollateralDF);

  191.         mapResult.put ("ParForward", dblParForward);

  192.         mapResult.put ("Price", dblPrice);

  193.         mapResult.put ("SpotFX", dblSpotFX);

  194.         return mapResult;
  195.     }

  196.     public java.util.Set<java.lang.String> getMeasureNames()
  197.     {
  198.         java.util.Set<java.lang.String> setstrMeasureNames = new java.util.TreeSet<java.lang.String>();

  199.         setstrMeasureNames.add ("CalcTime");

  200.         setstrMeasureNames.add ("DomesticCurrencyForeignCollateralDF");

  201.         setstrMeasureNames.add ("ForeignCollateralDF");

  202.         setstrMeasureNames.add ("ParForward");

  203.         setstrMeasureNames.add ("Price");

  204.         setstrMeasureNames.add ("SpotFX");

  205.         return setstrMeasureNames;
  206.     }
  207. }