aboutsummaryrefslogtreecommitdiffstats
path: root/python/collateral/barclays.py
blob: 999e5b287b9e132ba87f24f6d9bad12f69d08a29 (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
import logging
import pandas as pd
import PyPDF2
from . import DAILY_DIR

logger = logging.getLogger(__name__)

paths = {
    "Serenitas": ["NYops", "Margin Calls Barclays"],
    "Selene": ["SeleneOps", "Margin Barclays"],
}


def load_file(d, fund, pattern):
    file_mapping = {
        "Serenitas": "Serenitas Credit Gamma Master Fund LP",
        "Selene": "Iso Selene Inc",
    }
    if fund not in file_mapping:
        raise FileNotFoundError(f"No ISDA with Barclays for {fund}")
    try:
        fname = next(
            (DAILY_DIR / fund / "BARCLAYS_reports").glob(
                f"{pattern}*{file_mapping[fund]} - Regulatory as at {d:%d %b %Y}*"
            )
        )
    except StopIteration:
        raise FileNotFoundError(f"BARCLAYS file not found for date {d}")
    if fname.name.endswith("xls"):
        return pd.read_excel(fname, skiprows=3, skipfooter=23)
    else:
        return fname


def get_total_collateral(d, fund):
    with open(load_file(d, fund, "CreditSupportStatement"), "rb") as pdf_file:
        pdf_reader = PyPDF2.PdfFileReader(pdf_file)
        first_page = pdf_reader.getPage(0)
        page_text = first_page.extractText()
        csb_pos = page_text.find("Credit Support Balance")
        next_word_pos = page_text.find(" ", csb_pos + len("Credit Support Balance") + 1)
        collateral_balance = page_text[
            csb_pos + len("Credit Support Balance") + 1 : next_word_pos
        ]
        return float(collateral_balance.split("\n")[0].replace(",", ""))


def download_files(em, count=20, *, fund="Serenitas", **kwargs):
    if fund not in paths:
        return
    emails = em.get_msgs(path=paths[fund], count=count)
    DATA_DIR = DAILY_DIR / fund / "BARCLAYS_reports"
    for msg in emails:
        for attach in msg.attachments:
            fname = attach.name
            if fname.startswith("CreditSupport") or fname.startswith("Exposure"):
                p = DATA_DIR / fname
                if not p.exists():
                    p.write_bytes(attach.content)


def collateral(d, dawn_trades, *, fund="Serenitas", **kwargs):
    collateral = get_total_collateral(d, fund)
    df = load_file(d, fund, "Exposure Report")
    df = df[["Trade Reference", "Exposure (USD)", "Ind Amt (USD)"]]
    df["Trade Reference"] = df["Trade Reference"].astype(str)
    df = df.merge(
        dawn_trades, how="left", left_on="Trade Reference", right_on="cpty_id"
    )
    missing_ids = df.loc[df.cpty_id.isnull(), "Trade Reference"]
    if not missing_ids.empty:
        raise ValueError(f"{missing_ids.tolist()} not in the database for {fund}")
    df = df[["folder", "Exposure (USD)", "Ind Amt (USD)"]]
    df = df.groupby("folder", dropna=False).sum()
    df = df.sum(axis=1).to_frame(name="Amount")
    df["Currency"] = "USD"
    df = df.reset_index()
    df.columns = ["Strategy", "Amount", "Currency"]
    df.Amount *= -1
    df.loc[len(df.index)] = ["M_CSH_CASH", -collateral - df.Amount.sum(), "USD"]
    df["date"] = d
    return df.set_index("Strategy")