FXSwap.java

  1. package org.drip.sample.securitysuite;

  2. import java.util.Map;

  3. import org.drip.analytics.date.*;
  4. import org.drip.function.r1tor1.QuadraticRationalShapeControl;
  5. import org.drip.param.market.CurveSurfaceQuoteContainer;
  6. import org.drip.param.valuation.ValuationParams;
  7. import org.drip.product.fx.FXForwardComponent;
  8. import org.drip.product.params.CurrencyPair;
  9. import org.drip.service.env.EnvManager;
  10. import org.drip.spline.basis.PolynomialFunctionSetParams;
  11. import org.drip.spline.params.*;
  12. import org.drip.spline.stretch.*;
  13. import org.drip.state.creator.ScenarioFXCurveBuilder;
  14. import org.drip.state.estimator.LatentStateStretchBuilder;
  15. import org.drip.state.fx.FXCurve;
  16. import org.drip.state.inference.*;

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

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

  61. /**
  62.  * FXSwap demonstrates the Analytics Calculation/Reconciliation for an FX Swap.
  63.  *
  64.  * @author Lakshmi Krishnamurthy
  65.  */

  66. public class FXSwap {

  67.     private static FXForwardComponent[] FXForwardCalibComponent (
  68.         final CurrencyPair cp,
  69.         final JulianDate dtSpot,
  70.         final String[] astrMaturityTenor)
  71.         throws Exception
  72.     {
  73.         FXForwardComponent[] aFXForward = new FXForwardComponent[astrMaturityTenor.length];

  74.         for (int i = 0; i < astrMaturityTenor.length; ++i)
  75.             aFXForward[i] = new FXForwardComponent (
  76.                 cp.code() + "::FXFWD::" + astrMaturityTenor[i],
  77.                 cp,
  78.                 dtSpot.julian(),
  79.                 dtSpot.addTenor (astrMaturityTenor[i]).julian(),
  80.                 1.,
  81.                 null
  82.             );

  83.         return aFXForward;
  84.     }

  85.     public static final void main (
  86.         final String[] astrArgs)
  87.         throws Exception
  88.     {
  89.         EnvManager.InitEnv ("");

  90.         JulianDate dtSpot = DateUtil.CreateFromYMD (
  91.             2017,
  92.             DateUtil.AUGUST,
  93.             25
  94.         );

  95.         CurrencyPair cp = CurrencyPair.FromCode ("USD/EUR");

  96.         double dblSpot = 1.0993;

  97.         String[] astrMaturityTenor = new String[] {
  98.             "1W",
  99.             "1M",
  100.             "3M",
  101.             "6M",
  102.             "1Y",
  103.             "2Y",
  104.             "3Y"
  105.         };

  106.         FXForwardComponent[] aFXForward = FXForwardCalibComponent (
  107.             cp,
  108.             dtSpot,
  109.             astrMaturityTenor
  110.         );

  111.         double[] adblFXForward = new double[] {
  112.             1.1000, //  "1W",
  113.             1.1012, //  "1M",
  114.             1.1039, //  "3M",
  115.             1.1148, //  "6M",
  116.             1.1232, //  "1Y",
  117.             1.1497, //  "2Y",
  118.             1.1865, //  "3Y"
  119.         };

  120.         LatentStateStretchSpec fxForwardStretch = LatentStateStretchBuilder.FXStretchSpec (
  121.             "FXFORWARD",
  122.             aFXForward,
  123.             "Outright",
  124.             adblFXForward
  125.         );

  126.         LatentStateStretchSpec[] aStretchSpec = new LatentStateStretchSpec[] {
  127.             fxForwardStretch
  128.         };

  129.         LinearLatentStateCalibrator llsc = new LinearLatentStateCalibrator (
  130.             new SegmentCustomBuilderControl (
  131.                 MultiSegmentSequenceBuilder.BASIS_SPLINE_POLYNOMIAL,
  132.                 new PolynomialFunctionSetParams (4),
  133.                 SegmentInelasticDesignControl.Create (
  134.                     2,
  135.                     2
  136.                 ),
  137.                 new ResponseScalingShapeControl (
  138.                     true,
  139.                     new QuadraticRationalShapeControl (0.)
  140.                 ),
  141.                 null
  142.             ),
  143.             BoundarySettings.NaturalStandard(),
  144.             MultiSegmentSequence.CALIBRATE,
  145.             null,
  146.             null
  147.         );

  148.         ValuationParams valParams = new ValuationParams (
  149.             dtSpot,
  150.             dtSpot,
  151.             cp.denomCcy()
  152.         );

  153.         FXCurve fxCurve = ScenarioFXCurveBuilder.ShapePreservingFXCurve (
  154.             llsc,
  155.             aStretchSpec,
  156.             cp,
  157.             valParams,
  158.             null,
  159.             null,
  160.             null,
  161.             dblSpot
  162.         );

  163.         CurveSurfaceQuoteContainer csqc = new CurveSurfaceQuoteContainer();

  164.         csqc.setFXState (fxCurve);

  165.         JulianDate dtMaturity = DateUtil.CreateFromYMD (
  166.             2028,
  167.             DateUtil.NOVEMBER,
  168.             27
  169.         );

  170.         FXForwardComponent fxfc = new FXForwardComponent (
  171.             cp.code() + "::FXFWD::" + dtMaturity,
  172.             cp,
  173.             dtSpot.julian(),
  174.             dtMaturity.julian(),
  175.             1.,
  176.             null
  177.         );

  178.         Map<String, Double> mapOutput = fxfc.value (
  179.             valParams,
  180.             null,
  181.             csqc,
  182.             null
  183.         );

  184.         for (Map.Entry<String, Double> me : mapOutput.entrySet())
  185.             System.out.println ("\t[" + me.getKey() + "] => " + me.getValue());
  186.     }
  187. }