aboutsummaryrefslogtreecommitdiffstats
path: root/python/innocap_bond_setts.py
blob: 203023c8ff981fe29e68c8e52a0daeaa7e2cdad9 (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
from serenitas.analytics.dates import prev_business_day
from serenitas.utils.db import dbconn
from serenitas.utils.exchange import ExchangeMessage
import datetime
import logging

_recipients = {
    "ISOSEL": (
        "simon.oreilly@innocap.com",
        "margincalls@innocapglobal.com",
    ),
}


def _formatting(payment_values):
    if not payment_values:
        return None
    else:
        return "\n".join(
            f"\t* {settle_date}: {amount:,.2f} {currency}"
            for settle_date, (amount, currency) in payment_values.items()
        )


def payment_values(ped, conn, fund):
    sql_str = "SELECT settle_date, currency, sum(payment_amount) as payment_amount FROM payment_settlements ps2 WHERE fund=%s AND asset_class='bond' AND %s < (settle_date + INTERVAL '5' DAY) group by settle_date, currency;"
    with conn.cursor() as c:
        c.execute(sql_str, (fund, ped))
        return _formatting(
            {row.settle_date: (row.payment_amount, row.currency) for row in c}
        )


if __name__ == "__main__":
    import argparse

    parser = argparse.ArgumentParser()
    parser.add_argument(
        "date",
        nargs="?",
        type=datetime.date.fromisoformat,
        default=datetime.date.today(),
        help="working date",
    )
    args = parser.parse_args()
    logger = logging.getLogger(__name__)
    conn = dbconn("dawndb")
    em = ExchangeMessage()
    for fund in ("ISOSEL",):
        if vals := payment_values(args.date, conn, fund):
            em.send_email(
                f"Payment Settlements Bond NT: {fund} {args.date}",
                "Good morning, \n\nWe have the following amounts settling in the next few days:\n\n"
                + vals,
                to_recipients=_recipients[fund],
                cc_recipients=("Selene-Ops@lmcg.com",),
            )