Repository Public Research preview
1"""Build the daily currency option panel from the consolidated Bloomberg quote archive.23Input : RESEARCH_DATA_R02/data/processed/bloom/{option_price,spot_price,forward_price}.zip4Output: data/derived/fx_panel_daily.parquet (one row per currency x date x tenor)56Orientation. Every measure is expressed from the point of view of the non-USD currency i7against the US dollar. Bloomberg risk reversals are quoted as IV(call on base) - IV(put on8base) of the quoted pair. We define the *crash-risk* skew of currency i,910 CR_i = IV(25-delta put on i) - IV(25-delta call on i) (vol points),1112i.e. the price of protection against a depreciation of currency i relative to protection13against its appreciation. CR_i = -RR for pairs quoted iUSD and CR_i = +RR for USDi pairs.14(The delta-convention difference between a put on i and a call on USD is ignored, as in the15literature that uses quoted risk reversals.) Butterflies are symmetric and need no sign.16"""17import re18import zipfile1920import numpy as np21import pandas as pd2223from config import BUNDLE, DER, TENORS2425PAT = re.compile(r"^([A-Z]{3})([A-Z]{3})(V|\d+[RB])(\d+[DWMY]|ON) Curncy$")262728def load_options() -> pd.DataFrame:29 z = zipfile.ZipFile(BUNDLE / "data/processed/bloom/option_price.zip")30 d = pd.read_csv(z.open("option_price.csv"), engine="pyarrow")31 tk = pd.Series(d["ticker"].unique(), name="ticker")32 m = tk.str.extract(PAT)33 m.columns = ["base", "ctr", "q", "tenor"]34 m["ticker"] = tk35 assert m["base"].notna().all(), "unparsed option tickers"36 d = d.merge(m, on="ticker")37 d = d[((d.base == "USD") | (d.ctr == "USD")) & d.tenor.isin(TENORS)38 & d.q.isin(["V", "25R", "25B", "10R", "10B"])].copy()39 d["date"] = pd.to_datetime(d["date"])40 # quote: last, falling back on the bid-ask mid when last is missing41 mid = (d["PX_BID"] + d["PX_ASK"]) / 242 d["px"] = d["PX_LAST"].where(d["PX_LAST"].notna(), mid)43 d["ba"] = d["PX_ASK"] - d["PX_BID"]44 d["ccy"] = np.where(d.base == "USD", d.ctr, d.base)45 d["usd_base"] = d.base == "USD"46 return d474849def build() -> pd.DataFrame:50 d = load_options()51 w = d.pivot_table(index=["ccy", "usd_base", "tenor", "date"], columns="q", values="px",52 aggfunc="first").reset_index()53 ba = d[d.q == "V"].groupby(["ccy", "tenor", "date"])["ba"].first().rename("atm_ba")54 w = w.merge(ba, on=["ccy", "tenor", "date"], how="left")55 w = w[(w["V"] > 0) & w["25R"].notna() & w["25B"].notna()].copy()56 sgn = np.where(w.usd_base, 1.0, -1.0)57 out = pd.DataFrame({58 "ccy": w.ccy, "date": w.date, "tenor": w.tenor,59 "atm": w["V"], "cr25": sgn * w["25R"], "cr10": sgn * w["10R"],60 "bf25": w["25B"], "bf10": w["10B"], "atm_ba": w["atm_ba"],61 })62 # smile nodes (vol points) in currency-i orientation: put = protection against i depreciating63 out["iv_p25"] = out.atm + out.bf25 + out.cr25 / 264 out["iv_c25"] = out.atm + out.bf25 - out.cr25 / 265 out["iv_p10"] = out.atm + out.bf10 + out.cr10 / 266 out["iv_c10"] = out.atm + out.bf10 - out.cr10 / 267 # scale-free asymmetry and tail-thickness measures68 out["skew25"] = out.cr25 / out.atm69 out["skew10"] = out.cr10 / out.atm70 out["tail10"] = out.bf10 / out.atm71 # downside / upside wing levels relative to ATM (one-sided tail prices)72 out["left10"] = out.iv_p10 - out.atm73 out["right10"] = out.iv_c10 - out.atm74 # basic quote screens: implausible smiles are set to missing rather than winsorised away75 bad = (out.atm > 150) | (out.iv_p25 <= 0) | (out.iv_c25 <= 0) | (out.cr25.abs() > out.atm)76 out = out[~bad]77 bad10 = (out.iv_p10 <= 0) | (out.iv_c10 <= 0) | (out.cr10.abs() > 2 * out.atm)78 out.loc[bad10, ["cr10", "bf10", "iv_p10", "iv_c10", "skew10", "tail10", "left10", "right10"]] = np.nan79 return out.sort_values(["ccy", "tenor", "date"]).reset_index(drop=True)808182def load_spot() -> pd.DataFrame:83 z = zipfile.ZipFile(BUNDLE / "data/processed/bloom/spot_price.zip")84 s = pd.read_csv(z.open(z.namelist()[0]))85 m = s["ticker"].str.extract(r"^([A-Z]{3})([A-Z]{3}) BGN Curncy$")86 s["base"], s["ctr"] = m[0], m[1]87 s = s[s.PX_LAST > 0].copy()88 s["date"] = pd.to_datetime(s["date"])89 s["ccy"] = np.where(s.base == "USD", s.ctr, s.base)90 # USD price of one unit of currency i (up = i appreciates)91 s["s_usd"] = np.where(s.base == "USD", 1 / s.PX_LAST, s.PX_LAST)92 return s[["ccy", "date", "s_usd"]]939495if __name__ == "__main__":96 p = build()97 p.to_parquet(DER / "fx_panel_daily.parquet", index=False)98 load_spot().to_parquet(DER / "fx_spot_bundle.parquet", index=False)99 g = p[p.tenor == "1M"].groupby("ccy").agg(n=("cr25", "size"), first=("date", "min"),100 last=("date", "max"), cr25=("cr25", "mean"),101 atm=("atm", "mean"), bf10=("bf10", "mean"))102 print(g.round(2).to_string())103 print(p.shape)104