IK1Attribution.java

  1. package org.drip.sample.treasuryfuturespnl;

  2. import java.util.List;

  3. import org.drip.analytics.date.JulianDate;
  4. import org.drip.feed.loader.*;
  5. import org.drip.historical.attribution.*;
  6. import org.drip.numerical.common.FormatUtil;
  7. import org.drip.service.env.EnvManager;
  8. import org.drip.service.product.TreasuryFuturesAPI;

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

  12. /*!
  13.  * Copyright (C) 2018 Lakshmi Krishnamurthy
  14.  * Copyright (C) 2017 Lakshmi Krishnamurthy
  15.  * Copyright (C) 2016 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.  * IK1Attribution demonstrates the Invocation of the Historical PnL Horizon PnL Attribution analysis for the
  56.  *  IK1 Series.
  57.  *
  58.  * @author Lakshmi Krishnamurthy
  59.  */

  60. public class IK1Attribution {

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

  66.         String strPrintLocation = "C:\\DRIP\\CreditAnalytics\\Daemons\\Transforms\\TreasuryFuturesCloses\\IK1ClosesReconstitutor.csv";

  67.         CSVGrid csvGrid = CSVParser.StringGrid (
  68.             strPrintLocation,
  69.             true
  70.         );

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

  72.         double[] adblConversionFactor = csvGrid.doubleArrayAtColumn (1);

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

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

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

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

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

  78.         List<PositionChangeComponents> lsPCC = TreasuryFuturesAPI.HorizonChangeAttribution (
  79.             "BTPS",
  80.             adtEffective,
  81.             adtMaturity,
  82.             adblCoupon,
  83.             adtExpiry,
  84.             adtSpot,
  85.             adblCleanPrice,
  86.             adblConversionFactor
  87.         );

  88.         System.out.println ("FirstDate, SecondDate, ExpiryDate, CTD Bond, Expiry Clean Price, Conversion Factor, 1D Gross PnL, 1D Market PnL, 1D Roll-down PnL, 1D Accrual PnL, 1D Explained PnL, 1D Unexplianed PnL");

  89.         for (PositionChangeComponents pcc : lsPCC) {
  90.             TreasuryFuturesMarketSnap tfpms = (TreasuryFuturesMarketSnap) pcc.pmsSecond();

  91.             System.out.println (
  92.                 pcc.firstDate() + ", " +
  93.                 pcc.secondDate() + ", " +
  94.                 tfpms.expiryDate() + ", " +
  95.                 tfpms.ctdName() + ", " +
  96.                 FormatUtil.FormatDouble (tfpms.expiryCleanPrice(), 1, 5, 1.) + ", " +
  97.                 FormatUtil.FormatDouble (tfpms.conversionFactor(), 1, 5, 1.) + ", " +
  98.                 FormatUtil.FormatDouble (pcc.grossChange(), 2, 2, 10000.) + ", " +
  99.                 FormatUtil.FormatDouble (pcc.marketRealizationChange(), 2, 2, 10000.) + ", " +
  100.                 FormatUtil.FormatDouble (pcc.marketRollDownChange(), 2, 2, 10000.) + ", " +
  101.                 FormatUtil.FormatDouble (pcc.accrualChange(), 2, 2, 10000.) + ", " +
  102.                 FormatUtil.FormatDouble (pcc.explainedChange(), 2, 2, 10000.) + ", " +
  103.                 FormatUtil.FormatDouble (pcc.unexplainedChange(), 2, 2, 10000.)
  104.             );
  105.         }
  106.     }
  107. }