DU1KeyRateDuration.java

  1. package org.drip.sample.treasuryfuturesrisk;

  2. import java.util.*;

  3. import org.drip.analytics.date.JulianDate;
  4. import org.drip.feed.loader.*;
  5. import org.drip.historical.sensitivity.TenorDurationNodeMetrics;
  6. import org.drip.market.exchange.*;
  7. import org.drip.market.issue.*;
  8. import org.drip.numerical.common.FormatUtil;
  9. import org.drip.service.env.EnvManager;
  10. import org.drip.service.product.TreasuryFuturesAPI;

  11. /*
  12.  * -*- mode: java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  13.  */

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

  56. /**
  57.  * DU1KeyRateDuration demonstrates the Computation of the Key Rate Duration for the DU1 Treasury Futures.
  58.  *
  59.  * @author Lakshmi Krishnamurthy
  60.  */

  61. public class DU1KeyRateDuration {

  62.     public static final void main (
  63.         final String[] astrArgs)
  64.         throws Exception
  65.     {
  66.         EnvManager.InitEnv ("");

  67.         String strFuturesCode = "DU1";

  68.         TreasuryFuturesContract tfc = TreasuryFuturesContractContainer.TreasuryFuturesContract (strFuturesCode);

  69.         String strTreasuryCode = tfc.code();

  70.         TreasurySetting ts = TreasurySettingContainer.TreasurySetting (strTreasuryCode);

  71.         String strTreasuryMarkLocation = "C:\\DRIP\\CreditAnalytics\\Daemons\\Transforms\\TreasuryYieldMarks\\" +
  72.             TreasurySettingContainer.CurrencyBenchmarkCode (ts.currency()) +
  73.                 "BenchmarksReconstituted.csv";
  74.         String strPrintLocation = "C:\\DRIP\\CreditAnalytics\\Daemons\\Transforms\\TreasuryFuturesCloses\\" +
  75.             strFuturesCode + "ClosesReconstitutor.csv";

  76.         CSVGrid csvGridTreasuryMark = CSVParser.StringGrid (
  77.             strTreasuryMarkLocation,
  78.             true
  79.         );

  80.         JulianDate[] adtTreasuryMark = csvGridTreasuryMark.dateArrayAtColumn (0);

  81.         double[] adblYield02Y = csvGridTreasuryMark.doubleArrayAtColumn (1);

  82.         double[] adblYield03Y = csvGridTreasuryMark.doubleArrayAtColumn (2);

  83.         double[] adblYield04Y = csvGridTreasuryMark.doubleArrayAtColumn (3);

  84.         double[] adblYield05Y = csvGridTreasuryMark.doubleArrayAtColumn (4);

  85.         double[] adblYield07Y = csvGridTreasuryMark.doubleArrayAtColumn (5);

  86.         double[] adblYield10Y = csvGridTreasuryMark.doubleArrayAtColumn (6);

  87.         double[] adblYield20Y = csvGridTreasuryMark.doubleArrayAtColumn (7);

  88.         double[] adblYield30Y = csvGridTreasuryMark.doubleArrayAtColumn (8);

  89.         Map<JulianDate, Double> mapTreasuryMark02Y = new TreeMap<JulianDate, Double>();

  90.         Map<JulianDate, Double> mapTreasuryMark03Y = new TreeMap<JulianDate, Double>();

  91.         Map<JulianDate, Double> mapTreasuryMark04Y = new TreeMap<JulianDate, Double>();

  92.         Map<JulianDate, Double> mapTreasuryMark05Y = new TreeMap<JulianDate, Double>();

  93.         Map<JulianDate, Double> mapTreasuryMark07Y = new TreeMap<JulianDate, Double>();

  94.         Map<JulianDate, Double> mapTreasuryMark10Y = new TreeMap<JulianDate, Double>();

  95.         Map<JulianDate, Double> mapTreasuryMark20Y = new TreeMap<JulianDate, Double>();

  96.         Map<JulianDate, Double> mapTreasuryMark30Y = new TreeMap<JulianDate, Double>();

  97.         for (int i = 0; i < adtTreasuryMark.length; ++i) {
  98.             mapTreasuryMark02Y.put (adtTreasuryMark[i], adblYield02Y[i]);

  99.             mapTreasuryMark03Y.put (adtTreasuryMark[i], adblYield03Y[i]);

  100.             mapTreasuryMark04Y.put (adtTreasuryMark[i], adblYield04Y[i]);

  101.             mapTreasuryMark05Y.put (adtTreasuryMark[i], adblYield05Y[i]);

  102.             mapTreasuryMark07Y.put (adtTreasuryMark[i], adblYield07Y[i]);

  103.             mapTreasuryMark10Y.put (adtTreasuryMark[i], adblYield10Y[i]);

  104.             mapTreasuryMark20Y.put (adtTreasuryMark[i], adblYield20Y[i]);

  105.             mapTreasuryMark30Y.put (adtTreasuryMark[i], adblYield30Y[i]);
  106.         }

  107.         CSVGrid csvGrid = CSVParser.StringGrid (
  108.             strPrintLocation,
  109.             true
  110.         );

  111.         JulianDate[] adtSpot = csvGrid.dateArrayAtColumn (0);

  112.         double[] adblCleanPrice = csvGrid.doubleArrayAtColumn (2);

  113.         double[] adblCoupon = csvGrid.doubleArrayAtColumn (3);

  114.         JulianDate[] adtEffective = csvGrid.dateArrayAtColumn (4);

  115.         JulianDate[] adtMaturity = csvGrid.dateArrayAtColumn (5);

  116.         JulianDate[] adtExpiry = csvGrid.dateArrayAtColumn (6);

  117.         int iNumCompute = adtSpot.length;
  118.         JulianDate[] adtEffectiveCompute = new JulianDate[iNumCompute];
  119.         JulianDate[] adtMaturityCompute = new JulianDate[iNumCompute];
  120.         double[] adblCouponCompute = new double[iNumCompute];
  121.         JulianDate[] adtExpiryCompute = new JulianDate[iNumCompute];
  122.         JulianDate[] adtSpotCompute = new JulianDate[iNumCompute];
  123.         double[] adblCleanPriceCompute = new double[iNumCompute];
  124.         double[][] aadblComputeYield = new double[iNumCompute][8];
  125.         String[] astrBenchmarkTenor = new String[] {
  126.             "2Y",
  127.             "3Y",
  128.             "4Y",
  129.             "5Y",
  130.             "7Y",
  131.             "10Y",
  132.             "20Y",
  133.             "30Y"
  134.         };

  135.         for (int i = 0; i < iNumCompute; ++i) {
  136.             adtEffectiveCompute[i] = adtEffective[i];
  137.             adtMaturityCompute[i] = adtMaturity[i];
  138.             adblCouponCompute[i] = adblCoupon[i];
  139.             adtExpiryCompute[i] = adtExpiry[i];
  140.             adtSpotCompute[i] = adtSpot[i];
  141.             adblCleanPriceCompute[i] = adblCleanPrice[i];

  142.             aadblComputeYield[i][0] = mapTreasuryMark02Y.get (adtSpotCompute[i]);

  143.             aadblComputeYield[i][1] = mapTreasuryMark03Y.get (adtSpotCompute[i]);

  144.             aadblComputeYield[i][2] = mapTreasuryMark04Y.get (adtSpotCompute[i]);

  145.             aadblComputeYield[i][3] = mapTreasuryMark05Y.get (adtSpotCompute[i]);

  146.             aadblComputeYield[i][4] = mapTreasuryMark07Y.get (adtSpotCompute[i]);

  147.             aadblComputeYield[i][5] = mapTreasuryMark10Y.get (adtSpotCompute[i]);

  148.             aadblComputeYield[i][6] = mapTreasuryMark20Y.get (adtSpotCompute[i]);

  149.             aadblComputeYield[i][7] = mapTreasuryMark30Y.get (adtSpotCompute[i]);
  150.         }

  151.         List<TenorDurationNodeMetrics> lsTDNM = TreasuryFuturesAPI.HorizonKeyRateDuration (
  152.             strTreasuryCode,
  153.             adtEffectiveCompute,
  154.             adtMaturityCompute,
  155.             adblCouponCompute,
  156.             adtExpiryCompute,
  157.             adtSpotCompute,
  158.             adblCleanPriceCompute,
  159.             astrBenchmarkTenor,
  160.             aadblComputeYield
  161.         );

  162.         System.out.println ("SpotDate,ExpiryDate,CTDName,SpotCTDCleanPrice,ExpiryCTDCleanPrice,SpotGSpread,ExpiryGSpread,SpotYield,ExpiryYield,Parallel,2Y,3Y,4Y,5Y,7Y,10Y,20Y,30Y");

  163.         for (TenorDurationNodeMetrics tdnm : lsTDNM) {
  164.             String strTDNM =
  165.                 tdnm.dateSnap() + "," +
  166.                 tdnm.date ("ExpiryDate") + "," +
  167.                 tdnm.c1 ("CTDName") + "," +
  168.                 FormatUtil.FormatDouble (tdnm.r1 ("SpotCTDCleanPrice"), 1, 5, 100.) + "," +
  169.                 FormatUtil.FormatDouble (tdnm.r1 ("ExpiryCTDCleanPrice"), 1, 5, 100.) + "," +
  170.                 FormatUtil.FormatDouble (tdnm.r1 ("SpotGSpread"), 1, 1, 10000.) + "," +
  171.                 FormatUtil.FormatDouble (tdnm.r1 ("ExpiryGSpread"), 1, 1, 10000.) + "," +
  172.                 FormatUtil.FormatDouble (tdnm.r1 ("SpotYield"), 1, 4, 100.) + "," +
  173.                 FormatUtil.FormatDouble (tdnm.r1 ("ExpiryYield"), 1, 4, 100.) + "," +
  174.                 FormatUtil.FormatDouble (tdnm.r1 ("ParallelKRD"), 1, 4, 1.);

  175.             for (Map.Entry<String, Double> meTDNM : tdnm.krdMap().entrySet())
  176.                 strTDNM += "," + FormatUtil.FormatDouble (meTDNM.getValue(), 1, 4, 1.);

  177.             System.out.println (strTDNM);
  178.         }
  179.     }
  180. }