AlmgrenConstantTradingEnhanced.java

  1. package org.drip.sample.execution;

  2. import org.drip.execution.capture.TrajectoryShortfallEstimator;
  3. import org.drip.execution.dynamics.*;
  4. import org.drip.execution.impact.ParticipationRateLinear;
  5. import org.drip.execution.nonadaptive.*;
  6. import org.drip.execution.optimum.*;
  7. import org.drip.execution.profiletime.UniformParticipationRateLinear;
  8. import org.drip.execution.risk.MeanVarianceObjectiveUtility;
  9. import org.drip.execution.strategy.*;
  10. import org.drip.function.definition.R1ToR1;
  11. import org.drip.measure.gaussian.R1UnivariateNormal;
  12. import org.drip.numerical.common.FormatUtil;
  13. import org.drip.service.env.EnvManager;

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

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

  78. /**
  79.  * <i>AlmgrenConstantTradingEnhanced</i> demonstrates the Generation of the Optimal Trading Trajectory under
  80.  * the Condition of Constant Trading Enhanced Volatility using a Numerical Optimization Technique. The
  81.  * References are:
  82.  *
  83.  * <br><br>
  84.  *  <ul>
  85.  *      <li>
  86.  *          Almgren, R., and N. Chriss (1999): Value under Liquidation <i>Risk</i> <b>12 (12)</b>
  87.  *      </li>
  88.  *      <li>
  89.  *          Almgren, R., and N. Chriss (2000): Optimal Execution of Portfolio Transactions <i>Journal of
  90.  *              Risk</i> <b>3 (2)</b> 5-39
  91.  *      </li>
  92.  *      <li>
  93.  *          Almgren, R. (2003): Optimal Execution with Nonlinear Impact Functions and Trading-Enhanced Risk
  94.  *              <i>Applied Mathematical Finance</i> <b>10 (1)</b> 1-18
  95.  *      </li>
  96.  *      <li>
  97.  *          Almgren, R., and N. Chriss (2003): Bidding Principles <i>Risk</i> 97-102
  98.  *      </li>
  99.  *      <li>
  100.  *          Bertsimas, D., and A. W. Lo (1998): Optimal Control of Execution Costs <i>Journal of Financial
  101.  *              Markets</i> <b>1</b> 1-50
  102.  *      </li>
  103.  *  </ul>
  104.  *
  105.  * <br><br>
  106.  *  <ul>
  107.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/PortfolioCore.md">Portfolio Core Module</a></li>
  108.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/TransactionCostAnalyticsLibrary.md">Transaction Cost Analytics Library</a></li>
  109.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/sample/README.md">Sample</a></li>
  110.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/sample/execution/README.md">Execution</a></li>
  111.  *  </ul>
  112.  * <br><br>
  113.  *
  114.  * @author Lakshmi Krishnamurthy
  115.  */

  116. public class AlmgrenConstantTradingEnhanced {

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

  122.         double dblEta = 5.e-06;
  123.         double dblAlpha = 1.;
  124.         double dblSigma = 1.;
  125.         double dblLambda = 1.e-05;

  126.         double dblX = 100000.;
  127.         double dblT = 5.;
  128.         int iNumInterval = 500;

  129.         DiscreteTradingTrajectoryControl dttc = DiscreteTradingTrajectoryControl.FixedInterval (
  130.             new OrderSpecification (
  131.                 dblX,
  132.                 dblT
  133.             ),
  134.             iNumInterval
  135.         );

  136.         ArithmeticPriceEvolutionParameters apep = ArithmeticPriceEvolutionParametersBuilder.TradingEnhancedVolatility (
  137.             dblSigma,
  138.             new UniformParticipationRateLinear (ParticipationRateLinear.SlopeOnly (dblEta)),
  139.             new UniformParticipationRateLinear (
  140.                 new ParticipationRateLinear (
  141.                     dblAlpha,
  142.                     0.
  143.                 )
  144.             )
  145.         );

  146.         EfficientTradingTrajectoryDiscrete ettd = (EfficientTradingTrajectoryDiscrete) new StaticOptimalSchemeDiscrete (
  147.             dttc,
  148.             apep,
  149.             new MeanVarianceObjectiveUtility (dblLambda)
  150.         ).generate();

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

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

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

  154.         ContinuousConstantTradingEnhanced ccte = ContinuousConstantTradingEnhanced.Standard (
  155.             dblX,
  156.             dblT,
  157.             apep,
  158.             dblLambda
  159.         );

  160.         EfficientTradingTrajectoryContinuous ettc = (EfficientTradingTrajectoryContinuous) ccte.generate();

  161.         R1ToR1 r1ToR1Holdings = ettc.holdings();

  162.         double[] adblHoldingsCF = new double[adblExecutionTimeNode.length];

  163.         for (int i = 0; i < adblExecutionTimeNode.length; ++i)
  164.             adblHoldingsCF[i] = r1ToR1Holdings.evaluate (adblExecutionTimeNode[i]);

  165.         TrajectoryShortfallEstimator tse = new TrajectoryShortfallEstimator (ettd);

  166.         R1UnivariateNormal r1un = tse.totalCostDistributionSynopsis (apep);

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

  168.         System.out.println ("\t| NUMERICAL - CLOSED FORM CONTINUOUS TRAJECTORY  ||");

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

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

  171.         System.out.println ("\t|          - Execution Time Node                 ||");

  172.         System.out.println ("\t|          - Holdings (Numerical)                ||");

  173.         System.out.println ("\t|          - Holdings (Continuous Closed Form)   ||");

  174.         System.out.println ("\t|          - Trade List (Numerical)              ||");

  175.         System.out.println ("\t|          - Trade List (Continuous Closed Form) ||");

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

  177.         for (int i = 1; i < adblExecutionTimeNode.length; ++i) {
  178.             System.out.println ("\t| " +
  179.                 FormatUtil.FormatDouble (adblExecutionTimeNode[i], 1, 2, 1.) + " => " +
  180.                 FormatUtil.FormatDouble (adblHoldings[i] / dblX, 2, 2, 100.) + "% | " +
  181.                 FormatUtil.FormatDouble (adblHoldingsCF[i] / dblX, 2, 2, 100.) + "% | " +
  182.                 FormatUtil.FormatDouble (adblTradeList[i - 1] / dblX, 2, 2, 100.) + "% | " +
  183.                 FormatUtil.FormatDouble ((adblHoldingsCF[i] - adblHoldingsCF[i - 1]) / dblX, 2, 2, 100.) + "% ||"
  184.             );
  185.         }

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

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

  188.         System.out.println ("\t|  TRANSACTION COST RECONCILIATION: EXPLICIT vs. NUMERICAL vs. CLOSED FORM ||");

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

  190.         System.out.println (
  191.             "\t| Transaction Cost Expectation         : " +
  192.             FormatUtil.FormatDouble (r1un.mean(), 6, 1, 1.) + " | " +
  193.             FormatUtil.FormatDouble (ettd.transactionCostExpectation(), 6, 1, 1.) + " | " +
  194.             FormatUtil.FormatDouble (ettc.transactionCostExpectation(), 6, 1, 1.) + " ||"
  195.         );

  196.         System.out.println (
  197.             "\t| Transaction Cost Variance (X 10^-06) : " +
  198.             FormatUtil.FormatDouble (r1un.variance(), 6, 1, 1.e-06) + " | " +
  199.             FormatUtil.FormatDouble (ettd.transactionCostVariance(), 6, 1, 1.e-06) + " | " +
  200.             FormatUtil.FormatDouble (ettc.transactionCostVariance(), 6, 1, 1.e-06) + " ||"
  201.         );

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

  203.         EnvManager.TerminateEnv();
  204.     }
  205. }