aboutsummaryrefslogtreecommitdiffstats
path: root/python/notebooks/Valuation Backtest.ipynb
blob: cc5d1501554e8b85684e1fe5d4728891fe634ebb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "from matplotlib.ticker import FuncFormatter \n",
    "from datetime import datetime\n",
    "import pandas as pd\n",
    "\n",
    "import mark_backtest_underpar as mark\n",
    "import globeop_reports as ops"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#exclude sell price that are over 200\n",
    "df_long = mark.back_test('2013-01-01', '2018-01-01', sell_price_threshold = 200)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#%matplotlib nbagg\n",
    "%matplotlib inline\n",
    "mark.pretty_plot(df_long)\n",
    "#file saved in serenitas shared drive/edwin/"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#exclude trades that are over 5x mark for purpose of regression\n",
    "diff_threshold = 5\n",
    "results = mark.stats(df_long, diff_threshold)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#Regression Intercept\n",
    "round(results[0],1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#Sale Difference\n",
    "round(results[1],2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#Now Calculate alternate valuation methodologies\n",
    "df = mark.get_mark_df()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "mark.count_sources(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#difference by source\n",
    "nav = ops.get_net_navs()['endbooknav']\n",
    "difference = mark.diff_by_source_percentage(df)\n",
    "#difference.to_clipboard()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#plot\n",
    "ax = difference.plot(kind = 'bar', legend = True, figsize = [10, 3.5])\n",
    "\n",
    "visible = ax.xaxis.get_ticklabels()[::6]\n",
    "for label in ax.xaxis.get_ticklabels():\n",
    "    if label not in visible:\n",
    "        label.set_visible(False)\n",
    "        \n",
    "ax.xaxis.set_major_formatter(plt.FixedFormatter(difference.index.to_series().dt.strftime(\"%b %Y\")))\n",
    "ax.set_ylabel('NAV Impact vs. Fund Policy (%)')\n",
    "vals = ax.get_yticks()\n",
    "ax.set_yticklabels(['{:3.0f}%'.format(x*100) for x in vals])\n",
    "ax.set_xlabel('')\n",
    "ax.grid(False, which='major',axis = 'x')\n",
    "lgd = ax.legend(loc='upper center', bbox_to_anchor=(0.5, -.3),  shadow=True, ncol=5)\n",
    "ax.figure.savefig(\"/home/serenitas/edwin/PythonGraphs/Valuation_2.png\", bbox_extra_artists=(lgd,), bbox_inches='tight')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "results = mark.alt_navs()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#%matplotlib nbagg\n",
    "to_plot = ['mark_closest_all', 'mark_mean_all']\n",
    "to_plot1 = ['mark_manager']\n",
    "plot_df0 = results[1][to_plot]\n",
    "plot_df1 = results[1][to_plot1]\n",
    "\n",
    "plot_df0 = plot_df0.rename(columns = {'mark_closest_all': 'Third-pary mark closest to LMCG valuation', \\\n",
    "                                    'mark_mean_all': 'Average of all third-party marks'})\n",
    "plot_df1 = plot_df1.rename(columns = {'mark_manager': 'Marks per fund valuation policy'})\n",
    "\n",
    "ax = plot_df0.plot(figsize = [10, 3.5])\n",
    "ax = plot_df1.plot(marker = 'o', ax = ax)\n",
    "plt.rcParams[\"font.family\"] = \"sans-serif\"\n",
    "ax.set_xlabel('')\n",
    "ax.set_ylabel('NAV', weight = 'bold')\n",
    "ax.set_title('Fund Return Using Different Valuation Methods', weight = 'bold')\n",
    "lgd = ax.legend(loc='upper center', bbox_to_anchor=(0.5, -.1),  shadow=True, ncol=3)\n",
    "ax.figure.savefig(\"/home/serenitas/edwin/PythonGraphs/Valuation_1.png\", bbox_extra_artists=(lgd,), bbox_inches='tight')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#Fund cumulative returns from the last 12 months\n",
    "results[1]['mark_manager'][-12:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "round(mark.annual_performance(results[1])*100,2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#A positive impact % means the alternative methodology results in a higher NAV than the fund's valuation policy.\n",
    "round(pd.DataFrame(mark.alt_nav_impact())*100,2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "pnl_breakdown = ops.curr_port_PNL()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#Unrealized MTM Gains/Loss\n",
    "breakdown_summary = pd.DataFrame(index = {'unreal mark-to-market'}, columns={'Gains','Loss','Net'})\n",
    "breakdown_summary['Gains'] = pnl_breakdown[pnl_breakdown['mtdbookunrealmtm']>=0].sum()['mtdbookunrealmtm']\n",
    "breakdown_summary['Loss'] = pnl_breakdown[pnl_breakdown['mtdbookunrealmtm']<0].sum()['mtdbookunrealmtm']\n",
    "breakdown_summary['Net'] = pnl_breakdown.sum()['mtdbookunrealmtm']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "breakdown_summary / nav[-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df = ops.trade_performance()\n",
    "df = df.set_index('trade_date')\n",
    "df.days_held = df.days_held.dt.days\n",
    "winner = df[df.percent_gain > 0]\n",
    "df[df.days_held.notnull()].groupby(pd.Grouper(freq='A')).mean()\n",
    "df"
   ]
  }
 ],
 "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.6.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}