OptimalTrajectoryNoDrift.java

  1. package org.drip.sample.lvar;

  2. import org.drip.execution.capture.LinearImpactTrajectoryEstimator;
  3. import org.drip.execution.dynamics.*;
  4. import org.drip.execution.impact.*;
  5. import org.drip.execution.nonadaptive.StaticOptimalSchemeDiscrete;
  6. import org.drip.execution.optimum.EfficientTradingTrajectoryDiscrete;
  7. import org.drip.execution.parameters.ArithmeticPriceDynamicsSettings;
  8. import org.drip.execution.profiletime.UniformParticipationRateLinear;
  9. import org.drip.execution.risk.PowerVarianceObjectiveUtility;
  10. import org.drip.execution.strategy.*;
  11. import org.drip.function.r1tor1.FlatUnivariate;
  12. import org.drip.measure.gaussian.R1UnivariateNormal;
  13. import org.drip.numerical.common.FormatUtil;
  14. import org.drip.service.env.EnvManager;

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

  18. /*!
  19.  * Copyright (C) 2019 Lakshmi Krishnamurthy
  20.  * Copyright (C) 2018 Lakshmi Krishnamurthy
  21.  * Copyright (C) 2017 Lakshmi Krishnamurthy
  22.  * Copyright (C) 2016 Lakshmi Krishnamurthy
  23.  *
  24.  *  This file is part of DROP, an open-source library targeting risk, transaction costs, exposure, margin
  25.  *      calculations, valuation adjustment, and portfolio construction within and across fixed income,
  26.  *      credit, commodity, equity, FX, and structured products.
  27.  *  
  28.  *      https://lakshmidrip.github.io/DROP/
  29.  *  
  30.  *  DROP is composed of three modules:
  31.  *  
  32.  *  - DROP Analytics Core - https://lakshmidrip.github.io/DROP-Analytics-Core/
  33.  *  - DROP Portfolio Core - https://lakshmidrip.github.io/DROP-Portfolio-Core/
  34.  *  - DROP Numerical Core - https://lakshmidrip.github.io/DROP-Numerical-Core/
  35.  *
  36.  *  DROP Analytics Core implements libraries for the following:
  37.  *  - Fixed Income Analytics
  38.  *  - Asset Backed Analytics
  39.  *  - XVA Analytics
  40.  *  - Exposure and Margin Analytics
  41.  *
  42.  *  DROP Portfolio Core implements libraries for the following:
  43.  *  - Asset Allocation Analytics
  44.  *  - Transaction Cost Analytics
  45.  *
  46.  *  DROP Numerical Core implements libraries for the following:
  47.  *  - Statistical Learning
  48.  *  - Numerical Optimizer
  49.  *  - Spline Builder
  50.  *  - Algorithm Support
  51.  *
  52.  *  Documentation for DROP is Spread Over:
  53.  *
  54.  *  - Main                     => https://lakshmidrip.github.io/DROP/
  55.  *  - Wiki                     => https://github.com/lakshmiDRIP/DROP/wiki
  56.  *  - GitHub                   => https://github.com/lakshmiDRIP/DROP
  57.  *  - Repo Layout Taxonomy     => https://github.com/lakshmiDRIP/DROP/blob/master/Taxonomy.md
  58.  *  - Javadoc                  => https://lakshmidrip.github.io/DROP/Javadoc/index.html
  59.  *  - Technical Specifications => https://github.com/lakshmiDRIP/DROP/tree/master/Docs/Internal
  60.  *  - Release Versions         => https://lakshmidrip.github.io/DROP/version.html
  61.  *  - Community Credits        => https://lakshmidrip.github.io/DROP/credits.html
  62.  *  - Issues Catalog           => https://github.com/lakshmiDRIP/DROP/issues
  63.  *  - JUnit                    => https://lakshmidrip.github.io/DROP/junit/index.html
  64.  *  - Jacoco                   => https://lakshmidrip.github.io/DROP/jacoco/index.html
  65.  *
  66.  *  Licensed under the Apache License, Version 2.0 (the "License");
  67.  *      you may not use this file except in compliance with the License.
  68.  *  
  69.  *  You may obtain a copy of the License at
  70.  *      http://www.apache.org/licenses/LICENSE-2.0
  71.  *  
  72.  *  Unless required by applicable law or agreed to in writing, software
  73.  *      distributed under the License is distributed on an "AS IS" BASIS,
  74.  *      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  75.  *  
  76.  *  See the License for the specific language governing permissions and
  77.  *      limitations under the License.
  78.  */

  79. /**
  80.  * <i>OptimalTrajectoryNoDrift</i> generates the Trade/Holdings List of Optimal Execution Schedule based on
  81.  * the Evolution Walk Parameters specified according to the Liquidity VaR Optimal Objective Function,
  82.  * exclusive of Drift. The Generation follows a Numerical Optimizer Scheme. The References are:
  83.  *
  84.  * <br><br>
  85.  *  <ul>
  86.  *      <li>
  87.  *          Almgren, R., and N. Chriss (1999): Value under Liquidation <i>Risk</i> <b>12 (12)</b>
  88.  *      </li>
  89.  *      <li>
  90.  *          Almgren, R., and N. Chriss (2000): Optimal Execution of Portfolio Transactions <i>Journal of
  91.  *              Risk</i> <b>3 (2)</b> 5-39
  92.  *      </li>
  93.  *      <li>
  94.  *          Almgren, R. (2003): Optimal Execution with Non-linear Impact Functions and Trading Enhanced Risk
  95.  *              <i>Applied Mathematical Finance</i> <b>10</b> 1-18
  96.  *      </li>
  97.  *      <li>
  98.  *          Artzner, P., F. Delbaen, J. M. Eber, and D. Heath (1999): Coherent Measures of Risk
  99.  *              <i>Mathematical Finance</i> <b>9</b> 203-228
  100.  *      </li>
  101.  *      <li>
  102.  *          Basak, S., and A. Shapiro (2001): Value-at-Risk Based Risk Management: Optimal Policies and Asset
  103.  *              Prices <i>Review of Financial Studies</i> <b>14</b> 371-405
  104.  *      </li>
  105.  *  </ul>
  106.  *
  107.  * <br><br>
  108.  *  <ul>
  109.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/PortfolioCore.md">Portfolio Core Module</a></li>
  110.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/TransactionCostAnalyticsLibrary.md">Transaction Cost Analytics Library</a></li>
  111.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/sample/README.md">Sample</a></li>
  112.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/sample/lvar/README.md">Liquidity VaR</a></li>
  113.  *  </ul>
  114.  * <br><br>
  115.  *
  116.  * @author Lakshmi Krishnamurthy
  117.  */

  118. public class OptimalTrajectoryNoDrift {

  119.     public static void main (
  120.         final String[] astrArgs)
  121.         throws Exception
  122.     {
  123.         EnvManager.InitEnv ("");

  124.         double dblS0 = 50.;
  125.         double dblSigma = 0.9487;
  126.         double dblAlpha = 0.02;
  127.         double dblEpsilon = 0.0625;
  128.         double dblGamma = 2.5e-07;
  129.         double dblEta = 2.5e-06;
  130.         double dblConfidenceLevel = 0.90;

  131.         double dblX = 1000000.;
  132.         double dblT = 5.;
  133.         int iN = 5;

  134.         double dblLambdaV = R1UnivariateNormal.Standard().confidenceInterval (dblConfidenceLevel);

  135.         DiscreteTradingTrajectoryControl dttc = DiscreteTradingTrajectoryControl.FixedInterval (
  136.             new OrderSpecification (
  137.                 dblX,
  138.                 dblT
  139.             ),
  140.             iN
  141.         );

  142.         LinearPermanentExpectationParameters lpep = ArithmeticPriceEvolutionParametersBuilder.LinearExpectation (
  143.             new ArithmeticPriceDynamicsSettings (
  144.                 0.,
  145.                 new FlatUnivariate (dblSigma),
  146.                 0.
  147.             ),
  148.             new UniformParticipationRateLinear (
  149.                 new ParticipationRateLinear (
  150.                     0.,
  151.                     dblGamma
  152.                 )
  153.             ),
  154.             new UniformParticipationRateLinear (
  155.                 new ParticipationRateLinear (
  156.                     dblEpsilon,
  157.                     dblEta
  158.                 )
  159.             )
  160.         );

  161.         EfficientTradingTrajectoryDiscrete ettd = (EfficientTradingTrajectoryDiscrete) new StaticOptimalSchemeDiscrete (
  162.             dttc,
  163.             lpep,
  164.             PowerVarianceObjectiveUtility.LiquidityVaR (dblLambdaV)
  165.         ).generate();

  166.         double[] adblExecutionTimeNode = ettd.executionTimeNode();

  167.         double[] adblTradeList = ettd.tradeList();

  168.         double[] adblHoldings = ettd.holdings();

  169.         LinearImpactTrajectoryEstimator lite = new LinearImpactTrajectoryEstimator (ettd);

  170.         R1UnivariateNormal r1un = lite.totalCostDistributionSynopsis (lpep);

  171.         System.out.println ("\n\t|---------------------------------------------||");

  172.         System.out.println ("\t| ALMGREN-CHRISS TRAJECTORY GENERATOR INPUTS  ||");

  173.         System.out.println ("\t|---------------------------------------------||");

  174.         System.out.println ("\t| Initial Stock Price           : " + dblS0);

  175.         System.out.println ("\t| Initial Holdings              : " + dblX);

  176.         System.out.println ("\t| Liquidation Time              : " + dblT);

  177.         System.out.println ("\t| Number of Time Periods        : " + iN);

  178.         System.out.println ("\t| Daily Volume 5 million Shares : " + dblGamma);

  179.         System.out.println ("\t| VaR Confidence Level          :" + FormatUtil.FormatDouble (dblConfidenceLevel, 2, 2, 100.) + "%");

  180.         System.out.println ("\t| VaR Based Risk Aversion       : " + dblLambdaV);

  181.         System.out.println ("\t|");

  182.         System.out.println (
  183.             "\t| Daily Volatility              : " +
  184.             FormatUtil.FormatDouble (dblSigma, 1, 4, 1.)
  185.         );

  186.         System.out.println (
  187.             "\t| Daily Returns                 : " +
  188.             FormatUtil.FormatDouble (dblAlpha, 1, 4, 1.)
  189.         );

  190.         System.out.println ("\t| Temporary Impact Fixed Offset :  " + dblEpsilon);

  191.         System.out.println ("\t| Eta                           :  " + dblEta);

  192.         System.out.println ("\t| Gamma                         :  " + dblGamma);

  193.         System.out.println ("\t|---------------------------------------------||");

  194.         System.out.println ("\n\t|-----------------------------||");

  195.         System.out.println ("\t| Optimal Trading Trajectory  ||");

  196.         System.out.println ("\t| ------- ------- ----------  ||");

  197.         System.out.println ("\t|     L -> R:                 ||");

  198.         System.out.println ("\t|        Time Node            ||");

  199.         System.out.println ("\t|        Holdings             ||");

  200.         System.out.println ("\t|        Trade Amount         ||");

  201.         System.out.println ("\t|-----------------------------||");

  202.         for (int i = 0; i <= iN; ++i) {
  203.             if (i == 0)
  204.                 System.out.println (
  205.                     "\t|" + FormatUtil.FormatDouble (adblExecutionTimeNode[i], 1, 0, 1.) + " => " +
  206.                     FormatUtil.FormatDouble (adblHoldings[i], 7, 1, 1.) + " | " +
  207.                     FormatUtil.FormatDouble (0., 6, 1, 1.) + " ||"
  208.                 );
  209.             else
  210.                 System.out.println (
  211.                     "\t|" + FormatUtil.FormatDouble (adblExecutionTimeNode[i], 1, 0, 1.) + " => " +
  212.                     FormatUtil.FormatDouble (adblHoldings[i], 7, 1, 1.) + " | " +
  213.                     FormatUtil.FormatDouble (adblTradeList[i - 1], 6, 1, 1.) + " ||"
  214.                 );
  215.         }

  216.         System.out.println ("\t|-----------------------------||");

  217.         System.out.println ("\n\t|----------------------------------------------------------------||");

  218.         System.out.println ("\t|  TRANSACTION COST RECONCILIATION: OPTIMAL vs. EXPLICIT LINEAR  ||");

  219.         System.out.println ("\t|----------------------------------------------------------------||");

  220.         System.out.println (
  221.             "\t| Transaction Cost Expectation         : " +
  222.             FormatUtil.FormatDouble (r1un.mean(), 7, 1, 1.) + " | " +
  223.             FormatUtil.FormatDouble (ettd.transactionCostExpectation(), 7, 1, 1.) + " ||"
  224.         );

  225.         System.out.println (
  226.             "\t| Transaction Cost Variance (X 10^-06) : " +
  227.             FormatUtil.FormatDouble (r1un.variance(), 7, 1, 1.e-06) + " | " +
  228.             FormatUtil.FormatDouble (ettd.transactionCostVariance(), 7, 1, 1.e-06) + " ||"
  229.         );

  230.         System.out.println ("\t|----------------------------------------------------------------||");

  231.         EnvManager.TerminateEnv();
  232.     }
  233. }