aboutsummaryrefslogtreecommitdiffstats
path: root/python/bowdst.py
blob: efc3aa00676de5d8d4b55dfcbd1cb63a1eb4218b (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
import datetime
import pandas as pd
from env import DAILY_DIR
from exchange import ExchangeMessage, Message
from exchangelib import FileAttachment
from io import StringIO
from remote import SftpClient
from utils.db import dbconn

em = ExchangeMessage()
for msg in em.get_msgs(
    20, path=["BowdoinOps", "Reports"], subject__startswith="Document(s) from Reporting"
):
    if msg.sender == "notify@bnymellon.com":
        for attach in msg.attachments:
            fname = attach.name
            if fname.endswith("csv") and fname.startswith("Asset Detail"):
                date = datetime.datetime.strptime(
                    fname.split("_")[1].split(".")[0], "%d %b %Y"
                ).date()
                p = DAILY_DIR / str(date) / "Reports" / fname
                if not p.parent.exists():
                    p.parent.mkdir(parents=True)
                if not p.exists():
                    p.write_bytes(attach.content)

workdate = datetime.date.today()
p = DAILY_DIR / str(workdate) / "Reports" / f"Asset Detail_{workdate:%d %b %Y}.csv"
df = pd.read_csv(p, thousands=",")
df = df[df["Asset Type"] == "FIXED INCOME SECURITIES"]
df = df.set_index("CUSIP")
df = df[["Shares/Par", "Base Price", "Local Market Value"]]
for col in df.select_dtypes(include=["object"]).columns:
    df[col] = pd.to_numeric(df[col].str.replace(",", ""))
dawndb = dbconn("dawndb")
df_blotter = pd.read_sql_query(
    "SELECT * FROM risk_positions(%s, NULL, %s)",
    dawndb,
    params=(workdate, "BOWDST"),
    index_col=["identifier"],
)
check = df_blotter.join(df)

cds_positions = pd.read_sql_query(
    "SELECT * FROM list_cds_marks_pre(%s, NULL, %s)",
    dawndb,
    params=(workdate, "BOWDST"),
)


def send_email(workdate: datetime.date, df_bonds: pd.DataFrame, df_cds: pd.DataFrame):
    email = Message(
        account=em._account,
        folder=em._account.sent,
        subject=f"{workdate} positions",
        body="",
        to_recipients=("reconfiles@bnymellon.com", "hm-operations@bnymellon.com"),
        cc_recipients=("bowdoin-ops@lmcg.com",),
    )
    buf = StringIO()
    df_bonds.to_csv(buf)
    bond_attach = FileAttachment(
        name=f"{workdate} bonds.csv", content=buf.getvalue().encode()
    )
    buf.close()
    email.attach(bond_attach)
    buf = StringIO()
    df_cds.to_csv(buf, index=False)
    cds_attach = FileAttachment(
        name=f"{workdate} cds.csv", content=buf.getvalue().encode()
    )
    buf.close()
    email.attach(cds_attach)
    email.send_and_save()


# sftp = SftpClient.from_creds("gs")