Polars-Bloomberg
  • Home
  • Usage & Tutorials
    • BDP
    • BDH
    • BDIB
    • BSRCH
    • BQL
  • Examples
    • Equity Examples
    • Credits
    • Macro Examples
  • API Reference
  • Search
  • Previous
  • Next
  • Edit on GitHub
  • Equity Examples
    • S&P Annual Price Returns
    • S&P earning Per Share Estimates

Equity Examples¶

(c) Marek Ozana

Example usage of polars-bloomberg in Equity space

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

S&P Annual Price Returns¶

In [2]:
Copied!
# get S&P annual price returns
bin_names: list = [
    "< -50",
    "-50 to -40",
    "-40 to -30",
    "-30 to -20",
    "-20 to -10",
    "-10 to 0",
    "0 to 10",
    "10 to 20",
    "20 to 30",
    "30 to 40",
    "40 to 50",
    "> 50",
]
bins = list(range(-50, 60, 10))

query = f"""
let(
    #rets = dropna(pct_diff(px_last(dates=range(-100Y, 0Y), per=Y)));
    #bins = bins(#rets, {bins}, bin_names={bin_names});
)
get(#rets, #bins)
for(['SPX Index'])
"""
with BQuery() as bq:
    df = bq.bql(query).combine()
# get S&P annual price returns bin_names: list = [ "< -50", "-50 to -40", "-40 to -30", "-30 to -20", "-20 to -10", "-10 to 0", "0 to 10", "10 to 20", "20 to 30", "30 to 40", "40 to 50", "> 50", ] bins = list(range(-50, 60, 10)) query = f""" let( #rets = dropna(pct_diff(px_last(dates=range(-100Y, 0Y), per=Y))); #bins = bins(#rets, {bins}, bin_names={bin_names}); ) get(#rets, #bins) for(['SPX Index']) """ with BQuery() as bq: df = bq.bql(query).combine()
Out[3]:

S&P earning Per Share Estimates¶

In [27]:
Copied!
# Download data for multiple fiscal periods
with BQuery(timeout=50000) as con:
    res = []
    for n in range(1, 4):
        tbl = con.bql(f"""
            let(#eps=headline_eps_market(
                    fa_period_offset={n},
                    fa_period_type=A,
                    as_of_date=range('2009-12-31', TODAY(), frq=W)
                    );
            )
            get(#eps)
            for(['SPX Index'])
        """).combine()
        res.append(tbl)
data = pl.concat(res).drop_nulls().sort(by="AS_OF_DATE")
data.head(3)
# Download data for multiple fiscal periods with BQuery(timeout=50000) as con: res = [] for n in range(1, 4): tbl = con.bql(f""" let(#eps=headline_eps_market( fa_period_offset={n}, fa_period_type=A, as_of_date=range('2009-12-31', TODAY(), frq=W) ); ) get(#eps) for(['SPX Index']) """).combine() res.append(tbl) data = pl.concat(res).drop_nulls().sort(by="AS_OF_DATE") data.head(3)
Out[27]:
shape: (3, 6)
ID#epsCURRENCYREVISION_DATEAS_OF_DATEPERIOD_END_DATE
strf64strdatedatedate
"SPX Index"62.776665"USD"2009-12-312009-12-312009-12-31
"SPX Index"79.121505"USD"2009-12-312009-12-312010-12-31
"SPX Index"95.300733"USD"2009-12-312009-12-312011-12-31
Out[29]:

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