matthewgilbert / pdblp

pandas wrapper for Bloomberg Open API
MIT License
242 stars 67 forks source link

bsrch() function #11

Closed MarekOzana closed 7 years ago

MarekOzana commented 7 years ago

I am big fan of your package, it significantly simplifies my work. Thanks! The only thing I am missing is BSRCH() function which allows one to pull the results of saved bond search (SRCH <GO>). I did fast and dirty modification of your pdblp.py on my clone. Basically I just added new service to start():

       if not self.session.openService("//blp/exrsvc"):
            logging.error("Failed to open //blp/exrsvc")
            raise ConnectionError("Could not open a //blp/exrsvc service")
        # Obtain previously opened service
        self.exrService = self.session.getService("//blp/exrsvc")

and then implemented bsrch(domain):

    def bsrch(self, domain):
        """This function uses the Bloomberg API to retrieve 'bsrch'
        (Bloomberg SRCH Data) queries. Returns list of tickers.

        Parameters
        ----------
        domain: string
        A character string with the name of the domain to execute.
        It can be a user defined SRCH screen, commodity screen or
        one of the variety of Bloomberg examples. All domains are in the format 
        <domain>:<search_name>. Example "COMDTY:NGFLOW"
        Returns
        -------
        data: list(string) 
        List of bloomberg tickers from the BSRCH
        """
        request = self.exrService.createRequest("ExcelGetGridRequest")
        request.set("Domain", domain)
        self.session.sendRequest(request)
        data = []
        # Process received events
        while True:
            # We provide timeout to give the chance for Ctrl+C handling:
            event = self.session.nextEvent(0)
            if event.eventType() == blpapi.Event.RESPONSE or \
               event.eventType() == blpapi.Event.PARTIAL_RESPONSE:
                for msg in event:
                    logging.debug(msg)
                    for v in msg.getElement("DataRecords").values():
                        for f in v.getElement("DataFields").values():
                            data.append(f.getElementAsString("StringValue"))
            if event.eventType() == blpapi.Event.RESPONSE:
                break
        return pd.DataFrame(data)

Would you be interested to add implementation of bsrch to your code? Thanks for your time! Marek

matthewgilbert commented 7 years ago

Glad you find the package helpful. I'm happy to include this functionality, thanks for doing. Could you please set this up as a pull request, and also add a unit test to pdblp/tests/test_pdblp.py as well as running flake8 on your changes? Also a quick scan of the function looks like it returns a DataFrame of strings but the docstring indicates a list(string)?

For my own benefit, do you know where in the Bloomberg docs they detail the exrsvc service? I have been unable to find reference to it scanning https://data.bloomberglp.com/professional/sites/10/2017/03/BLPAPI-Core-Developer-Guide.pdf

MarekOzana commented 7 years ago

I just created pull request. I do not think exrsvc service is documented yet, I just requested a working python example from Bloomberg help. It is also used in Rblpapi::bsrch project. Thanks for your time Matthew.

matthewgilbert commented 7 years ago

Thanks for the feature. Closed by 76b1f8b0fa5a83a336a102ec90af72664530c0c6