OTCCrossCurrencySwaps.java

  1. package org.drip.sample.xccy;

  2. import org.drip.analytics.date.*;
  3. import org.drip.analytics.support.CaseInsensitiveTreeMap;
  4. import org.drip.function.r1tor1.FlatUnivariate;
  5. import org.drip.market.otc.*;
  6. import org.drip.numerical.common.FormatUtil;
  7. import org.drip.param.market.CurveSurfaceQuoteContainer;
  8. import org.drip.param.valuation.*;
  9. import org.drip.product.params.CurrencyPair;
  10. import org.drip.product.rates.FloatFloatComponent;
  11. import org.drip.service.env.EnvManager;
  12. import org.drip.state.creator.*;
  13. import org.drip.state.discount.*;
  14. import org.drip.state.forward.ForwardCurve;
  15. import org.drip.state.identifier.*;

  16. /*
  17.  * -*- mode: java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  18.  */

  19. /*!
  20.  * Copyright (C) 2018 Lakshmi Krishnamurthy
  21.  * Copyright (C) 2017 Lakshmi Krishnamurthy
  22.  * Copyright (C) 2016 Lakshmi Krishnamurthy
  23.  * Copyright (C) 2015 Lakshmi Krishnamurthy
  24.  *
  25.  *  This file is part of DRIP, a free-software/open-source library for buy/side financial/trading model
  26.  *      libraries targeting analysts and developers
  27.  *      https://lakshmidrip.github.io/DRIP/
  28.  *  
  29.  *  DRIP is composed of four main libraries:
  30.  *  
  31.  *  - DRIP Fixed Income - https://lakshmidrip.github.io/DRIP-Fixed-Income/
  32.  *  - DRIP Asset Allocation - https://lakshmidrip.github.io/DRIP-Asset-Allocation/
  33.  *  - DRIP Numerical Optimizer - https://lakshmidrip.github.io/DRIP-Numerical-Optimizer/
  34.  *  - DRIP Statistical Learning - https://lakshmidrip.github.io/DRIP-Statistical-Learning/
  35.  *
  36.  *  - DRIP Fixed Income: Library for Instrument/Trading Conventions, Treasury Futures/Options,
  37.  *      Funding/Forward/Overnight Curves, Multi-Curve Construction/Valuation, Collateral Valuation and XVA
  38.  *      Metric Generation, Calibration and Hedge Attributions, Statistical Curve Construction, Bond RV
  39.  *      Metrics, Stochastic Evolution and Option Pricing, Interest Rate Dynamics and Option Pricing, LMM
  40.  *      Extensions/Calibrations/Greeks, Algorithmic Differentiation, and Asset Backed Models and Analytics.
  41.  *
  42.  *  - DRIP Asset Allocation: Library for model libraries for MPT framework, Black Litterman Strategy
  43.  *      Incorporator, Holdings Constraint, and Transaction Costs.
  44.  *
  45.  *  - DRIP Numerical Optimizer: Library for Numerical Optimization and Spline Functionality.
  46.  *
  47.  *  - DRIP Statistical Learning: Library for Statistical Evaluation and Machine Learning.
  48.  *
  49.  *  Licensed under the Apache License, Version 2.0 (the "License");
  50.  *      you may not use this file except in compliance with the License.
  51.  *  
  52.  *  You may obtain a copy of the License at
  53.  *      http://www.apache.org/licenses/LICENSE-2.0
  54.  *  
  55.  *  Unless required by applicable law or agreed to in writing, software
  56.  *      distributed under the License is distributed on an "AS IS" BASIS,
  57.  *      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  58.  *  
  59.  *  See the License for the specific language governing permissions and
  60.  *      limitations under the License.
  61.  */

  62. /**
  63.  * OTCCrossCurrencySwaps demonstrates the Construction and Valuation of the Cross-Currency Floating Swap of
  64.  *  OTC contracts.
  65.  *
  66.  * @author Lakshmi Krishnamurthy
  67.  */

  68. public class OTCCrossCurrencySwaps {

  69.     private static final FloatFloatComponent OTCCrossCurrencyFloatFloat (
  70.         final String strReferenceCurrency,
  71.         final String strDerivedCurrency,
  72.         final JulianDate dtSpot,
  73.         final String strMaturityTenor,
  74.         final double dblBasis,
  75.         final double dblDerivedNotionalScaler)
  76.     {
  77.         CrossFloatSwapConvention ccfc = CrossFloatConventionContainer.ConventionFromJurisdiction (
  78.             strReferenceCurrency,
  79.             strDerivedCurrency
  80.         );

  81.         return ccfc.createFloatFloatComponent (
  82.             dtSpot,
  83.             strMaturityTenor,
  84.             dblBasis,
  85.             1.,
  86.             -1. * dblDerivedNotionalScaler
  87.         );
  88.     }

  89.     private static final void OTCCrossCurrencyRun (
  90.         final JulianDate dtSpot,
  91.         final String strReferenceCurrency,
  92.         final String strDerivedCurrency,
  93.         final String strMaturityTenor,
  94.         final double dblBasis,
  95.         final double dblReferenceDerivedFXRate)
  96.         throws Exception
  97.     {
  98.         double dblReferenceFundingRate = 0.02;
  99.         double dblDerived3MForwardRate = 0.02;

  100.         double dblReferenceFundingVol = 0.3;
  101.         double dblDerivedForward3MVol = 0.3;
  102.         double dblReferenceDerivedFXVol = 0.3;

  103.         double dblDerived3MReferenceDerivedFXCorr = 0.1;
  104.         double dblReferenceFundingDerived3MCorr = 0.1;
  105.         double dblReferenceFundingReferenceDerivedFXCorr = 0.1;

  106.         MergedDiscountForwardCurve dcReferenceFunding = ScenarioDiscountCurveBuilder.ExponentiallyCompoundedFlatRate (
  107.             dtSpot,
  108.             strReferenceCurrency,
  109.             dblReferenceFundingRate
  110.         );

  111.         ForwardLabel friDerived3M = ForwardLabel.Create (
  112.             strDerivedCurrency,
  113.             "3M"
  114.         );

  115.         ForwardCurve fcDerived3M = ScenarioForwardCurveBuilder.FlatForwardForwardCurve (
  116.             dtSpot,
  117.             friDerived3M,
  118.             dblDerived3MForwardRate
  119.         );

  120.         CurrencyPair cp = CurrencyPair.FromCode (
  121.             strReferenceCurrency + "/" + strDerivedCurrency
  122.         );

  123.         FXLabel fxLabel = FXLabel.Standard (cp);

  124.         FundingLabel fundingLabelReference = org.drip.state.identifier.FundingLabel.Standard (
  125.             strReferenceCurrency
  126.         );

  127.         CurveSurfaceQuoteContainer mktParams = new CurveSurfaceQuoteContainer();

  128.         mktParams.setForwardState (
  129.             fcDerived3M
  130.         );

  131.         mktParams.setFundingState (
  132.             dcReferenceFunding
  133.         );

  134.         mktParams.setFXState (
  135.             ScenarioFXCurveBuilder.CubicPolynomialCurve (
  136.                 "FX::" + cp.code(),
  137.                 dtSpot,
  138.                 cp,
  139.                 new String[] {"10Y"},
  140.                 new double[] {dblReferenceDerivedFXRate},
  141.                 dblReferenceDerivedFXRate
  142.             )
  143.         );

  144.         mktParams.setForwardVolatility (
  145.             ScenarioDeterministicVolatilityBuilder.FlatForward (
  146.                 dtSpot.julian(),
  147.                 VolatilityLabel.Standard (friDerived3M),
  148.                 strDerivedCurrency,
  149.                 dblDerivedForward3MVol
  150.             )
  151.         );

  152.         mktParams.setFundingVolatility (
  153.             ScenarioDeterministicVolatilityBuilder.FlatForward (
  154.                 dtSpot.julian(),
  155.                 VolatilityLabel.Standard (fundingLabelReference),
  156.                 strReferenceCurrency,
  157.                 dblReferenceFundingVol
  158.             )
  159.         );

  160.         mktParams.setFXVolatility (
  161.             ScenarioDeterministicVolatilityBuilder.FlatForward (
  162.                 dtSpot.julian(),
  163.                 VolatilityLabel.Standard (fxLabel),
  164.                 strDerivedCurrency,
  165.                 dblReferenceDerivedFXVol
  166.             )
  167.         );

  168.         mktParams.setForwardFundingCorrelation (
  169.             friDerived3M,
  170.             fundingLabelReference,
  171.             new FlatUnivariate (
  172.                 dblReferenceFundingDerived3MCorr
  173.             )
  174.         );

  175.         mktParams.setForwardFXCorrelation (
  176.             friDerived3M,
  177.             fxLabel,
  178.             new FlatUnivariate (
  179.                 dblDerived3MReferenceDerivedFXCorr
  180.             )
  181.         );

  182.         mktParams.setFundingFXCorrelation (
  183.             fundingLabelReference,
  184.             fxLabel,
  185.             new FlatUnivariate (
  186.                 dblReferenceFundingReferenceDerivedFXCorr
  187.             )
  188.         );

  189.         FloatFloatComponent xccySwap = OTCCrossCurrencyFloatFloat (
  190.             strReferenceCurrency,
  191.             strDerivedCurrency,
  192.             dtSpot,
  193.             strMaturityTenor,
  194.             dblBasis,
  195.             1. / dblReferenceDerivedFXRate
  196.         );

  197.         xccySwap.setPrimaryCode (
  198.             strDerivedCurrency + "_" + strReferenceCurrency + "_OTC::FLOATFLOAT::" + strMaturityTenor
  199.         );

  200.         mktParams.setFixing (
  201.             xccySwap.effectiveDate(),
  202.             fxLabel,
  203.             dblReferenceDerivedFXRate
  204.         );

  205.         ValuationParams valParams = new ValuationParams (
  206.             dtSpot,
  207.             dtSpot,
  208.             strReferenceCurrency + "," + strDerivedCurrency
  209.         );

  210.         CaseInsensitiveTreeMap<Double> mapXCcyOutput = xccySwap.value (
  211.             valParams,
  212.             null,
  213.             mktParams,
  214.             null
  215.         );

  216.         System.out.println (
  217.             "\t| " + xccySwap.name() + "  [" + xccySwap.effectiveDate() + " -> " + xccySwap.maturityDate() + "]  =>  " +
  218.             FormatUtil.FormatDouble (mapXCcyOutput.get ("Price"), 1, 2, 1.) + "  |  " +
  219.             FormatUtil.FormatDouble (mapXCcyOutput.get ("DerivedParBasisSpread"), 1, 2, 1.) + "  |  " +
  220.             FormatUtil.FormatDouble (mapXCcyOutput.get ("ReferenceParBasisSpread"), 1, 2, 1.) + "  |  " +
  221.             FormatUtil.FormatDouble (mapXCcyOutput.get ("DerivedCleanDV01"), 1, 2, 10000.) + "  |  " +
  222.             FormatUtil.FormatDouble (mapXCcyOutput.get ("ReferenceCleanDV01"), 1, 2, 10000.) + "  |"
  223.         );
  224.     }

  225.     public static final void main (
  226.         final String[] astrArgs)
  227.         throws Exception
  228.     {

  229.         /*
  230.          * Initialize the Credit Analytics Library
  231.          */

  232.         EnvManager.InitEnv ("");

  233.         JulianDate dtSpot = DateUtil.Today();

  234.         System.out.println ("\t---------------------------------------------------------");

  235.         System.out.println ("\t\tCROSS-CURRENCY FLOAT-FLOAT COMPONENT RUNS");

  236.         System.out.println ("\t---------------------------------------------------------");

  237.         System.out.println ("\tL -> R:");

  238.         System.out.println ("\t\tCross Currency Swap Name");

  239.         System.out.println ("\t\tFloat-Float Effective");

  240.         System.out.println ("\t\tFloat-Float Maturity");

  241.         System.out.println ("\t\tPrice");

  242.         System.out.println ("\t\tDerived Stream Par Basis Spread");

  243.         System.out.println ("\t\tReference Stream Par Basis Spread");

  244.         System.out.println ("\t\tAnnualized Derived Stream Duration");

  245.         System.out.println ("\t\tAnnualized Reference Stream Duration");

  246.         System.out.println ("\t------------------------------------------------------------------------------------------------------------------");

  247.         OTCCrossCurrencyRun (dtSpot, "USD", "AUD", "2Y", 0.0003, 0.7769);

  248.         OTCCrossCurrencyRun (dtSpot, "USD", "CAD", "2Y", 0.0003, 0.7861);

  249.         OTCCrossCurrencyRun (dtSpot, "USD", "CHF", "2Y", 0.0003, 1.0811);

  250.         OTCCrossCurrencyRun (dtSpot, "USD", "CLP", "2Y", 0.0003, 0.0016);

  251.         OTCCrossCurrencyRun (dtSpot, "USD", "DKK", "2Y", 0.0003, 0.1517);

  252.         OTCCrossCurrencyRun (dtSpot, "USD", "EUR", "2Y", 0.0003, 1.1294);

  253.         OTCCrossCurrencyRun (dtSpot, "USD", "GBP", "2Y", 0.0003, 1.5004);

  254.         OTCCrossCurrencyRun (dtSpot, "USD", "JPY", "2Y", 0.0003, 0.0085);

  255.         OTCCrossCurrencyRun (dtSpot, "USD", "MXN", "2Y", 0.0003, 0.0666);

  256.         OTCCrossCurrencyRun (dtSpot, "USD", "NOK", "2Y", 0.0003, 0.1288);

  257.         OTCCrossCurrencyRun (dtSpot, "USD", "PLN", "2Y", 0.0003, 0.2701);

  258.         OTCCrossCurrencyRun (dtSpot, "USD", "SEK", "2Y", 0.0003, 0.1211);

  259.         System.out.println ("\t------------------------------------------------------------------------------------------------------------------");
  260.     }
  261. }