Polars-Bloomberg
  • Home
  • Usage & Tutorials
    • BDP
    • BDH
    • BDIB
    • BSRCH
    • BQL
  • Examples
    • Equity Examples
    • Credits
    • Macro Examples
  • API Reference
  • Search
  • Previous
  • Next
  • Edit on GitHub
  • Macro Examples
    • Visualizing S&P 500 Valuation Scenarios

Macro Examples¶

© 2025 Jim Domeij & Marek Ozana

These examples show how to use polars-bloomberg for macro analysis, inspired by ideas from my friend Jim Domeij.
Thank you, Jim, for contributing your perspective and making it possible for others to learn from your macro thinking.

In [1]:
Copied!
import altair as alt
import polars as pl

from polars_bloomberg import BQuery
import altair as alt import polars as pl from polars_bloomberg import BQuery

Visualizing S&P 500 Valuation Scenarios¶

This chart shows potential S&P 500 price levels and returns for the next 12 months. It uses a scenario matrix driven by two factors: Forward P/E ratios (vertical axis, from historical averages) and Forward EPS growth (horizontal axis, centered on consensus). Each cell displays the resulting index price and percentage return from the current price. This helps investors visualize risk and potential upside under different market conditions.

In [ ]:
Copied!
query = """
    let(
        #last_price = px_last();
        #12m_trail_eps = headline_eps_market(fpt=LTM);
        #12m_fwd_eps = headline_eps_market(fpt=BT, fpo=1);
        #epsg_consensus = #12m_fwd_eps/#12m_trail_eps - 1;
        #current_pe = headline_pe_ratio(fpt=BT, fpo=1);
        #5y_avg_pe = avg(headline_pe_ratio(fpt=BT, fpo=1, fill=PREV, dates=range(-5Y, 0D, frq=W)));
        #10y_avg_pe = avg(headline_pe_ratio(fpt=BT, fpo=1, fill=PREV, dates=range(-10Y, 0D, frq=W)));
        #20y_avg_pe = avg(headline_pe_ratio(fpt=BT, fpo=1, fill=PREV, dates=range(-20Y, 0D, frq=W)));
        #35y_avg_pe = avg(headline_pe_ratio(fpt=BT, fpo=1, fill=PREV, dates=range(-35Y, 0D, frq=W)));
        #max_pe = max(headline_pe_ratio(fpt=BT, fpo=1, fill=PREV, dates=range(-35Y, 0D, frq=W)));
    )
    get(
        #last_price,
        #12m_trail_eps,
        #12m_fwd_eps,
        #epsg_consensus,
        #current_pe,
        #5y_avg_pe,
        #10y_avg_pe,
        #20y_avg_pe,
        #35y_avg_pe,
        #max_pe
    )
    preferences(dropCols=['CURRENCY', 'DATE', 'AS_OF_DATE', 'REVISION_DATE', 'PERIOD_END_DATE'])
    for('SPX Index')
"""  # noqa: E501
with BQuery() as bq:
    res = bq.bql(query)

df = res.combine()
df
query = """ let( #last_price = px_last(); #12m_trail_eps = headline_eps_market(fpt=LTM); #12m_fwd_eps = headline_eps_market(fpt=BT, fpo=1); #epsg_consensus = #12m_fwd_eps/#12m_trail_eps - 1; #current_pe = headline_pe_ratio(fpt=BT, fpo=1); #5y_avg_pe = avg(headline_pe_ratio(fpt=BT, fpo=1, fill=PREV, dates=range(-5Y, 0D, frq=W))); #10y_avg_pe = avg(headline_pe_ratio(fpt=BT, fpo=1, fill=PREV, dates=range(-10Y, 0D, frq=W))); #20y_avg_pe = avg(headline_pe_ratio(fpt=BT, fpo=1, fill=PREV, dates=range(-20Y, 0D, frq=W))); #35y_avg_pe = avg(headline_pe_ratio(fpt=BT, fpo=1, fill=PREV, dates=range(-35Y, 0D, frq=W))); #max_pe = max(headline_pe_ratio(fpt=BT, fpo=1, fill=PREV, dates=range(-35Y, 0D, frq=W))); ) get( #last_price, #12m_trail_eps, #12m_fwd_eps, #epsg_consensus, #current_pe, #5y_avg_pe, #10y_avg_pe, #20y_avg_pe, #35y_avg_pe, #max_pe ) preferences(dropCols=['CURRENCY', 'DATE', 'AS_OF_DATE', 'REVISION_DATE', 'PERIOD_END_DATE']) for('SPX Index') """ # noqa: E501 with BQuery() as bq: res = bq.bql(query) df = res.combine() df
Out[ ]:
shape: (1, 11)
ID#last_price#12m_trail_eps#12m_fwd_eps#epsg_consensus#current_pe#5y_avg_pe#10y_avg_pe#20y_avg_pe#35y_avg_pe#max_pe
strf64f64f64f64f64f64f64f64f64f64
"SPX Index"6850.44269.074714306.1763290.13788622.3613320.04806818.82408116.31579516.52546924.071401

Documentation built with MkDocs.

Search

From here you can search these documents. Enter your search terms below.

Keyboard Shortcuts

Keys Action
? Open this help
n Next page
p Previous page
s Search