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
|
from sqlalchemy import Table, create_engine, MetaData
from sqlalchemy.exc import IntegrityError
import os
import csv
import datetime
import pdb
engine = create_engine('postgresql://mlpdb_user:Serenitas1@debian/mlpdb')
metadata = MetaData(bind = engine)
quotes = Table('tranche_quotes', metadata, autoload = True)
ins = quotes.insert()
def convert(x):
try:
return float(x)
except:
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():
root_dir = '/home/share/CorpCDOs'
quote_dir = os.path.join(root_dir, 'Tranche_data', 'Quotes')
quotefiles = [f for f in os.listdir(quote_dir) if 'csv' in f]
for quotefile in quotefiles:
with open(os.path.join(quote_dir, quotefile)) as fh:
reader = csv.DictReader(fh)
data = []
for csvdict in reader:
try:
timestamp = datetime.datetime.strptime(csvdict['Date'], "%d %b %H:%M")
timestamp = timestamp.replace(year=2014)
except ValueError:
pdb.set_trace()
attach = int(csvdict['Attach'])
series = int(csvdict['Series'])
version = int(csvdict['Version'])
if csvdict['Ref']=='': #no delta quote maybe
continue
#common values to all indices
d = {'quotedate' : timestamp,
'tranchedelta': convert(csvdict['Delta']),
'quotesource' : csvdict['Source'],
'series': series,
'version': version,
'attach': attach,
'detach': int(csvdict['Detach']),
'tenor': tenordict[csvdict['Tenor']]
}
if csvdict['Ticker'] == 'CDX-NAHY':
if csvdict['Price Bid']=='':
continue
if series==9:
version = 20
elif series==10:
version = 19
d.update({'indexrefprice': float(csvdict['Ref']),
'indexrefspread': 375 if series==9 else 500,
'trancheupfront' : (float(csvdict['Price Bid'])+float(csvdict['Price Ask']))/2,
'trancherunning' : 0 if series in [9, 10] and attach==10 else 500,
'index': 'HY'
})
elif csvdict['Ticker'] == 'CDX-NAIG':
if series>=22:
continue
try:
trancheupfront = (float(csvdict['Upfront Bid'])+float(csvdict['Upfront Ask']))/2
except ValueError:
trancheupfront = None
d.update({
'indexrefspread': int(float(csvdict['Ref'])),
'trancheupfront' : trancheupfront,
'trancherunning' : runningdict2[attach] if series<19 else runningdict1[attach],
'index' : 'IG'})
elif csvdict['Ticker'] == 'ITRAXX-Europe':
if series==9:
if attach<=6:
trancherunning = 500 if attach<=3 else 300
try:
trancheupfront = (float(csvdict['Upfront Bid'])+float(csvdict['Upfront Ask']))/2
except ValueError:
trancheupfront = None
else:
try:
trancherunning=(float(csvdict['Bid'])+float(csvdict['Ask']))/2
except ValueError:
trancherunning = None
trancheupfront=0
if series==19:
if attach<=3:
trancherunning = 500
trancheupfront = (float(csvdict['Upfront Bid'])+float(csvdict['Upfront Ask']))/2
else:
try:
trancherunning=(float(csvdict['Bid'])+float(csvdict['Ask']))/2
except ValueError:
trancherunning=(float(csvdict['Upfront Bid'])+float(csvdict['Upfront Ask']))/2
trancheupfront = 0
if series==21:
if attach<=3:
trancherunning =100
try:
trancheupfront = (float(csvdict['Upfront Bid'])+float(csvdict['Upfront Ask']))/2
except ValueError:
trancheupfront = None
else:
trancherunning=(float(csvdict['Bid'])+float(csvdict['Ask']))/2
trancheupfront = 0
d.update({'indexrefspread': int(float(csvdict['Ref'])),
'trancheupfront' : trancheupfront,
'trancherunning' : trancherunning,
'index' : 'EU',
})
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 as e:
print(e.orig)
t.rollback()
os.unlink(os.path.join(quote_dir, quotefile))
if __name__=="__main__":
insert_quotes()
|