aboutsummaryrefslogtreecommitdiffstats
path: root/python/collateral/sg.py
blob: 9aefd6e143cc6c1d8358be26715b89cbb49e883d (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
import logging
import pandas as pd
import time

from . import DAILY_DIR
from paramiko import Transport, SFTPClient

logger = logging.getLogger(__name__)


def get_sftp_client():
    transport = Transport(("prmssp.amer.sgcib.com", 22))
    transport.connect(username="SerenitasGamma@USA", password="SSqrrLL99")
    return SFTPClient.from_transport(transport)


def download_files(
    d=None,
    report_types=[
        "OTC_CASH_ACTIVITY",
        "OTC_POSITIONS",
        "OTC_MARGIN",
        "OTC_MARGIN_EX_DEF",
        "OTC_STATEMENT",
    ],
    retry_count=0,
):
    if retry_count > 20:
        return
    DATA_DIR = DAILY_DIR / "SG_reports"
    sftp = get_sftp_client()
    if d is None:
        for f in sftp.listdir("OTC"):
            if f.endswith("OTC_STATEMENT.xls"):
                print(f)
                sftp.get(f"OTC/{f}", localpath=DATA_DIR / f)
            else:
                for report_type in report_types[:-1]:
                    if f.endswith(f"{report_type}.csv"):
                        print(f)
                        sftp.get(f"OTC/{f}", localpath=DATA_DIR / f)
                else:
                    continue

    else:
        file_list = sftp.listdir("OTC")
        for report_type in report_types:
            if report_type == "OTC_STATEMENT":
                f = f"{d:%Y%m%d}_{report_type}.xls"
            else:
                f = f"{d:%Y%m%d}_{report_type}.csv"
            if f not in file_list:
                logger.info("File not here yet, trying again in 500s...")
                logger.info(f"Try count: {retry_count}")
                time.sleep(500)
                sftp.close()
                download_files(d, report_types, retry_count + 1)
            else:
                sftp.get(f"OTC/{f}", localpath=DATA_DIR / f)
    sftp.close()


def collateral(d, engine):
    df_activity = pd.read_csv(
        DAILY_DIR / "SG_reports" / f"{d:%Y%m%d}_OTC_CASH_ACTIVITY.csv",
        usecols=["Ticket Reference", "Record Type", "Currency", "Amount"],
    )
    df_position = pd.read_csv(
        DAILY_DIR / "SG_reports" / f"{d:%Y%m%d}_OTC_POSITIONS.csv",
        usecols=["Ticket Reference", "Reference Entity", "Mtm Value"],
    )
    df_activity = df_activity.loc[df_activity["Record Type"] == "VM"].set_index(
        "Ticket Reference"
    )
    df_margin = pd.read_csv(
        DAILY_DIR / "SG_reports" / f"{d:%Y%m%d}_OTC_MARGIN_EX_DEF.csv",
        usecols=["Currency", "SG IMR"],
    )
    df_position = df_position.set_index("Ticket Reference")
    # expired_trades
    # df_position = df_position.append(
    #     pd.DataFrame({"Reference Entity": 'CDX-NAIGS29V1-5Y', "Mtm Value": 0.},
    #                  index=['T2201711010000A3K20000045561220U']))
    df = df_activity.join(df_position)
    # expired trade (need to figure out how to get them from the report)
    # df.loc['N201811090000A3K215946925849228U1', 'Mtm Value'] = 0.
    # df.loc['N201811090000A3K215946925849228U1', 'Reference Entity'] = 'CDX-NAIGS31V1-5Y'

    df["Collateral"] = df["Mtm Value"] - df["Amount"]
    ref_entity = df["Reference Entity"].str.split("-", expand=True)
    del ref_entity[0]
    ref_entity.columns = ["to_split", "tenor"]
    ref_entity = ref_entity.join(
        ref_entity["to_split"].str.extract(r"(IG|HY|EUROPE)S(\d+)V(\d+)$", expand=True)
    )
    del ref_entity["to_split"]
    ref_entity.columns = ["tenor", "index_type", "series", "version"]
    ref_entity.index_type[ref_entity.index_type == "EUROPE"] = "EU"
    df = df.join(ref_entity)
    df = df.groupby(["index_type", "series", "tenor"])["Collateral"].sum()
    positions = pd.read_sql_query(
        "SELECT security_desc, folder, notional, currency "
        "FROM  list_cds_positions_by_strat(%s)",
        engine,
        params=(d.date(),),
    )
    instruments = positions.security_desc.str.split(expand=True)[[1, 3, 4]]
    instruments.columns = ["index_type", "series", "tenor"]
    instruments.series = instruments.series.str.extract(r"S(\d+)")
    instruments.index_type[instruments.index_type == "EUR"] = "EU"
    positions = positions.join(instruments)
    del positions["security_desc"]
    positions = positions.set_index(["index_type", "series", "tenor"])
    df = positions.join(df)

    def f(g):
        g.Collateral = g.Collateral * g.notional / g.notional.sum()
        return g

    df = df.groupby(level=["index_type", "series", "tenor"]).apply(f)
    df = df.groupby(["folder"]).agg({"Collateral": "sum", "currency": "first"})
    df = df.reset_index("folder")
    df = df.rename(
        columns={"folder": "Strategy", "currency": "Currency", "Collateral": "Amount"}
    )
    df.Strategy = df.Strategy.map(
        {
            "HEDGE_MBS": "MBSCDSCSH",
            "SER_ITRXCURVE": "SER_ITRXCVCSH",
            "SER_IGCURVE": "SER_IGCVECSH",
            "HYOPTDEL": "HYCDSCSH",
            "IGOPTDEL": "IGCDSCSH",
        }
    )
    df_margin["account"] = "SGNSCLMASW"
    df_margin = df_margin.rename(columns={"SG IMR": "amount", "Currency": "currency"})
    df_margin["date"] = d
    try:
        df_margin.to_sql("fcm_im", engine, if_exists="append", index=False)
    except IntegrityError:
        pass
    df["date"] = d
    return df