diff options
Diffstat (limited to 'python/notebooks')
| -rw-r--r-- | python/notebooks/swaption_risk.ipynb | 93 |
1 files changed, 34 insertions, 59 deletions
diff --git a/python/notebooks/swaption_risk.ipynb b/python/notebooks/swaption_risk.ipynb index c480995e..a7810818 100644 --- a/python/notebooks/swaption_risk.ipynb +++ b/python/notebooks/swaption_risk.ipynb @@ -30,56 +30,35 @@ }, "outputs": [], "source": [ - "w = widgets.Dropdown(\n", - " options=['BOWDST', 'SERCGMAST', 'ISOSEL'],\n", - " value='SERCGMAST',\n", - " description='fund',\n", - " disabled=False,\n", - ")\n", - "w" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "jupyter": { - "source_hidden": true - }, - "tags": [] - }, - "outputs": [], - "source": [ - "fund = w.value" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "tags": [] - }, - "outputs": [], - "source": [ "from risk.swaptions import get_swaption_portfolio\n", "import datetime\n", "from serenitas.analytics.base import Trade\n", "from serenitas.utils.pool import dawn_pool\n", + "from serenitas.analytics.option import BlackSwaption\n", "value_date = datetime.date.today()\n", - "Trade.init_ontr(value_date)" + "Trade.init_ontr(value_date)\n", + "BlackSwaption.clear_cache()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { + "jupyter": { + "source_hidden": true + }, "tags": [] }, "outputs": [], "source": [ "with dawn_pool.connection() as conn:\n", - " portf = get_swaption_portfolio(value_date, conn, fund=fund)\n", - "portf" + " portf = get_swaption_portfolio(value_date, conn)\n", + "portf.index_formatter = lambda t: (t[2], t[1])\n", + "df = portf._todf().dropna(axis=1, how=\"all\")\n", + "df.index.names = [\"fund\", \"trade_id\"]\n", + "df = df.reset_index()\n", + "df = df.set_index([\"Expiry\", \"Desc\", \"Strike\", \"fund\"]).sort_index()\n", + "df" ] }, { @@ -88,42 +67,38 @@ "metadata": {}, "outputs": [], "source": [ - "df = portf._todf()\n", - "positions = df.set_index(\"Desc\")[[\"Delta\", \"Notional\"]].prod(axis=1).groupby(level=\"Desc\").sum()\n", - "positions.name = 'current_delta'\n", - "gamma = df.set_index(\"Desc\")[[\"Gamma\", \"Notional\"]].prod(axis=1).groupby(level=\"Desc\").sum()\n", - "gamma.name = 'gamma'" + "greeks = df.assign(Delta=lambda df: df.Delta * df.Notional, Gamma=lambda df: df.Gamma * df.Notional).groupby([\"Desc\", \"fund\"], group_keys=False)[\"Delta\", \"Gamma\"].sum()" ] }, { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "jupyter": { + "source_hidden": true + }, + "tags": [] + }, "outputs": [], "source": [ + "hedges = {}\n", "with dawn_pool.connection() as conn:\n", - " hedges = pd.read_sql_query(\"SELECT security_desc, notional FROM list_cds_positions_by_strat(%s, %s) \"\n", - " \"WHERE folder in ('IGOPTDEL', 'HYOPTDEL')\",\n", - " conn, params=(value_date, fund))" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ + " for fund in [\"SERCGMAST\", \"BOWDST\", \"ISOSEL\"]:\n", + " hedges[fund] = pd.read_sql_query(\"SELECT security_desc, notional FROM list_cds_positions_by_strat(%s, %s) \"\n", + " \"WHERE folder in ('IGOPTDEL', 'HYOPTDEL')\",\n", + " conn, params=(value_date, fund))\n", + "hedges = pd.concat(hedges, names=[\"fund\"])\n", + "hedges = hedges.groupby([\"security_desc\", \"fund\"]).sum()\n", "def f(s):\n", " l = s.split(\" \")\n", " return f\"{l[1]}{l[3][1:]} {l[4].lower()}r\"\n", "\n", - "hedges[\"Index\"] = hedges[\"security_desc\"].apply(f)\n", - "hedges = hedges.rename(columns={\"notional\": \"current hedge\"})\n", - "hedges = hedges.set_index(\"Index\")[\"current hedge\"]\n", - "risk = pd.concat([hedges, positions, gamma], axis=1)\n", - "risk = risk.fillna(0.0)\n", - "risk['net_delta'] = risk[\"current hedge\"] + risk.current_delta\n", - "risk" + "hedges = hedges.reset_index(\"security_desc\")\n", + "hedges[\"security_desc\"] = hedges[\"security_desc\"].apply(f)\n", + "hedges = hedges.rename(columns={\"notional\": \"current hedge\", \"security_desc\": \"Desc\"}).set_index(\"Desc\", append=True).swaplevel()\n", + "risk = pd.concat([hedges, greeks], axis=1)\n", + "risk = risk.assign(net_delta=risk[\"current hedge\"] + risk.Delta)\n", + "risk.swaplevel().sort_index()" ] }, { @@ -217,7 +192,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.6" + "version": "3.10.8" } }, "nbformat": 4, |
