aboutsummaryrefslogtreecommitdiffstats
path: root/python/position_file_bowdst.py
blob: c89714d7451877f4d7faa37aee1b333e87281fee (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
from serenitas.utils.db import dbconn
import datetime
import csv
from io import StringIO
from pathlib import Path
from process_queue import rename_keys


def process_upload(trades, asset_type):
    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()
    dest = Path("/home/serenitas/flint/{asset_type}.csv")
    dest.write_bytes(buf)


def build_line(obj, asset_type):
    return [obj.get(h, None) for h in HEADERS[asset_type]]


# variables

date = datetime.date(2021, 11, 9)


dawndb = dbconn("dawndb")


HEADERS = {
    "bond": [
        "AccountNumber",
        "COB Date",
        "Prime Broker",
        "SecurityType",
        "CUSIP",
        "ISIN",
        "SEDOL",
        "SecurityDescription",
        "Position",
        "MarketPrice",
        "Currency",
        "Base Market Value",
        "Local Market Value",
        "Fx Rate",
    ],
    "otc": [
        "Client Name",
        "Fund Name",
        "Counterparty",
        "Product Type",
        "Unique Deal ID",
        "TransactionIndicator (Buy/Sell)",
        "PutCall Indicator (Call/Put)",
        "CapFloorIndicator",
        "CurrencyPair",
        "DealCurrencyA",
        "DealCurrencyB",
        "NotionalA",
        "NotionalB",
        "OriginalPrice",
        "Strike",
        "FixedRate",
        "Quantity",
        "Start Date",
        "Effective Date",
        "Maturity Date",
        "Underlying Maturity",
        "RecPayFixed",
        "Underlying (ISIN / CUSP / RED CODES)",
        "Underlying Desc",
        "Exercise Type",
        "MTM Currency",
        "MTM Valuation",
        "COB Date",
        "Clearing House Name",
    ],
}


with dawndb.cursor() as c:
    c.execute("SELECT * FROM risk_positions(%s, null, 'BOWDST') ", (date,))
    trades = []
    for row in c:
        obj = row._asdict()
        rename_keys(
            obj,
            {
                "identifier": "CUSIP",
                "description": "SecurityDescription",
                "notional": "Position",
                "price": "MarketPrice",
                "local_market_value": "Local Market Value",
                "usd_market_value": "Base Market Value",
            },
        )
        try:
            obj["Fx Rate"] = obj["Local Market Value"] / obj["Base Market Value"]
        except ZeroDivisionError:
            obj["Fx Rate"] = 1
        obj["AccountNumber"] = "TELHEEACPB"
        obj["Prime Broker"] = "TEST"
        obj["COB Date"] = date
        trades.append(obj)
        process_upload(trades, "bond")

    c.execute(
        "SELECT trb.trade_id, trb.serenitas_clean_nav + trb.serenitas_accrued as mtm, trb.notional as active_notional, cds.* FROM tranche_risk_bowdst trb left join cds on trade_id=id WHERE date=%s",
        (date,),
    )
    trades = []
    for row in c:
        obj = row._asdict()
        obj["Client Name"] = "HEDGEMARK"
        obj["Fund Name"] = "BOS_PAT_BOWDOIN"
        obj["Product Type"] = "Credit Index Tranche"
        obj["TransactionIndicator (Buy/Sell)"] = (
            "B" if obj["protection"] == "Buyer" else "S"
        )
        obj["MTM Currency"] = "USD"
        obj["COB Date"] = date
        obj["Clearing House Name"] = "Bilateral"
        rename_keys(
            obj,
            {
                "dealid": "Unique Deal ID",
                "cp_code": "Counterparty",
                "currency": "DealCurrencyA",
                "active_notional": "NotionalA",
                "fixed_rate": "FixedRate",
                "trade_date": "Start Date",
                "effective_date": "EffectiveDate",
                "maturity": "Maturity Date",
                "security_id": "Underlying (ISIN / CUSP / RED CODES)",
                "security_desc": "Underlying Desc",
                "mtm": "MTM Valuation",
            },
        )
        trades.append(obj)
        process_upload(trades, "otc")
    c.execute(
        "SELECT abs(spr.notional) AS active_notional, spr.serenitas_nav, swaptions.*, index_version_markit.annexdate FROM list_swaption_positions_and_risks(%s, 'BOWDST') spr LEFT JOIN swaptions  ON deal_id=dealid LEFT JOIN index_version_markit ON swaptions.security_id=redindexcode;",
        (date,),
    )
    trades = []
    for row in c:
        obj = row._asdict()
        obj["Client Name"] = "HEDGEMARK"
        obj["Fund Name"] = "BOS_PAT_BOWDOIN"
        obj["Product Type"] = "CD Swaption"
        obj["TransactionIndicator (Buy/Sell)"] = "B" if obj["buysell"] else "S"
        obj["PutCall Indicator (Call/Put)"] = (
            "P" if obj["option_type"] == "PAYER" else "C"
        )
        obj["Exercise Type"] = "European"
        obj["MTM Currency"] = "USD"
        obj["COB Date"] = date
        obj["Clearing House Name"] = "Bilateral"
        rename_keys(
            obj,
            {
                "dealid": "Unique Deal ID",
                "cp_code": "Counterparty",
                "currency": "DealCurrencyA",
                "active_notional": "NotionalA",
                "fixed_rate": "FixedRate",
                "strike": "Strike",
                "annexdate": "EffectiveDate",
                "trade_date": "Start Date",
                "maturity": "Maturity Date",
                "expiration_date": "Underlying Maturity",
                "security_id": "Underlying (ISIN / CUSP / RED CODES)",
                "security_desc": "Underlying Desc",
                "serenitas_nav": "MTM Valuation",
            },
        )
        trades.append(obj)
        process_upload(trades, "otc")