Skip to content

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

License

Notifications You must be signed in to change notification settings

MarekOzana/polars-bloomberg

Repository files navigation

Polars Bloomberg Logo

Polars + Bloomberg Open API

Tests License

polars-bloomberg is a Python library that extracts Bloomberg’s financial data directly into Polars DataFrames.
If you’re a quant financial analyst, data scientist, or quant developer working in capital markets, this library makes it easy to fetch, transform, and analyze Bloomberg data right in Polars—offering speed, efficient memory usage, and a lot of fun to use!

Why use polars-bloomberg?

  • User-Friendly Functions: Shortcuts like bdp(), bdh(), and bql() (inspired by Excel-like Bloomberg calls) let you pull data with minimal boilerplate.
  • High-Performance Analytics: Polars is a lightning-fast DataFrame library. Combined with Bloomberg’s rich dataset, you get efficient data retrieval and minimal memory footprint
  • No Pandas Dependency: Enjoy a clean integration that relies solely on Polars for speed and simplicity.

Table of Contents

  1. Introduction
  2. Prerequisites
  3. Installation
  4. Quick Start
  5. Core Methods
  6. Additional Documentation and Resources

Introduction

Working with Bloomberg data in Python often feels more complicated than using their well-known Excel interface. Great projects like blp, xbbg, and pdblp have made this easier by pulling data directly into pandas.

With polars-bloomberg, you can enjoy the speed and simplicity of Polars DataFrames—accessing both familiar Excel-style calls (bdp, bdh) and advanced bql queries—without extra pandas conversions.

For detailed documentation and function references, visit the documentation site https://marekozana.github.io/polars-bloomberg.

I hope you enjoy using it as much as I had fun building it!

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.12+ recommended.

Installation

pip install polars-bloomberg

Quick Start

"Hello World" Example (under 1 minute):

from polars_bloomberg import BQuery

# Fetch the latest price for Apple (AAPL US Equity)
with BQuery() as bq:
    df = bq.bdp(["AAPL US Equity"], ["PX_LAST"])
    print(df)

┌────────────────┬─────────┐
│ securityPX_LAST │
│ ------     │
│ strf64     │
╞════════════════╪═════════╡
│ AAPL US Equity248.13  │
└────────────────┴─────────┘

What this does:

  • Establishes a Bloomberg connection using the context manager.
  • Retrieves the last price of Apple shares.
  • Returns the result as a Polars DataFrame.

If you see a price in df, your setup is working 🤩!!!

Core Methods

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

Use Case: Fetch the latest single-value data points (like last price, currency, or descriptive fields).

Example: Fetching the Last Price & Currency of Apple and SEB

with BQuery() as bq:
    df = bq.bdp(["AAPL US Equity", "SEBA SS Equity"], ["PX_LAST", "CRNCY"])
    print(df)

┌────────────────┬─────────┬───────┐
│ securityPX_LASTCRNCY │
│ ---------   │
│ strf64str   │
╞════════════════╪═════════╪═══════╡
│ AAPL US Equity248.13USD   │
│ SEBA SS Equity155.2SEK   │
└────────────────┴─────────┴───────┘
Expand for 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"])

┌───────────────────┬────────────────┬─────────────┬───────┬─────────────┐
│ securitySECURITY_DESYAS_ZSPREADCRNCYNXT_CALL_DT │
│ ---------------         │
│ strstrf64strdate        │
╞═══════════════════╪════════════════╪═════════════╪═══════╪═════════════╡
│ XS2930103580 CorpSEB 6 3/4 PERP304.676112USD2031-11-04  │
│ USX60003AC87 CorpNDAFH 6.3 PERP292.477506USD2031-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")],
    )

┌───────────────┬─────────┬───────────────────┐
│ securityPX_LASTCRNCY_ADJ_PX_LAST │
│ ---------               │
│ strf64f64               │
╞═══════════════╪═════════╪═══════════════════╡
│ IBM US Equity230.822535.174          │
└───────────────┴─────────┴───────────────────┘

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")])

┌───────────────────┬────────────┐
│ securitySETTLE_DT  │
│ ------        │
│ strdate       │
╞═══════════════════╪════════════╡
│ USX60003AC87 Corp2024-10-15 │
└───────────────────┴────────────┘
with BQuery() as bq:
    df = bq.bdp(['USDSEK Curncy', 'SEKCZK Curncy'], 
                ['SETTLE_DT', 'PX_LAST'], 
                overrides=[('REFERENCE_DATE', '20200715')]
               )

┌───────────────┬────────────┬─────────┐
│ securitySETTLE_DTPX_LAST │
│ ---------     │
│ strdatef64     │
╞═══════════════╪════════════╪═════════╡
│ USDSEK Curncy2020-07-1710.9778 │
│ SEKCZK Curncy2020-07-172.1698  │
└───────────────┴────────────┴─────────┘

BDH

Use Case: Retrieve historical data over a date range, such as daily closing prices or volumes.

with BQuery() as bq:
    df = bq.bdh(
        ["TLT US Equity"],
        ["PX_LAST"],
        start_date=date(2019, 1, 1),
        end_date=date(2019, 1, 7),
    )
    print(df)

┌───────────────┬────────────┬─────────┐
│ securitydatePX_LAST │
│ ---------     │
│ strdatef64     │
╞═══════════════╪════════════╪═════════╡
│ TLT US Equity2019-01-02122.15  │
│ TLT US Equity2019-01-03123.54  │
│ TLT US Equity2019-01-04122.11  │
│ TLT US Equity2019-01-07121.75  │
└───────────────┴────────────┴─────────┘
Expand for more BDH examples

BDH with multiple securities / fields

with BQuery() as bq:
    df = bq.bdh(
        securities=["SPY US Equity", "TLT US Equity"],
        fields=["PX_LAST", "VOLUME"],
        start_date=date(2019, 1, 1),
        end_date=date(2019, 1, 10),
        options={"adjustmentSplit": True},
    )
    print(df)

shape: (14, 4)
┌───────────────┬────────────┬─────────┬──────────────┐
│ securitydatePX_LASTVOLUME       │
│ ------------          │
│ strdatef64f64          │
╞═══════════════╪════════════╪═════════╪══════════════╡
│ SPY US Equity2019-01-02250.181.26925199e8 │
│ SPY US Equity2019-01-03244.211.44140692e8 │
│ SPY US Equity2019-01-04252.391.42628834e8 │
│ SPY US Equity2019-01-07254.381.031391e8   │
│ SPY US Equity2019-01-08256.771.02512587e8 │
│ …             ┆ …          ┆ …       ┆ …            │
│ TLT US Equity2019-01-04122.111.2970226e7  │
│ TLT US Equity2019-01-07121.758.498104e6   │
│ TLT US Equity2019-01-08121.437.737103e6   │
│ TLT US Equity2019-01-09121.249.349245e6   │
│ TLT US Equity2019-01-10120.468.22286e6    │
└───────────────┴────────────┴─────────┴──────────────┘

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"})

┌────────────────┬────────────┬─────────┐
│ securitydatePX_LAST │
│ ---------     │
│ strdatef64     │
╞════════════════╪════════════╪═════════╡
│ AAPL US Equity2019-01-3141.61   │
│ AAPL US Equity2019-02-2843.288  │
│ AAPL US Equity2019-03-2947.488  │
└────────────────┴────────────┴─────────┘

BQL

Use Case: Run more advanced queries to screen securities, calculate analytics (like moving averages), or pull fundamental data with complex conditions.

Returns: The bql() method returns a BqlResult object, which:

  • Acts like a list of Polars DataFrames (one for each item in BQL get statement).
  • Provides a .combine() method to merge DataFrames on common columns.

1. Basic Example: Single Item and Single Security

# Fetch the last price of IBM stock
with BQuery() as bq:
    results = bq.bql("get(px_last) for(['IBM US Equity'])")
    print(results[0])  # Access the first DataFrame

Output:

┌───────────────┬─────────┬────────────┬──────────┐
│ IDpx_lastDATECURRENCY │
│ ------------      │
│ strf64datestr      │
╞═══════════════╪═════════╪════════════╪══════════╡
│ IBM US Equity230.822024-12-14USD      │
└───────────────┴─────────┴────────────┴──────────┘

2. Multiple Securities with a Single Item

# Fetch the last price for IBM and SEB
with BQuery() as bq:
    results = bq.bql("get(px_last) for(['IBM US Equity', 'SEBA SS Equity'])")
    print(results[0])

Output:

┌────────────────┬─────────┬────────────┬──────────┐
│ IDpx_lastDATECURRENCY │
│ ------------      │
│ strf64datestr      │
╞════════════════╪═════════╪════════════╪══════════╡
│ IBM US Equity230.822024-12-14USD      │
│ SEBA SS Equity155.22024-12-14SEK      │
└────────────────┴─────────┴────────────┴──────────┘

3. Multiple Items

When querying for multiple items, bql() returns a list of DataFrames

# Fetch name and last price of IBM (two items)
with BQuery() as bq:
    results = bq.bql("get(name, px_last) for(['IBM US Equity'])")

Output:

>>> print(len(results))  # 2 DataFrames
n=2

>>> print(results[0])    # First DataFrame: 'name'
┌───────────────┬────────────────────────────────┐
│ IDname                           │
│ ------                            │
│ strstr                            │
╞═══════════════╪════════════════════════════════╡
│ IBM US EquityInternational Business Machine │
└───────────────┴────────────────────────────────┘

>>> print(results[1])    # Second DataFrame: 'px_last'
┌───────────────┬─────────┬────────────┬──────────┐
│ IDpx_lastDATECURRENCY │
│ ------------      │
│ strf64datestr      │
╞═══════════════╪═════════╪════════════╪══════════╡
│ IBM US Equity230.822024-12-14USD      │
└───────────────┴─────────┴────────────┴──────────┘

Combining Results

>>> combined_df = results.combine()
>>> print(combined_df)

Output:

┌───────────────┬────────────────────────────────┬─────────┬────────────┬──────────┐
│ IDnamepx_lastDATECURRENCY │
│ ---------------      │
│ strstrf64datestr      │
╞═══════════════╪════════════════════════════════╪═════════╪════════════╪══════════╡
│ IBM US EquityInternational Business Machine230.822024-12-14USD      │
└───────────────┴────────────────────────────────┴─────────┴────────────┴──────────┘

4. Advanced Example: Screening Securities

Find list of SEB and Handelsbanken's AT1 bonds and print their names, duration and Z-Spread.

query="""
    let(#dur=duration(duration_type=MODIFIED); 
        #zsprd=spread(spread_type=Z);) 
    get(name(), #dur, #zsprd) 
    for(filter(screenresults(type=SRCH, screen_name='@COCO'), 
            ticker in ['SEB', 'SHBASS']))
"""

with BQuery() as bq:
    results = bq.bql(query)
    combined_df = results.combine()
    print(combined_df)

Output:

┌───────────────┬─────────────────┬──────┬────────────┬────────┐
│ IDname()          ┆ #dur ┆ DATE       ┆ #zsprd │---------------    │
│ strstrf64datef64    │
╞═══════════════╪═════════════════╪══════╪════════════╪════════╡
│ BW924993 CorpSEB 6PERP2.232024-12-16212.0  │
│ YV402592 CorpSEB Float PERP0.212024-12-16233.0  │
│ ZQ349286 CorpSEB 5PERP0.392024-12-16186.0  │
│ ZO703315 CorpSHBASS 4PERP1.952024-12-16213.0  │
│ ZO703956 CorpSHBASS 4 ¾ PERP4.942024-12-16256.0  │
│ YU819930 CorpSEB 6 ¾ PERP5.372024-12-16309.0  │
└───────────────┴─────────────────┴──────┴────────────┴────────┘

Average PE per Sector

This example shows aggregation (average) per group (sector) for members of an index. The resulting list has only one element since there is only one data-item in get

query = """
    let(#avg_pe=avg(group(pe_ratio(), gics_sector_name()));)
    get(#avg_pe)
    for(members('OMX Index'))
"""
with BQuery() as bq:
    results = bq.bql(query)
    print(results[0].head(5))

Output:

┌──────────────┬───────────┬──────────────┬────────────┬──────────────┬──────────────┬─────────────┐
│ ID#avg_pe   ┆ REVISION_DAT ┆ AS_OF_DATE ┆ PERIOD_END_D ┆ ORIG_IDS     ┆ GICS_SECTOR │------E---ATE---_NAME()     │
│ strf64---date---str---         │
│              ┆           ┆ date         ┆            ┆ date         ┆              ┆ str         │
╞══════════════╪═══════════╪══════════════╪════════════╪══════════════╪══════════════╪═════════════╡
│ Communicatio19.5617542024-10-242024-12-142024-09-30nullCommunicati │
│ n Services   ┆           ┆              ┆            ┆              ┆              ┆ on Services │
│ Consumer Dis19.1172952024-10-242024-12-142024-09-30nullConsumer    │
│ cretionary   ┆           ┆              ┆            ┆              ┆              ┆ Discretiona │
│              ┆           ┆              ┆            ┆              ┆              ┆ ry          │
│ Consumer15.9847432024-10-242024-12-142024-09-30ESSITYB SSConsumer    │
│ Staples      ┆           ┆              ┆            ┆              ┆ EquityStaples     │
│ Financials6.8158952024-10-242024-12-142024-09-30nullFinancials  │
│ Health Care22.006282024-11-122024-12-142024-09-30nullHealth Care │
└──────────────┴───────────┴──────────────┴────────────┴──────────────┴──────────────┴─────────────┘

Axes

Get current axes of all Swedish USD AT1 bonds

# Get current axes for Swedish AT1 bonds in USD
query="""
    let(#ax=axes();)
    get(security_des, #ax)
    for(filter(bondsuniv(ACTIVE),
        crncy()=='USD' and
        basel_iii_designation() == 'Additional Tier 1' and
        country_iso() == 'SE'))
"""

with BQuery() as bq:
    results = bq.bql(query)
    print(results.combine())

┌───────────────┬─────────────────┬─────┬───────────┬───────────┬────────────────┬────────────────┐
│ IDsecurity_des#ax ┆ ASK_DEPTH ┆ BID_DEPTH ┆ ASK_TOTAL_SIZE ┆ BID_TOTAL_SIZE │---------------------            │
│ strstrstri64i64f64f64            │
╞═══════════════╪═════════════════╪═════╪═══════════╪═══════════╪════════════════╪════════════════╡
│ YU819930 CorpSEB 6 ¾ PERPY2null5.6e6null           │
│ ZO703315 CorpSHBASS 4PERPY125e66e6            │
│ BR069680 CorpSWEDA 4 PERPYnull1null3e6            │
│ ZL122341 CorpSWEDA 7PERPYnull6null2.04e7         │
│ ZQ349286 CorpSEB 5PERPY245.5e63e7            │
│ ZF859199 CorpSWEDA 7 ¾ PERPY112e62e6            │
│ ZO703956 CorpSHBASS 4 ¾ PERPY131.2e61.1e7          │
│ BW924993 CorpSEB 6PERPY135e61.1e7          │
└───────────────┴─────────────────┴─────┴───────────┴───────────┴────────────────┴────────────────┘

Axes with all columns

# RT1 Axes with all columns
query = """
let(#ax=axes();)
get(name, #ax, amt_outstanding)
for(filter(bondsuniv(ACTIVE),
    crncy() in ['USD', 'EUR'] and
    solvency_ii_designation() == 'Restricted Tier 1' and
    amt_outstanding() > 7.5e8 and
    is_axed('Bid') == True))
preferences(addcols=all)
"""

with BQuery() as bq:
    results = bq.bql(query)
    print(results.combine())

Output:

shape: (3, 33)
IDname#axASK_PRICEBID_PRICEASK_DEPTHBID_DEPTHASK_DEALERBID_DEALERASK_SIZEBID_SIZEASK_TOTAL_SIZEBID_TOTAL_SIZEASK_PRICE_IS_DERIVEDBID_PRICE_IS_DERIVEDASK_SPREADBID_SPREADASK_SPREAD_IS_DERIVEDBID_SPREAD_IS_DERIVEDASK_YIELDBID_YIELDASK_YIELD_IS_DERIVEDBID_YIELD_IS_DERIVEDASK_AXE_SOURCEBID_AXE_SOURCEASK_BROKERBID_BROKERASK_HIST_AGG_SIZEBID_HIST_AGG_SIZEamt_outstandingCURRENCY_OF_ISSUEMULTIPLIERCURRENCY
strstrstrf64f64i64i64strstrf64f64f64f64boolboolf64f64boolboolf64f64boolboolstrstrstrstrf64f64f64strf64str
"BM368057 Corp""ALVGR 2 ⅝ PERP""Y"88.03487.42751"BARC""IMI"1.2e61e67.2e61e6nullnull287.031300.046truetrue4.8544.976truetrue"ERUN""ERUN""BXOL""IMIC"6.68e68.92e61.2500e9"EUR"1.0"EUR"
"EK588238 Corp""ASSGEN 4.596 PERP""Y"101.0100.1346"MSAX""A2A"500000.0100000.01.556e73.83e7nullnull108.9207.889truetrue3.4664.434nulltrue"ERUN""BBX""MSAX""A2A"1.70424e73.17e71.0004e9"EUR"1.0"EUR"
"BR244025 Corp""ALVGR 3.2 PERP""Y"88.086.87534"UBS""DB"5e61e61.1e71.4e7nullnull49.33414.602truetrue7.342588.553nulltrue"ERUN""ERUN""UBSW""DABC"1.6876e63.6e71.2500e9"USD"1.0"USD"

Segments

The following example shows handling of two data-items with different length. The first dataframe describes the segments (and has length 5 in this case), while the second dataframe contains time series. One can join the dataframes on common columns and pivot the segments into columns as shown below:

# revenue per segment
query = """
    let(#segment=segment_name();
        #revenue=sales_Rev_turn(fpt=q, fpr=range(2023Q3, 2024Q3));
        )
    get(#segment, #revenue)
    for(segments('GTN US Equity',type=reported,hierarchy=PRODUCT, level=1))
"""
with BQuery() as bq:
    results = bq.bql(query)
    df = results.combine().pivot(
        index="PERIOD_END_DATE", on="#segment", values="#revenue"
    )
    print(df)

Output:

┌─────────────────┬──────────────┬──────────────────────┬────────┬────────────┐
│ PERIOD_END_DATEBroadcastingProduction CompaniesOtherAdjustment │
│ ---------------        │
│ datef64f64f64f64        │
╞═════════════════╪══════════════╪══════════════════════╪════════╪════════════╡
│ 2023-09-307.83e82e71.6e7null       │
│ 2023-12-318.13e83.2e71.9e7null       │
│ 2024-03-317.8e82.4e71.9e7null       │
│ 2024-06-308.08e81.8e70.0null       │
│ 2024-09-309.24e82.6e71.7e7null       │
└─────────────────┴──────────────┴──────────────────────┴────────┴────────────┘

Actual and Forward EPS Estimates

with BQuery() as bq:
    results = bq.bql("""
        let(#eps=is_eps(fa_period_type='A',
                        fa_period_offset=range(-4,2));)
        get(#eps)
        for(['IBM US Equity'])
    """)
    print(results[0])

┌───────────────┬───────┬───────────────┬────────────┬─────────────────┬──────────┐
│ ID#eps  ┆ REVISION_DATE ┆ AS_OF_DATE ┆ PERIOD_END_DATE ┆ CURRENCY │------------------      │
│ strf64datedatedatestr      │
╞═══════════════╪═══════╪═══════════════╪════════════╪═════════════════╪══════════╡
│ IBM US Equity10.632022-02-222024-12-142019-12-31USD      │
│ IBM US Equity6.282023-02-282024-12-142020-12-31USD      │
│ IBM US Equity6.412023-02-282024-12-142021-12-31USD      │
│ IBM US Equity1.822024-03-182024-12-142022-12-31USD      │
│ IBM US Equity8.232024-03-182024-12-142023-12-31USD      │
│ IBM US Equity7.8912024-12-132024-12-142024-12-31USD      │
│ IBM US Equity9.2362024-12-132024-12-142025-12-31USD      │
└───────────────┴───────┴───────────────┴────────────┴─────────────────┴──────────┘

Average issuer OAS spread per maturity bucket

# Example: Average 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:
    results = bq.bql(query)
    print(results[0])

Output:

┌───────────┬─────────────────┬────────────┬───────────────┬───────────┐
│ ID#average_spread ┆ DATE       ┆ ORIG_IDS      ┆ #BINS     │---------------       │
│ strf64datestrstr       │
╞═══════════╪═════════════════╪════════════╪═══════════════╪═══════════╡
│ (1) 0-331.1956892024-12-14QZ552396 Corp ┆ (1) 0-3   │
│ (2) 3-959.5803832024-12-14null          ┆ (2) 3-9   │
│ (3) 9-18110.6144162024-12-14BH393780 Corp ┆ (3) 9-18  │
│ (4) 18-30135.1602792024-12-14BH393781 Corp ┆ (4) 18-30 │
│ (5) 30+150.7134052024-12-14BH393782 Corp ┆ (5) 30+   │
└───────────┴─────────────────┴────────────┴───────────────┴───────────┘

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

with BQuery() as bq:
    results = 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 > 53)))
        with(fill=PREV)
        """
    )
    print(results.combine())

Output:

┌─────────────────┬──────────────────┬────────────┬────────────┬──────────┬────────────┬───────────┐
│ IDname()           ┆ #ema20     ┆ DATE       ┆ CURRENCY ┆ #ema200    ┆ #rsi      │---------------------       │
│ strstrf64datestrf64f64       │
╞═════════════════╪══════════════════╪════════════╪════════════╪══════════╪════════════╪═══════════╡
│ ERICB SS EquityTelefonaktiebola90.1526042024-12-16SEK75.07215156.010028 │
│                 ┆ get LM Ericsso   ┆            ┆            ┆          ┆            ┆           │
│ ABB SS EquityABB Ltd630.6224692024-12-16SEK566.57118353.763102 │
│ SEBA SS EquitySkandinaviska153.805952024-12-16SEK150.74239456.460733 │
│                 ┆ Enskilda Banken  ┆            ┆            ┆          ┆            ┆           │
│ ASSAB SS EquityAssa Abloy AB339.0175912024-12-16SEK317.05757353.351619 │
└─────────────────┴──────────────────┴────────────┴────────────┴──────────┴────────────┴───────────┘

Bond Universe from Equity Ticker

# Get Bond Universe from Equity Ticker
query = """
let(#rank=normalized_payment_rank();
    #oas=spread(st=oas);
    #nxt_call=nxt_call_dt();
    )
get(name(), #rank, #nxt_call, #oas)
for(filter(bonds('GTN US Equity'), series() == '144A'))
"""

with BQuery() as bq:
    results = bq.bql(query)
    df = results.combine()
    print(df)

Output:

┌───────────────┬───────────────────┬──────────────────┬────────────┬────────────┬────────────┐
│ ID            ┆ name()            ┆ #rank            ┆ #nxt_call  ┆ #oas       ┆ DATE       │
│ ---           ┆ ---               ┆ ---              ┆ ---        ┆ ---        ┆ ---        │
│ str           ┆ str               ┆ str              ┆ date       ┆ f64        ┆ date       │
╞═══════════════╪═══════════════════╪══════════════════╪════════════╪════════════╪════════════╡
│ YX231113 Corp ┆ GTN 10 ½ 07/15/29 ┆ 1st Lien Secured ┆ 2026-07-15 ┆ 598.66491  ┆ 2024-12-17 │
│ BS116983 Corp ┆ GTN 5 ⅜ 11/15/31  ┆ Sr Unsecured     ┆ 2026-11-15 ┆ 1193.17529 ┆ 2024-12-17 │
│ AV438089 Corp ┆ GTN 7 05/15/27    ┆ Sr Unsecured     ┆ 2024-12-24 ┆ 400.340456 ┆ 2024-12-17 │
│ ZO860846 Corp ┆ GTN 4 ¾ 10/15/30  ┆ Sr Unsecured     ┆ 2025-10-15 ┆ 1249.34346 ┆ 2024-12-17 │
│ LW375188 Corp ┆ GTN 5 ⅞ 07/15/26  ┆ Sr Unsecured     ┆ 2025-01-13 ┆ 173.761744 ┆ 2024-12-17 │
└───────────────┴───────────────────┴──────────────────┴────────────┴────────────┴────────────┘

Bonds Total Returns

This is example of a single-item query returning total return for all GTN bonds in a long dataframe. We can easily pivot it into wide format, as in the example below

# Total Return of GTN Bonds
query = """
let(#rng = range(-1M, 0D);
    #rets = return_series(calc_interval=#rng,per=W);)
get(#rets)
for(filter(bonds('GTN US Equity'), series() == '144A'))
"""

with BQuery() as bq:
    results = bq.bql(query)
    df = results[0].pivot(on="ID", index="DATE", values="#rets")
    print(df)

Output:

shape: (6, 6)
┌────────────┬───────────────┬───────────────┬───────────────┬───────────────┬───────────────┐
│ DATEYX231113 CorpBS116983 CorpAV438089 CorpZO860846 CorpLW375188 Corp │
│ ------------------           │
│ datef64f64f64f64f64           │
╞════════════╪═══════════════╪═══════════════╪═══════════════╪═══════════════╪═══════════════╡
│ 2024-11-17nullnullnullnullnull          │
│ 2024-11-240.0016530.0511790.0203630.001371-0.002939     │
│ 2024-12-010.0028370.010405-0.0014660.0072750.000581      │
│ 2024-12-08-0.0000410.0161450.0007660.0249840.000936      │
│ 2024-12-150.001495-0.047-0.000233-0.0435090.002241      │
│ 2024-12-170.00008-0.000004-0.0035-0.0079370.000064      │
└────────────┴───────────────┴───────────────┴───────────────┴───────────────┴───────────────┘

Maturity Wall for US HY Bonds

query = """
let(#mv=sum(group(amt_outstanding(currency=USD),
                  by=[year(maturity()), industry_sector()]));)
get(#mv)
for(members('LF98TRUU Index'))
"""
with BQuery() as bq:
    results = bq.bql(query)
df = results.combine().rename(
    {"YEAR(MATURITY())": "maturity", "INDUSTRY_SECTOR()": "sector", "#mv": "mv"}
)

print(df.pivot(index="maturity", on="sector", values="mv").head())

Output:

shape: (5, 11)
┌──────────┬───────────┬───────────┬───────────┬───┬───────────┬───────────┬───────────┬───────────┐
│ maturityBasicConsumer, ┆ Energy    ┆ … ┆ FinancialTechnologUtilitiesDiversifi │
│ ---MaterialsNon-cycli---       ┆   ┆ ---y---ed        │
│ i64---calf64       ┆   ┆ f64---f64---       │
│          ┆ f64---       ┆           ┆   ┆           ┆ f64       ┆           ┆ f64       │
│          ┆           ┆ f64       ┆           ┆   ┆           ┆           ┆           ┆           │
╞══════════╪═══════════╪═══════════╪═══════════╪═══╪═══════════╪═══════════╪═══════════╪═══════════╡
│ 20251.5e85.34916e85e8       ┆ … ┆ nullnullnullnull      │
│ 20264.4013e99.3293e98.2931e9  ┆ … ┆ 1.3524e104.0608e92.5202e9null      │
│ 20278.3921e92.3409e101.2427e10 ┆ … ┆ 1.9430e104.3367e93.6620e9null      │
│ 20281.4701e103.7457e102.2442e10 ┆ … ┆ 2.3341e109.9143e97.6388e95e8       │
│ 20291.6512e105.7381e103.9286e10 ┆ … ┆ 4.2337e102.2660e105.8558e9null      │
└──────────┴───────────┴───────────┴───────────┴───┴───────────┴───────────┴───────────┴───────────┘

Additional Documentation & Resources

About

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

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages