.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/market_microstructure/execution/plot_03_almgren_chriss.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code or to run this example in your browser via JupyterLite. .. rst-class:: sphx-glr-example-title .. _sphx_glr_api_gallery_market_microstructure_execution_plot_03_almgren_chriss.py: Almgren and Chriss: the efficient frontier of execution ======================================================= Robert Almgren and Neil Chriss (2000) split impact into a permanent part :math:`\gamma` and a temporary part :math:`\eta` that punishes trading fast. Selling slowly is cheap in expectation but leaves the position exposed to volatility for longer. Minimising :math:`E + \lambda V` gives .. math:: x(t) = X \frac{\sinh(\kappa(T - t))}{\sinh(\kappa T)}, \qquad \kappa \approx \sqrt{\lambda\sigma^2/\eta}, and sweeping the risk aversion :math:`\lambda` traces an efficient frontier of expected cost against variance. The parameters are those of their worked example: one million shares at $50, 30% annual volatility, sold over five days. .. GENERATED FROM PYTHON SOURCE LINES 21-30 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from financialkit.market_microstructure import almgren_chriss from financialkit.market_microstructure.visualizers import plot_execution X, T, N = 1e6, 5.0, 5 sigma, eta, gamma, eps = 0.95, 2.5e-6, 2.5e-7, 0.0625 .. GENERATED FROM PYTHON SOURCE LINES 31-33 Trajectories ------------ .. GENERATED FROM PYTHON SOURCE LINES 33-41 .. code-block:: Python lams = [0.0, 2e-7, 2e-6, 2e-5] results = [almgren_chriss(X, T, N, sigma, eta, lam, gamma, eps) for lam in lams] ax = plot_execution(results, labels=[f"$\\lambda$ = {lam:g}" for lam in lams]) ax.set_xlabel("days") for lam, r in zip(lams, results, strict=True): print(f"lambda {lam:7.0e}: E = ${r.expected_cost / 1e6:.3f}M, sd = ${np.sqrt(r.variance) / 1e6:.3f}M") .. image-sg:: /api/gallery/market_microstructure/execution/images/sphx_glr_plot_03_almgren_chriss_001.png :alt: plot 03 almgren chriss :srcset: /api/gallery/market_microstructure/execution/images/sphx_glr_plot_03_almgren_chriss_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none lambda 0e+00: E = $0.662M, sd = $1.041M lambda 2e-07: E = $0.688M, sd = $0.896M lambda 2e-06: E = $1.141M, sd = $0.449M lambda 2e-05: E = $2.110M, sd = $0.101M .. GENERATED FROM PYTHON SOURCE LINES 42-44 The efficient frontier ---------------------- .. GENERATED FROM PYTHON SOURCE LINES 44-56 .. code-block:: Python frontier = [almgren_chriss(X, T, N, sigma, eta, lam, gamma, eps) for lam in np.logspace(-8, -4, 60)] fig, ax = plt.subplots(figsize=(6, 4)) ax.plot([np.sqrt(r.variance) / 1e6 for r in frontier], [r.expected_cost / 1e6 for r in frontier], "C0-") for lam, r in zip(lams, results, strict=True): ax.plot(np.sqrt(r.variance) / 1e6, r.expected_cost / 1e6, "o", label=f"$\\lambda$ = {lam:g}") ax.set_xlabel("standard deviation of cost ($M)") ax.set_ylabel("expected cost ($M)") ax.legend() fig.tight_layout() plt.show() .. image-sg:: /api/gallery/market_microstructure/execution/images/sphx_glr_plot_03_almgren_chriss_002.png :alt: plot 03 almgren chriss :srcset: /api/gallery/market_microstructure/execution/images/sphx_glr_plot_03_almgren_chriss_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.087 seconds) .. _sphx_glr_download_api_gallery_market_microstructure_execution_plot_03_almgren_chriss.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: lite-badge .. image:: images/jupyterlite_badge_logo.svg :target: ../../../../lite/lab/index.html?path=api/gallery/market_microstructure/execution/plot_03_almgren_chriss.ipynb :alt: Launch JupyterLite :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_03_almgren_chriss.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_03_almgren_chriss.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_03_almgren_chriss.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_