aboutsummaryrefslogtreecommitdiffstats
path: root/python/reallocate_iam.py
blob: 520b4291e8e7fb27a9d5c4644e295a8ee61aa664 (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
import datetime
from serenitas.ops.funds import Service
from report_ops.cash import IAMDeal
from serenitas.analytics.dates import prev_business_day

_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",
    "CS": "CSFBBO",
    "GS": "GOLDNY",
    "BNP": "BNPBNY",
    "MS": "MSCSNY",
    "JPM": "JPCBNY",
    "GS_FCM": "GOLDNY",
}


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


def get_custodian_account(fund, iam_broker):
    match fund, iam_broker:
        case ("SERCGMAST", "WF_FCM"):
            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 globeop_mapping(fund, iam_broker):
    (custodian, cash_account) = get_custodian_account(fund, iam_broker)
    return {
        "custodian": custodian,
        "cash_account": cash_account,
        "counterparty": get_counterparty(fund, iam_broker),
    }


def gen_new_iam_deals(fund, cob, conn):
    with conn.cursor() as c:
        c.execute(
            "SELECT * FROM list_iam(%s, %s)",
            (
                cob,
                fund,
            ),
        )
        for row in c:
            d = row._asdict() | globeop_mapping(fund, row.broker)
            yield IAMDeal.from_dict(**d)


def gen_new_iam_offsets(fund, cob, conn):
    with conn.cursor() as c:
        c.execute(
            "SELECT trade_date, broker, currency, fund, sum(start_money) AS start_money "
            "FROM list_iam(%s, %s) GROUP BY (trade_date, broker, currency, fund);",
            (
                cob,
                fund,
            ),
        )
        for row in c:
            d = (
                row._asdict()
                | globeop_mapping(fund, row.broker)
                | {"offset": True, "folder": "M_CSH_CASH"}
            )
            return IAMDeal.from_dict(**d)


def main(fund, cob, conn):
    service = Service[fund]
    for iam in gen_iam_deals(fund, cob, conn):
        iam.stage()
    if fund in ("BOWDST"):
        for iam in gen_iam_offsets(fund, cob, conn):
            iam.stage()
    for iam in IamDeal.commit():
        service.push(iam)
    buf, dest = service.build_buffer()


if __name__ == "__main__":
    import argparse
    from serenitas.utils.db import dbconn

    conn = dbconn("dawndb")
    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()),
    )
    args = parser.parse_args()
    for fund in ("BOWDST",):
        main(fund, args.cob, conn)