-
Notifications
You must be signed in to change notification settings - Fork 25
/
bbg.py
547 lines (468 loc) · 21.9 KB
/
bbg.py
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
"""
methods for using the bloomberg COM API v3 from python
Written by Brian P. Smith ([email protected])
"""
from pythoncom import PumpWaitingMessages
from win32com.client import DispatchWithEvents, constants, CastTo
from collections import defaultdict, namedtuple
from datetime import datetime, timedelta
from pandas import DataFrame, to_datetime, concat, Panel
import numpy as np
SecurityErrorAttrs = ['security', 'source', 'code', 'category', 'message', 'subcategory']
SecurityError = namedtuple('SecurityError', SecurityErrorAttrs)
FieldErrorAttrs = ['security', 'field', 'source', 'code', 'category', 'message', 'subcategory']
FieldError = namedtuple('FieldError', FieldErrorAttrs)
# poor mans debugging
DEBUG = False
class XmlHelper(object):
@staticmethod
def security_iter(nodearr):
""" provide a security data iterator by returning a tuple of (Element, SecurityError) which are mutually exclusive """
assert nodearr.Name == 'securityData' and nodearr.IsArray
for i in range(nodearr.NumValues):
node = nodearr.GetValue(i)
err = XmlHelper.get_security_error(node)
result = (None, err) if err else (node, None)
yield result
@staticmethod
def message_iter(evt):
""" provide a message iterator which checks for a response error prior to returning """
iter = evt.CreateMessageIterator()
while iter.Next():
msg = iter.Message
if DEBUG:
print msg.Print
if msg.AsElement.HasElement('responseError'):
raise Exception(msg.AsElement.GetValue('message'))
yield msg
@staticmethod
def get_sequence_value(node):
"""Convert an element with DataType Sequence to a DataFrame.
Note this may be a naive implementation as I assume that bulk data is always a table
"""
assert node.Datatype == 15
data = defaultdict(list)
cols = []
for i in range(node.NumValues):
row = node.GetValue(i)
if i == 0: # Get the ordered cols and assume they are constant
cols = [str(row.GetElement(_).Name) for _ in range(row.NumElements)]
for cidx in range(row.NumElements):
col = row.GetElement(cidx)
data[str(col.Name)].append(XmlHelper.as_value(col))
return DataFrame(data, columns=cols)
@staticmethod
def as_value(ele):
""" convert the specified element as a python value """
dtype = ele.Datatype
#print '%s = %s' % (ele.Name, dtype)
if dtype in (1, 2, 3, 4, 5, 6, 7, 9, 12):
# BOOL, CHAR, BYTE, INT32, INT64, FLOAT32, FLOAT64, BYTEARRAY, DECIMAL)
return ele.Value
elif dtype == 8: # String
return str(ele.Value)
elif dtype == 10: # Date
v = ele.Value
return datetime(year=v.year, month=v.month, day=v.day).date() if v else np.nan
elif dtype == 11: # Time
v = ele.Value
return datetime(hour=v.hour, minute=v.minute, second=v.second).time() if v else np.nan
elif dtype == 13: # Datetime
v = ele.Value
return datetime(year=v.year, month=v.month, day=v.day, hour=v.hour, minute=v.minute, second=v.second)
elif dtype == 14: # Enumeration
raise NotImplementedError('ENUMERATION data type needs implemented')
elif dtype == 16: # Choice
raise NotImplementedError('CHOICE data type needs implemented')
elif dtype == 15: # SEQUENCE
return XmlHelper.get_sequence_value(ele)
else:
raise NotImplementedError('Unexpected data type %s. Check documentation' % dtype)
@staticmethod
def get_child_value(parent, name):
""" return the value of the child element with name in the parent Element """
return XmlHelper.as_value(parent.GetElement(name))
@staticmethod
def get_child_values(parent, names):
""" return a list of values for the specified child fields. If field not in Element then replace with nan. """
vals = []
for name in names:
if parent.HasElement(name):
vals.append(XmlHelper.as_value(parent.GetElement(name)))
else:
vals.append(np.nan)
return vals
@staticmethod
def as_security_error(node, secid):
""" convert the securityError element to a SecurityError """
assert node.Name == 'securityError'
src = XmlHelper.get_child_value(node, 'source')
code = XmlHelper.get_child_value(node, 'code')
cat = XmlHelper.get_child_value(node, 'category')
msg = XmlHelper.get_child_value(node, 'message')
subcat = XmlHelper.get_child_value(node, 'subcategory')
return SecurityError(security=secid, source=src, code=code, category=cat, message=msg, subcategory=subcat)
@staticmethod
def as_field_error(node, secid):
""" convert a fieldExceptions element to a FieldError or FieldError array """
assert node.Name == 'fieldExceptions'
if node.IsArray:
return [XmlHelper.as_field_error(node.GetValue(_), secid) for _ in range(node.NumValues)]
else:
fld = XmlHelper.get_child_value(node, 'fieldId')
info = node.GetElement('errorInfo')
src = XmlHelper.get_child_value(info, 'source')
code = XmlHelper.get_child_value(info, 'code')
cat = XmlHelper.get_child_value(info, 'category')
msg = XmlHelper.get_child_value(info, 'message')
subcat = XmlHelper.get_child_value(info, 'subcategory')
return FieldError(security=secid, field=fld, source=src, code=code, category=cat, message=msg, subcategory=subcat)
@staticmethod
def get_security_error(node):
""" return a SecurityError if the specified securityData element has one, else return None """
assert node.Name == 'securityData' and not node.IsArray
if node.HasElement('securityError'):
secid = XmlHelper.get_child_value(node, 'security')
err = XmlHelper.as_security_error(node.GetElement('securityError'), secid)
return err
else:
return None
@staticmethod
def get_field_errors(node):
""" return a list of FieldErrors if the specified securityData element has field errors """
assert node.Name == 'securityData' and not node.IsArray
nodearr = node.GetElement('fieldExceptions')
if nodearr.NumValues > 0:
secid = XmlHelper.get_child_value(node, 'security')
errors = XmlHelper.as_field_error(nodearr, secid)
return errors
else:
return None
def debug_event(evt):
print 'unhandled event: %s' % evt.EventType
if evt.EventType in [constants.RESPONSE, constants.PARTIAL_RESPONSE]:
print 'messages:'
for msg in XmlHelper.message_iter(evt):
print msg.Print
class ResponseHandler(object):
def do_init(self, handler):
""" will be called prior to waiting for the message """
self.waiting = True
self.exc_info = None
self.handler = handler
def set_evt_handler(self, handler):
self.handler = handler
def OnProcessEvent(self, evt):
try:
evt = CastTo(evt, 'Event')
if not self.handler:
debug_event(evt)
if evt.EventType == constants.RESPONSE:
self.handler.on_event(evt, is_final=True)
self.waiting = False
elif evt.EventType == constants.PARTIAL_RESPONSE:
self.handler.on_event(evt, is_final=False)
else:
self.handler.on_admin_event(evt)
except Exception:
import sys
self.waiting = False
self.exc_info = sys.exc_info()
@property
def has_deferred_exception(self):
return self.exc_info is not None
def raise_deferred_exception(self):
raise self.exc_info[1], None, self.exc_info[2]
def do_cleanup(self):
self.waiting = False
self.exc_info = None
self.handler = None
class Request(object):
def __init__(self, ignore_security_error=0, ignore_field_error=0):
self.field_errors = []
self.security_errors = []
self.ignore_security_error = ignore_security_error
self.ignore_field_error = ignore_field_error
@property
def has_exception(self):
if not self.ignore_security_error and len(self.security_errors) > 0:
return True
if not self.ignore_field_error and len(self.field_errors) > 0:
return True
def raise_exception(self):
if not self.ignore_security_error and len(self.security_errors) > 0:
msgs = ['(%s, %s, %s)' % (s.security, s.category, s.message) for s in self.security_errors]
raise Exception('SecurityError: %s' % ','.join(msgs))
if not self.ignore_field_error and len(self.field_errors) > 0:
msgs = ['(%s, %s, %s, %s)' % (s.security, s.field, s.category, s.message) for s in self.field_errors]
raise Exception('FieldError: %s' % ','.join(msgs))
raise Exception('Programmer Error: No exception to raise')
def get_bbg_request(self, svc, session):
raise NotImplementedError()
def get_bbg_service_name(self):
raise NotImplementedError()
def on_event(self, evt, is_final):
raise NotImplementedError()
def on_admin_event(self, evt):
pass
def execute(self):
Terminal.execute_request(self)
return self
@staticmethod
def apply_overrides(request, omap):
""" add the given overrides (omap) to bloomberg request """
if omap:
for k, v in omap.iteritems():
o = request.GetElement('overrides').AppendElment()
o.SetElement('fieldId', k)
o.SetElement('value', v)
class ReferenceDataRequest(Request):
def __init__(self, symbols, fields, overrides=None, response_type='frame', ignore_security_error=0, ignore_field_error=0):
"""
response_type: (frame, map) how to return the results
"""
assert response_type in ('frame', 'map')
Request.__init__(self, ignore_security_error=ignore_security_error, ignore_field_error=ignore_field_error)
self.symbols = isinstance(symbols, basestring) and [symbols] or symbols
self.fields = isinstance(fields, basestring) and [fields] or fields
self.overrides = overrides
# response related
self.response = {} if response_type == 'map' else defaultdict(list)
self.response_type = response_type
def get_bbg_service_name(self):
return '//blp/refdata'
def get_bbg_request(self, svc, session):
# create the bloomberg request object
request = svc.CreateRequest('ReferenceDataRequest')
[request.GetElement('securities').AppendValue(sec) for sec in self.symbols]
[request.GetElement('fields').AppendValue(fld) for fld in self.fields]
Request.apply_overrides(request, self.overrides)
return request
def on_security_node(self, node):
sid = XmlHelper.get_child_value(node, 'security')
farr = node.GetElement('fieldData')
fdata = XmlHelper.get_child_values(farr, self.fields)
assert len(fdata) == len(self.fields), 'field length must match data length'
if self.response_type == 'map':
self.response[sid] = fdata
else:
self.response['security'].append(sid)
[self.response[f].append(d) for f, d in zip(self.fields, fdata)]
# Add any field errors if
ferrors = XmlHelper.get_field_errors(node)
ferrors and self.field_errors.extend(ferrors)
def on_event(self, evt, is_final):
""" this is invoked from in response to COM PumpWaitingMessages - different thread """
for msg in XmlHelper.message_iter(evt):
for node, error in XmlHelper.security_iter(msg.GetElement('securityData')):
if error:
self.security_errors.append(error)
else:
self.on_security_node(node)
if is_final and self.response_type == 'frame':
index = self.response.pop('security')
frame = DataFrame(self.response, columns=self.fields, index=index)
frame.index.name = 'security'
self.response = frame
class HistoricalDataRequest(Request):
def __init__(self, symbols, fields, start=None, end=None, period='DAILY', addtl_sets=None, ignore_security_error=0, ignore_field_error=0):
"""Historical data request for bloomberg.
Parameters
----------
symbols : string or list
fields : string or list
start : start date (if None then use 1 year ago)
end : end date (if None then use today)
period : ('DAILY', 'WEEKLY', 'MONTHLY', 'QUARTERLY', 'SEMI-ANNUAL', 'YEARLY')
ignore_field_errors : bool
ignore_security_errors : bool
"""
Request.__init__(self, ignore_security_error=ignore_security_error, ignore_field_error=ignore_field_error)
assert period in ('DAILY', 'WEEKLY', 'MONTHLY', 'QUARTERLY', 'SEMI-ANNUAL', 'YEARLY')
self.symbols = isinstance(symbols, basestring) and [symbols] or symbols
self.fields = isinstance(fields, basestring) and [fields] or fields
if start is None:
start = datetime.today() - timedelta(365)
if end is None:
end = datetime.today()
self.start = to_datetime(start)
self.end = to_datetime(end)
self.period = period
# response related
self.response = {}
def get_bbg_service_name(self):
return '//blp/refdata'
def get_bbg_request(self, svc, session):
# create the bloomberg request object
request = svc.CreateRequest('HistoricalDataRequest')
[request.GetElement('securities').AppendValue(sec) for sec in self.symbols]
[request.GetElement('fields').AppendValue(fld) for fld in self.fields]
request.Set('startDate', self.start.strftime('%Y%m%d'))
request.Set('endDate', self.end.strftime('%Y%m%d'))
request.Set('periodicitySelection', self.period)
return request
def on_security_data_node(self, node):
"""process a securityData node - FIXME: currently not handling relateDate node """
sid = XmlHelper.get_child_value(node, 'security')
farr = node.GetElement('fieldData')
dmap = defaultdict(list)
for i in range(farr.NumValues):
pt = farr.GetValue(i)
[dmap[f].append(XmlHelper.get_child_value(pt, f)) for f in ['date'] + self.fields]
idx = dmap.pop('date')
frame = DataFrame(dmap, columns=self.fields, index=idx)
frame.index.name = 'date'
self.response[sid] = frame
def on_event(self, evt, is_final):
""" this is invoked from in response to COM PumpWaitingMessages - different thread """
for msg in XmlHelper.message_iter(evt):
# Single security element in historical request
node = msg.GetElement('securityData')
if node.HasElement('securityError'):
self.security_errors.append(XmlHelper.as_security_error(node.GetElement('securityError')))
else:
self.on_security_data_node(node)
def response_as_single(self, copy=0):
""" convert the response map to a single data frame with Multi-Index columns """
arr = []
for sid, frame in self.response.iteritems():
if copy:
frame = frame.copy()
'security' not in frame and frame.insert(0, 'security', sid)
arr.append(frame.reset_index().set_index(['date', 'security']))
return concat(arr).unstack()
def response_as_panel(self, swap=False):
panel = Panel(self.response)
if swap:
panel = panel.swapaxes('items', 'minor')
return panel
class IntrdayBarRequest(Request):
def __init__(self, symbol, interval, start=None, end=None, event='TRADE'):
"""Intraday bar request for bloomberg
Parameters
----------
symbols : string
interval : number of minutes
start : start date
end : end date (if None then use today)
event : (TRADE,BID,ASK,BEST_BID,BEST_ASK)
"""
Request.__init__(self)
assert event in ('TRADE', 'BID', 'ASK', 'BEST_BID', 'BEST_ASK')
assert isinstance(symbol, basestring)
if start is None:
start = datetime.today() - timedelta(30)
if end is None:
end = datetime.today()
self.symbol = symbol
self.interval = interval
self.start = to_datetime(start)
self.end = to_datetime(end)
self.event = event
# response related
self.response = defaultdict(list)
def get_bbg_service_name(self):
return '//blp/refdata'
def get_bbg_request(self, svc, session):
# create the bloomberg request object
start, end = self.start, self.end
request = svc.CreateRequest('IntradayBarRequest')
request.Set('security', self.symbol)
request.Set('interval', self.interval)
request.Set('eventType', self.event)
request.Set('startDateTime', session.CreateDatetime(start.year, start.month, start.day, start.hour, start.minute))
request.Set('endDateTime', session.CreateDatetime(end.year, end.month, end.day, end.hour, end.minute))
return request
def on_event(self, evt, is_final):
""" this is invoked from in response to COM PumpWaitingMessages - different thread """
response = self.response
for msg in XmlHelper.message_iter(evt):
bars = msg.GetElement('barData').GetElement('barTickData')
for i in range(bars.NumValues):
bar = bars.GetValue(i)
ts = bar.GetElement(0).Value
response['time'].append(datetime(ts.year, ts.month, ts.day, ts.hour, ts.minute))
response['open'].append(bar.GetElement(1).Value)
response['high'].append(bar.GetElement(2).Value)
response['low'].append(bar.GetElement(3).Value)
response['close'].append(bar.GetElement(4).Value)
response['volume'].append(bar.GetElement(5).Value)
response['events'].append(bar.GetElement(6).Value)
if is_final:
idx = response.pop('time')
self.response = DataFrame(response, columns=['open', 'high', 'low', 'close', 'volume', 'events'], index=idx)
class Terminal(object):
@staticmethod
def execute_request(request):
session = DispatchWithEvents('blpapicom.ProviderSession.1', ResponseHandler)
session.Start()
try:
svcname = request.get_bbg_service_name()
if not session.OpenService(svcname):
raise Exception('failed to open service %s' % svcname)
svc = session.GetService(svcname)
asbbg = request.get_bbg_request(svc, session)
session.SendRequest(asbbg)
session.do_init(request)
while session.waiting:
PumpWaitingMessages()
session.has_deferred_exception and session.raise_deferred_exception()
request.has_exception and request.raise_exception()
return request
finally:
session.Stop()
session.do_cleanup()
if __name__ == '__main__':
# 5 days ago
import pandas
d = pandas.datetools.BDay(-4).apply(datetime.now())
m = pandas.datetools.BMonthBegin(-2).apply(datetime.now())
def banner(msg):
print '*' * 25
print msg
print '*' * 25
banner('ReferenceDataRequest: single security, single field, frame response')
req = ReferenceDataRequest('msft us equity', 'px_last', response_type='frame')
print req.execute().response
banner('ReferenceDataRequest: single security, single field, map response')
req = ReferenceDataRequest('msft us equity', 'px_last', response_type='map')
print req.execute().response
banner('ReferenceDataRequest: multi-security, multi-field')
req = ReferenceDataRequest(['eurusd curncy', 'msft us equity'], ['px_open', 'px_last'])
print req.execute().response
banner('ReferenceDataRequest: single security, multi-field (with bulk), frame response')
req = ReferenceDataRequest('eurusd curncy', ['px_last', 'fwd_curve'])
req.execute()
print req.response
# DataFrame within a DataFrame
print req.response.fwd_curve[0].tail()
banner('ReferenceDataRequest: multi security, multi-field, bad field')
req = ReferenceDataRequest(['eurusd curncy', 'msft us equity'], ['px_last', 'fwd_curve'], ignore_field_error=1)
req.execute()
print req.response
banner('HistoricalDataRequest: multi security, multi-field, daily data')
req = HistoricalDataRequest(['eurusd curncy', 'msft us equity'], ['px_last', 'px_open'], start=d)
req.execute()
print req.response
print '--------- AS SINGLE TABLE ----------'
print req.response_as_single()
banner('HistoricalDataRequest: multi security, multi-field, weekly data')
req = HistoricalDataRequest(['eurusd curncy', 'msft us equity'], ['px_last', 'px_open'], start=m, period='WEEKLY')
req.execute()
print req.response
print '--------- AS SINGLE TABLE ----------'
print req.response_as_single()
print '--------- AS PANEL (id indexed) ----------'
print req.response_as_panel()
print '--------- AS PANEL (field indexed) ----------'
print req.response_as_panel(swap=1)
banner('IntrdayBarRequest: every hour')
req = IntrdayBarRequest('eurusd curncy', 60, start=d)
req.execute()
print req.response[-10:]
#
# HOW TO
#
# - Retrieve an fx vol surface: BbgReferenceDataRequest('eurusd curncy', 'DFLT_VOL_SURF_MID')
# - Retrieve a fx forward curve: BbgReferenceDataRequest('eurusd curncy', 'FWD_CURVE')
# - Retrieve dividends: BbgReferenceDataRequest('csco us equity', 'BDVD_PR_EX_DTS_DVD_AMTS_W_ANN')