{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "jupyter": { "source_hidden": true } }, "outputs": [], "source": [ "# better formatting for large floats\n", "import pandas as pd\n", "pd.options.display.float_format = \"{:,.2f}\".format\n", "from ipywidgets import widgets" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "jupyter": { "source_hidden": true } }, "outputs": [], "source": [ "w = widgets.Dropdown(\n", " options=['BOWDST', 'SERCGMAST'],\n", " value='SERCGMAST',\n", " description='fund',\n", " disabled=False,\n", ")\n", "w" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "jupyter": { "source_hidden": true } }, "outputs": [], "source": [ "fund = w.value" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from risk.swaptions import get_swaption_portfolio\n", "import datetime\n", "from serenitas.utils.db import dbconn\n", "from serenitas.analytics import init_ontr\n", "conn = dbconn('dawndb')\n", "conn.autocommit = True\n", "value_date = datetime.date.today()\n", "init_ontr(value_date)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "portf = get_swaption_portfolio(value_date, conn, source_list=[\"MS\"], fund=fund)\n", "portf" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "df = portf._todf()\n", "positions = df.set_index(\"Index\")[[\"Delta\", \"Notional\"]].prod(axis=1).groupby(level=\"Index\").sum()\n", "positions.name = 'current_delta'\n", "gamma = df.set_index(\"Index\")[[\"Gamma\", \"Notional\"]].prod(axis=1).groupby(level=\"Index\").sum()\n", "gamma.name = 'gamma'" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "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": [ "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", "hedges = hedges.reindex(positions.index, fill_value=0.)\n", "risk = pd.concat([hedges, positions, gamma], axis=1)\n", "risk['net_delta'] = risk[\"current hedge\"] + risk.current_delta\n", "risk" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "portf.theta" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "sr = np.linspace(65, 80, 100)\n", "rec= []\n", "for s in sr:\n", " delta = 0.\n", " gamma = 0.\n", " for t in portf.trades:\n", " if t.index.index_type == \"IG\":\n", " t.index.mark(ref=s)\n", " delta += t.delta * t.notional\n", " gamma += t.gamma\n", " rec.append((s, gamma, delta))\n", "df = pd.DataFrame.from_records(rec, columns=['spread', 'gamma', 'delta'])\n", "df = df.set_index('spread')\n", "df.delta += risk.loc['IG34 5yr', 'current hedge']" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from matplotlib import pyplot as plt\n", "def get_newaxes(num):\n", " plt.close(num)\n", " fix, axes = plt.subplots(figsize=(10,5), num=num)\n", " return axes\n", "\n", "axes = get_newaxes(1)\n", "df.delta.plot(ax=axes)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "axes = get_newaxes(2)\n", "df.gamma.plot(ax=axes)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "portf.pv" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.1" } }, "nbformat": 4, "nbformat_minor": 4 }