Skip to main content

Python library providing a Polars DataFrame interface for easy and intuitive access to Bloomberg API.

Project description

Polars Bloomberg Logo

Polars + Bloomberg Open API

Tests License

Polars + Bloomberg Open API is a Python library that facilitates integration of Bloomberg data into Polars DataFrames. Designed for users familiar with Pandas or Excel, it offers minimal-boilerplate functions such as bdp(), bdh(), and bql(). Leverage Polars' high-performance capabilities alongside the Bloomberg API for lightning-fast DataFrame operations and a minimal memory footprint.

Key Benefits:

  • Intuitive "Excel-like" methods: bdp(), bdh(), bql()
  • Outputs data as Polars DataFrames
  • Lightweight design with no dependency on pandas
  • Quickly prototype, test, and scale complex financial analyses.

Prerequisites

  • Bloomberg Access: A valid Bloomberg terminal license.
  • Bloomberg Python API: The blpapi library must be installed. See the Bloomberg API Library for guidance.
  • Python Version: Python 3.8+ recommended.
  • Installation: pip install polars-bloomberg

Quick Start Guide (5 Minutes)

Below is a simple example to get you started. For more comprehensive examples, please see the examples/ directory.

Concept:
BQuery is your main interface. Using a context manager ensures the connection opens and closes cleanly. Within this session, you can use:

  • bq.bdp() for Bloomberg Data Points (single-value fields).
  • bq.bdh() for Historical Data (time series).
  • bq.bql() for complex Bloomberg Query Language requests.

BDP - Bloomberg Data Point

Example: Fetching the Last Price of Apple and Microsoft

from polars_bloomberg import BQuery

with BQuery() as bq:
    df = bq.bdp(['AAPL US Equity', 'MSFT US Equity'], ['PX_LAST'])
shape: (2, 2)
securityPX_LAST
strf64
"AAPL US Equity"242.84
"MSFT US Equity"443.57
More BDP Examples

BDP with different column types

polars-bloomberg correctly infers column type as shown in this example:

with BQuery() as bq:
    df = bq.bdp(["XS2930103580 Corp", "USX60003AC87 Corp"],
                ["SECURITY_DES", "YAS_ZSPREAD", "CRNCY", "NXT_CALL_DT"])
shape: (2, 5)
securitySECURITY_DESYAS_ZSPREADCRNCYNXT_CALL_DT
strstrf64strdate
"XS2930103580 Corp""SEB 6 3/4 PERP"327.309349"USD"2031-11-04
"USX60003AC87 Corp""NDAFH 6.3 PERP"315.539222"USD"2031-09-25

BDP with overrides

User can submit list of tuples with overrides

with BQuery() as bq:
    df = bq.bdp(["IBM US Equity"], ["PX_LAST", "CRNCY_ADJ_PX_LAST"], 
                overrides=[("EQY_FUND_CRNCY", "SEK")])
</style> shape: (1, 3)
securityPX_LASTCRNCY_ADJ_PX_LAST
strf64f64
"IBM US Equity"238.042607.401

BDP with date overrides

Overrides for dates has to be in format YYYYMMDD

with BQuery() as bq:
    df = bq.bdp(["USX60003AC87 Corp"], ["SETTLE_DT"], overrides=[("USER_LOCAL_TRADE_DATE", "20241014")])
shape: (1, 2)
securitySETTLE_DT
strdate
"USX60003AC87 Corp"2024-10-15
with BQuery() as bq:
    df = bq.bdp(['USDSEK Curncy', 'SEKCZK Curncy'], 
                ['SETTLE_DT', 'PX_LAST'], 
                overrides=[('REFERENCE_DATE', '20200715')]
               )
shape: (2, 3)
securitySETTLE_DTPX_LAST
strdatef64
"USDSEK Curncy"2020-07-1710.9343
"SEKCZK Curncy"2020-07-172.1718

BDH - Bloomberg Data History

with BQuery() as bq:
    df = bq.bdh(['AAPL US Equity', 'TSLA US Equity'], 
                ['PX_LAST', 'VOLUME'], 
                start_date=date(2019, 1, 1), 
                end_date=date(2019, 1, 10))
shape: (14, 4)
securitydatePX_LASTVOLUME
strdatef64f64
"AAPL US Equity"2019-01-0239.481.48158948e8
"AAPL US Equity"2019-01-0335.5483.6524878e8
"TSLA US Equity"2019-01-0922.56878.1494175e7
"TSLA US Equity"2019-01-1022.9989.084531e7
More BDH Examples

BDH with options - periodicitySelection: Monthly

with BQuery() as bq:
    df = bq.bdh(['AAPL US Equity'], 
                ['PX_LAST'], 
                start_date=date(2019, 1, 1), 
                end_date=date(2019, 3, 29),
                options={"periodicitySelection": "MONTHLY"})
shape: (3, 3)
securitydatePX_LAST
strdatef64
"AAPL US Equity"2019-01-3141.61
"AAPL US Equity"2019-02-2843.288
"AAPL US Equity"2019-03-2947.488

BQL - Bloomberg Query Language

Allows to run complex bql queries and get result in wide polars.DataFramewith correct polars types

with BQuery() as bq:
    df = bq.bql("get(px_last) for(['IBM US Equity', 'OMX Index'])")
shape: (2, 4)
IDpx_lastpx_last.DATEpx_last.CURRENCY
strf64datestr
"IBM US Equity"238.042024-12-07"USD"
"OMX Index"2614.2682024-12-07"SEK"
More BQL Examples

Actual and Forward EPS Estimates

df = bq.bql("""
    let(#eps=is_eps(fa_period_type='A',
                    fa_period_offset=range(-4,2));)
    get(#eps)
    for(['IBM US Equity'])
""")
shape: (7, 6)
ID#eps#eps.REVISION_DATE#eps.AS_OF_DATE#eps.PERIOD_END_DATE#eps.CURRENCY
strf64datedatedatestr
"IBM US Equity"10.632022-02-222024-12-072019-12-31"USD"
"IBM US Equity"6.282023-02-282024-12-072020-12-31"USD"
"IBM US Equity"9.2362024-12-072024-12-072025-12-31"USD"

Average issuer OAS spread per maturity bucket

query = """
let( 
    #bins = bins(maturity_years,
                 [3,9,18,30],
                 ['(1) 0-3','(2) 3-9','(3) 9-18','(4) 18-30','(5) 30+']);
    #average_spread = avg(group(spread(st=oas),#bins));
)
get(#average_spread)
for(filter(bonds('NVDA US Equity', issuedby = 'ENTITY'),
           maturity_years != NA))
"""

with BQuery() as bq:
    df = bq.bql(query)
shape: (5, 5)
ID#average_spread#average_spread.DATE#average_spread.ORIG_IDS#average_spread.#BINS
strf64datestrstr
"(1) 0-3"30.742024-12-08"QZ552396 Corp""(1) 0-3"
"(2) 3-9"59.792024-12-08null"(2) 3-9"
"(3) 9-18"105.392024-12-08"BH393780 Corp""(3) 9-18"
"(4) 18-30"131.722024-12-08"BH393781 Corp""(4) 18-30"
"(5) 30+"150.332024-12-08"BH393782 Corp""(5) 30+"

Technical Analysis: stocks with 20d EMA > 200d EMA and RSI > 70

with BQuery() as bq:
    df = bq.bql(
        """
        let(#ema20=emavg(period=20); 
            #ema200=emavg(period=200); 
            #rsi=rsi(close=px_last());)
        get(name(), #ema20, #ema200, #rsi)
        for(filter(members('OMX Index'), 
                    and(#ema20 > #ema200, #rsi > 70)))
        with(fill=PREV)
        """
    )
shape: (2, 10)
IDname()#ema20#ema20.DATE#ema20.CURRENCY#ema200#ema200.DATE#ema200.CURRENCY#rsi#rsi.DATE
strstrf64datestrf64datestrf64date
"SKFB SS Equity""SKF AB"210.1850192024-12-08"SEK"204.167562024-12-08"SEK"72.2555682024-12-08
"ABB SS Equity""ABB Ltd"623.4969422024-12-08"SEK"561.9025772024-12-08"SEK"72.1445562024-12-08

API Documentation

Read the API documentation in examples/ directory

More Examples

Explore additional usage examples in the examples/ directory.

Bloomberg Documentation

For documentation on the Bloomberg API, check out the Bloomberg Developer's page.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

polars_bloomberg-0.2.2.tar.gz (20.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

polars_bloomberg-0.2.2-py3-none-any.whl (13.2 kB view details)

Uploaded Python 3

File details

Details for the file polars_bloomberg-0.2.2.tar.gz.

File metadata

  • Download URL: polars_bloomberg-0.2.2.tar.gz
  • Upload date:
  • Size: 20.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.8

File hashes

Hashes for polars_bloomberg-0.2.2.tar.gz
Algorithm Hash digest
SHA256 a659f9ec521d8658686a94a05c5c4f67df78a18f063265a8231c49ddb86397a3
MD5 1aad80e7bd2c7a2b6addcdac787e59b9
BLAKE2b-256 ae64d2461ca1624e3f57827eb1f999d1bdeb241049ca79d6edbdb463b22b7184

See more details on using hashes here.

File details

Details for the file polars_bloomberg-0.2.2-py3-none-any.whl.

File metadata

File hashes

Hashes for polars_bloomberg-0.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 c59340f0ddc87802301221f8a5b02d2f145d765409f66fb7aa2b2321584dac17
MD5 a2dbc3b9c9df5db40aa5e36d76654d6f
BLAKE2b-256 691174ce9560895cfd9eee1146cd739413aa96af77c6fa028792fcceac70da9b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page