diff options
Diffstat (limited to 'python/load_loanprices_data.py')
| -rw-r--r-- | python/load_loanprices_data.py | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/python/load_loanprices_data.py b/python/load_loanprices_data.py new file mode 100644 index 00000000..8f1d42bb --- /dev/null +++ b/python/load_loanprices_data.py @@ -0,0 +1,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) |
