AlmgrenLinearTradingEnhanced.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.measure.gaussian.R1UnivariateNormal;
  11. import org.drip.numerical.common.FormatUtil;
  12. import org.drip.service.env.EnvManager;

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

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

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

  115. public class AlmgrenLinearTradingEnhanced {

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

  121.         double dblEta = 5.e-06;
  122.         double dblBeta = 2.e-06;
  123.         double dblSigma = 1.;
  124.         double dblLambda = 1.e-05;

  125.         double dblX = 100000.;
  126.         double dblT = 5.;
  127.         int iNumInterval = 20;

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

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

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

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

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

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

  153.         DiscreteLinearTradingEnhanced dlte = DiscreteLinearTradingEnhanced.Standard (
  154.             dblX,
  155.             dblT,
  156.             iNumInterval,
  157.             apep,
  158.             dblLambda
  159.         );

  160.         TradingEnhancedDiscrete ted = (TradingEnhancedDiscrete) dlte.generate();

  161.         double[] adblTradeListCF = ted.tradeList();

  162.         double[] adblHoldingsCF = ted.holdings();

  163.         TrajectoryShortfallEstimator tse = new TrajectoryShortfallEstimator (ettd);

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  201.         EnvManager.TerminateEnv();
  202.     }
  203. }