ForeignCollateralizedDomesticForward.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>ForeignCollateralizedDomesticForward</i> contains the Foreign Currency Collateralized Domestic 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 ForeignCollateralizedDomesticForward {
  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 ForeignCollateralizedDomesticForward 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 ForeignCollateralizedDomesticForward (
  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 ("ForeignCollateralizedDomesticForward 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.         System.out.println (astrCodeTokens[0]);

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

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

  147.         return null;
  148.     }

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

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

  160.         long lStart = System.nanoTime();

  161.         int iValueDate = valParams.valueDate();

  162.         if (iValueDate > _iMaturityDate) return null;

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

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

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

  166.         org.drip.state.discount.MergedDiscountForwardCurve dcForeignCollateral =
  167.             csqs.payCurrencyCollateralCurrencyCurve (strForeignCurrency, strForeignCurrency);

  168.         if (null == dcForeignCollateral) return null;

  169.         org.drip.state.discount.MergedDiscountForwardCurve dcDomesticCurrencyForeignCollateral =
  170.             csqs.payCurrencyCollateralCurrencyCurve (_cp.denomCcy(), strForeignCurrency);

  171.         if (null == dcDomesticCurrencyForeignCollateral) 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 dblForeignCollateralDF = java.lang.Double.NaN;
  176.         double dblDomesticCurrencyForeignCollateralDF = java.lang.Double.NaN;

  177.         try {
  178.             dblPrice = (dblSpotFX = fxfc.fx (iValueDate)) * (dblForeignCollateralDF =
  179.                 dcForeignCollateral.df (_iMaturityDate)) - ((dblDomesticCurrencyForeignCollateralDF =
  180.                     dcDomesticCurrencyForeignCollateral.df (_iMaturityDate)) * _dblForexForwardStrike);

  181.             dblParForward = dblSpotFX * dblForeignCollateralDF / dblDomesticCurrencyForeignCollateralDF;
  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 ("DomesticCurrencyForeignCollateralDF", dblDomesticCurrencyForeignCollateralDF);

  190.         mapResult.put ("ForeignCollateralDF", dblForeignCollateralDF);

  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. }