Skip to content

Instantly share code, notes, and snippets.

@ddimtirov
Created November 2, 2013 14:42
Show Gist options
  • Save ddimtirov/7279583 to your computer and use it in GitHub Desktop.
Save ddimtirov/7279583 to your computer and use it in GitHub Desktop.
Use Bloomberg ActiveX control from Python to dump some prices to a file.
import logging
import win32com.client as com
class EvtLogger:
"""Event handler for Bloomberg.Data COM events."""
statusLog = logging.getLogger("Bloomberg.Data.onStatus")
dataLog = logging.getLogger("Bloomberg.Data.onData")
def OnStatus(self, code, subcode, descr): statusLog.info("%d.%d - %s" % (code, subcode, descr))
def OnData(self, code, cookie, fields, data, status): dataLog.debug(locals())
blpData = com.DispatchWithEvents("Bloomberg.Data", EvtLogger)
print "Instantiated Bloomberg.Data ActiveX control."
# read input
f = open('symbols.txt', 'r')
symbols = f.readlines()
f.close()
symbols = [symbol.strip() for symbol in symbols] # trim leading and trailing whitespace
symbols = filter(lambda x: len(x)!=0, symbols) # filter empty lines
print "Read %d symbols." % len(symbols)
bids = []
# request bids for the specified symbols, specifying value for
# the Result argument to force blocking (synchronous) call
blpData.Subscribe(symbols , 1, 'Bid', Results=bids, Monitor=False)
assert len(bids)==len(symbols), "Expected bids count: %d; actually received %d" % (len(symbols), len(bids))
print "Received all bids."
# write output file
f = open('bids.txt', 'w')
for row in zip(symbols, bids): print >> f, "%s\t%s" % row
f.close
print 'Created "bids.txt" output file'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment