aboutsummaryrefslogtreecommitdiffstats
path: root/python/task_server/insert_tranche_quotes.py
blob: b4f3dfd14745e3d6b128f9a181c32002078df928 (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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
from sqlalchemy import Table, create_engine, MetaData
from sqlalchemy.exc import IntegrityError, DataError, SAWarning
from serenitas.utils.env import BASE_DIR
import csv
import datetime
import logging
import warnings

logger = logging.getLogger(__name__)

engine = create_engine("postgresql://serenitas_user@debian/serenitasdb")
metadata = MetaData(bind=engine)
with warnings.catch_warnings():
    warnings.simplefilter("ignore", category=SAWarning)
    quotes = Table("tranche_quotes", metadata, autoload=True)
ins = quotes.insert()


def convert(x):
    if x:
        try:
            return float(x)
        except ValueError:
            logger.info("couldn't convert {0} to float".format(x))
    else:
        return None


def convert_int(x):
    if x:
        try:
            return int(float(x))
        except ValueError:
            logger.info("couldn't convert {0} to int".format(x))
    else:
        return None


tenordict = {"3": "3yr", "5": "5yr", "7": "7yr", "10": "10yr"}
runningdict1 = {0: 500, 3: 100, 7: 100, 15: 25}
runningdict2 = {0: 500, 3: 500, 7: 500, 10: 100, 15: 100, 30: 100}


def insert_quotes(year=2016, quote_dir=None):
    if not quote_dir:
        quote_dir = BASE_DIR / "Tranche_data" / "Quotes"
    quotefiles = [f for f in quote_dir.iterdir() if f.stem == ".csv"]
    for quotefile in quotefiles:
        with quotefile.open() as fh:
            reader = csv.DictReader(fh)
            data = []
            for csvdict in reader:
                for p in ["%d %b %Y %H:%M", "%m/%d/%Y %H:%M"]:
                    try:
                        timestamp = datetime.datetime.strptime(csvdict["Date"], p)
                        break
                    except ValueError:
                        logger.error("%s: Date in the wrong format", csvdict["Date"])
                        continue
                else:
                    logger.error("%s: Date in the wrong format", csvdict["Date"])

                attach = int(csvdict["Attach"])
                detach = int(csvdict["Detach"])
                series = int(csvdict["Series"])
                version = int(csvdict["Version"])
                if csvdict["Ref"] == "":  # no delta quote maybe
                    logger.info("ref missing")

                # common values to all indices
                try:
                    d = {
                        "quotedate": timestamp,
                        "tranchedelta": convert(csvdict["Delta"]),
                        "quotesource": csvdict["Source"],
                        "series": series,
                        "version": version,
                        "attach": attach,
                        "detach": detach,
                        "tenor": tenordict[csvdict["Tenor"]],
                    }
                except KeyError:
                    continue

                if csvdict["Ticker"] == "CDX-NAHY":
                    trancheupfrontbid = convert(csvdict["Price Bid"])
                    trancheupfrontask = convert(csvdict["Price Ask"])

                    try:
                        trancheupfrontmid = (trancheupfrontbid + trancheupfrontask) / 2
                    except TypeError:
                        trancheupfrontmid = None
                    running = 0 if series in [9, 10] and attach == 10 else 500
                    d.update(
                        {
                            "indexrefprice": convert(csvdict["Ref"]),
                            "indexrefspread": 375 if series == 9 else 500,
                            "trancheupfrontbid": trancheupfrontbid,
                            "trancheupfrontmid": trancheupfrontmid,
                            "trancheupfrontask": trancheupfrontask,
                            "trancherunningbid": running,
                            "trancherunningmid": running,
                            "trancherunningask": running,
                            "index": "HY",
                        }
                    )

                elif csvdict["Ticker"] == "CDX-NAIG":

                    if series >= 30:
                        logger.info("series doesn't exist")
                        continue
                    trancheupfrontbid = convert(csvdict["Upfront Bid"])
                    trancheupfrontask = convert(csvdict["Upfront Ask"])
                    try:
                        trancheupfrontmid = (trancheupfrontbid + trancheupfrontask) / 2
                    except TypeError:
                        trancheupfrontmid = None
                    if series >= 25:
                        running = 100
                    else:
                        running = (
                            runningdict2[attach]
                            if series < 19
                            else runningdict1[attach]
                        )
                    try:
                        d.update(
                            {
                                "indexrefspread": convert_int(csvdict["Ref"]),
                                "trancheupfrontbid": trancheupfrontbid,
                                "trancheupfrontmid": trancheupfrontmid,
                                "trancheupfrontask": trancheupfrontask,
                                "trancherunningbid": running,
                                "trancherunningmid": running,
                                "trancherunningask": running,
                                "index": "IG",
                            }
                        )
                    except KeyError:
                        continue
                elif csvdict["Ticker"] == "ITRAXX-Europe":
                    if series <= 15:
                        if attach <= 6:
                            trancherunningbid = (
                                trancherunningmid
                            ) = trancherunningask = (500 if attach <= 3 else 300)
                            trancheupfrontbid = convert(csvdict["Upfront Bid"])
                            trancheupfrontask = convert(csvdict["Upfront Ask"])
                            try:
                                trancheupfrontmid = (
                                    trancheupfrontbid + trancheupfrontask
                                ) / 2
                            except TypeError:
                                trancheupfrontmid = None
                        else:
                            trancherunningbid = convert(csvdict["Bid"])
                            trancherunningask = convert(csvdict["Ask"])
                            try:
                                trancherunningmid = (
                                    trancherunningbid + trancherunningask
                                ) / 2
                            except TypeError:
                                trancherunning = None
                            trancheupfrontbid = (
                                trancheupfrontmid
                            ) = trancheupfrontask = 0
                    if series in [19, 21, 22, 24, 26]:
                        if attach <= 3:
                            trancherunningbid = (
                                trancherunningask
                            ) = trancherunningmid = (500 if series == 19 else 100)
                            trancheupfrontbid = convert(csvdict["Upfront Bid"])
                            trancheupfrontask = convert(csvdict["Upfront Ask"])
                            try:
                                trancheupfrontmid = (
                                    trancheupfrontbid + trancheupfrontask
                                ) / 2
                            except TypeError:
                                trancheupfrontmid = None
                        else:
                            trancherunningbid = convert(csvdict["Bid"])
                            trancherunningask = convert(csvdict["Ask"])
                            try:
                                trancherunningmid = (
                                    trancherunningbid + trancherunningask
                                ) / 2
                            except TypeError:
                                trancherunning = None
                            trancheupfrontbid = (
                                trancheupfrontmid
                            ) = trancheupfrontask = 0

                    d.update(
                        {
                            "indexrefspread": convert_int(csvdict["Ref"]),
                            "trancheupfrontbid": trancheupfrontbid,
                            "trancheupfrontmid": trancheupfrontmid,
                            "trancheupfrontask": trancheupfrontask,
                            "trancherunningbid": trancherunningbid,
                            "trancherunningmid": trancherunningmid,
                            "trancherunningask": trancherunningask,
                            "index": "EU",
                        }
                    )
                elif csvdict["Ticker"] == "ITRAXX-Xover":
                    if attach < 35:
                        ## the quote is sometimes in Price Bid, sometimes in Bid/Ask
                        ## so we try both
                        trancheupfrontbid = convert(csvdict["Upfront Bid"])
                        trancheupfrontask = convert(csvdict["Upfront Ask"])
                        try:
                            trancheupfrontmid = (
                                trancheupfrontbid + trancheupfrontask
                            ) / 2
                        except TypeError:
                            trancheupfrontmid = None
                        trancherunningbid = trancherunningask = trancherunningmid = 500
                    else:
                        trancherunningbid = convert(csvdict["Bid"])
                        trancherunningask = convert(csvdict["Ask"])
                        try:
                            trancherunningmid = (
                                trancherunningbid + trancherunningask
                            ) / 2
                        except TypeError:
                            trancherunningmid = None
                        trancheupfrontbid = trancheupfrontmid = trancheupfrontask = 0
                    d.update(
                        {
                            "indexrefspread": convert_int(csvdict["Ref"]),
                            "trancheupfrontbid": trancheupfrontbid,
                            "trancheupfrontmid": trancheupfrontmid,
                            "trancheupfrontask": trancheupfrontask,
                            "trancherunningbid": trancherunningbid,
                            "trancherunningmid": trancherunningmid,
                            "trancherunningask": trancherunningask,
                            "index": "XO",
                        }
                    )
                data.append(d)
        with engine.connect() as conn:
            for i, l in enumerate(data):
                with conn.begin() as t:
                    try:
                        conn.execute(ins, l)
                    except (IntegrityError, DataError) as e:
                        logging.debug(e.orig)
                        t.rollback()

        quotefile.unlink()


if __name__ == "__main__":
    insert_quotes()