Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save eo1989/1c4a97d31fbc69e9117c3071533d3b08 to your computer and use it in GitHub Desktop.
Save eo1989/1c4a97d31fbc69e9117c3071533d3b08 to your computer and use it in GitHub Desktop.
Optimal execution with a percentage of volume target (Chap 9.2 of Algorithmic and High-Frequency Trading (c) Cartea, Jaimungal, & Penalva 2015 Cambridge University Press)
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"import time\n",
"import math\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"with __import__('importnb').Notebook(): \n",
" import TargetRate_MarketSpeed_Helper\n",
"from scipy import interpolate\n",
"np.random.seed(30)\n",
"np.seterr(divide='ignore', invalid='ignore')\n",
"font = {'family': 'serif',\n",
" 'style': 'italic',\n",
" # 'color': 'darkred',\n",
" 'weight': 1,\n",
" 'size': 16,\n",
" }"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"# Plot Path Map with respective to Time\n",
"def PlotPath(t, T, Y, idxfig, title, lw=0.5, midline=None):\n",
" fig_1 = plt.figure()\n",
" plt.tick_params(direction='in', bottom=True, top=True, left=True, right=True)\n",
" axes = fig_1.gca()\n",
" axes.set_xlim([0, T])\n",
" axes.set_ylim([np.nanmin(Y[idxfig]), np.nanmax(Y[idxfig])*1.1])\n",
"\n",
" for i in range(len(idxfig)):\n",
" plt.plot(t, Y[idxfig[i]], linewidth=lw, label=i+1)\n",
" \n",
" if midline!=None:\n",
" plt.plot(t, midline[1], '--k' )\n",
"\n",
" plt.ylabel(title, fontdict=font)\n",
" plt.xlabel('Time ($t$) ', fontdict=font)\n",
" plt.legend()\n",
" plt.show()\n"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
"# Plot Frequency Map \n",
"def PlotHist(X, xlabel, bins=50, percentiles=[0.01, 0.1, 0.5, 0.9, 0.99]):\n",
" # Visualizing price difference between strategies \n",
" fig_7 = plt.figure()\n",
" plt.tick_params(direction='in', bottom=True, top=True, left=True, right=True)\n",
"\n",
" n, b, p = plt.hist(X, bins)\n",
"\n",
" q = np.quantile(X, percentiles)\n",
" maxHeight = 1.1*np.max(n)\n",
" for i in range(len(q)):\n",
" plt.plot(q[i]*np.array([1,1]), np.array([0,maxHeight]), '--', label= percentiles[i])\n",
"\n",
" plt.ylabel('Frequency', fontdict=font)\n",
" plt.xlabel(xlabel, fontdict=font)\n",
" plt.ylim((0,maxHeight))\n",
" plt.legend(('qtl=0.01','qtl=0.10','qtl=0.50','qtl=0.90','qtl=0.99'))\n",
" plt.show()"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"# Plot Heat/Density Map\n",
"def MakeHeatMap(t, y, xlabel, ylabel, fignum=False, nct=20, Nbins=100, Ndt=200, lower_threshold=0.1, upper_threshold=0.48):\n",
"\n",
" miny = np.nanmin(y)\n",
" maxy = np.nanmax(y)\n",
" dy = (maxy - miny) / Nbins\n",
" bins = np.linspace(miny, maxy, 100)\n",
"\n",
"\n",
" yr = np.full([Ndt, len(bins)], np.nan)\n",
"\n",
" mydt = (t[-1] - t[0]) / (Ndt - 1)\n",
"\n",
" tr = np.full([Ndt, ], np.nan)\n",
"\n",
" for i in range(Ndt):\n",
" kk = np.where(t < t[0] + mydt * (i + 1))[-1][-1]\n",
" count = np.histogram(y[:, kk], np.arange(miny, maxy + 0.00001, dy))\n",
" yr[i, :] = count[0]\n",
" tr[i] = t[kk].item()\n",
"\n",
" zr = yr.T / len(y[:, 0])\n",
"\n",
" if not fignum:\n",
" fig = plt.figure()\n",
" else:\n",
" fig = plt.figure(fignum)\n",
" \n",
" plt.tick_params(direction='in', bottom=True, top=True, left=True, right=True)\n",
" axes = fig.gca()\n",
" axes.set_xlim(left=0)\n",
" axes.set_ylim(bottom=0, top=np.max(maxy))\n",
" x_cord_i, y_cord_i = np.meshgrid(tr, bins)\n",
" zr[zr < np.max(zr)*lower_threshold] = 0\n",
" zr[zr > np.max(zr)*upper_threshold] = np.max(zr)*upper_threshold\n",
" cmap = plt.get_cmap('YlOrRd')\n",
"\n",
" plt.contour(x_cord_i, y_cord_i, zr, nct, cmap=cmap, levels=np.linspace(zr.min(), zr.max(), 1000))\n",
"\n",
" lim = np.around(np.max(zr), int(np.around(-(np.log(0.10972799999999999)/np.log(10)))))\n",
" plt.colorbar(ticks=np.arange(0, np.max(zr), lim/10))\n",
"\n",
" y_1 = np.quantile(y, 0.05, axis=0)\n",
" y_2 = np.quantile(y, 0.5, axis=0)\n",
" y_3 = np.quantile(y, 0.95, axis=0)\n",
"\n",
" plt.plot(t, y_1, '--k', linewidth=2)\n",
" plt.plot(t, y_2, '--k', linewidth=2)\n",
" plt.plot(t, y_3, '--k', linewidth=2)\n",
"\n",
" plt.xlabel(xlabel)\n",
" plt.ylabel(ylabel)\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment