aboutsummaryrefslogtreecommitdiffstats
path: root/python/parse_emails.py
blob: 5c5fb8bdc543ba795ccb9b9d418ba9dee345a184 (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
import pandas as pd
import re
import os
import pdb
from download_emails import update_emails
import datetime
import logging
import pickle
import sys


logging.basicConfig(filename=os.path.join(os.getenv("LOG_DIR"), 'emails_parsing.log'),
                    level=logging.WARNING,
                    format='%(asctime)s %(message)s')

def makedf(r, indextype, quote_source):
    if indextype=='IG':
        cols = ['strike', 'rec_bid', 'rec_offer', 'delta_rec', 'pay_bid',
                'pay_offer', 'delta_pay', 'vol']
    else:
        cols = ['strike', 'rec_bid', 'rec_offer', 'delta_rec', 'pay_bid',
                'pay_offer', 'delta_pay', 'vol', 'price_vol']
    if quote_source == "BAML":
        cols.append('gamma')
    df = pd.DataFrame.from_records(r, columns = cols)
    for col in ['delta_rec', 'delta_pay', 'vol', 'price_vol', 'gamma']:
        if col in df:
            df[col] = df[col].str.strip("%").astype('float')/100
    for k in df:
        if df.dtypes[k] == 'object':
            try:
                df[k] = pd.to_numeric(df[k])
            except ValueError:
                pdb.set_trace()
    df['quote_source'] = quote_source
    df.set_index('strike', inplace=True)
    return df

def parse_quotedate(fh, date_received):
    for line in fh:
        line = line.rstrip()
        if line.startswith("At"):
            for p in ['%m/%d  %H:%M:%S', '%b  %d %Y %H:%M:%S']:
                try:
                    quotedate = pd.to_datetime(line, format=p, exact=False)
                except ValueError:
                    continue
                else:
                    if quotedate.year == 1900:
                        quotedate = quotedate.replace(year=date_received.year)
                    break
            else:
                raise RuntimeError("can't parse date")
            return quotedate

def parse_refline(line):
    regex = "Ref:(?P<ref>\S+)\s+(?:Fwd Px:(?P<fwdprice>\S+)\s+)?" \
                    "Fwd(?: Spd)?:(?P<fwdspread>\S+)\s+Fwd Bpv:(?P<fwdbpv>\S+)" \
                    "\s+Expiry:(?P<expiry>\S+)"
    m = re.match(regex, line)
    try:
        d = m.groupdict()
        d['expiry'] = pd.to_datetime(d['expiry'], format='%d-%b-%y')
    except AttributeError:
        logging.error("something wrong with " + fh.name)
    return d

def parse_baml(fh, indextype, series, quotedate):
    option_stack = {}
    fwd_index = []
    line = ""
    while True:
        if line == "":
            try:
                line = next(fh)
            except StopIteration:
                break
        if line.startswith("Ref"):
            d = parse_refline(line)
            d.update({'quotedate': quotedate, 'index': indextype, 'series': series})
            df, line = parse_baml_block(fh, indextype)
            option_stack[d['expiry']] = df
            fwd_index.append(d)
        else:
            line = ""
    if option_stack:
        fwd_index = pd.DataFrame.from_records(fwd_index,
                                              index='quotedate')
        return option_stack, fwd_index
    else:
        raise RuntimeError("empty email: " + fh.name)


def parse_baml_block(fh, indextype):
    next(fh) ## skip header
    r = []
    line = ""
    for line in fh:
        line = line.strip()
        if line.startswith("Ref") or line == "":
            break
        line = re.sub("[/|]", " ", line)
        vals = re.sub(" +", " ", line).rstrip().split(" ")
        if len(vals) < 3: ## something went wrong
            line = ""
            break
        r.append(vals)
    return makedf(r, indextype, "BAML"), line

def parse_ms_block(fh, indextype):
    next(fh) ## skip header
    r = []
    for line in fh:
        line = line.rstrip()
        if line == "":
            break
        strike, payer, receiver, vol = line.split("|")
        strike = strike.strip()
        if indextype == "HY":
            strike = strike.split()[0]
        pay_bid, pay_offer, pay_delta = payer.strip().split()
        rec_bid, rec_offer, rec_delta = receiver.strip().split()

        vals = [strike, rec_bid, rec_offer, rec_delta,
                pay_bid, pay_offer, pay_delta]
        vol = vol.strip()
        if indextype == "HY":
            vol, price_vol = vol.replace("[","").replace("]","").split()
            vals += [vol, price_vol]
        else:
            vals += [vol]
        r.append(vals)
    return makedf(r, indextype, "MS")

def parse_ms(fh, indextype):
    option_stack = {}
    for line in fh:
        line = line.rstrip()
        if "EXPIRY" in line:
            expiry = line.split(" ")[1]
            expiry = pd.to_datetime(expiry, format="%d-%b-%Y")
            option_stack[expiry] = parse_ms_block(fh, indextype)
    return option_stack

subject_BAML = re.compile("(?:Fwd:){0,2}(?:BAML )?(\w{2})([0-9]{1,2})\s")
subject_MS = re.compile("\$\$ MS CDX OPTIONS: (IG|HY)(\d{2})[^\d]*([\d.]+)")

def parse_email(email):
    with open(email.path, "rt") as fh:
        date_received = datetime.datetime.fromtimestamp(int(fh.readline())/1000)
        subject = next(fh)
        m = subject_BAML.match(subject)
        if m:
            indextype, series = m.groups()
            series = int(series)
            quotedate = parse_quotedate(fh, date_received)
            return (quotedate, indextype, series), parse_baml(fh, indextype, series, quotedate)
        m = subject_MS.match(subject)
        if m:
            indextype, series, ref = m.groups()
            series = int(series)
            ref = float(ref)
            quotedate = parse_quotedate(fh, date_received)
            option_stack = parse_ms(fh, indextype)
            fwd_index = pd.DataFrame({'quotedate': quotedate,
                                      'ref': ref,
                                      'index': indextype,
                                      'series': series,
                                      'expiry': list(option_stack.keys())})
            fwd_index.set_index('quotedate', inplace = True)
            return (quotedate, indextype, series), (option_stack, fwd_index)
        raise RuntimeError("can't parse subject line: {0} for email {1}".format(
            subject, email.name))

def write_todb(swaption_stack, index_data):
    from sqlalchemy import MetaData, Table
    from db import dbengine, nan_to_null
    import psycopg2
    serenitasdb  = dbengine('serenitasdb')
    psycopg2.extensions.register_adapter(float, nan_to_null)
    meta = MetaData(bind=serenitasdb)
    swaption_quotes = Table('swaption_quotes', meta, autoload=True)
    ins = swaption_quotes.insert().values(swaption_stack.to_dict(orient='records')).execute()
    index_data.to_sql('swaption_ref_quotes', serenitasdb, if_exists='append', index=False)

def get_email_list(date):
    """returns a list of email file names for a given date

    Parameters
    ----------
    date : string
    """
    with open(".pickle", "rb") as fh:
        already_uploaded = pickle.load(fh)
    df = pd.DataFrame.from_dict(already_uploaded, orient='index')
    df.columns = ['quotedate']
    df = df.reset_index().set_index('quotedate')
    return df.loc[date,'index'].tolist()

if __name__=="__main__":
    update_emails()
    data_dir = os.path.join(os.getenv("DATA_DIR"), "swaptions")
    emails = [f for f in os.scandir(data_dir) if f.is_file()]
    swaption_stack = {}
    index_data = pd.DataFrame()
    try:
        with open(".pickle", "rb") as fh:
            already_uploaded = pickle.load(fh)
    except FileNotFoundError:
        already_uploaded = {}
    for f in emails:
        if f.name in already_uploaded:
            continue
        else:
            try:
                key, (option_stack, fwd_index) = parse_email(f)
            except RuntimeError as e:
                logging.error(e)
            else:
                swaption_stack[key] = pd.concat(option_stack, names=['expiry', 'strike'])
                index_data = index_data.append(fwd_index)
                already_uploaded[f.name] = key[0]
    if index_data.empty:
        sys.exit()
    for col in ['fwdbpv', 'fwdprice', 'fwdspread', 'ref']:
        index_data[col] = index_data[col].astype('float')
    index_data['index'] = index_data['index'].astype('category')

    swaption_stack = pd.concat(swaption_stack, names=['quotedate', 'index', 'series'])
    # import feather
    # feather.write_dataframe(swaption_stack, '../../data/swaptions.fth')
    # feather.write_dataframe(index_data, '../../data/index_data.fth')

    swaption_stack = swaption_stack.reset_index()
    swaption_stack = swaption_stack.drop_duplicates(['quotedate', 'index', 'series', 'expiry', 'strike'])
    index_data = index_data.reset_index()
    index_data = index_data.drop_duplicates(['quotedate', 'index', 'series', 'expiry'])
    write_todb(swaption_stack, index_data)
    with open(".pickle", "wb") as fh:
       pickle.dump(already_uploaded, fh)