LinearImpactWithDrift.java

  1. package org.drip.sample.execution;

  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.*;
  8. import org.drip.execution.profiletime.UniformParticipationRateLinear;
  9. import org.drip.execution.risk.MeanVarianceObjectiveUtility;
  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>LinearImpactWithDrift</i> generates the Trade/Holdings List of Optimal Execution Schedule based on the
  81.  * Evolution Walk Parameters specified. The Generation follows a Numerical Optimizer Scheme, as opposed to
  82.  * the Almgren-Chriss Closed Form; it includes the Impact of Drift. 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.  *          Bertsimas, D., and A. W. Lo (1998): Optimal Control of Execution Costs <i>Journal of Financial
  95.  *              Markets</i> <b>1</b> 1-50
  96.  *      </li>
  97.  *      <li>
  98.  *          Chan, L. K. C., and J. Lakonishak (1995): The Behavior of Stock Prices around Institutional
  99.  *              Trades <i>Journal of Finance</i> <b>50</b> 1147-1174
  100.  *      </li>
  101.  *      <li>
  102.  *          Keim, D. B., and A. Madhavan (1997): Transaction Costs and Investment Style: An Inter-exchange
  103.  *              Analysis of Institutional Equity Trades <i>Journal of Financial Economics</i> <b>46</b>
  104.  *              265-292
  105.  *      </li>
  106.  *  </ul>
  107.  *
  108.  * <br><br>
  109.  *  <ul>
  110.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/PortfolioCore.md">Portfolio Core Module</a></li>
  111.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/TransactionCostAnalyticsLibrary.md">Transaction Cost Analytics Library</a></li>
  112.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/sample/README.md">Sample</a></li>
  113.  *      <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>
  114.  *  </ul>
  115.  * <br><br>
  116.  *
  117.  * @author Lakshmi Krishnamurthy
  118.  */

  119. public class LinearImpactWithDrift {

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

  125.         double dblS0 = 50.;
  126.         double dblX = 1000000.;
  127.         double dblT = 5.;
  128.         int iN = 5;
  129.         double dblAnnualVolatility = 0.30;
  130.         double dblAnnualReturns = 0.10;
  131.         double dblBidAsk = 0.125;
  132.         double dblDailyVolume = 5.e06;
  133.         double dblDailyVolumePermanentImpact = 0.1;
  134.         double dblDailyVolumeTemporaryImpact = 0.01;
  135.         double dblLambdaU = 1.e-06;

  136.         ArithmeticPriceDynamicsSettings apds = ArithmeticPriceDynamicsSettings.FromAnnualReturnsSettings (
  137.             dblAnnualReturns,
  138.             dblAnnualVolatility,
  139.             0.,
  140.             dblS0
  141.         );

  142.         double dblAlpha = apds.drift();

  143.         double dblSigma = apds.epochVolatility();

  144.         PriceMarketImpactLinear pmil = new PriceMarketImpactLinear (
  145.             new AssetTransactionSettings (
  146.                 dblS0,
  147.                 dblDailyVolume,
  148.                 dblBidAsk
  149.             ),
  150.             dblDailyVolumePermanentImpact,
  151.             dblDailyVolumeTemporaryImpact
  152.         );

  153.         ParticipationRateLinear prlPermanent = (ParticipationRateLinear) pmil.permanentTransactionFunction();

  154.         ParticipationRateLinear prlTemporary = (ParticipationRateLinear) pmil.temporaryTransactionFunction();

  155.         DiscreteTradingTrajectoryControl dttc = DiscreteTradingTrajectoryControl.FixedInterval (
  156.             new OrderSpecification (
  157.                 dblX,
  158.                 dblT
  159.             ),
  160.             iN
  161.         );

  162.         LinearPermanentExpectationParameters lpep = ArithmeticPriceEvolutionParametersBuilder.LinearExpectation (
  163.             new ArithmeticPriceDynamicsSettings (
  164.                 dblAlpha,
  165.                 new FlatUnivariate (dblSigma),
  166.                 0.
  167.             ),
  168.             new UniformParticipationRateLinear (prlPermanent),
  169.             new UniformParticipationRateLinear (prlTemporary)
  170.         );

  171.         EfficientTradingTrajectoryDiscrete ettd = (EfficientTradingTrajectoryDiscrete) new StaticOptimalSchemeDiscrete (
  172.             dttc,
  173.             lpep,
  174.             new MeanVarianceObjectiveUtility (dblLambdaU)
  175.         ).generate();

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

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

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

  179.         LinearImpactTrajectoryEstimator lite = new LinearImpactTrajectoryEstimator (ettd);

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

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

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

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

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

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

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

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

  188.         System.out.println ("\t| Annual Volatility             :" + FormatUtil.FormatDouble (dblAnnualVolatility, 1, 0, 100.) + "%");

  189.         System.out.println ("\t| Annual Growth                 :" + FormatUtil.FormatDouble (dblAnnualReturns, 1, 0, 100.) + "%");

  190.         System.out.println ("\t| Bid-Ask Spread                : " + dblBidAsk);

  191.         System.out.println ("\t| Daily Volume                  : " + dblDailyVolume);

  192.         System.out.println ("\t| Daily Volume Temporary Impact : " + dblDailyVolumeTemporaryImpact);

  193.         System.out.println ("\t| Daily Volume Permanent Impact : " + dblDailyVolumePermanentImpact);

  194.         System.out.println ("\t| Daily Volume 5 million Shares : " + prlPermanent.slope());

  195.         System.out.println ("\t| Static Holdings 11,000 Shares : " + dblLambdaU);

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

  197.         System.out.println (
  198.             "\t| Daily Volatility              : " +
  199.             FormatUtil.FormatDouble (dblSigma, 1, 4, 1.)
  200.         );

  201.         System.out.println (
  202.             "\t| Daily Returns                 : " +
  203.             FormatUtil.FormatDouble (dblAlpha, 1, 4, 1.)
  204.         );

  205.         System.out.println ("\t| Temporary Impact Fixed Offset :  " + prlTemporary.offset());

  206.         System.out.println ("\t| Eta                           :  " + prlTemporary.slope());

  207.         System.out.println ("\t| Gamma                         :  " + prlPermanent.slope());

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

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

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

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

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

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

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

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

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

  217.         for (int i = 0; i <= iN; ++i) {
  218.             if (i == 0)
  219.                 System.out.println (
  220.                     "\t|" + FormatUtil.FormatDouble (adblExecutionTimeNode[i], 1, 0, 1.) + " => " +
  221.                     FormatUtil.FormatDouble (adblHoldings[i], 7, 1, 1.) + " | " +
  222.                     FormatUtil.FormatDouble (0., 6, 1, 1.) + " ||"
  223.                 );
  224.             else
  225.                 System.out.println (
  226.                     "\t|" + FormatUtil.FormatDouble (adblExecutionTimeNode[i], 1, 0, 1.) + " => " +
  227.                     FormatUtil.FormatDouble (adblHoldings[i], 7, 1, 1.) + " | " +
  228.                     FormatUtil.FormatDouble (adblTradeList[i - 1], 6, 1, 1.) + " ||"
  229.                 );
  230.         }

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

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

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

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

  235.         System.out.println (
  236.             "\t| Transaction Cost Expectation         : " +
  237.             FormatUtil.FormatDouble (r1un.mean(), 6, 1, 1.) + " | " +
  238.             FormatUtil.FormatDouble (ettd.transactionCostExpectation(), 6, 1, 1.) + " ||"
  239.         );

  240.         System.out.println (
  241.             "\t| Transaction Cost Variance (X 10^-06) : " +
  242.             FormatUtil.FormatDouble (r1un.variance(), 6, 1, 1.e-06) + " | " +
  243.             FormatUtil.FormatDouble (ettd.transactionCostVariance(), 6, 1, 1.e-06) + " ||"
  244.         );

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

  246.         EnvManager.TerminateEnv();
  247.     }
  248. }