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 | #eps | CURRENCY | REVISION_DATE | AS_OF_DATE | PERIOD_END_DATE |
|---|---|---|---|---|---|
| str | f64 | str | date | date | date |
| "SPX Index" | 62.776665 | "USD" | 2009-12-31 | 2009-12-31 | 2009-12-31 |
| "SPX Index" | 79.121505 | "USD" | 2009-12-31 | 2009-12-31 | 2010-12-31 |
| "SPX Index" | 95.300733 | "USD" | 2009-12-31 | 2009-12-31 | 2011-12-31 |
Out[29]: