aboutsummaryrefslogtreecommitdiffstats
path: root/python/load_loanprices_data.py
blob: 8f1d42bb4a7c5e269ed0ef8618b711ed24473fd2 (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
import blpapi
import sys

# Fill SessionOptions
sessionOptions = blpapi.SessionOptions()
sessionOptions.setServerHost('192.168.1.108')
sessionOptions.setServerPort(8194)
session = blpapi.Session(sessionOptions)

# Start a Session
if not session.start():
    print "Failed to start session."
    sys.exit(0)

if not session.openService("//blp/refdata"):
    print "Failed to open //blp/refdata"
    sys.exit(0)

refDataService = session.getService("//blp/refdata")
request = refDataService.createRequest("ReferenceDataRequest")
fields = ["PX_LAST","LAST_UPDATE_DT","ISSUER","MATURITY","CPN","CPN_TYP",
          "CPN_FREQ","FLT_SPREAD","LIBOR_FLOOR","LN_CURRENT_MARGIN",
          "LN_COVENANT_LITE","SECOND_LIEN_INDICATOR","DEFAULTED", "PRICING_SOURCE"]

# append securities to request
cusips=set([])
with open("/home/share/CorpCDOs/data/bbgcusips.txt") as fh:
    cusips = [line.rstrip() for line in fh]
cusips = set(cusips)
for cusip in cusips:
    request.append("securities", "{0} Corp".format(cusip))

# append fields to request
for field in fields:
    request.append("fields", field)

session.sendRequest(request)

data = []
try:
    # Process received events
    while(True):
        # We provide timeout to give the chance to Ctrl+C handling:
        ev = session.nextEvent(500)
        if ev.eventType() in [blpapi.Event.PARTIAL_RESPONSE, blpapi.Event.RESPONSE]:
            for msg in ev:
                data.append(msg)
            # Response completely received, so we could exit
        if ev.eventType() == blpapi.Event.RESPONSE:
            break
finally:
    # Stop the session
    session.stop()

i=0
data2=[]
for msg in data:
    if msg.messageType() == blpapi.Name("ReferenceDataResponse"):
        securityDataArray = msg.getElement("securityData")
        for securityData in securityDataArray.values():
            i+=1
            securityName = securityData.getElementValue("security")
            fieldData = securityData.getElement("fieldData")
            row = {}
            for fieldName in fields:
                try:
                    fieldValue = fieldData.getElementValue(fieldName)
                    row[fieldName] = fieldValue
                except blpapi.NotFoundException:
                    row[fieldName] = None
            data2.append(row)