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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
|
from serenitas.utils.exchange import ExchangeMessage, FileAttachment
from io import BytesIO
from serenitas.utils.db import serenitas_pool, dbconn
import datetime
import pandas as pd
from pandas.tseries.offsets import BDay
from psycopg2.extras import execute_values
from pyisda.date import cds_accrued, default_accrual, previous_twentieth
from serenitas.analytics.index import CreditIndex
from copy import copy
def get_outstanding_positions(conn, trade_date, fcm, fund="SERCGMAST"):
with conn.cursor() as c:
c.execute(
"SELECT security_id, notional, folder, b.redindexcode, currency, "
"maturity, b.indexfactor "
"FROM list_cds_positions_by_strat_fcm(%s, %s, %s) a "
"LEFT JOIN index_version_markit b "
"ON a.security_id=b.prevredindexcode "
"LEFT JOIN index_version_markit c "
"ON a.security_id=c.redindexcode "
"WHERE b.redindexcode IS NOT NULL AND b.activeversion",
(trade_date, fcm, fund),
)
yield from c
def new_version_quotes(
conn, auction_date: datetime.date, recovery, accrual_days, coupon=0.05
):
adj_recovery = 100 * (
recovery + accrual_days * coupon / 360 - cds_accrued(auction_date, coupon)
)
with conn.cursor() as c1, conn.cursor() as c2:
c1.execute(
"SELECT index, series, old.version, old.indexfactor AS oldfactor, "
"new.indexfactor AS newfactor FROM index_version old "
"LEFT JOIN index_version new USING (index, series) "
"WHERE old.lastdate=%s AND new.version=old.version+1",
(auction_date,),
)
for index, series, version, oldfactor, newfactor in c1:
c2.execute(
"SELECT tenor, close_price FROM index_quotes_pre "
"WHERE index=%s AND series=%s AND version=%s AND date=%s AND source='MKIT'",
(index, series, version, auction_date),
)
w = oldfactor / newfactor
to_insert = []
for tenor, closeprice in c2:
new_price = w * closeprice + (1 - w) * adj_recovery
ind = CreditIndex(
index,
series,
tenor,
version + 1,
value_date=auction_date,
freeze_version=True,
)
try:
ind.price = new_price
except ValueError:
continue
to_insert.append((tenor, new_price, ind.spread))
c2.executemany(
"INSERT INTO index_quotes_pre(date, index, series, version, "
"tenor, close_price, close_spread, source) "
"VALUES (%s, %s, %s, %s, %s, %s, %s, %s)",
[
(auction_date, index, series, version + 1, *t, "SRNTAS")
for t in to_insert
],
)
conn.commit()
def default_adjustment(conn, company_id, seniority, end_date):
with conn.cursor() as c:
c.execute(
"SELECT recovery, event_date, auction_date FROM defaulted WHERE id=%s "
"AND seniority=%s",
(company_id, seniority),
)
recovery, event_date, auction_date = c.fetchone()
fee = 1 - recovery
start_date = previous_twentieth(event_date)
accrual_days, _ = default_accrual(
auction_date, event_date, start_date, end_date, 1.0, 1.0, "NONE"
)
return accrual_days, fee
PORTFOLIO = {
"HYOPTDEL": "OPTIONS",
"HEDGE_MBS": "MORTGAGES",
"HYINX": "TRANCHE",
"SER_IGCURVE": "CURVE",
"HEDGE_CLO": "CLO",
"HEDGE_MAC": "HEDGE_MAC",
"XOINX": "TRANCHE",
"SER_HYCURVE": "CURVE",
"CASH_BASIS": "HEDGE_MAC",
}
def desc_str(index_type, series):
if index_type in ("IG", "HY", "HYBB"):
return f"CDX {index_type} CDSI S{series} 5Y"
elif index_type == "XO":
return f"ITRX XOVER CDSI S{series} 5Y"
elif index_type == "EU":
return f"ITRX EUR CDSI S{series} 5Y"
def rebook(conn, trade_date, company_id, seniority, fcm, fund="SERCGMAST"):
dawndb = dbconn("dawndb")
upfront_settle_date = trade_date + 3 * BDay()
effective_date = trade_date + datetime.timedelta(days=1)
for r in get_outstanding_positions(dawndb, trade_date, fcm, fund):
accrual_days, fee = default_adjustment(conn, company_id, seniority, r.maturity)
index_old = CreditIndex(
redcode=r.security_id,
maturity=r.maturity,
value_date=trade_date,
notional=-r.notional,
freeze_version=True,
)
adj = (
(fee - accrual_days * index_old.fixed_rate * 1e-4 / 360)
* r.notional
* (r.indexfactor - index_old.factor)
)
index_old.mark()
trade_old = {
"fund": fund,
"action": "NEW",
"portfolio": PORTFOLIO[r.folder],
"folder": r.folder,
"cp_code": "CONTRA",
"custodian": "NONE",
"trade_date": trade_date,
"effective_date": effective_date,
"maturity": r.maturity,
"currency": r.currency,
"payment_rolldate": "Following",
"notional": abs(r.notional),
"fixed_rate": index_old.fixed_rate / 100,
"day_count": "ACT/360",
"frequency": 4,
"protection": index_old.direction,
"security_id": r.security_id,
"security_desc": desc_str(index_old.index_type, index_old.series),
"upfront": index_old.pv,
"upfront_settle_date": upfront_settle_date,
"swap_type": "CD_INDEX",
"account_code": fcm,
}
trade_new = copy(trade_old)
trade_new["protection"] = (
"Seller" if trade_old["protection"] == "Buyer" else "Buyer"
)
trade_new["upfront"] = -adj - index_old.pv
trade_new["security_id"] = r.redindexcode
sql_str = f"INSERT INTO cds({','.join(trade_new.keys())}) VALUES %s"
with dawndb.cursor() as c:
execute_values(
c, sql_str, [tuple(trade_old.values()), tuple(trade_new.values())]
)
dawndb.commit()
dawndb.close()
def insert_newids(dawndb, d: datetime.date, df: pd.DataFrame):
with dawndb.cursor() as c:
c.execute(
"INSERT INTO id_mapping ( "
" SELECT %s, 'CDS', serenitas_id, new_id FROM ( "
" SELECT * FROM unnest(%s, %s) AS t(globeop_id, new_id) "
" LEFT JOIN id_mapping USING (globeop_id)) a"
")",
(d, df.old_ids.tolist(), df.new_ids.to_list()),
)
dawndb.commit()
def send_csv(dawndb, d: datetime.date, ticker):
columns = [
"dealid",
"folder",
"cp_code",
"trade_date",
"effective_date",
"maturity",
"currency",
"payment_rolldate",
"notional",
"fixed_rate",
"day_count",
"frequency",
"protection",
"security_id",
"security_desc",
"upfront",
"upfront_settle_date",
"swap_type",
"account_code",
"portfolio",
"fund",
"indexfactor",
"version",
]
buf = BytesIO()
with dawndb.cursor() as c:
sql_str = (
f"COPY (SELECT {','.join(columns)} "
"FROM cds "
"JOIN index_version "
"ON security_id=redindexcode "
f"WHERE fund='BOWDST' AND cp_code='CONTRA' AND trade_date='{d}')"
" TO STDOUT WITH (FORMAT CSV, HEADER)"
)
c.copy_expert(sql_str, buf)
dawndb.commit()
buf = buf.getvalue()
em = ExchangeMessage()
body = f"""Hello,
These are the the rebookings due to {ticker} credit event on {d:%m/%d}. Let me know if you have any questions.
Thanks,
Guillaume"""
em.send_email(
f"{ticker} credit event",
body,
to_recipients=(
"caagtradecapture@bnymellon.com",
"hm-operations@bnymellon.com",
"julie.picariello@bnymellon.com",
),
cc_recipients=(
"sa1futures.optionsprocessing@bnymellon.com",
"bowdoin-ops@lmcg.com",
"Viraphong.Douangmany@BNYMellon.com",
),
attach=(FileAttachment(name=f"{ticker}_rebooking.csv", content=buf),),
)
if __name__ == "__main__":
conn = serenitas_pool.getconn()
dawndb = dbconn("dawndb")
# PKD
# rebook(datetime.date(2019, 1, 24), 101148)
# WINDSSE
# rebook(datetime.date(2019, 4, 8), 36806879)
# WFT
# rebook(datetime.date(2019, 7, 26), 103633, "WF")
# rebook(datetime.date(2019, 7, 26), 103633, "BAML")
# DF
# rebook(datetime.date(2019, 12, 11), 154954, "Senior", "BAML")
# MNI
# rebook(datetime.date(2020, 3, 13), 100957, "Senior", "BAML")
# rebook(datetime.date(2020, 3, 13), 100957, "Senior", "WF")
# WLL
# rebook(datetime.date(2020, 5, 7), 8240322, "Senior", "BAML")
# rebook(datetime.date(2020, 5, 7), 8240322, "Senior", "WF")
# rebook(conn, datetime.date(2020, 5, 7), 8240322, "Senior", "GS", "BOWDST")
# FCA
# rebook(conn, datetime.date(2020, 5, 14), 100337, "Senior", "BAML")
# rebook(conn, datetime.date(2020, 5, 14), 100337, "Senior", "WF")
# rebook(conn, datetime.date(2020, 5, 14), 100337, "Senior", "GS", "BOWDST")
# DO
# rebook(conn, datetime.date(2020, 5, 26), 171248, "Senior", "BAML")
# rebook(conn, datetime.date(2020, 5, 26), 171248, "Senior", "WF")
# rebook(conn, datetime.date(2020, 5, 26), 171248, "Senior", "GS", "BOWDST")
# NMG
# rebook(conn, datetime.date(2020, 5, 29), 101056, "Senior", "BAML")
# rebook(conn, datetime.date(2020, 5, 29), 101056, "Senior", "WF")
# rebook(conn, datetime.date(2020, 5, 29), 101056, "Senior", "GS", "BOWDST")
# JCP
# rebook(conn, datetime.date(2020, 6, 9), 101154, "Senior", "BAML")
# rebook(conn, datetime.date(2020, 6, 9), 101154, "Senior", "WF")
# rebook(conn, datetime.date(2020, 6, 9), 101154, "Senior", "GS", "BOWDST")
# HTZ
# rebook(conn, datetime.date(2020, 6, 24), 9833831, "Senior", "BAML")
# rebook(conn, datetime.date(2020, 6, 24), 9833831, "Senior", "WF")
# rebook(conn, datetime.date(2020, 6, 24), 9833831, "Senior", "GS", "BOWDST")
# CRC
# rebook(conn, datetime.date(2020, 7, 7), 39035708, "Senior", "BAML")
# rebook(conn, datetime.date(2020, 7, 7), 39035708, "Senior", "WF")
# rebook(conn, datetime.date(2020, 7, 7), 39035708, "Senior", "GS", "BOWDST")
# CHK
# rebook(conn, datetime.date(2020, 8, 4), 117019, "Senior", "BAML")
# rebook(conn, datetime.date(2020, 8, 4), 117019, "Senior", "WF")
# rebook(conn, datetime.date(2020, 8, 4), 117019, "Senior", "GS", "BOWDST")
# HEMA
# rebook(conn, datetime.date(2020, 9, 8), 40678877, "Senior", "BAML")
# rebook(conn, datetime.date(2020, 9, 8), 40678877, "Senior", "GS", "BOWDST")
# send_csv(dawndb, datetime.date(2020, 9, 8), "HEMA")
# NE
# rebook(conn, datetime.date(2020, 9, 10), 105641, "Senior", "BAML")
# rebook(conn, datetime.date(2020, 9, 10), 105641, "Senior", "GS", "BOWDST")
# send_csv(dawndb, datetime.date(2020, 9, 10), "NE")
# MATAFIN
# rebook(conn, datetime.date(2020, 9, 15), 15334701, "Subordinated", "BAML")
# rebook(conn, datetime.date(2020, 9, 15), 15334701, "Senior", "GS", "BOWDST")
# send_csv(dawndb, datetime.date(2020, 9, 15), "MATAFIN")
# PIZZAFI
# rebook(conn, datetime.date(2020, 10, 1), 41488375, "Senior", "BAML")
# rebook(conn, datetime.date(2020, 10, 1), 41488375, "Senior", "GS", "BOWDST")
# send_csv(dawndb, datetime.date(2020, 10, 1), "PIZZAFI")
# SELEGRO
# rebook(conn, datetime.date(2020, 10, 20), 40769171, "Senior", "BAML")
# rebook(conn, datetime.date(2020, 10, 20), 40769171, "Senior", "GS", "BOWDST")
# send_csv(dawndb, datetime.date(2020, 10, 20), "SELEGRO")
# EUROPCA
# rebook(conn, datetime.date(2021, 1, 13), 10758505, "Senior", "BAML")
# TALEN
rebook(conn, datetime.date(2022, 6, 7), 7359109, "Senior", "BAML")
rebook(conn, datetime.date(2022, 6, 7), 7359109, "Senior", "GS", "BOWDST")
rebook(conn, datetime.date(2022, 6, 7), 7359109, "Senior", "WF")
serenitas_pool.putconn(conn)
|