aboutsummaryrefslogtreecommitdiffstats
path: root/python/notebooks/VaR.ipynb
blob: 178d72b340b6d3b45bcbfa65e9b2e9acac86c5c5 (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
259
260
261
262
263
264
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from analytics.curve_trades import curve_pos, on_the_run\n",
    "from analytics.index_data import get_index_quotes\n",
    "from analytics.scenarios import run_portfolio_scenarios\n",
    "from analytics import Swaption, BlackSwaption, CreditIndex, BlackSwaptionVolSurface, Portfolio, ProbSurface, DualCorrTranche\n",
    "from db import dbconn, dbengine\n",
    "\n",
    "import datetime\n",
    "import exploration.VaR as var\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "\n",
    "conn = dbconn('dawndb')\n",
    "dawndb = dbengine('dawndb')\n",
    "serenitasdb = dbengine('serenitasdb')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "date = (datetime.date.today() - pd.tseries.offsets.BDay(3)).date()\n",
    "report_date = (date + pd.tseries.offsets.BMonthEnd(-1)).date()\n",
    "index_type = \"IG\"\n",
    "quantile = .025"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#IG Curve VaR\n",
    "portf = curve_pos(date, index_type)\n",
    "ig_curve_var = abs(var.hist_var(portf, quantile=quantile, years=5))\n",
    "ig_curve_var"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#EU Curve VaR\n",
    "index_type = \"EU\"\n",
    "portf = curve_pos(date, index_type)\n",
    "eu_curve_var = abs(var.hist_var(portf, quantile=quantile, years=5))\n",
    "eu_curve_var"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#Mortgage Hedge VaR - use IG spread relative move for VaR\n",
    "df = pd.read_sql_query(\"SELECT * from list_cds_marks_by_strat(%s) where strategy ='HEDGE_MBS'\",\n",
    "                                 dawndb, params=(date,))\n",
    "portf = Portfolio([CreditIndex(row.p_index, row.p_series, row.tenor,\n",
    "                                       report_date, -row.notional)\n",
    "                      for row in df[['p_index', 'tenor', 'p_series', 'notional']].\n",
    "                       itertuples(index=False)])\n",
    "portf.mark()\n",
    "mort_hedge_var = abs(var.hist_var(portf, index_type = \"IG\", quantile=quantile, years=3))\n",
    "mort_hedge_var"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#Import the IM at the FCM account: calculate the IM share of different strategies as a share of VaR\n",
    "filename = date.strftime('%Y%m%d') + \"_OTC_MARGIN.csv\"\n",
    "margin_df = pd.read_csv(\"/home/serenitas/Daily/SG_reports/\" + filename, index_col='System Currency')\n",
    "mortg_hedge_im = mort_hedge_var + mort_hedge_var/(mort_hedge_var + ig_curve_var) * margin_df.loc[('USD', 'SG Settlement Margin')]\n",
    "mortg_hedge_im"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#Oct ME Bond HY Equiv\n",
    "bond_HY_equiv = -.12088\n",
    "percentile = .95"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#Calculate amount of stress for reports\n",
    "df = get_index_quotes('HY', list(range(on_the_run('HY') - 10, on_the_run('HY') + 1)),\n",
    "                          tenor=['5yr'], years=5)\n",
    "df = df.xs('5yr', level='tenor')['close_spread'].groupby(['date', 'series']).last()\n",
    "\n",
    "widen, tighten = [], []\n",
    "#approximately 1,3,6 months move (22 each months)\n",
    "for days in [22, 66, 132]: \n",
    "    calc = df.unstack().pct_change(freq= str(days)+'B').stack().groupby('date').last()\n",
    "    widen.append(calc.max())\n",
    "    tighten.append(calc.min())\n",
    "pd.DataFrame([widen, tighten], columns=['1M', '3M', '6M'], index=['widen', 'tighten'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#Current tranche and swaptions positions\n",
    "t_sql_string = (\"SELECT id, sum(notional * case when protection='Buyer' then -1 else 1 end) \"\n",
    "              \"OVER (partition by security_id, attach) AS ntl_agg \"\n",
    "              \"FROM cds WHERE swap_type='CD_INDEX_TRANCHE' AND termination_cp IS NULL \"\n",
    "               \"AND trade_date <= %s\")\n",
    "swaption_sql_string = (\"select id, security_desc from swaptions where date(expiration_date) \"\n",
    "                       \"> %s and swap_type = 'CD_INDEX_OPTION' \"\n",
    "                      \"AND trade_date <= %s\")\n",
    "index_sql_string = (\"SELECT id, sum(notional * case when protection='Buyer' then -1 else 1 end) \"\n",
    "                \"OVER (partition by security_id, attach) AS ntl_agg \"\n",
    "                \"FROM cds WHERE swap_type='CD_INDEX' AND termination_cp IS null \"\n",
    "                \"AND folder = 'IGOPTDEL' OR folder = 'HYOPTDEL' \"\n",
    "                \"AND trade_date <= %s\")\n",
    "with conn.cursor() as c:\n",
    "    #Get Tranche Trade Ids\n",
    "    c.execute(t_sql_string, (date,))\n",
    "    t_trade_ids = [dealid for dealid, ntl in c if ntl != 0]\n",
    "    #Get Swaption Trade Ids\n",
    "    c.execute(swaption_sql_string, (date, date))\n",
    "    swaption_trades = c.fetchall()\n",
    "    #Get Index/deltas Trade Ids\n",
    "    c.execute(index_sql_string, (date,))\n",
    "    index_trade_ids = [dealid for dealid, ntl in c if ntl != 0]\n",
    "    \n",
    "portf = Portfolio([DualCorrTranche.from_tradeid(dealid) for dealid in t_trade_ids],\n",
    "                  t_trade_ids)\n",
    "for row in swaption_trades:\n",
    "    option_delta = CreditIndex(row[1].split()[1], row[1].split()[3][1:], '5yr', date)\n",
    "    option_delta.mark()\n",
    "    portf.add_trade(BlackSwaption.from_tradeid(row[0], option_delta), 'opt_' + str(row[0]))\n",
    "for index_id in index_trade_ids:\n",
    "    portf.add_trade(CreditIndex.from_tradeid(index_id), 'index_' + str(index_id))\n",
    "    \n",
    "#Update manually - positive notional = long risk\n",
    "non_trancheSwap_risk_notional =    49119912 \n",
    "\n",
    "portf.add_trade(CreditIndex('HY', on_the_run('HY'), '5yr', value_date = date, notional = -non_trancheSwap_risk_notional), 'bond')\n",
    "    \n",
    "portf.value_date = date\n",
    "portf.mark(interp_method=\"bivariate_spline\")\n",
    "portf.reset_pv()\n",
    "    \n",
    "vs = BlackSwaptionVolSurface(portf.swaptions[0].index.index_type, \n",
    "                             portf.swaptions[0].index.series, \n",
    "                             value_date=date, \n",
    "                             interp_method = \"bivariate_spline\")\n",
    "vol_surface = vs[vs.list(option_type='payer')[-1]]\n",
    "vol_shock = [0]\n",
    "corr_shock = [0]\n",
    "spread_shock = widen + tighten\n",
    "date_range = [pd.Timestamp(date)]\n",
    "\n",
    "scens = run_portfolio_scenarios(portf, date_range, params=[\"pnl\"],\n",
    "                                spread_shock=spread_shock,\n",
    "                                vol_shock=vol_shock,\n",
    "                                corr_shock=corr_shock,\n",
    "                                vol_surface=vol_surface)\n",
    "\n",
    "scens.sum(axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "spread_shock = np.arange(-.4, 2.2, .2)\n",
    "scens = run_portfolio_scenarios(portf, date_range, params=[\"pnl\"],\n",
    "                                spread_shock=spread_shock,\n",
    "                                vol_shock=vol_shock,\n",
    "                                corr_shock=corr_shock,\n",
    "                                vol_surface=vol_surface)\n",
    "scens.sum(axis=1)\n",
    "\n",
    "risk_notional = [t.notional * t._index.duration for t in portf.indices]\n",
    "portf.trades[0]._index.duration()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#Calculate the margins for cleared CDS required for each strategy\n",
    "df = pd.read_sql_query(\"SELECT * from list_cds_marks_by_strat(%s)\",\n",
    "                                 dawndb, params=(date,))\n",
    "percentile = .95 #monthly 90%tile case.\n",
    "shocks, widen, tighten, onTR_dur, onTR_spread = {}, {}, {}, {}, {}\n",
    "for ind in ['IG', 'HY', 'EU']:\n",
    "    shocks[ind], onTR_spread[ind], onTR_dur[ind] = rel_spread_diff(date, index=ind)\n",
    "    widen[ind] = shocks[ind].quantile(percentile)\n",
    "    tighten[ind] = shocks[ind].quantile(1-percentile)\n",
    "\n",
    "df['onTR_notional'] = df.apply(lambda df:\n",
    "                               df.notional * df.factor * df.duration / onTR_dur[df.p_index], axis=1)\n",
    "df['widen'] = df.apply(lambda df:\n",
    "                       df.onTR_notional * onTR_spread[df.p_index] * onTR_dur[df.p_index] * widen[df.p_index]/10000, axis=1)\n",
    "df['tighten'] = df.apply(lambda df:\n",
    "                         df.onTR_notional * onTR_spread[df.p_index] * onTR_dur[df.p_index] * tighten[df.p_index]/10000, axis=1)\n",
    "delta_alloc = df.groupby('strategy').sum()\n",
    "delta_alloc['total'] = delta_alloc.apply(lambda df: max(abs(df.widen), abs(df.tighten)), axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "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.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}