Python library providing a Polars DataFrame interface for easy and intuitive access to Bloomberg API.
Project description
Polars + Bloomberg Open API
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
blpapilibrary 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'])
| security | PX_LAST |
|---|---|
| str | f64 |
| "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"])
| security | SECURITY_DES | YAS_ZSPREAD | CRNCY | NXT_CALL_DT |
|---|---|---|---|---|
| str | str | f64 | str | date |
| "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")])
| security | PX_LAST | CRNCY_ADJ_PX_LAST |
|---|---|---|
| str | f64 | f64 |
| "IBM US Equity" | 238.04 | 2607.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")])
| security | SETTLE_DT |
|---|---|
| str | date |
| "USX60003AC87 Corp" | 2024-10-15 |
with BQuery() as bq:
df = bq.bdp(['USDSEK Curncy', 'SEKCZK Curncy'],
['SETTLE_DT', 'PX_LAST'],
overrides=[('REFERENCE_DATE', '20200715')]
)
| security | SETTLE_DT | PX_LAST |
|---|---|---|
| str | date | f64 |
| "USDSEK Curncy" | 2020-07-17 | 10.9343 |
| "SEKCZK Curncy" | 2020-07-17 | 2.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))
| security | date | PX_LAST | VOLUME |
|---|---|---|---|
| str | date | f64 | f64 |
| "AAPL US Equity" | 2019-01-02 | 39.48 | 1.48158948e8 |
| "AAPL US Equity" | 2019-01-03 | 35.548 | 3.6524878e8 |
| … | … | … | … |
| "TSLA US Equity" | 2019-01-09 | 22.5687 | 8.1494175e7 |
| "TSLA US Equity" | 2019-01-10 | 22.998 | 9.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"})
| security | date | PX_LAST |
|---|---|---|
| str | date | f64 |
| "AAPL US Equity" | 2019-01-31 | 41.61 |
| "AAPL US Equity" | 2019-02-28 | 43.288 |
| "AAPL US Equity" | 2019-03-29 | 47.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'])")
| ID | px_last | px_last.DATE | px_last.CURRENCY |
|---|---|---|---|
| str | f64 | date | str |
| "IBM US Equity" | 238.04 | 2024-12-07 | "USD" |
| "OMX Index" | 2614.268 | 2024-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'])
""")
| ID | #eps | #eps.REVISION_DATE | #eps.AS_OF_DATE | #eps.PERIOD_END_DATE | #eps.CURRENCY |
|---|---|---|---|---|---|
| str | f64 | date | date | date | str |
| "IBM US Equity" | 10.63 | 2022-02-22 | 2024-12-07 | 2019-12-31 | "USD" |
| "IBM US Equity" | 6.28 | 2023-02-28 | 2024-12-07 | 2020-12-31 | "USD" |
| … | … | … | … | … | … |
| "IBM US Equity" | 9.236 | 2024-12-07 | 2024-12-07 | 2025-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)
| ID | #average_spread | #average_spread.DATE | #average_spread.ORIG_IDS | #average_spread.#BINS |
|---|---|---|---|---|
| str | f64 | date | str | str |
| "(1) 0-3" | 30.74 | 2024-12-08 | "QZ552396 Corp" | "(1) 0-3" |
| "(2) 3-9" | 59.79 | 2024-12-08 | null | "(2) 3-9" |
| "(3) 9-18" | 105.39 | 2024-12-08 | "BH393780 Corp" | "(3) 9-18" |
| "(4) 18-30" | 131.72 | 2024-12-08 | "BH393781 Corp" | "(4) 18-30" |
| "(5) 30+" | 150.33 | 2024-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)
"""
)
| ID | name() | #ema20 | #ema20.DATE | #ema20.CURRENCY | #ema200 | #ema200.DATE | #ema200.CURRENCY | #rsi | #rsi.DATE |
|---|---|---|---|---|---|---|---|---|---|
| str | str | f64 | date | str | f64 | date | str | f64 | date |
| "SKFB SS Equity" | "SKF AB" | 210.185019 | 2024-12-08 | "SEK" | 204.16756 | 2024-12-08 | "SEK" | 72.255568 | 2024-12-08 |
| "ABB SS Equity" | "ABB Ltd" | 623.496942 | 2024-12-08 | "SEK" | 561.902577 | 2024-12-08 | "SEK" | 72.144556 | 2024-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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file polars_bloomberg-0.2.3.tar.gz.
File metadata
- Download URL: polars_bloomberg-0.2.3.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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cdb27cb1c52264397de837c738de366281e08befb12bc3bee7776855947f4d39
|
|
| MD5 |
0e817052ecb05528226a531acef05183
|
|
| BLAKE2b-256 |
96e1d1b9339433af3b1098e6c035326a728bfe74be04ee456be9133fee338db9
|
File details
Details for the file polars_bloomberg-0.2.3-py3-none-any.whl.
File metadata
- Download URL: polars_bloomberg-0.2.3-py3-none-any.whl
- Upload date:
- Size: 13.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0e472d375e146e3f5988dd75d37de65dd1273bb74ea9f0e8a3ba5f3f2cc6066f
|
|
| MD5 |
9061900cb283521fb6485b7d5670e229
|
|
| BLAKE2b-256 |
c4e9ce26a9b583c37c897996688adc482c478895e30cd8f0eb01f81a86fdc419
|