def main():

    sessionOptions = blpapi.SessionOptions()
    sessionOptions.setServerHost(d_host)
    sessionOptions.setServerPort(d_port)

    print("Connecting to %s:%d" % (d_host, d_port))

    eventHandler = SessionEventHandler()

    session = blpapi.Session(sessionOptions, eventHandler.processEvent)

    if not session.startAsync():
        print("Failed to start session.")
        return

    global bEnd
    while bEnd == False:
        pass

    print("Terminating...")

    session.stop()
Esempio n. 2
0
    def __init__(self, host='localhost', port=8194, debug=False):
        """
        Create an object which manages connection to the Bloomberg API session

        Parameters
        ----------
        host: str
            Host name
        port: int
            Port to connect to
        debug: Boolean {True, False}
            Boolean corresponding to whether to log Bloomberg Open API request
            and response messages to stdout
        """
        # Fill SessionOptions
        sessionOptions = blpapi.SessionOptions()
        sessionOptions.setServerHost(host)
        sessionOptions.setServerPort(port)
        self._sessionOptions = sessionOptions
        # Create a Session
        self.session = blpapi.Session(sessionOptions)
        # initialize logger
        self.debug = debug
Esempio n. 3
0
def main():
    options = parseCmdLine()

    # Fill SessionOptions
    sessionOptions = blpapi.SessionOptions()
    for idx, host in enumerate(options.hosts):
        sessionOptions.setServerAddress(host, options.port, idx)
    sessionOptions.setSessionIdentityOptions(options.auth['option'])
    sessionOptions.setAutoRestartOnDisconnection(True)
    sessionOptions.setNumStartAttempts(len(options.hosts))

    print("Connecting to port %d on %s" % (
        options.port, ", ".join(options.hosts)))

    providerEventHandler = MyProviderEventHandler(options.service)
    providerSession = blpapi.ProviderSession(sessionOptions,
                                             providerEventHandler.processEvent)

    requesterEventHandler = MyRequesterEventHandler()
    requesterSession = blpapi.Session(sessionOptions,
                                      requesterEventHandler.processEvent)

    if options.role in ["server", "both"]:
        serverRun(providerSession, options)

    if options.role in ["client", "both"]:
        clientRun(requesterSession, options)

    # wait for enter key to exit application
    print("Press ENTER to quit")
    input()

    if options.role in ["server", "both"]:
        providerSession.stop()

    if options.role in ["client", "both"]:
        requesterSession.stop()
Esempio n. 4
0
    def start_bloomberg_session(self):
        tries = 0

        session = None

        # try up to 5 times to start a session
        while(tries < 5):
            try:
                # fill SessionOptions
                sessionOptions = blpapi.SessionOptions()
                sessionOptions.setServerHost(DataConstants().bbg_server)
                sessionOptions.setServerPort(DataConstants().bbg_server_port)

                self.logger.info("Starting Bloomberg session...")

                # create a Session
                session = blpapi.Session(sessionOptions)

                # start a Session
                if not session.start():
                    self.logger.error("Failed to start session.")
                    return

                self.logger.info("Returning session...")

                tries = 5
            except:
                tries = tries + 1

        # BBGLowLevelTemplate._session = session

        if session is None:
            self.logger.error("Failed to start session.")
            return


        return session
def prepareStandardSessionOptions(options):
    """Prepare SessionOptions for a regular session"""

    sessionOptions = blpapi.SessionOptions()
    for idx, host in enumerate(options.hosts):
        sessionOptions.setServerAddress(host, options.port, idx)

    # NOTE: If running without a backup server, make many attempts to
    # connect/reconnect to give that host a chance to come back up (the
    # larger the number, the longer it will take for SessionStartupFailure
    # to come on startup, or SessionTerminated due to inability to fail
    # over).  We don't have to do that in a redundant configuration - it's
    # expected at least one server is up and reachable at any given time,
    # so only try to connect to each server once.
    sessionOptions.setNumStartAttempts(len(options.hosts)
                                       if len(options.hosts) > 1 else 1000)

    print("Connecting to port %d on %s" % (
        options.port, ", ".join(options.hosts)))

    if options.tlsOptions:
        sessionOptions.setTlsOptions(options.tlsOptions)

    return sessionOptions
Esempio n. 6
0
    def __init__(self, server_host, server_port, connect_timeout, auto_restart_on_disconnection, num_start_attempts):
        # Fill SessionOptions
        session_options = blpapi.SessionOptions()
        session_options.setServerHost(server_host)
        session_options.setServerPort(server_port)
        session_options.setConnectTimeout(connect_timeout)
        session_options.setAutoRestartOnDisconnection(auto_restart_on_disconnection)
        session_options.setNumStartAttempts(num_start_attempts)

        print("Connecting to bloomberg host %s:%d" % (server_host, server_port))

        # Create a Session
        self.session = blpapi.Session(session_options)

        # Start a Session
        if not self.session.start():
            print("Failed to start session.")
            raise ConnectionError

        if not self.session.openService("//blp/refdata"):
            print("Failed to open //blp/refdata")
            raise ConnectionError

        self.ref_data_service = self.session.getService("//blp/refdata")
Esempio n. 7
0
    def connect(self):

        sessionOptions = blpapi.SessionOptions()
        sessionOptions.setServerHost(Bloomberg.BBG_ADDRESS['host'])
        sessionOptions.setServerPort(Bloomberg.BBG_ADDRESS['port'])

        logging.info(
            "Connecting to %s:%s" %
            (Bloomberg.BBG_ADDRESS['host'], Bloomberg.BBG_ADDRESS['port']))
        # Create a Session
        session = blpapi.Session(sessionOptions)

        # Start a Session
        if not session.start():
            logging.error("Failed to start Bloomberg session.")

        # Open service to get historical data from
        if not session.openService("//blp/refdata"):
            logging.error("Failed to open //blp/refdata")

        # Obtain previously opened service
        self.service = session.getService("//blp/refdata")  # Start a Session

        self.session = session
def main():
    print('main init')
    options = parseCmdLine()

    print(options)
    # Fill SessionOptions
    sessionOptions = blpapi.SessionOptions()
    sessionOptions.setServerHost(options.host)
    sessionOptions.setServerPort(options.port)

    print("Connecting to %s:%s" % (options.host, options.port))
    # Create a Session
    session = blpapi.Session(sessionOptions)

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

    try:
        # Open service to get historical data from
        if not session.openService("//blp/refdata"):
            print("Failed to open //blp/refdata")
            return

        # Obtain previously opened service
        refDataService = session.getService("//blp/refdata")

        # Create and fill the request for the historical data
        request = refDataService.createRequest("HistoricalDataRequest")
        # request.getElement("securities").appendValue("IBM US Equity")
        if options.security1 != '':
            request.getElement("securities").appendValue(options.security1)
        if options.security2 != '':
            request.getElement("securities").appendValue(options.security2)
        if options.security3 != '':
            request.getElement("securities").appendValue(options.security3)
        if options.security4 != '':
            request.getElement("securities").appendValue(options.security4)
        if options.security5 != '':
            request.getElement("securities").appendValue(options.security5)
        if options.security6 != '':
            request.getElement("securities").appendValue(options.security6)
        if options.security7 != '':
            request.getElement("securities").appendValue(options.security7)
        if options.security8 != '':
            request.getElement("securities").appendValue(options.security8)
        if options.security9 != '':
            request.getElement("securities").appendValue(options.security9)
        if options.security10 != '':
            request.getElement("securities").appendValue(options.security10)

        request.getElement("fields").appendValue("PX_LAST")
        request.getElement("fields").appendValue("OPEN")
        request.set("periodicityAdjustment", "ACTUAL")
        request.set("periodicitySelection", "DAILY")
        request.set("startDate", "20170108")
        request.set("endDate", "20180910")
        request.set("maxDataPoints", 10000)

        print("Sending Request:", request)
        # Send the request
        session.sendRequest(request)

        # Process received events
        i=0
        columns = ['OPEN', 'LAST', 'DATE', 'SEC']
        dftt = pd.DataFrame(columns=columns)
        pd_data = pd.DataFrame(columns=columns)

        while(True):
            # We provide timeout to give the chance for Ctrl+C handling:
            ev = session.nextEvent(500)
            # print msg.getElement('securityData').getElementAsString('security')
            for msg in ev:
                # print 'placeholder'
                i = i + 1
                try:
                    print(msg.getElement('securityData').getElementAsString('security'))
                    for test in msg.getElement('securityData').getElement('fieldData').values():
                        del dftt
                        sec = msg.getElement('securityData').getElementAsString('security')
                        date = test.getElementAsDatetime('date')
                        print(date)
                        px_open = test.getElementAsFloat('OPEN')
                        print(px_open)
                        px_last = test.getElementAsFloat('PX_LAST')
                        print(px_last)
                        d = {'col1': [1, 2], 'col2': [3, 4]}
                        dftt = pd.DataFrame([[px_open, px_last, date, sec]], columns=columns)
                        pd_data = pd_data.append(dftt, ignore_index=True)
                except:
                    pass

            if ev.eventType() == blpapi.Event.RESPONSE:
                # Response completly received, so we could exit
                break
    finally:
        # Stop the session
        session.stop()

        pd_data.to_pickle(r'C:\SRDEV\Data\BBG\bbg_data.pickle')
        pd_data.to_csv(r'C:\SRDEV\Data\BBG\bbg_data.csv', header = True, index = False)
        print('retrieve done')
def main():
    options = parseCmdLine()

    # Fill SessionOptions
    sessionOptions = blpapi.SessionOptions()
    for idx, host in enumerate(options.hosts):
        sessionOptions.setServerAddress(host, options.port, idx)
    sessionOptions.setAuthenticationOptions(options.auth)
    sessionOptions.setAutoRestartOnDisconnection(True)
    sessionOptions.setNumStartAttempts(len(options.hosts))

    myEventHandler = MyEventHandler()

    # Create a Session
    session = blpapi.ProviderSession(sessionOptions,
                                     myEventHandler.processEvent)

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

    providerIdentity = session.createIdentity()

    if options.auth:
        isAuthorized = False
        authServiceName = "//blp/apiauth"
        if session.openService(authServiceName):
            authService = session.getService(authServiceName)
            isAuthorized = authorize(authService, providerIdentity, session,
                                     blpapi.CorrelationId("auth"))
        if not isAuthorized:
            print "No authorization"
            return

    topicList = blpapi.TopicList()
    topicList.add(options.service + "/" + options.topic,
                  blpapi.CorrelationId(MyStream(options.topic)))

    # Create topics
    session.createTopics(topicList,
                         blpapi.ProviderSession.AUTO_REGISTER_SERVICES,
                         providerIdentity)
    # createTopics() is synchronous, topicList will be updated
    # with the results of topic creation (resolution will happen
    # under the covers)

    streams = []
    for i in xrange(topicList.size()):
        stream = topicList.correlationIdAt(i).value()
        status = topicList.statusAt(i)
        topicString = topicList.topicStringAt(i)

        if (status == blpapi.TopicList.CREATED):
            stream.topic = session.getTopic(topicList.messageAt(i))
            streams.append(stream)
        else:
            print "Stream '%s': topic not resolved, status = %d" % (stream.id,
                                                                    status)

    service = session.getService(options.service)

    try:
        # Now we will start publishing
        while streams and g_running:
            event = service.createPublishEvent()
            eventFormatter = blpapi.EventFormatter(event)

            for stream in streams:
                eventFormatter.appendMessage("PageData", stream.topic)
                eventFormatter.pushElement("rowUpdate")

                eventFormatter.appendElement()
                eventFormatter.setElement("rowNum", 1)
                eventFormatter.pushElement("spanUpdate")

                eventFormatter.appendElement()
                eventFormatter.setElement("startCol", 20)
                eventFormatter.setElement("length", 4)
                eventFormatter.setElement("text", "TEST")
                eventFormatter.popElement()

                eventFormatter.appendElement()
                eventFormatter.setElement("startCol", 25)
                eventFormatter.setElement("length", 4)
                eventFormatter.setElement("text", "PAGE")
                eventFormatter.popElement()

                tm = time.strftime("%X")
                eventFormatter.appendElement()
                eventFormatter.setElement("startCol", 30)
                eventFormatter.setElement("length", len(tm))
                eventFormatter.setElement("text", tm)
                eventFormatter.setElement("attr", "BLINK")
                eventFormatter.popElement()

                eventFormatter.popElement()
                eventFormatter.popElement()

                eventFormatter.appendElement()
                eventFormatter.setElement("rowNum", 2)
                eventFormatter.pushElement("spanUpdate")
                eventFormatter.appendElement()
                eventFormatter.setElement("startCol", 20)
                eventFormatter.setElement("length", 9)
                eventFormatter.setElement("text", "---------")
                eventFormatter.setElement("attr", "UNDERLINE")
                eventFormatter.popElement()
                eventFormatter.popElement()
                eventFormatter.popElement()

                eventFormatter.appendElement()
                eventFormatter.setElement("rowNum", 3)
                eventFormatter.pushElement("spanUpdate")
                eventFormatter.appendElement()
                eventFormatter.setElement("startCol", 10)
                eventFormatter.setElement("length", 9)
                eventFormatter.setElement("text", "TEST LINE")
                eventFormatter.popElement()
                eventFormatter.appendElement()
                eventFormatter.setElement("startCol", 23)
                eventFormatter.setElement("length", 5)
                eventFormatter.setElement("text", "THREE")
                eventFormatter.popElement()
                eventFormatter.popElement()
                eventFormatter.popElement()
                eventFormatter.popElement()

                eventFormatter.setElement("contributorId",
                                          options.contributorId)
                eventFormatter.setElement("productCode", 1)
                eventFormatter.setElement("pageNumber", 1)

            for msg in event:
                print msg

            session.publish(event)
            time.sleep(10)
    finally:
        # Stop the session
        session.stop()
def main():
    parseTickerList('ticker_list.txt')
    options = parseCmdLine()

    # Fill SessionOptions
    sessionOptions = blpapi.SessionOptions()
    sessionOptions.setServerHost(options.host)
    sessionOptions.setServerPort(options.port)

    print "Connecting to %s:%d" % (options.host, options.port)

    # Create a Session
    session = blpapi.Session(sessionOptions)

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

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

    #tickers = ['FB', 'MSFT', 'IBM', 'TWTR', 'AAPL', 'GOOG']
    tickers = parseTickerList('ticker_list.txt')
    data = {}
    data['info'] = 'Provides ticker->[(date, value)]'
    data['tickers'] = {}

    to_query = []
    for ticker in tickers:
        data['tickers'][ticker] = []
        today = datetime.date.today()
        today -= datetime.timedelta(days=1)
        year = 2014
        months = range(8, 10)
        first_weekends = [2, 6]

        ticker_vals = []
        for month, first_weekend in zip(months, first_weekends):
            for day in xrange(1, 31):
                avg_val = 0.0
                nval = 0
                if (day - first_weekend) % 7 == 0 or (day - first_weekend -
                                                      1) % 7 == 0:
                    continue
                for hour in xrange(13, 20):
                    to_query.append((year, month, day, hour, ticker))

    lock = Lock()
    threads = []
    for year, month, day, hour, ticker in to_query:
        t = Thread(target=queryData,
                   args=(session, year, month, day, hour, ticker, data, lock))
        threads.append(t)

    batch_size = 50
    for k in xrange(len(threads) / batch_size):
        for l in xrange(batch_size * k, min(batch_size * (k + 1),
                                            len(threads))):
            threads[l].start()

        for l in xrange(batch_size * k, min(batch_size * (k + 1),
                                            len(threads))):
            threads[l].join()

    #print data['tickers']['PBR']
    f = open('data/ticker_data.pickle', 'w+')
    pickle.dump(data, f)
    f.close()

    print 'DONE!'
Esempio n. 11
0
def main():
    options = parseCmdLine()

    # Fill SessionOptions
    sessionOptions = blpapi.SessionOptions()
    for idx, host in enumerate(options.hosts):
        sessionOptions.setServerAddress(host, options.port, idx)
    sessionOptions.setAuthenticationOptions(options.auth)
    sessionOptions.setAutoRestartOnDisconnection(True)

    # NOTE: If running without a backup server, make many attempts to
    # connect/reconnect to give that host a chance to come back up (the
    # larger the number, the longer it will take for SessionStartupFailure
    # to come on startup, or SessionTerminated due to inability to fail
    # over).  We don't have to do that in a redundant configuration - it's
    # expected at least one server is up and reachable at any given time,
    # so only try to connect to each server once.
    sessionOptions.setNumStartAttempts(1 if len(options.hosts) > 1 else 1000)

    print("Connecting to port %d on %s" %
          (options.port, " ".join(options.hosts)))

    PUBLISH_MESSAGE_TYPE = blpapi.Name(options.messageType)

    myEventHandler = MyEventHandler(options.service, PUBLISH_MESSAGE_TYPE,
                                    options.fields, options.eids, options.rssc)

    # Create a Session
    session = blpapi.ProviderSession(sessionOptions,
                                     myEventHandler.processEvent)

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

    providerIdentity = session.createIdentity()

    if options.auth:
        isAuthorized = False
        authServiceName = "//blp/apiauth"
        if session.openService(authServiceName):
            authService = session.getService(authServiceName)
            isAuthorized = authorize(authService, providerIdentity, session,
                                     blpapi.CorrelationId("auth"))
        if not isAuthorized:
            print("No authorization")
            return

    serviceOptions = blpapi.ServiceRegistrationOptions()
    if options.groupId is not None:
        serviceOptions.setGroupId(options.groupId)
    serviceOptions.setServicePriority(options.priority)

    if options.ssc:
        sscBegin, sscEnd, sscPriority = map(int, options.ssc.split(","))
        print(("Adding active sub service code range [%s, %s] @ %s" %
               (sscBegin, sscEnd, sscPriority)))
        try:
            serviceOptions.addActiveSubServiceCodeRange(
                sscBegin, sscEnd, sscPriority)
        except blpapi.Exception as e:
            print(("FAILED to add active sub service codes."
                   " Exception %s" % e.description()))

    if not session.registerService(options.service, providerIdentity,
                                   serviceOptions):
        print("Failed to register '%s'" % options.service)
        return

    service = session.getService(options.service)
    elementDef = service.getEventDefinition(PUBLISH_MESSAGE_TYPE)
    eventCount = 0

    try:
        numPublished = 0
        while g_running:
            event = service.createPublishEvent()

            with g_condition:
                while g_availableTopicCount == 0:
                    # Set timeout to 1 - give a chance for CTRL-C
                    g_condition.wait(1)
                    if not g_running:
                        return

                publishNull = False
                if (options.clearInterval > 0
                        and eventCount == options.clearInterval):
                    eventCount = 0
                    publishNull = True
                eventFormatter = blpapi.EventFormatter(event)
                for topicName, stream in g_streams.items():
                    if not stream.isAvailable():
                        continue
                    eventFormatter.appendMessage(PUBLISH_MESSAGE_TYPE,
                                                 stream.topic)
                    if publishNull:
                        stream.fillDataNull(eventFormatter, elementDef)
                    else:
                        eventCount += 1
                        stream.next()
                        stream.fillData(eventFormatter, elementDef)

            for msg in event:
                print(msg)

            session.publish(event)
            time.sleep(1)
            numPublished += 1
            if numPublished % 10 == 0:
                deactivate(options, session)
                time.sleep(30)
                activate(options, session)

    finally:
        # Stop the session
        session.stop()
def main():
    global options
    options = parseCmdLine()

    # Fill SessionOptions
    sessionOptions = blpapi.SessionOptions()
    sessionOptions.setServerHost(options.host)
    sessionOptions.setServerPort(options.port)

    print("Connecting to %s:%d" % (options.host, options.port))

    # Create a Session
    session = blpapi.Session(sessionOptions)

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

    if not session.openService(APIFLDS_SVC):
        print("Failed to open", APIFLDS_SVC)
        return

    fieldInfoService = session.getService(APIFLDS_SVC)
    request = fieldInfoService.createRequest("CategorizedFieldSearchRequest")
    request.set("searchSpec", "last price")
    exclude = request.getElement("exclude")
    exclude.setElement("fieldType", "Static")
    request.set("returnFieldDocumentation", False)

    print("Sending Request:", request)
    session.sendRequest(request)

    try:
        # Process received events
        while(True):
            # We provide timeout to give the chance to Ctrl+C handling:
            event = session.nextEvent(500)
            if event.eventType() != blpapi.Event.RESPONSE and \
                    event.eventType() != blpapi.Event.PARTIAL_RESPONSE:
                continue
            for msg in event:
                if msg.hasElement(FIELD_SEARCH_ERROR):
                    print(msg)
                    continue

                categories = msg.getElement("category")
                for category in list(categories.values()):
                    Name = category.getElementAsString("categoryName")
                    Id = category.getElementAsString("categoryId")
                    print("\n  Category Name:%s\tId:%s" % \
                        (Name.ljust(CAT_NAME_LEN), Id))

                    printHeader()
                    fields = category.getElement("fieldData")
                    for field in list(fields.values()):
                        printField(field)
                    print()

            # Response completly received, so we could exit
            if event.eventType() == blpapi.Event.RESPONSE:
                break
    finally:
        # Stop the session
        session.stop()
Esempio n. 13
0
def req_historical_data(bbg_identifier, startDate, endDate):

    # Recast start & end dates in Bloomberg's format
    startDate = date_to_str(startDate, "%Y%m%d")
    endDate = date_to_str(endDate, "%Y%m%d")

    if (pd.to_datetime(startDate) >= pd.to_datetime(endDate)):
        sys.exit(
            "in req_historical_data in 'bloomberg_functions.py': " + \
            "specified startDate is later than endDate!"
        )

    # First, check to see if there is already a local .p data file with the
    # data you need for bbg_identifier. If it's not there, create it.
    if not os.path.isdir("bbg_data"):
        os.makedirs("bbg_data")
        print("created the 'bbg_data' folder.")

    if (bbg_identifier + ".csv") in os.listdir("bbg_data"):
        old_bbg_data = pd.read_csv("bbg_data/" + bbg_identifier + ".csv")

        first_old = pd.to_datetime(min(old_bbg_data['Date'])).date()
        last_old = pd.to_datetime(max(old_bbg_data['Date'])).date()

        first_new = pd.to_datetime(startDate).date()
        last_new = pd.to_datetime(endDate).date()

        if first_old <= first_new and last_old >= last_new:
            # Don't need to make a query; have all data we need.
            histdata = old_bbg_data[[(pd.to_datetime(x).date() <= last_new) &
                                     (pd.to_datetime(x).date() >= first_new)
                                     for x in old_bbg_data['Date']]]
            histdata.reset_index(drop=True, inplace=True)
            histdata.Date = pd.to_datetime(histdata.Date)
            return histdata

        if first_old > first_new and last_old < last_new:
            # do nothing for now, just requery the bigger dataset. Can refine
            # this case later.
            print(
                "overwriting old data with date range: " + startDate + \
                " to " + endDate
            )
        else:
            if first_new < first_old:
                endDate = date_to_str(first_old, "%Y%m%d")
            else:
                startDate = date_to_str(last_old, "%Y%m%d")

    print(startDate)

    options = parseCmdLine()

    # Fill SessionOptions
    sessionOptions = blpapi.SessionOptions()
    sessionOptions.setServerHost(options.host)
    sessionOptions.setServerPort(options.port)

    print("Connecting to %s:%s" % (options.host, options.port))
    # Create a Session
    session = blpapi.Session(sessionOptions)

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

    try:
        # Open service to get historical data from
        if not session.openService("//blp/refdata"):
            print("Failed to open //blp/refdata")
            return

        # Obtain previously opened service
        refDataService = session.getService("//blp/refdata")

        # Create and fill the request for the historical data
        request = refDataService.createRequest("HistoricalDataRequest")
        request.getElement("securities").appendValue(bbg_identifier)
        request.getElement("fields").appendValue("OPEN")
        request.getElement("fields").appendValue("HIGH")
        request.getElement("fields").appendValue("LOW")
        request.getElement("fields").appendValue("PX_LAST")
        request.getElement("fields").appendValue("EQY_WEIGHTED_AVG_PX")
        request.set("periodicityAdjustment", "ACTUAL")
        request.set("periodicitySelection", "DAILY")
        request.set("startDate", startDate)
        request.set("endDate", endDate)
        request.set("maxDataPoints", 1400)  # Don't adjust please :)

        print("Sending Request:", request)
        # Send the request
        session.sendRequest(request)

        # Process received events
        while (True):
            # We provide timeout to give the chance for Ctrl+C handling:
            ev = session.nextEvent(500)
            for msg in ev:
                if str(msg.messageType()) == "HistoricalDataResponse":

                    histdata = []

                    for fd in msg.getElement("securityData").getElement(
                            "fieldData").values():
                        histdata.append([fd.getElementAsString("date"), \
                                         fd.getElementAsFloat("OPEN"),
                                         fd.getElementAsFloat(
                                             "HIGH"),
                                         fd.getElementAsFloat("LOW"), \
                                         fd.getElementAsFloat("PX_LAST"), \
                                         fd.getElementAsFloat(
                                             "EQY_WEIGHTED_AVG_PX")])

                    histdata = pd.DataFrame(histdata,
                                            columns=[
                                                "Date", "Open", "High", "Low",
                                                "Close", "VWAP"
                                            ])

            if ev.eventType() == blpapi.Event.RESPONSE:
                # Response completely received, so we could exit
                if 'old_bbg_data' in locals():
                    histdata = pd.concat([histdata, old_bbg_data], axis=0)
                    histdata = histdata.drop_duplicates('Date')
                    histdata = histdata.sort_values('Date')
                    histdata.reset_index(drop=True, inplace=True)

                pd.DataFrame.to_csv(histdata,
                                    "bbg_data/" + bbg_identifier + ".csv",
                                    index=False)
                histdata.Date = pd.to_datetime(histdata.Date)
                return histdata
    finally:
        # Stop the session
        session.stop()
Esempio n. 14
0
def main():
    options = parseCmdLine()

    # Fill SessionOptions
    sessionOptions = blpapi.SessionOptions()
    for idx, host in enumerate(options.hosts):
        sessionOptions.setServerAddress(host, options.port, idx)
    sessionOptions.setAuthenticationOptions(options.auth)
    sessionOptions.setAutoRestartOnDisconnection(True)

    # NOTE: If running without a backup server, make many attempts to
    # connect/reconnect to give that host a chance to come back up (the
    # larger the number, the longer it will take for SessionStartupFailure
    # to come on startup, or SessionTerminated due to inability to fail
    # over).  We don't have to do that in a redundant configuration - it's
    # expected at least one server is up and reachable at any given time,
    # so only try to connect to each server once.
    sessionOptions.setNumStartAttempts(1 if len(options.hosts) > 1 else 1000)

    print "Connecting to port %d on %s" % (options.port, " ".join(
        options.hosts))

    PUBLISH_MESSAGE_TYPE = blpapi.Name(options.messageType)

    myEventHandler = MyEventHandler(options.service, PUBLISH_MESSAGE_TYPE,
                                    options.fields, options.eids)

    # Create a Session
    session = blpapi.ProviderSession(sessionOptions,
                                     myEventHandler.processEvent)

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

    providerIdentity = session.createIdentity()

    if options.auth:
        isAuthorized = False
        authServiceName = "//blp/apiauth"
        if session.openService(authServiceName):
            authService = session.getService(authServiceName)
            isAuthorized = authorize(authService, providerIdentity, session,
                                     blpapi.CorrelationId("auth"))
        if not isAuthorized:
            print "No authorization"
            return

    serviceOptions = blpapi.ServiceRegistrationOptions()
    if options.groupId is not None:
        serviceOptions.setGroupId(options.groupId)
    serviceOptions.setServicePriority(options.priority)

    if not session.registerService(options.service, providerIdentity,
                                   serviceOptions):
        print "Failed to register '%s'" % options.service
        return

    service = session.getService(options.service)
    elementDef = service.getEventDefinition(PUBLISH_MESSAGE_TYPE)

    try:
        while g_running:
            event = service.createPublishEvent()

            with g_condition:
                while g_availableTopicCount == 0:
                    # Set timeout to 1 - give a chance for CTRL-C
                    g_condition.wait(1)
                    if not g_running:
                        return

                eventFormatter = blpapi.EventFormatter(event)
                for topicName, stream in g_streams.iteritems():
                    if not stream.isAvailable():
                        continue
                    stream.next()
                    eventFormatter.appendMessage(PUBLISH_MESSAGE_TYPE,
                                                 stream.topic)
                    stream.fillData(eventFormatter, elementDef)

            for msg in event:
                print msg

            session.publish(event)
            time.sleep(10)

    finally:
        # Stop the session
        session.stop()
Esempio n. 15
0
 def _create_session(self):
     opts = blpapi.SessionOptions()
     opts.setServerHost(self.host)
     opts.setServerPort(self.port)
     return blpapi.Session(opts)
Esempio n. 16
0
def main(ticker, eventDateTime, daysPre=5, daysPost=5, interval=15):
    global options

    sessionOptions = blpapi.SessionOptions()

    try:
        options = parseCmdLine()
        # Fill SessionOptions
        host = options.host
        port = options.port
    except:
        host = 'localhost'
        port = 8194

    sessionOptions.setServerHost(host)
    sessionOptions.setServerPort(port)

    print("\n\nConnecting to %s:%d\n" % (host, port))

    # Create a Session
    session = blpapi.Session(sessionOptions)

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

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

    refDataService = session.getService("//blp/refdata")
    request = refDataService.createRequest("IntradayBarRequest")
    request.set("security", ticker + " US Equity")
    request.set("eventType", "TRADE") # last price
    request.set("interval", interval)  # bar interval in minutes


    request.set("startDateTime", getStartDate(eventDateTime, daysPre))
    request.set("endDateTime"  , getEndDate(eventDateTime, daysPost))



    # print("Sending Request:", request)
    session.sendRequest(request)

    print('hello')
    try:

        # Process received events
        data = []
        while(True):
            # We provide timeout to give the chance to Ctrl+C handling:
            ev = session.nextEvent(500)

            flds = ['time', 'close', 'volume']

            for msg in ev:
                if msg.messageType() == 'IntradayBarResponse':
                    barTickData = msg.getElement('barData').getElement('barTickData')
                    for i in range(barTickData.numValues()):
                        data_bar = {}
                        for fld in flds:
                            dt = barTickData.getValue(i).getElement(0).getValue()
                            val = (barTickData.getValue(i).getElement(fld).getValue())
                            data_bar[fld] = val
                        data.append(data_bar)

            # Response completly received, so we could exit
            if ev.eventType() == blpapi.Event.RESPONSE:
                break

        return _to_dataframe(data)

    finally:
        # Stop the session
        session.stop()
        print('Session Closed')
Esempio n. 17
0
	def initialize(self):

		self.securities=[]
		self.subscription_fields=[]
		
		self.session_option = blpapi.SessionOptions()
		
		self.session_options.setServerHost(self.host)
		self.session_options.setServerPort(self.port)
		
		self.session = blpapi.Session(options=self.session_options, eventHandler=self.process_event)

		if not self.session.start():
			raise ("Failed to start session.")
			return

		while not self.ready:
			pass

		def add_field(self, field_name):
			self.subscription_fields.append(field_name)

		def add_security(self, ticker):
			new_sec = Security(self, ticker)
			self.securities.append(new_sec)
			return new_sec

		def start(self):

			for s in self.securities:
				self.add_subscription(s)

		def submit_request(self,req,message_handler):
			try:
				cID = self.session.sendRequest(request=req)
				self.request_message_handlers[cID.value()] = message_handler
				 print("Request submitted (" + str(cID) + "): \n" + str(req))

			except Exception as err:
				print("EasyIOI >> Error submitting request: " + str(err))

		def add_subscription(self, security):
			try:
				self.next_cor_id+=1
				c_id = blpapi.CorrelationId(self.next_cor_id)
				subscriptions = blpapi.SubscriptionList()
				flds = ""
				for s in self.subscription_fields:
					flds += s + ","
				subscriptions.add(security.name, flds[:-1], "", c_id)
				self.session.subscribe(subscriptions)
				self.subscription_message_handlers[c_id.value()] = security.process_message
				print("Request submitted (" + str(c_id.value()) + "): " + security.name + "/" +str(flds[:-1])+"\n")

			except Exception as err:
				print("EasyIOI >> Error subscribing to topic: " + str(err))

		def process_event(self, event, session):

			print("Processing Event (" + str(event.eventType()) + ")")

			if event.eventType() == blpapi.Event.ADMIN:
				self.process_admin_event(event, session)

			elif event.eventType() == blpapi.Event.SESSION_STATUS:
				self.process_session_status_event(event, session)

			elif event.eventType() == blpapi.Event.SERVICE_STATUS:
				self.process_service_status_event(event, session)

			elif event.eventType() == blpapi.Event.SUBSCRIPTION_DATA:
				self.process_service_subscription_data_event(event, session)

			elif event.eventType() == blpapi.Event.SUBSCRIPTION_STATUS:
				self.process_service_subscription_status_event(event, session)

			elif event.eventType() == blpapi.Event.RESPONSE or event.eventype() ==blpapi.Event.PARTIAL_RESPONSE:
				self.process_response_event(event, session)

			else:
				self.process_misc_event(event, session)

			return False

		def process_admin_event(self,event,session):

			print("Processing ADMIN event...")

			for msg in event:
				if msg.messageType() == SLOW_CONSUMER_WARNING:
					print("Slow Consumer Warning")
				elif msg.messageType() == SLOW_CONSUMER_WARNING_CLEARED:
					print("Slow Consumer Warning Cleared")

		def process_session_status_event(self,event,session):

			print("Processing SESSION_STATUS event...")

			for msg in event:
				if msg.messageType() == SESSION_STARTED:
					session.openServiceAsync(self.ioi_service_name)
				elif msg.messageType() == SESSION_STARTUP_FAILURE:
					print("Session Startup Failure")
				elif msg.messageType() == SESSION_TERMINATED:
					print("Session Terminated")
					pass				
				elif msg.messageType() == SESSION_CONNECTION_UP:
					print("Session Connection Up")
					pass
				elif msg.messageType() == SESSION_CONNECTION_DOWN:
					print("Session Connection Down")
					pass

		def process_service_status_event(self,event,session):
			print("Processing SERVICE_STATUS event...")

			for msg in event:
				if msg.messageType() == SERVICE_OPENED:
					svc = msg.getElementAsString("serviceName")
					if svc == self.ioi_service_name:
						self.ioi = session.getService(self.ioi_service_name)
					if self.svc_opened:
						self.ready = True
					else:
						self.svc_opened = True

				elif msg.messageType() == SERVICE_OPEN_FAILURE:
					print("Service Open Failure")

		def process_subscription_status_event(self,event,session):
			print("Processing SUBSCRIPTION_STATUS event...")

			for msg in event:
				if msg.messageType() == SUBSCRIPTION_STARTED:
					pass # Log event
				elif msg.messageType() == SUBSCRIPTION_FAILURE:
					print("Error: subscription failure" + msg.correlationIds()[0].value())
				elif msg.messageType() == SUBSCRIPTION_TERMINATED:
					pass # Log event

		def process_service_subscription_data_event(self,event,session):
			print("Processing SUBSCRIPTION_DATA event...")

			for msg in event:
				c_id = msg.correlationIds()[0].value()
				if c_id in self.subscription_message_handlers:
					self.subscription_message_handlers[c_id](msg)
				else:
					print("Unrecognized correlation ID in subscription data event. No event handler can be found for cID: " + str(c_id))

		def process_response_event(self,event,session):
Esempio n. 18
0
def main():
    global options
    options = parseCmdLine()

    # Fill SessionOptions
    sessionOptions = blpapi.SessionOptions()
    sessionOptions.setServerHost(options.host)
    sessionOptions.setServerPort(options.port)

    print("Connecting to %s:%d" % (options.host, options.port))

    # Create a Session
    session = blpapi.Session(sessionOptions)

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

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

    refDataService = session.getService("//blp/refdata")
    request = refDataService.createRequest("ReferenceDataRequest")

    # append securities to request
    request.append("securities", "IBM US Equity")
    request.append("securities", "MSFT US Equity")

    # append fields to request
    request.append("fields", "PX_LAST")
    request.append("fields", "DS002")
    request.append("fields", "EQY_WEIGHTED_AVG_PX")

    # add overrides
    overrides = request.getElement("overrides")
    override1 = overrides.appendElement()
    override1.setElement("fieldId", "VWAP_START_TIME")
    override1.setElement("value", "9:30")
    override2 = overrides.appendElement()
    override2.setElement("fieldId", "VWAP_END_TIME")
    override2.setElement("value", "11:30")

    print("Sending Request:", request)
    cid = session.sendRequest(request)

    try:
        # Process received events
        while (True):
            # We provide timeout to give the chance to Ctrl+C handling:
            ev = session.nextEvent(500)
            for msg in ev:
                if cid in msg.correlationIds():
                    processMessage(msg)
            # Response completly received, so we could exit
            if ev.eventType() == blpapi.Event.RESPONSE:
                break
    finally:
        # Stop the session
        session.stop()
Esempio n. 19
0
 def __init__(self, host="localhost", port=8194):
     self.options = blpapi.SessionOptions()
     self.options.setServerHost(host)
     self.options.setServerPort(port)
     self.session = None
     self.ref_data_service = None
Esempio n. 20
0
        def bloomberg_subscribe():
            # Bloomberg API runs on localhost:8194
            options = {'host': 'localhost', 'port': 8194}

            # securities you want data of
            securities = [
                "XAU Curncy", "COA Comdty", "GI1 Index", "SPX INDEX",
                "FSSTI Index", "HSI Index", "BDIY Index"
            ]

            # fields which you want to get the data of
            fields = ["LAST_PRICE", "RT_PX_CHG_NET_1D", "RT_PX_CHG_PCT_1D"]

            # Fill SessionOptions
            sessionOptions = blpapi.SessionOptions()
            sessionOptions.setServerHost(options['host'])
            sessionOptions.setServerPort(options['port'])

            self.logger.info("Bloomberg API - Connecting to %s:%d" %
                             (options['host'], options['port']))

            # Create a Session
            session = blpapi.Session(sessionOptions)

            # Start a Session
            if not session.start():
                event = session.nextEvent()
                for msg in event:
                    self.logger.error(msg)
                self.logger.error("Failed to start session.")
                return 1
            else:
                self.logger.info("Bloomberg session started")

            if not session.openService("//blp/mktdata"):
                self.logger.error("Failed to open //blp/mktdata")
                return 1
            else:
                self.logger.info("Opening service to //blp/mktdata")

            # Creating subscriptions to required Bloomberg data
            subscriptions = blpapi.SubscriptionList()
            for sec in securities:
                subscriptions.add(sec, fields, "", blpapi.CorrelationId(sec))

            session.subscribe(subscriptions)

            try:
                # Handle received events in an event loop
                eventCount = 4950
                self.logger.info("Subscription begins")
                while (True):
                    event = session.nextEvent(500)
                    eventCount = (eventCount + 1) % 5001
                    # self.market_data.append({"TIME_RETRIEVED": str(datetime.now())})

                    for msg in event:
                        # NOTE: To print the entire message for debugging:
                        # print("%s - %s" % (msg.correlationIds()[0].value(), msg))
                        if event.eventType(
                        ) == blpapi.Event.SUBSCRIPTION_DATA and msg.hasElement(
                                fields[0]):

                            d = {}
                            d["TITLE"] = msg.correlationIds()[0].value()
                            d['TIME_RETRIEVED'] = str(datetime.now())

                            for field_name in fields:
                                if (msg.asElement().hasElement(field_name)):
                                    field_value = msg.asElement(
                                    ).getElementAsString(field_name)
                                else:
                                    field_value = 0

                                if field_name == "LAST_PRICE":
                                    d['LAST_PRICE'] = field_value
                                elif field_name == "RT_PX_CHG_NET_1D":
                                    d['NET_CHANGE'] = field_value
                                elif field_name == "RT_PX_CHG_PCT_1D":
                                    d['PCT_CHANGE'] = field_value
                                else:
                                    d[field_name] = field_value

                            self.market_data[msg.correlationIds()
                                             [0].value()] = d

                    # roughly every 10 minutes, otherwise we'll be logging too much
                    if eventCount == 5000:
                        self.logger.info("Data From Bloomberg: " +
                                         str(self.market_data))
            except Exception as e:
                self.logger.error(e)
            finally:
                # Stop the session
                self.logger.info("Terminating Bloomberg session")
                session.stop()
                return 1
def main():
    global options
    options = parseCmdLine()

    # Fill SessionOptions
    sessionOptions = blpapi.SessionOptions()
    sessionOptions.setServerHost(options.host)
    sessionOptions.setServerPort(options.port)

    print("Connecting to %s:%d" % (options.host, options.port))

    # Create a Session
    session = blpapi.Session(sessionOptions)

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

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

    refDataService = session.getService("//blp/refdata")
    request = refDataService.createRequest("ReferenceDataRequest")

    # Add securities to request.
    request.append("securities", "CWHL 2006-20 1A1 Mtge")
    # ...

    # Add fields to request. Cash flow is a table (data set) field.

    request.append("fields", "MTG_CASH_FLOW")
    request.append("fields", "SETTLE_DT")

    # Add scalar overrides to request.

    overrides = request.getElement("overrides")
    override1 = overrides.appendElement()
    override1.setElement("fieldId", "ALLOW_DYNAMIC_CASHFLOW_CALCS")
    override1.setElement("value", "Y")
    override2 = overrides.appendElement()
    override2.setElement("fieldId", "LOSS_SEVERITY")
    override2.setElement("value", 31)

    # Add table overrides to request.

    tableOverrides = request.getElement("tableOverrides")
    tableOverride = tableOverrides.appendElement()
    tableOverride.setElement("fieldId", "DEFAULT_VECTOR")
    rows = tableOverride.getElement("row")

    # Layout of input table is specified by the definition of
    # 'DEFAULT_VECTOR'. Attributes are specified in the first rows.
    # Subsequent rows include rate, duration, and transition.

    row = rows.appendElement()
    cols = row.getElement("value")
    cols.appendValue("Anchor")  # Anchor type
    cols.appendValue("PROJ")  # PROJ = Projected
    row = rows.appendElement()
    cols = row.getElement("value")
    cols.appendValue("Type")  # Type of default
    cols.appendValue("CDR")  # CDR = Conditional Default Rate

    rateVectors = [
        [1.0, 12, "S"],  # S = Step
        [2.0, 12, "R"]  # R = Ramp
    ]

    for rateVector in rateVectors:
        row = rows.appendElement()
        cols = row.getElement("value")
        for value in rateVector:
            cols.appendValue(value)

    print("Sending Request:", request)
    cid = session.sendRequest(request)

    try:
        # Process received events
        while (True):
            # We provide timeout to give the chance to Ctrl+C handling:
            ev = session.nextEvent(500)
            for msg in ev:
                if cid in msg.correlationIds():
                    # Process the response generically.
                    processMessage(msg)
            # Response completly received, so we could exit
            if ev.eventType() == blpapi.Event.RESPONSE:
                break
    finally:
        # Stop the session
        session.stop()
Esempio n. 22
0
def blp_wrapper(secList, startDt, endDt, periodicitySelection='DAILY'):

    # Bloomberg API wrapper to retrieve Historical price data

    startDt = parser.parse(startDt).strftime('%Y%m%d')
    endDt = parser.parse(endDt).strftime('%Y%m%d')
    periodicitySelection = periodicitySelection.upper()

    index = pd.date_range(startDt, endDt, freq='D')
    df_output = pd.DataFrame(index=index, columns=np.arange(len(secList)))
    df_output = df_output.fillna(np.NAN)

    # Connect to blp services
    sessionOptions = blp.SessionOptions()
    sessionOptions.setServerHost('localhost')
    sessionOptions.setServerPort(8194)

    # Open Bloomberg Session
    print "connecting to blp session..."
    session = blp.Session(sessionOptions)
    if not session.start():
        print "...fail to connect!"
    else:
        print "...success!"

    # Open Bloomberg Session Service
    if not session.openService('//blp/refdata'):
        print 'Failed to open //blp/refdata'
    refDataService = session.getService('//blp/refdata')

    # Create Bloomberg request
    try:
        request = refDataService.createRequest('HistoricalDataRequest')
        for sec in secList:
            request.getElement('securities').appendValue(sec)
        request.getElement('fields').appendValue('PX_LAST')
        request.set('periodicityAdjustment', 'CALENDAR')
        request.set('periodicitySelection', periodicitySelection)
        request.set('startDate', startDt)
        request.set('endDate', endDt)
    except:
        print 'Error: Please check request parameters, request cannot be sent!'
        return

    print 'sending request...'

    session.sendRequest(request)

    print 'receiving request...'

    # Count the number of valid securities retrieved
    secCount = 0
    while (True):
        ev = session.nextEvent()
        for msg in ev:
            # Skip event msg that does not contain securiity data
            if not msg.hasElement('securityData'):
                continue

            secDataArray = msg.getElement(SECURITY_DATA)
            fieldData = secDataArray.getElement(FIELD_DATA)

            print secDataArray.getElementAsString(
                SECURITY), " has ", fieldData.numValues()

            # Rename column header to 'security name'
            df_output.rename(
                columns={secCount: secDataArray.getElementAsString(SECURITY)},
                inplace=True)

            for i in range(0, fieldData.numValues()):
                field = fieldData.getValue(i)
                df_output.loc[field.getElement(0).getValue(),
                              secDataArray.getElementAsString(
                                  SECURITY)] = field.getElement(1).getValue()

            secCount += 1

        # Exit Condition
        if ev.eventType() == blp.Event.RESPONSE:
            print '...all data have been received!'
            break

    # Return system message and output
    print "# of securities retrieved: ", secCount
    session.stop()
    df_output.dropna(how='all', inplace=True)
    return df_output
Esempio n. 23
0
def main():
    global options
    options = parseCmdLine()

    # Fill SessionOptions
    sessionOptions = blpapi.SessionOptions()
    sessionOptions.setServerHost(options.host)
    sessionOptions.setServerPort(options.port)

    if options.auth:
        sessionOptions.setAuthenticationOptions(options.auth)

    print("Connecting to %s:%d" % (options.host, options.port))

    # Create a Session
    session = blpapi.Session(sessionOptions)

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

    print("Session started.")

    # Perform authentication
    if options.auth:
        print("auth options used: ", options.auth)
        identity = auth(session)

        print("Authentication passed: ", )
        if (identity.getSeatType() == Identity.BPS):
            print("BPS User")
        elif (identity.getSeatType() == Identity.NONBPS):
            print("Non-BPS User")
        else:
            print("Invalid User")
            return 9
    else:
        identity = None
        print("No authentication specified")

    # Open service to get reference data from
    if not session.openService("//blp/refdata"):
        print("Failed to open //blp/refdata")
        return

    refDataService = session.getService("//blp/refdata")
    request = refDataService.createRequest("HistoricalDataRequest")

    # Add securities to request.
    for s in options.securities:
        request.append("securities", s)

    # Add fields to request.
    for f in options.fields:
        request.append("fields", f)

    # Create and fill the request for the historical data
    request.set("periodicityAdjustment", "ACTUAL")
    request.set("periodicitySelection", "MONTHLY")
    request.set("startDate", "20170501")
    request.set("endDate", "20170531")
    request.set("maxDataPoints", 100)

    if identity == None:
        print("Sending Request without identity:", request)
        cid = session.sendRequest(request)
    else:
        print("Sending Request with identity:", request)
        cid = session.sendRequest(request, identity)

    try:
        # Process received events
        while (True):
            # We provide timeout to give the chance to Ctrl+C handling:
            ev = session.nextEvent(500)
            for msg in ev:
                if cid in msg.correlationIds():
                    # Process the response generically.
                    print(msg)
            # Response completely received, so we could exit
            if ev.eventType() == blpapi.Event.RESPONSE:
                break
    finally:
        # Stop the session
        session.stop()
def main():
    """main entry point"""
    global options
    options = parseCmdLine()

    # Fill SessionOptions
    sessionOptions = blpapi.SessionOptions()
    for idx, host in enumerate(options.hosts):
        sessionOptions.setServerAddress(host, options.port, idx)
    sessionOptions.setSessionIdentityOptions(options.auth['option'])
    sessionOptions.setAutoRestartOnDisconnection(True)

    # NOTE: If running without a backup server, make many attempts to
    # connect/reconnect to give that host a chance to come back up (the
    # larger the number, the longer it will take for SessionStartupFailure
    # to come on startup, or SessionTerminated due to inability to fail
    # over).  We don't have to do that in a redundant configuration - it's
    # expected at least one server is up and reachable at any given time,
    # so only try to connect to each server once.
    sessionOptions.setNumStartAttempts(1 if len(options.hosts) > 1 else 1000)

    print("Connecting to port %d on %s" %
          (options.port, ", ".join(options.hosts)))

    session = blpapi.Session(sessionOptions)

    if not session.start():
        print("Failed to start session.")
        return

    fieldStr = "?fields=" + ",".join(options.fields)

    snapshots = []
    nextCorrelationId = 0
    for i, topic in enumerate(options.topics):
        subscriptionString = options.service + topic + fieldStr
        snapshots.append(
            session.createSnapshotRequestTemplate(subscriptionString,
                                                  blpapi.CorrelationId(i)))
        nextCorrelationId += 1

    requestTemplateAvailable = blpapi.Name('RequestTemplateAvailable')
    eventCount = 0
    try:
        while True:
            # Specify timeout to give a chance for Ctrl-C
            event = session.nextEvent(1000)
            for msg in event:
                if event.eventType() == blpapi.Event.ADMIN and  \
                        msg.messageType() == requestTemplateAvailable:

                    for requestTemplate in snapshots:
                        try:
                            session.sendRequestTemplate(
                                requestTemplate,
                                blpapi.CorrelationId(nextCorrelationId))
                        except blpapi.Exception as err:
                            print("Failed to send due to: ", err)
                            snapshots.remove(requestTemplate)
                        nextCorrelationId += 1

                elif event.eventType() == blpapi.Event.RESPONSE or \
                        event.eventType() == blpapi.Event.PARTIAL_RESPONSE:

                    cid = msg.correlationIds()[0].value()
                    print("%s - %s" % (cid, msg))
                else:
                    print(msg)
            if event.eventType() == blpapi.Event.RESPONSE:
                eventCount += 1
                if eventCount >= options.maxEvents:
                    print("%d events processed, terminating." % eventCount)
                    break
            elif event.eventType() == blpapi.Event.TIMEOUT:
                for requestTemplate in snapshots:
                    try:
                        session.sendRequestTemplate(
                            requestTemplate,
                            blpapi.CorrelationId(nextCorrelationId))
                    except blpapi.Exception as err:
                        print("Failed to send due to: ", err)
                        snapshots.remove(requestTemplate)
                    nextCorrelationId += 1

    finally:
        session.stop()
Esempio n. 25
0
    def CustomDataFetch(TargetDB, name, assetsToProcess, fieldsIn):

        dataOutCustomData = []

        EXCEPTIONS = blpapi.Name("exceptions")
        FIELD_ID = blpapi.Name("fieldId")
        REASON = blpapi.Name("reason")
        CATEGORY = blpapi.Name("category")
        DESCRIPTION = blpapi.Name("description")
        ERROR_INFO = blpapi.Name("ErrorInfo")

        class Window(object):
            def __init__(self, name):
                self.name = name

            def displaySecurityInfo(self, msg):
                print("%s: %s" % (self.name, msg))
                # print("%s:" % (self.name))

                d = msg.getElement('securityData')
                size = d.numValues()
                fieldDataList = [[d.getValueAsElement(i).getElement("security").getValueAsString(),
                                  d.getValueAsElement(i).getElement("fieldData")] for i in range(0, size)]
                for x in fieldDataList:
                    subData = []
                    # print(x, "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^")
                    for fld in fieldsIn:
                        try:
                            subData.append([fld, x[1].getElement(fld).getValueAsString()])
                        except Exception as e:
                            # print(e)
                            subData.append([fld, np.nan])
                        subDataDF = pd.DataFrame(subData, columns=["field", x[0]]).set_index('field', drop=True)
                    dataOutCustomData.append(subDataDF)

                dfOUT = pd.concat(dataOutCustomData, axis=1).T
                dfOUT.to_sql(name, sqlite3.connect(TargetDB), if_exists='replace')

        def parseCmdLine():
            parser = OptionParser(description="Retrieve reference data.")
            parser.add_option("-a",
                              "--ip",
                              dest="host",
                              help="server name or IP (default: %default)",
                              metavar="ipAddress",
                              default="localhost")
            parser.add_option("-p",
                              dest="port",
                              type="int",
                              help="server port (default: %default)",
                              metavar="tcpPort",
                              default=8194)

            (options, args) = parser.parse_args()

            return options

        def startSession(session):
            if not session.start():
                print("Failed to connect!")
                return False

            if not session.openService("//blp/refdata"):
                print("Failed to open //blp/refdata")
                session.stop()
                return False

            return True

        global options
        options = parseCmdLine()

        # Fill SessionOptions
        sessionOptions = blpapi.SessionOptions()
        sessionOptions.setServerHost(options.host)
        sessionOptions.setServerPort(options.port)

        print("Connecting to %s:%d" % (options.host, options.port))

        # Create a Session
        session = blpapi.Session(sessionOptions)

        # Start a Session
        if not startSession(session):
            return

        refDataService = session.getService("//blp/refdata")
        request = refDataService.createRequest("ReferenceDataRequest")
        for asset in assetsToProcess:
            request.append("securities", asset)
        for fld in fieldsIn:
            request.append("fields", fld)

        secInfoWindow = Window("SecurityInfo")
        cid = blpapi.CorrelationId(secInfoWindow)

        # print("Sending Request:", request)
        session.sendRequest(request, correlationId=cid)

        try:
            # Process received events
            while (True):
                # We provide timeout to give the chance to Ctrl+C handling:
                event = session.nextEvent(500)
                for msg in event:
                    if event.eventType() == blpapi.Event.RESPONSE or \
                            event.eventType() == blpapi.Event.PARTIAL_RESPONSE:
                        window = msg.correlationIds()[0].value()
                        window.displaySecurityInfo(msg)

                # Response completly received, so we could exit
                if event.eventType() == blpapi.Event.RESPONSE:
                    break
        finally:
            # Stop the session
            session.stop()
Esempio n. 26
0
import blpapi

if __name__ == "__main__":
    print "SimpleHistory"

    opts = blpapi.SessionOptions()
    opts.setServerHost("http-api.openbloomberg.com")
    opts.setServerPort(443)

    session = blpapi.Session(opts)
    assert session.start()

    refdata = session.getService("//blp/refdata")

    req = refdata.createRequest("IntradayBarRequest")
    request.set("eventType", "TRADE")
    request.set("security", "IBM")

    request.set("startDateTime", "20150302")
    request.set("endDateTime", "20150702")
    request.set("interval", 60)

    print "sending request", request
    session.sendRequest(request)

    try:
        while True:
            ev = session.nextEvent(500)
            for msg in ev:
                print msg
Esempio n. 27
0
import appconfig
from compute_engine import ComputeEngine
from notifier import Notifier
from event_processor import EventProcessor
from token_generator import TokenGenerator
from authorizer import Authorizer
from subscriber import Subscriber
from application import Application

if __name__ == "__main__":
    options = appconfig.parseCmdLine()
    compute_engine = ComputeEngine()
    notifier = Notifier()
    event_processor = EventProcessor(notifier, compute_engine)

    so = blpapi.SessionOptions()
    for i, host in enumerate(options.hosts):
        so.setServerAddress(host, options.port, i)

    so.setAuthenticationOptions(options.auth['option'])

    session = blpapi.Session(so, event_processor.processEvent)

    token_generator = TokenGenerator(session)
    authorizer = Authorizer(session, token_generator)

    subscriber = Subscriber(session)

    application = Application(session, authorizer, subscriber, options)

    application.run()
Esempio n. 28
0
def main():
    global options
    options = parseCmdLine()

    # Fill SessionOptions
    sessionOptions = blpapi.SessionOptions()
    for idx, host in enumerate(options.hosts):
        sessionOptions.setServerAddress(host, options.port, idx)
    sessionOptions.setAuthenticationOptions(options.auth)
    sessionOptions.setAutoRestartOnDisconnection(True)

    # NOTE: If running without a backup server, make many attempts to
    # connect/reconnect to give that host a chance to come back up (the
    # larger the number, the longer it will take for SessionStartupFailure
    # to come on startup, or SessionTerminated due to inability to fail
    # over).  We don't have to do that in a redundant configuration - it's
    # expected at least one server is up and reachable at any given time,
    # so only try to connect to each server once.
    sessionOptions.setNumStartAttempts(1 if len(options.hosts) > 1 else 1000)

    print "Connecting to port %d on %s" % (options.port, ", ".join(
        options.hosts))

    session = blpapi.Session(sessionOptions)

    if not session.start():
        print "Failed to start session."
        return

    subscriptionIdentity = session.createIdentity()

    if options.auth:
        isAuthorized = False
        authServiceName = "//blp/apiauth"
        if session.openService(authServiceName):
            authService = session.getService(authServiceName)
            isAuthorized = authorize(authService, subscriptionIdentity,
                                     session, blpapi.CorrelationId("auth"))
        if not isAuthorized:
            print "No authorization"
            return

    subscriptions = blpapi.SubscriptionList()
    for t in options.topics:
        topic = options.service + t
        subscriptions.add(topic, options.fields, options.options,
                          blpapi.CorrelationId(topic))

    session.subscribe(subscriptions, subscriptionIdentity)

    try:
        eventCount = 0
        while True:
            # Specify timeout to give a chance for Ctrl-C
            event = session.nextEvent(1000)
            for msg in event:
                if event.eventType() == blpapi.Event.SUBSCRIPTION_STATUS or \
                        event.eventType() == blpapi.Event.SUBSCRIPTION_DATA:
                    print "%s - %s" % (msg.correlationIds()[0].value(), msg)
                else:
                    print msg
            if event.eventType() == blpapi.Event.SUBSCRIPTION_DATA:
                eventCount += 1
                if eventCount >= options.maxEvents:
                    break
    finally:
        session.stop()
def main():
    global options
    options = parseCmdLine()

    # Fill SessionOptions
    sessionOptions = blpapi.SessionOptions()
    sessionOptions.setServerHost(options.host)
    sessionOptions.setServerPort(options.port)

    print "Connecting to %s:%d" % (options.host, options.port)

    myEventHandler = MyEventHandler()
    # Create a Session
    session = blpapi.Session(sessionOptions, myEventHandler.processEvent)

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

    if not session.openService("//blp/mktdata"):
        print "Failed to open //blp/mktdata"
        return

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

    print "Subscribing to IBM US Equity"
    cid = blpapi.CorrelationId(1)
    subscriptions = blpapi.SubscriptionList()
    subscriptions.add("IBM US Equity", "LAST_PRICE", "", cid)
    session.subscribe(subscriptions)

    print "Requesting reference data IBM US Equity"
    refDataService = session.getService("//blp/refdata")
    request = refDataService.createRequest("ReferenceDataRequest")
    request.append("securities", "IBM US Equity")
    request.append("fields", "DS002")

    cid2 = blpapi.CorrelationId(2)
    eventQueue = blpapi.EventQueue()
    session.sendRequest(request, correlationId=cid2, eventQueue=eventQueue)

    try:
        # Process received events
        while (True):
            # We provide timeout to give the chance to Ctrl+C handling:
            ev = eventQueue.nextEvent(500)
            for msg in ev:
                print msg
            # Response completly received, so we could exit
            if ev.eventType() == blpapi.Event.RESPONSE:
                break

        # Wait for enter key to exit application
        print "Press ENTER to quit"
        raw_input()
    finally:
        # Stop the session
        session.stop()
def main():
    options = parseCmdLine()

    # Fill SessionOptions
    sessionOptions = blpapi.SessionOptions()
    for idx, host in enumerate(options.hosts):
        sessionOptions.setServerAddress(host, options.port, idx)
    sessionOptions.setAuthenticationOptions(options.auth)
    sessionOptions.setAutoRestartOnDisconnection(True)
    sessionOptions.setNumStartAttempts(len(options.hosts))

    print("Connecting to port %d on %s" %
          (options.port, " ".join(options.hosts)))

    myEventHandler = MyEventHandler(options.service)

    # Create a Session
    session = blpapi.ProviderSession(sessionOptions,
                                     myEventHandler.processEvent)

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

    providerIdentity = session.createIdentity()

    if options.auth:
        isAuthorized = False
        authServiceName = "//blp/apiauth"
        if session.openService(authServiceName):
            authService = session.getService(authServiceName)
            isAuthorized = authorize(authService, providerIdentity, session,
                                     blpapi.CorrelationId("auth"))
        if not isAuthorized:
            print("No authorization")
            return

    serviceOptions = blpapi.ServiceRegistrationOptions()
    if options.groupId is not None:
        serviceOptions.setGroupId(options.groupId)
    serviceOptions.setServicePriority(options.priority)

    if not session.registerService(options.service, providerIdentity,
                                   serviceOptions):
        print("Failed to register '%s'" % options.service)
        return

    service = session.getService(options.service)

    try:
        # Now we will start publishing
        value = 1
        while g_running:
            event = service.createPublishEvent()

            with g_condition:
                while g_availableTopicCount == 0:
                    # Set timeout to 1 - give a chance for CTRL-C
                    g_condition.wait(1)
                    if not g_running:
                        return

                eventFormatter = blpapi.EventFormatter(event)
                for topicName, stream in g_streams.items():
                    if not stream.isAvailable():
                        continue

                    value += 1

                    if not stream.isInitialPaintSent:
                        eventFormatter.appendRecapMessage(stream.topic)
                        eventFormatter.setElement("numRows", 25)
                        eventFormatter.setElement("numCols", 80)
                        eventFormatter.pushElement("rowUpdate")
                        for i in range(1, 6):
                            eventFormatter.appendElement()
                            eventFormatter.setElement("rowNum", i)
                            eventFormatter.pushElement("spanUpdate")
                            eventFormatter.appendElement()
                            eventFormatter.setElement("startCol", 1)
                            eventFormatter.setElement("length", 10)
                            eventFormatter.setElement("text", "INITIAL")
                            eventFormatter.setElement("fgColor", "RED")
                            eventFormatter.popElement()
                            eventFormatter.popElement()
                            eventFormatter.popElement()
                        eventFormatter.popElement()
                        stream.isInitialPaintSent = True

                    eventFormatter.appendMessage("RowUpdate", stream.topic)
                    eventFormatter.setElement("rowNum", 1)
                    eventFormatter.pushElement("spanUpdate")
                    eventFormatter.appendElement()
                    START_COL = blpapi.Name("startCol")
                    eventFormatter.setElement(START_COL, 1)
                    strValue = str(value)
                    eventFormatter.setElement("length", len(strValue))
                    eventFormatter.setElement("text", strValue)
                    eventFormatter.popElement()
                    eventFormatter.popElement()

            for msg in event:
                print(msg)

            session.publish(event)
            time.sleep(10)

    finally:
        # Stop the session
        session.stop()