-
-
Save rajgauttam/b7ecb3a36367827ddf21c05dcf709522 to your computer and use it in GitHub Desktop.
Optimal trading with order-flow and short-term alpha
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import numpy as np\n", | |
"import matplotlib.pyplot as plt\n", | |
"from scipy import interpolate\n", | |
"# random.seed(20)\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", | |
" }\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"def MakeHeatMap(t, y, fignum=False, nct=20, lower_threshold=0.1, upper_threshold=0.48):\n", | |
"\n", | |
" Nbins = 100\n", | |
"\n", | |
" miny = np.nanmin(y)\n", | |
" maxy = np.nanmax(y)\n", | |
" dy = (maxy - miny) / Nbins\n", | |
"\n", | |
" bins = np.linspace(miny, maxy, 100)\n", | |
"\n", | |
"\n", | |
" myNdt = 200\n", | |
" yr = np.full([myNdt, len(bins)], np.nan)\n", | |
"\n", | |
" mydt = (t[-1] - t[0]) / (myNdt - 1)\n", | |
"\n", | |
" tr = np.full([myNdt, ], np.nan)\n", | |
"\n", | |
" for i in range(myNdt):\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", | |
" 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", | |
"\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", | |
" #print(x_cord_i.shape)\n", | |
" #print(y_cord_i.shape)\n", | |
" #print(zr.shape)\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", | |
" return [fig, tr, yr, zr]\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"def PlotPath(t, T, Y, idxfig, sigma, y_range, title, lw=1):\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(y_range)\n", | |
"\n", | |
" for i in range(len(idxfig)):\n", | |
" plt.plot(t, Y[idxfig[i]], linewidth=lw, label=i+1)\n", | |
"\n", | |
" plt.ylabel(title, fontdict=font)\n", | |
" plt.xlabel('Time (Day) ', fontdict=font)\n", | |
" plt.legend()\n", | |
" plt.show()\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"def PlotHeatMap(t, Y, Y_AC, title, n=20, lower_threshold=0.1, upper_threshold=0.48):\n", | |
" out = MakeHeatMap(t, Y, nct=n)\n", | |
" [fig, tr, yr, zr] = out\n", | |
" plt.plot(t, Y_AC, '--k')\n", | |
" plt.ylabel(title, fontdict=font)\n", | |
" plt.xlabel('Time (Day) ', fontdict=font)\n", | |
" plt.show()" | |
] | |
} | |
], | |
"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