aboutsummaryrefslogtreecommitdiffstats
path: root/python/load_bbh_reports.py
blob: 586ac657096dd459457b7a33c504412983c9fc67 (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
import datetime
import pandas as pd
from env import DAILY_DIR
from utils.db import dbengine
from remote import SftpClient


def pnl_report(f):
    df = pd.read_csv(
        f,
        thousands=",",
        usecols=[
            "Accounting Date",
            "Security ID",
            "Custody Head Account Number",
            "Issue Name",
            "Local Currency",
            "Base Market Value",
            "Base Change Income",
            "Base Change FX Realized Gain Loss",
            "Base Change FX Unrealized Gain Loss",
            "Base Change Unrealized Gain Loss",
            "Base Change Realized Gain Loss",
            "Base Change Miscellaneous Income",
            "Base Change Expense",
            "Base Change Total",
            "Sub Security Type Code",
            "Source",
        ],
    )
    df.columns = df.columns.str.lower().str.replace(" ", "_")
    df["row"] = df.index
    df.to_sql("bbh_pnl", dbengine("dawndb"), if_exists="append", index=False)


def val_report(f):
    df = pd.read_csv(
        f,
        thousands=",",
        usecols=[
            "Accounting Date",
            "Custody Head Account Number",
            "Security ID",
            "Security Description",
            "Asset Currency",
            "Original Face",
            "Base Price",
            "Local Unit Cost",
            "Base Unit Cost",
            "Local Market Value",
            "Base Market Value",
            "Security ID Type",
            "Sub Security Type Code",
            "Source",
            "Investment Type Code",
            "Investment Type Description",
            "Security Long Description",
            "Security Type Code",
            "Total Current Assets",
            "Total Current Liabilities",
            "Total Net Assets",
        ],
    )
    for col in df.select_dtypes(include="object"):
        df[col] = df[col].str.strip()
    df.columns = df.columns.str.lower().str.replace(" ", "_")
    df["row"] = df.index
    df.to_sql("bbh_val", dbengine("dawndb"), if_exists="append", index=False)


def load_reports(date):
    path = DAILY_DIR / str(date) / "Reports"
    pnl_report(path / f"BBH_PNL.{date:%Y%m%d}.csv")
    val_report(path / f"BBH_VAL.{date:%Y%m%d}.csv")


def download_val_report(date):
    reports_dir = DAILY_DIR / str(date) / "Reports"
    sftp = SftpClient.from_creds("bbh")
    for f in sftp.client.listdir("frombbh"):
        if f.split(".")[1] == f"{date:%Y%m%d}":
            sftp.client.get(f"frombbh/{f}", localpath=reports_dir / f)


if __name__ == "__main__":
    import argparse

    parser = argparse.ArgumentParser()
    parser.add_argument(
        "workdate",
        nargs="?",
        type=datetime.date.fromisoformat,
        default=datetime.date.today(),
        help="working date",
    )
    args = parser.parse_args()
    download_val_report(args.workdate)
    load_reports(args.workdate)