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
|
from serenitas.utils.db import dbconn
from process_queue import rename_keys
from io import StringIO
import csv
from serenitas.utils.env import DAILY_DIR
from serenitas.utils.remote import SftpClient
import datetime
HEADERS = {
"tranche": [
"Markit Field Name",
"Swap ID",
"Allocation ID",
"Description",
"Broker Id",
"DTCC CounterParty ID",
"Trade ID",
"Trade Date",
"Effective Date",
"Settle Date",
"Maturity Date",
"Account Abbreviation",
"1st Leg Notional",
"Currency Code",
"1st Leg Rate",
"Initial Payment",
"Initial Payment Currency",
"Original Issue Date",
"Interest Payment Method Description",
"First Payment Date",
"Product Type",
"Product Sub Type",
"Transaction Type",
"Protection",
"Transaction Code",
"Remaining Party",
"DTCC Remaining CounterParty ID",
"Independent Amount (%)",
"Independent Amount ($)",
"RED",
"Issuer Name",
"Settlement Amount",
"Trader",
"Executing Broker",
"Dealer",
"Trade ID",
"Notes",
"Parent Transaction Code",
"Parent Trade Date",
"Parent Notional",
"Parent Currency Code",
"Parent Net Amount",
"Parent Effective Date",
"Parent First Payment Date",
"Parent Settle Date",
"ComplianceHubAction",
"DTCC Ineligible",
"Master Document Date",
"Master Document Version",
"Include Contractual Supplement",
"Contractual Supplement",
"Supplement Date",
"Entity Matrix",
"Entity Matrix Date",
"Modified Equity Delivery",
"Calculation Agent Business Center",
"Calculation Agent",
"Attachment Point",
"Exhaustion Point",
"Strategy",
"First Payment Period Accrual Start Date",
"TieOut Ineligible",
"Electronic Consent Ineligible",
"External OMS ID",
"Independent Amount Currency",
"Independent Amount Payer",
"Trade Revision",
"Alternate Swap ID",
"Alternate Trade ID",
"Definitions Type",
]
}
def tranche_trades(conn):
with conn.cursor() as c:
trades = []
c.execute(
"SELECT * FROM cds where attach is not NULL and trade_date > %s",
(datetime.date(2021, 12, 1),),
)
for row in c:
obj = row._asdict()
rename_keys(
obj,
{
"dealid": "Trade ID",
"cp_code": "Broker Id",
"trade_date": "Trade Date",
"effective_date": "Effective Date",
"maturity": "Maturity Date",
"notional": "1st Leg Notional",
"fixed_rate": "1st Leg Rate",
"upfront": "Initial Payment",
"security_id": "RED",
"orig_attach": "Attachment Point",
"orig_detach": "Exhaustion Point",
# "upfront_settle_date": "First Payment Date",
},
)
if obj["Initial Payment"] >= 0:
obj["Transaction Code"] = "Receive"
else:
obj["Initial Payment"] = abs(obj["Initial Payment"])
obj["Transaction Code"] = "Pay"
obj["Swap ID"] = obj["Trade ID"]
obj["Product Type"] = "TRN"
obj["Transaction Type"] = "NEW"
obj["Protection"] = "Buy" if obj["protection"] == "Buyer" else "Sell"
obj["Trader"] = "Serenitas_Trader"
obj["Entity Matrix"] = "Publisher"
obj["Definitions Type"] = "ISDA2003Credit"
obj["Independent Amount (%)"] = obj["initial_margin_percentage"]
if "ITRX" in obj["security_desc"]:
obj["Include Contractual Supplement"] = "Y"
obj["Contractual Supplement"] = "iTraxxEuropeTranche"
# Temporary Static Values
obj["Account Abbreviation"] = "Serenitas-test1"
obj["Currency Code"] = "USD"
# obj["Initial Payment Currency"] = "USD"
# obj["First Payment Date"] = "2022-01-18"
obj["Broker Id"] = "0000571T"
# obj['DTCC Ineligible'] = 'N'
# obj['Master Document Date'] = '2001-01-01'
trades.append(obj)
return trades
def build_line(obj, asset_type):
return [obj.get(h, None) for h in HEADERS[asset_type]]
def process_upload(trades, asset_type, upload):
buf = StringIO()
csvwriter = csv.writer(buf)
csvwriter.writerow(HEADERS[asset_type])
csvwriter.writerows(build_line(trade, asset_type) for trade in trades)
buf = buf.getvalue().encode()
fname = f"MTM.{datetime.datetime.now():%Y%m%d.%H%M%S}.{asset_type.capitalize()}.csv"
sftp = SftpClient.from_creds("mtm")
sftp.put(buf, fname)
dest = DAILY_DIR / str(datetime.date.today()) / fname
dest.write_bytes(buf)
def upload_trades(conn):
process_upload(tranche_trades(conn), "tranche", upload=False)
if __name__ == "__main__":
conn = dbconn("dawndb")
upload_trades(conn)
|