.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/market_microstructure/dealers/plot_03_avellaneda_stoikov.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_dealers_plot_03_avellaneda_stoikov.py: Avellaneda and Stoikov: market making in a limit order book =========================================================== Marco Avellaneda and Sasha Stoikov (2008) let a market maker quote around a Brownian mid price :math:`s`, with fills arriving at rate :math:`A e^{-k\delta}` at distance :math:`\delta`. A trader with exponential utility first computes the *reservation price* :math:`r = s - q\gamma\sigma^2(T - t)`, the mid price adjusted for its inventory :math:`q`, and then quotes a spread :math:`\gamma\sigma^2(T-t) + \frac{2}{\gamma}\ln(1 + \gamma/k)` around it. The example reproduces the comparison of their Table 1: the inventory strategy gives up a little expected profit for a much smaller variance. .. GENERATED FROM PYTHON SOURCE LINES 17-25 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from financialkit.market_microstructure import AvellanedaStoikov, SymmetricQuotes, simulate_market_making from financialkit.market_microstructure.visualizers import plot_market_making s0, sigma, T, n_steps, A, k = 100.0, 2.0, 1.0, 200, 140.0, 1.5 .. GENERATED FROM PYTHON SOURCE LINES 26-28 One trading session ------------------- .. GENERATED FROM PYTHON SOURCE LINES 28-33 .. code-block:: Python res = simulate_market_making(AvellanedaStoikov(0.1, sigma, k, T), s0, sigma, T, n_steps, A, k, n_paths=1, rng=5) axes = plot_market_making(res) axes[0].set_title(f"inventory strategy, gamma = 0.1: profit {res.pnl[0]:.1f}") .. image-sg:: /api/gallery/market_microstructure/dealers/images/sphx_glr_plot_03_avellaneda_stoikov_001.png :alt: inventory strategy, gamma = 0.1: profit 66.3 :srcset: /api/gallery/market_microstructure/dealers/images/sphx_glr_plot_03_avellaneda_stoikov_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Text(0.5, 1.0, 'inventory strategy, gamma = 0.1: profit 66.3') .. GENERATED FROM PYTHON SOURCE LINES 34-40 Inventory against symmetric quoting ----------------------------------- The symmetric strategy quotes the same average spread centred on the mid price. Paper values for :math:`\gamma = 0.1`: profit 62.94 (sd 5.89) against 67.21 (sd 13.43). .. GENERATED FROM PYTHON SOURCE LINES 40-51 .. code-block:: Python print(f"{'gamma':>6s} {'strategy':>10s} {'spread':>7s} {'profit':>7s} {'sd':>6s} {'q(T)':>6s} {'sd q':>6s}") for g in (0.01, 0.1, 0.5): inv = simulate_market_making(AvellanedaStoikov(g, sigma, k, T), s0, sigma, T, n_steps, A, k, n_paths=1000, rng=6) half = float(np.mean(inv.ask - inv.bid)) / 2 sym = simulate_market_making(SymmetricQuotes(half), s0, sigma, T, n_steps, A, k, n_paths=1000, rng=6) for name, r in [("inventory", inv), ("symmetric", sym)]: q = r.inventory[:, -1] print(f"{g:6.2f} {name:>10s} {2 * half:7.2f} {r.pnl.mean():7.2f} {r.pnl.std():6.2f} {q.mean():6.2f} {q.std():6.2f}") plt.show() .. rst-class:: sphx-glr-script-out .. code-block:: none gamma strategy spread profit sd q(T) sd q 0.01 inventory 1.35 68.26 8.82 0.10 5.16 0.01 symmetric 1.35 68.37 13.53 0.10 8.56 0.10 inventory 1.49 64.84 6.55 0.04 2.88 0.10 symmetric 1.49 68.24 13.48 0.12 8.36 0.50 inventory 2.16 48.21 5.94 0.04 1.90 0.50 symmetric 2.16 59.40 12.13 0.17 6.94 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.082 seconds) .. _sphx_glr_download_api_gallery_market_microstructure_dealers_plot_03_avellaneda_stoikov.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/dealers/plot_03_avellaneda_stoikov.ipynb :alt: Launch JupyterLite :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_03_avellaneda_stoikov.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_03_avellaneda_stoikov.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_03_avellaneda_stoikov.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_