Repo.java

  1. package org.drip.sample.securitysuite;

  2. import org.drip.analytics.date.*;
  3. import org.drip.analytics.daycount.Convention;
  4. import org.drip.param.valuation.ValuationParams;
  5. import org.drip.product.creator.BondBuilder;
  6. import org.drip.product.credit.BondComponent;
  7. import org.drip.service.env.EnvManager;
  8. import org.drip.service.template.LatentMarketStateBuilder;
  9. import org.drip.state.discount.MergedDiscountForwardCurve;

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

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

  54. /**
  55.  * Repo generates the Full Suite of Replication Metrics for a Sample Repo Instrument.
  56.  *
  57.  * @author Lakshmi Krishnamurthy
  58.  */

  59. public class Repo {

  60.     private static final MergedDiscountForwardCurve OvernightCurve (
  61.         final JulianDate dtSpot,
  62.         final String strCurrency)
  63.         throws Exception
  64.     {
  65.         String[] astrDepositMaturityTenor = new String[] {
  66.             "1D",
  67.             "3D"
  68.         };

  69.         double[] adblDepositQuote = new double[] {
  70.             0.0004,     // 1D
  71.             0.0004      // 3D
  72.         };

  73.         String[] astrShortEndOISMaturityTenor = new String[] {
  74.             "1W",
  75.             "2W",
  76.             "3W",
  77.             "1M"
  78.         };

  79.         double[] adblShortEndOISQuote = new double[] {
  80.             0.00070,    //   1W
  81.             0.00069,    //   2W
  82.             0.00078,    //   3W
  83.             0.00074     //   1M
  84.         };

  85.         String[] astrOISFuturesEffectiveTenor = new String[] {
  86.             "1M",
  87.             "2M",
  88.             "3M",
  89.             "4M",
  90.             "5M"
  91.         };

  92.         String[] astrOISFuturesMaturityTenor = new String[] {
  93.             "1M",
  94.             "1M",
  95.             "1M",
  96.             "1M",
  97.             "1M"
  98.         };

  99.         double[] adblOISFuturesQuote = new double[] {
  100.              0.00046,    //   1M x 1M
  101.              0.00016,    //   2M x 1M
  102.             -0.00007,    //   3M x 1M
  103.             -0.00013,    //   4M x 1M
  104.             -0.00014     //   5M x 1M
  105.         };

  106.         String[] astrLongEndOISMaturityTenor = new String[] {
  107.             "15M",
  108.             "18M",
  109.             "21M",
  110.             "02Y",
  111.             "03Y",
  112.             "04Y",
  113.             "05Y",
  114.             "06Y",
  115.             "07Y",
  116.             "08Y",
  117.             "09Y",
  118.             "10Y",
  119.             "11Y",
  120.             "12Y",
  121.             "15Y",
  122.             "20Y",
  123.             "25Y",
  124.             "30Y"
  125.         };

  126.         double[] adblLongEndOISQuote = new double[] {
  127.             0.00002,    //  15M
  128.             0.00008,    //  18M
  129.             0.00021,    //  21M
  130.             0.00036,    //   2Y
  131.             0.00127,    //   3Y
  132.             0.00274,    //   4Y
  133.             0.00456,    //   5Y
  134.             0.00647,    //   6Y
  135.             0.00827,    //   7Y
  136.             0.00996,    //   8Y
  137.             0.01147,    //   9Y
  138.             0.01280,    //  10Y
  139.             0.01404,    //  11Y
  140.             0.01516,    //  12Y
  141.             0.01764,    //  15Y
  142.             0.01939,    //  20Y
  143.             0.02003,    //  25Y
  144.             0.02038     //  30Y
  145.         };

  146.         return LatentMarketStateBuilder.SmoothOvernightCurve (
  147.             dtSpot,
  148.             strCurrency,
  149.             astrDepositMaturityTenor,
  150.             adblDepositQuote,
  151.             "Rate",
  152.             astrShortEndOISMaturityTenor,
  153.             adblShortEndOISQuote,
  154.             "SwapRate",
  155.             astrOISFuturesEffectiveTenor,
  156.             astrOISFuturesMaturityTenor,
  157.             adblOISFuturesQuote,
  158.             "SwapRate",
  159.             astrLongEndOISMaturityTenor,
  160.             adblLongEndOISQuote,
  161.             "SwapRate"
  162.         );
  163.     }

  164.     public static final void main (
  165.         final String[] astArgs)
  166.         throws Exception
  167.     {
  168.         EnvManager.InitEnv ("");

  169.         JulianDate dtSpot = DateUtil.CreateFromYMD (
  170.             2017,
  171.             DateUtil.JULY,
  172.             10
  173.         );

  174.         int iCouponFreq = 2;
  175.         String strName = "Repo";
  176.         String strCurrency = "USD";
  177.         double dblMarketPrice = 1.;
  178.         double dblRepoRate = 0.04;
  179.         double dblCouponRate = 0.0667;
  180.         String strCouponDayCount = "30/360";

  181.         MergedDiscountForwardCurve dcOvernight = OvernightCurve (
  182.             dtSpot,
  183.             strCurrency
  184.         );

  185.         JulianDate dtBondEffective = DateUtil.CreateFromYMD (
  186.             2010,
  187.             3,
  188.             18
  189.         );

  190.         JulianDate dtBondMaturity = DateUtil.CreateFromYMD (
  191.             2030,
  192.             4,
  193.             7
  194.         );

  195.         BondComponent bond = BondBuilder.CreateSimpleFixed (
  196.             strName,
  197.             strCurrency,
  198.             strName,
  199.             dblCouponRate,
  200.             iCouponFreq,
  201.             strCouponDayCount,
  202.             dtBondEffective,
  203.             dtBondMaturity,
  204.             null,
  205.             null
  206.         );

  207.         double dblCurrentYield = bond.yieldFromPrice (
  208.             ValuationParams.Spot (dtSpot.julian()),
  209.             null,
  210.             null,
  211.             dblMarketPrice
  212.         );

  213.         System.out.println ("Current Yield: " + dblCurrentYield);

  214.         JulianDate dtRepoEffective = DateUtil.CreateFromYMD (
  215.             2018,
  216.             3,
  217.             20
  218.         );

  219.         JulianDate dtRepoMaturity = DateUtil.CreateFromYMD (
  220.             2018,
  221.             9,
  222.             20
  223.         );

  224.         double dblRepoEffectiveCleanPrice = bond.priceFromYield (
  225.             ValuationParams.Spot (dtRepoEffective.julian()),
  226.             null,
  227.             null,
  228.             dblCurrentYield
  229.         );

  230.         double dblRepoMaturityCleanPrice = bond.priceFromYield (
  231.             ValuationParams.Spot (dtRepoMaturity.julian()),
  232.             null,
  233.             null,
  234.             dblCurrentYield
  235.         );

  236.         System.out.println (dblRepoEffectiveCleanPrice + " | " + dblRepoMaturityCleanPrice);

  237.         double dblRepoEffectiveDatePV = dblRepoEffectiveCleanPrice * dcOvernight.df (dtRepoEffective);

  238.         double dblRepoMaturityDatePV = dblRepoMaturityCleanPrice * dcOvernight.df (dtRepoMaturity);

  239.         System.out.println (dblRepoEffectiveDatePV + " | " + dblRepoMaturityDatePV);

  240.         double dblBondAccrual = dblCouponRate * Convention.YearFraction (
  241.             dtRepoEffective.julian(),
  242.             dtRepoMaturity.julian(),
  243.             strCouponDayCount,
  244.             false,
  245.             null,
  246.             strCurrency
  247.         );

  248.         double dblBondLegValue = dblRepoEffectiveDatePV - dblRepoMaturityDatePV - dblBondAccrual;

  249.         double dblRepoAccrual = dblRepoRate * Convention.YearFraction (
  250.             dtRepoEffective.julian(),
  251.             dtRepoMaturity.julian(),
  252.             strCouponDayCount,
  253.             false,
  254.             null,
  255.             strCurrency
  256.         );

  257.         double dblRepoValue = dblBondLegValue + dblRepoAccrual;

  258.         System.out.println (dblRepoEffectiveDatePV + " | " + dblRepoMaturityDatePV);

  259.         System.out.println (dblBondLegValue + " | " + dblRepoAccrual + " | " + dblRepoValue);
  260.     }
  261. }