import yaml
import pandas as pd
from tulip.plots import plot_histograms
from tulip_mania.helper_functions.fetch_with_cache import fetch_equity_data
covid_start_date = "2020-03-01"
covid_end_date = "2022-03-01"
ERP = 0.02
DIV_PO = 0.5
FIELDS_OF_INTEREST = [
"BEST_SALES",
"TRAIL_12M_SALES_PER_SH",
"BEST_EBITDA",
"TRAIL_12M_EBITDA_PER_SHARE",
"BEST_EPS",
"TRAIL_12M_EPS",
]
with open("equity_indices.yaml") as f:
data = yaml.safe_load(f)
EQUITY_INDICES = {
k: [v["index"], v["nominal_bond"], v["breakeven"]]
for k, v in data["equity_indices"].items()
}
equity_indices = [v[0] for v in EQUITY_INDICES.values()]
equity_df = fetch_equity_data(
tuple(equity_indices), pd.Timestamp("1980-01-01"), pd.Timestamp.today()
)
eps_analysis = {
k: dict() for k in equity_df.index.get_level_values("security").unique()
}
for s in eps_analysis.keys():
# Get data for the selected market
eps_analysis[s]["ts"] = ts = (
equity_df.xs(s)[FIELDS_OF_INTEREST].ffill().resample("QE").last()
)
# Fix deprecation warning by specifying fill_method=None
eps_analysis[s]["ann_chg"] = ann_chg = eps_analysis[s]["ts"].pct_change(4, fill_method=None)
eps_analysis[s]["expectations"] = expectations = pd.DataFrame(
index=eps_analysis[s]["ts"].index,
data={
"EXP_EPS": eps_analysis[s]["ts"]["BEST_EPS"]
/ eps_analysis[s]["ts"]["TRAIL_12M_EPS"]
- 1,
"EXP_EBITDA": eps_analysis[s]["ts"]["BEST_EBITDA"]
/ eps_analysis[s]["ts"]["TRAIL_12M_EBITDA_PER_SHARE"]
- 1,
"EXP_SALES": eps_analysis[s]["ts"]["BEST_SALES"]
/ eps_analysis[s]["ts"]["TRAIL_12M_SALES_PER_SH"]
- 1,
},
)
eps_analysis[s]["sales_yoy"] = sales_yoy = (
ann_chg["TRAIL_12M_SALES_PER_SH"].dropna().rename("Sales YoY") * 100
)
eps_analysis[s]["sales_yoy_expectation"] = sales_yoy_expectation = (
expectations["EXP_SALES"].dropna().iloc[-1] * 100
)
eps_analysis[s]["ebitda_yoy"] = ebitda_yoy = (
ann_chg["TRAIL_12M_EBITDA_PER_SHARE"].dropna().rename("EBITDA YoY") * 100
)
eps_analysis[s]["ebitda_yoy_expectation"] = ebitda_yoy_expectation = (
expectations["EXP_EBITDA"].dropna().iloc[-1] * 100
)
eps_analysis[s]["eps_yoy"] = eps_yoy = (
ann_chg["TRAIL_12M_EPS"].dropna().rename("EPS Actual YoY") * 100
)
eps_analysis[s]["eps_yoy_expectation"] = eps_yoy_expectation = (
expectations["EXP_EPS"].dropna().iloc[-1] * 100
)
eps_analysis[s]["eps_yoy_expectations"] = eps_yoy_expectations = (
expectations["EXP_EPS"].dropna().rename("EPS Expected YoY") * 100
)
eps_analysis[s]["histogram_chart"] = plot_histograms(
[sales_yoy, ebitda_yoy, eps_yoy, eps_yoy_expectations],
thresholds=[
sales_yoy_expectation,
ebitda_yoy_expectation,
eps_yoy_expectation,
eps_yoy_expectation,
],
titles=["Sales YoY", "EBITDA YoY", "EPS Actual YoY", "EPS Expected YoY"],
title=f"<b>{s}</b>",
xlabel="YoY Change (%)",
layout="grid",
)