aboutsummaryrefslogtreecommitdiffstats
path: root/python/parse_gs.py
blob: 999d3e95b08741e15930c35ebacc32a22e83888c (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
import pandas as pd
import pdb
import re
import os
import pdb

os.chdir("quotes")
for f in os.listdir("."):
    with open(os.path.abspath(f), "rb") as fh:
        flag = False
        masterdf = {}
        for line in fh:
            line = line.decode('utf-8', 'ignore')
            line = line.rstrip()
            m = re.search("(IG|HY)24 5y SWAPTION UPDATE - Ref\D+(.+)$", line)
            if m:
                indextype = m.groups()[0]
                if indextype=='HY':
                    refprice, refspread = map(float,
                                              re.match("([\S]+)\s+\(([^)]+)\)", m.groups()[1]).groups())
                else:
                    refspread = float(m.groups()[1])
                continue
            if line.startswith("At"):
                quotedate = pd.to_datetime(line[4:])
                continue
            if line.startswith("Expiry"):
                m = re.match("Expiry (\d{2}\w{3}\d{2}) \((?:([\S]+) )?([\S]+)\)", line)
                if m:
                    date, fwprice, fwspread = m.groups()
                    date = pd.datetime.strptime(date, '%d%b%y')
                continue
            if line.startswith("Stk"):
                flag = True
                r = []
                continue
            if flag:
                if line:
                    vals = re.sub(" +", " ", line).split(" ")
                    if indextype=='HY':
                        vals.pop(2)
                        vals.pop(9)
                    else:
                        vals.pop(1)
                        vals.pop(8)
                    r.append(vals)
                    continue
                else:
                    if indextype=='HY':
                        cols = ['Stk', 'Sprd', 'Pay', 'Delta', 'Rec', 'Vol',
                                'VolChg', 'VolBpd', 'Tail']
                    else:
                        cols = ['Stk', 'Pay', 'Delta', 'Rec', 'Vol',
                                'VolChg', 'VolBpd', 'Tail']
                    df = pd.DataFrame.from_records(r, columns = cols)
                    df['refspread'] = refspread
                    if indextype=='HY':
                        df['refprice'] = refprice
                    df[['PayBid', 'PayOffer']] = df.Pay.str.split('/', expand=True)
                    df[['RecBid', 'RecOffer']] = df.Rec.str.split('/', expand=True)
                    df.drop(['Pay', 'Rec'], axis=1, inplace=True)
                    df = df.convert_objects(convert_numeric=True)
                    df.set_index('Stk', inplace=True)
                    masterdf[date]=df
                    flag = False
                    r = []
                    continue
        masterdf = pd.concat(masterdf)
        pdb.set_trace()