aboutsummaryrefslogtreecommitdiffstats
path: root/python/reallocate_iam.py
blob: 2846938dedb47fd9ad805ee4804f9830b68d3d72 (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
import datetime
import argparse
from dataclasses import field, dataclass

from serenitas.ops.funds import Service
from serenitas.ops.trade_dataclasses import Deal, Fund
from serenitas.ops.headers import DealType
from serenitas.analytics.dates import prev_business_day, next_business_day
from serenitas.utils.db import dbconn

_bowdst_iam_cp = {
    "BAML_ISDA": "BOANNY",
    "CS": "CSITLN",
    "GS": "GOLINY",
    "BNP": "BNPBNY",
    "MS": "MSCILN",
    "JPM": "JPCBNY",
    "GS_FCM": "GOLDNY",
}

_serenitas_iam_cp = {
    "BAML_ISDA": "BAMSNY",
    "BAML_FCM": "BAMSNY",
    "CS": "CSFBBO",
    "GS": "GOLDNY",
    "BNP": "BNPBNY",
    "MS": "MSCSNY",
    "JPM": "JPCBNY",
    "WELLS": "WELFEI",
    "CITI": "CITINY",
    "BARCLAYS": "BARCNY",
}


def get_custodian_account(fund, iam_broker):
    match fund, iam_broker:
        case ("SERCGMAST", "WELLS"):
            return ("WELLSFCM", "WFNSCLMFCM")
        case ("SERCGMAST", "BAML_FCM"):
            return ("BOMLCM", "V0NSCLMFCM")
        case ("BOWDST", "GS_FCM"):
            return ("GS", "057363418ICE-CDS")
        case ("SERCGMAST", _):
            return ("UMB", "159260.1")
        case ("BOWDST", _):
            return ("BNY", "751254")


def get_counterparty(fund, iam_broker):
    match fund:
        case "SERCGMAST":
            return _serenitas_iam_cp[iam_broker]
        case "BOWDST":
            return _bowdst_iam_cp[iam_broker]


@dataclass
class IAMDeal(Deal, deal_type=DealType.IAM, table_name="iams"):
    trade_date: datetime.date = field(metadata={"globeop": "Trade Date"})
    folder: str = field(metadata={"globeop": "Folder"})
    portfolio: str = field(metadata={"globeop": "Portfolio"})
    broker: str
    start_money: float = field(metadata={"globeop": "StartMoney"})
    currency: str = field(metadata={"globeop": "Currency"})
    fund: Fund = field(metadata={"globeop": "Fund"})
    maturity: datetime.date = field(
        default=None, metadata={"globeop": "ExpirationDate"}
    )
    uploaded: bool = False
    is_offset: bool = False
    id: int = field(default=None, metadata={"insert": False})
    dealid: str = field(
        default=None,
        metadata={
            "insert": False,
            "globeop": "Deal Id",
        },
    )
    cash_account: str = field(
        default=None,
        metadata={"insert": False, "select": False, "globeop": "Cash Account"},
    )
    custodian: str = field(default=None, metadata={"insert": False, "select": False})

    def to_globeop(self, action):
        obj = super().to_globeop(action)
        obj["CallNoticeIndicator"] = "24H" if not self.maturity else None
        obj["TransactionIndicator"] = "DEPOSIT" if obj["StartMoney"] > 0 else "LOAN"
        obj["StartMoney"] = abs(obj["StartMoney"])
        obj["DealFunction"] = "OTC"
        obj["MarginType"] = "Net"
        obj["Collateralized"] = "Y"
        obj["Basis"] = "ACT/360"
        obj["SettlementDate"] = self.trade_date
        obj["ClearingFacilityCcpTradeRef"] = (
            "ICE-CLEAR"
            if (self.broker.endswith("_FCM") or self.broker == "WELLS")
            else None
        )
        (custodian, cash_account) = get_custodian_account(self.fund, self.broker)
        obj = obj | {
            "Custodian": custodian,
            "Cash Account": cash_account,
            "Counterparty": get_counterparty(self.fund, self.broker),
        }
        return obj


def cancel_old_iam_trades(fund: str, cob: datetime.date, conn) -> "Iterable":
    """Finds and cancels old IAM trades"""
    with conn.cursor() as cursor:
        cursor.execute(
            "DELETE FROM iams WHERE trade_date=%s AND fund=%s RETURNING *",
            (cob, fund),
        )
        for row in cursor:
            trade_data = row._asdict()
            yield IAMDeal.from_dict(**trade_data)
        conn.commit()


def generate_new_iam_trades(fund: str, cob: datetime.date, conn) -> "Iterable":
    """Generates new IAM deals"""
    with conn.cursor() as cursor:
        cursor.execute(
            "SELECT * FROM list_iam(%s, %s)",
            (cob, fund),
        )
        for row in cursor:
            trade_data = row._asdict() | {"trade_date": cob}
            yield IAMDeal.from_dict(**trade_data)


def generate_new_iam_offset_trades(fund: str, cob: datetime.date, conn) -> "Iterable":
    """Generates offsets if the sma has already updated these IAM deals"""
    _ignore = {"BOWDST": ("GS_FCM",)}
    with conn.cursor() as cursor:
        cursor.execute(
            "SELECT broker, currency, fund, sum(-start_money) AS start_money "
            "FROM list_iam(%s, %s) GROUP BY (broker, currency, fund);",
            (cob, fund),
        )
        for row in cursor:
            if row.broker not in _ignore[fund]:
                trade_data = row._asdict() | {
                    "is_offset": True,
                    "folder": "M_CSH_CASH",
                    "portfolio": "CASH",
                    "trade_date": cob,
                }
                yield IAMDeal.from_dict(**trade_data)


def update_matured_iam_trades(fund: str, prev_date: datetime.date, conn) -> "Iterable":
    """Sets previous days as matured"""
    with conn.cursor() as cursor:
        cursor.execute(
            "UPDATE iams SET maturity=%s WHERE maturity is NULL AND trade_date<=%s AND fund=%s RETURNING *",
            (next_business_day(prev_date), prev_date, fund),
        )
        for row in cursor:
            trade_data = row._asdict() | {}
            yield IAMDeal.from_dict(**trade_data)
        conn.commit()


def build_iam(fund: str, cob: datetime.date, conn, upload: bool):
    """Generates IAM file for globeop"""
    service = Service[fund]
    for old_iam in cancel_old_iam_trades(fund, cob, conn):
        service.push_trade(old_iam, "CANCEL")
    for new_iam in generate_new_iam_trades(fund, cob, conn):
        new_iam.stage()
    if fund == "BOWDST":
        for new_iam_offset in generate_new_iam_offset_trades(fund, cob, conn):
            new_iam_offset.stage()
    for iam in IAMDeal.commit(returning=True):
        service.push_trade(iam, "NEW")
    for update_iam in update_matured_iam_trades(fund, prev_business_day(cob), conn):
        service.push_trade(update_iam, "UPDATE")
    buf, dest = service.build_buffer(trade_type="iam")
    if upload:
        service.upload(buf, dest.name)
    service().clear()


def parse_args():
    parser = argparse.ArgumentParser(description="Generate IAM file for globeop")
    parser.add_argument(
        "cob",
        nargs="?",
        type=datetime.date.fromisoformat,
        default=prev_business_day(datetime.date.today()),
    )
    parser.add_argument("-n", "--no-upload", action="store_true", help="do not upload")
    parser.add_argument(
        "-b", "--backfill", action="store_true", help="backfill an old date"
    )
    return parser.parse_args()


if __name__ == "__main__":
    conn = dbconn("dawndb")
    args = parse_args()
    for fund in ("SERCGMAST", "BOWDST"):
        build_iam(fund, args.cob, conn, not args.no_upload)