blp Python Library

repository·master·Indexed 41 days ago

https://github.com/matthewgilbert/blp

A Python library providing a simplified, pythonic interface to the Bloomberg API (blpapi). It serves as the successor to pdblp, focusing on extensible session management and event aggregation. The library includes high-level methods for retrieving historical data (bdh), reference data points (bdp), data sets (bds), intraday bars (bdib), equity screening (beqs), and Bloomberg Query Language (bql) requests, returning results as pandas DataFrames.

Tokens
5.4K
Snippets
20
Records
28
Agent score
50%

What's inside blp

  1. How to specify instrument identifiers like ISIN, CUSIP, or SEDOL

    master

    By default, the Bloomberg API assumes a /ticker topic prefix (e.g., IBM US Equity). To use other identifiers like CUSIP or SEDOL, you must explicitly include the topic prefix in your security string.

    Examples of explicit topic prefixes:

    • CUSIP: /cusip/097023105
    • SEDOL: /sedol1/2108601

    If you do not specify a prefix, the API uses the defaultTopicPrefix from the SessionOptions object, which is /ticker by default. Note that the required format for a topic may vary depending on the specific subscription service being used.

    # Examples of explicit topic prefixes:
    /cusip/097023105
    /sedol1/2108601
  2. Install blp via pip or conda

    master

    You can install the blp package using either pip or conda. blp provides a pythonic interface to the blpapi package and is designed for explicit separation of session management, event parsing, and event aggregation.

    pip install blp
    # or
    conda install -c conda-forge blp
  3. Access Bloomberg API documentation

    master

    For technical details regarding the underlying Bloomberg API, use a Bloomberg Terminal to access the following resources:

    • Developer's Guide: For general Bloomberg API documentation.
    • Services and Schemas Reference Guide: For documentation on specific Bloomberg fields used to access data.

    Access Path: From a Bloomberg Terminal, type WAPI <GO> and select API Developer's Guide.

  4. Understand parsed response schemas for BlpParser

    master

    The BlpParser converts various Bloomberg response types into standardized dictionary formats. Common schemas include:

    1. ReferenceDataResponse: {'security': <str>, 'fields': <list of str>, 'data': <dict of field:value>}
    2. HistoricalDataResponse: {'security': <str>, 'fields': <list of str>, 'data': <list of dict of field:value>}
    3. IntradayBarResponse: {'security': <str>, 'events': [<str>], 'data': <list of {'time': <Timestamp>, 'open': <float>, ...}>}
    4. IntradayTickResponse: {'security': <str>, 'events': <list of str>, 'data': <list of {'time': <Timestamp>, 'type': <str>, 'value': <float>, 'size': <int>}>}
    5. fieldResponse: {'id': <list of str>, 'data': {<str>: {field: value}}}
    6. InstrumentListResponse: {'security': <str>, 'description': <str>}
    7. GetFillsResponse: {'Fills': <list of dicts>}
  5. Apply overrides to Bloomberg queries

    master

    You can modify the behavior of a request by passing an overrides argument. Overrides are provided as a list of tuples in the format [(override_field, override_value), ...]. This is useful for changing currency, adjusting dates, or other field-specific behaviors.

    # Example: Overriding currency for a BDP request
    bquery.bdp(
        ["IBM US Equity"],
        ["CRNCY_ADJ_PX_LAST"],
        overrides=[("EQY_FUND_CRNCY", "EUR")]
    )
    
    # Example: Overriding an end date for a BDS request
    bquery.bds(
        "DAX Index",
        "INDX_MWEIGHT_HIST",
        overrides=[("END_DATE_OVERRIDE", "20230630")]
    )
  6. Initialize the BlpQuery client

    master

    To start making Bloomberg queries, instantiate blp.BlpQuery() and call .start(). You can also use blp.BlpQuery as a context manager to ensure proper cleanup.

    from blp import blp
    
    # Standard instantiation
    bquery = blp.BlpQuery().start()
    
    # Using as a context manager
    with blp.BlpQuery() as bq:
        df = bq.bdh(
            ["GME US Equity"],
            ["PX_LAST", "VOLUME"],
            start_date="20210101",
            end_date="20210130",
        )
  7. Handle security errors in queries

    master

    By default, invalid tickers or securities will raise errors (e.g., securityError). To prevent these errors from stopping your execution, instantiate BlpQuery with a BlpParser where raise_security_errors is set to False.

    # Instantiate with a parser that ignores security errors
    bquery = blp.BlpQuery(parser=blp.BlpParser(raise_security_errors=False)).start()
    
    # This will no longer raise an exception for invalid tickers
    bquery.bdh(
        ["NOT_A_TICKER US Equity", "SPY US Equity"],
        ["PX_LAST"],
        start_date="20190101",
        end_date="20190110",
    )
  8. Use SEDOLs for security identification

    master

    Bloomberg allows identifying securities using SEDOLs. You can use the /sedol1/ prefix in your security list for both bdp() and bdh() calls.

    # Using SEDOLs in BDP
    bquery.bdp(["SPY US Equity", "TLT US Equity"], ["ID_SEDOL1"])
    
    # Using SEDOLs in BDH
    bquery.bdh(
        ["/sedol1/2840215", "/sedol1/2971546"],
        ["PX_LAST", "VOLUME"],
        start_date="20190101",
        end_date="20190110",
    )