aboutsummaryrefslogtreecommitdiffstats
path: root/python/citco_monitor.py
blob: 56bbb0e34abd337b9fb0f9a2dc0962334db9bc02 (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
import datetime
from exchangelib import HTMLBody

from serenitas.analytics.dates import prev_business_day
from serenitas.utils.db import dbconn
from serenitas.ops.dataclass_mapping import _citco_cp_isda

from report_ops.utils import check_cleared_cds, Monitor
from report_ops.misc import _recon_recipients, _cc_recipients


class BondFactorMonitor(
    Monitor,
    headers=(
        "date",
        "citco_security_id",
        "security_id",
        "serenitas_factor",
        "admin_factor",
        "difference",
    ),
    num_format=[("{0:,.2f}", 3), ("{0:,.2f}", 4), ("{0:,.2f}", 5)],
):
    @classmethod
    def email(cls, fund):
        if not cls._staging_queue:
            return
        cls._em.send_email(
            f"*ACTION REQUESTED* Incorrect Factors: {fund}",
            HTMLBody(
                f"""
<html>
  <head>
    <style>
      table, th, td {{ border: 1px solid black;  border-collapse: collapse;}}
      th, td {{ padding: 5px; }}
    </style>
  </head>
  <body>
  Hello,<br><br>We see the below factor mismatches. Could you please correct the below?<br><br>{cls.to_tabulate()}
  </body>
</html>"""
            ),
            to_recipients=_recon_recipients[fund],
            cc_recipients=_cc_recipients[fund],
        )


class SwaptionExerciseNotification(
    Monitor,
    headers=(
        "swaption_fix_id",
        "security_desc",
        "fund",
        "expiration_date",
        "strike",
        "clearing_agent",
        "putcall",
        "notional",
        "swaption_expiration_status",
        "exercised_fix_id",
    ),
    num_format=[("{0:,.4f}", 4), ("{0:,.2f}", 7)],
):
    @classmethod
    def email(cls, fund, expiration_date):
        if not cls._staging_queue:
            return
        cls._em.send_email(
            f"Citco Exercise Notification: {fund} Expiration: {expiration_date}",
            HTMLBody(
                f"""
<html>
  <head>
    <style>
      table, th, td {{ border: 1px solid black;  border-collapse: collapse;}}
      th, td {{ padding: 5px; }}
    </style>
  </head>
  <body>
  Hello,<br><br>We would like to notify you of the exercised/expired swaptions:<br><br>{cls.to_tabulate()}
  </body>
</html>"""
            ),
            to_recipients=_recon_recipients[fund],
            cc_recipients=_cc_recipients[fund],
        )


def check_bond_factors(date, fund, conn):
    with conn.cursor() as c:
        c.execute(
            "SELECT citco_security_id, security_id, serenitas_factor, admin_factor, serenitas_factor-admin_factor as difference FROM compare_citco_bonds (%s, %s) WHERE abs(serenitas_factor - admin_factor) > .01;",
            (date, fund),
        )
        for row in c:
            d = row._asdict() | {"date": date}
            BondFactorMonitor.stage(d)
        BondFactorMonitor.email(fund)
        BondFactorMonitor.clear()


def notify_swaption_exercise(expiration_date, fund, conn):
    with conn.cursor() as c:
        c.execute(
            "SELECT * FROM get_exercised_swaptions(%s, %s)",
            (
                expiration_date,
                fund,
            ),
        )
        for row in c:
            d = row._asdict()
            if d["security_desc"].startswith("CDX IG"):
                d["strike"] /= 100
            d["putcall"] = "PUT" if d["option_type"] == "PAYER" else "CALL"
            d["notional"] = d["notional"] * (1 if d["buysell"] else -1)
            d["swaption_fix_id"] = d["dealid"]
            d["exercised_fix_id"] = d["new_trade"]
            d["clearing_agent"] = _citco_cp_isda[d["cp_code"]]
            d["swaption_expiration_status"] = (
                "EXERCISED" if d["new_trade"] else "EXPIRED"
            )
            SwaptionExerciseNotification.stage(d)
        SwaptionExerciseNotification.email(fund, expiration_date)
        SwaptionExerciseNotification.clear()


if __name__ == "__main__":
    import argparse

    parser = argparse.ArgumentParser()
    parser.add_argument(
        "cob",
        nargs="?",
        type=datetime.date.fromisoformat,
        default=prev_business_day(datetime.date.today()),
        help="working date",
    )
    args = parser.parse_args()
    conn = dbconn("dawndb")
    for fund in ("ISOSEL",):
        check_cleared_cds(args.cob, fund, conn)
        check_bond_factors(args.cob, fund, conn)
        notify_swaption_exercise(args.cob, fund, conn)