aboutsummaryrefslogtreecommitdiffstats
path: root/python/position_file_bowdst.py
blob: 2d45ead0206fa77645dc62834aa3b95941306229 (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
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, 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)'] = 'Buyer' if obj['notional'] > 0 else 'Seller'
        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",
                "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",
                "protection": "TransactionIndicator (Buy/Sell)",
                "mtm": "MTM Valuation",
            },
        )
        trades.append(obj)
        process_upload(trades, "otc")