diff options
Diffstat (limited to 'python/notebooks/VaR.ipynb')
| -rw-r--r-- | python/notebooks/VaR.ipynb | 53 |
1 files changed, 26 insertions, 27 deletions
diff --git a/python/notebooks/VaR.ipynb b/python/notebooks/VaR.ipynb index ba59d717..abb580d4 100644 --- a/python/notebooks/VaR.ipynb +++ b/python/notebooks/VaR.ipynb @@ -10,8 +10,9 @@ "from analytics.index_data import get_index_quotes\n", "from analytics.scenarios import run_portfolio_scenarios\n", "from analytics import Swaption, BlackSwaption, CreditIndex, BlackSwaptionVolSurface, Portfolio, ProbSurface, DualCorrTranche\n", - "from db import dbconn, dbengine\n", + "from utils.db import dbconn, dbengine\n", "\n", + "import analytics\n", "import datetime\n", "import exploration.VaR as var\n", "import pandas as pd\n", @@ -19,8 +20,10 @@ "import globeop_reports as go\n", "\n", "conn = dbconn('dawndb')\n", + "conn.autocommit = True\n", "dawndb = dbengine('dawndb')\n", - "serenitasdb = dbengine('serenitasdb')" + "serenitasdb = dbengine('serenitasdb')\n", + "analytics.init_ontr()" ] }, { @@ -128,8 +131,8 @@ "metadata": {}, "outputs": [], "source": [ - "position_date = (datetime.date.today() - pd.tseries.offsets.BDay(1)).date()\n", - "shock_date = (datetime.date.today() - pd.tseries.offsets.BDay(1)).date()\n", + "position_date = (datetime.date.today() - pd.tseries.offsets.BDay(3)).date()\n", + "shock_date = (datetime.date.today() - pd.tseries.offsets.BDay(3)).date()\n", "(position_date, shock_date)" ] }, @@ -140,47 +143,43 @@ "outputs": [], "source": [ "#Current tranche and swaptions positions\n", - "t_sql_string = (\"SELECT id, sum(notional * case when protection='Buyer' then -1 else 1 end) \"\n", + "t_sql_string = (\"SELECT id, folder, sum(notional * case when protection='Buyer' then -1 else 1 end) \"\n", " \"OVER (partition by security_id, attach) AS ntl_agg \"\n", " \"FROM cds WHERE swap_type='CD_INDEX_TRANCHE' AND termination_cp IS NULL \"\n", " \"AND trade_date <= %s\")\n", - "swaption_sql_string = (\"select id, security_desc from swaptions where date(expiration_date) \"\n", + "swaption_sql_string = (\"select id, security_desc, folder from swaptions where date(expiration_date) \"\n", " \"> %s and swap_type = 'CD_INDEX_OPTION' \"\n", " \"AND trade_date <= %s AND termination_date iS NULL\")\n", - "index_sql_string = (\"SELECT id, sum(notional * case when protection='Buyer' then -1 else 1 end) \"\n", + "index_sql_string = (\"SELECT id, folder, sum(notional * case when protection='Buyer' then -1 else 1 end) \"\n", " \"OVER (partition by security_id, attach) AS ntl_agg \"\n", " \"FROM cds WHERE swap_type='CD_INDEX' AND termination_cp IS null \"\n", - " \"AND folder = 'IGOPTDEL' OR folder = 'HYOPTDEL' \"\n", " \"AND trade_date <= %s\")\n", "with conn.cursor() as c:\n", - " #Get Tranche Trade Ids\n", " c.execute(t_sql_string, (position_date,))\n", - " t_trade_ids = [dealid for dealid, ntl in c if ntl != 0]\n", - " #Get Swaption Trade Ids\n", + " t_trades = [[dealid, f\"{folder}_{dealid}\"] for dealid, folder, ntl in c if ntl != 0]\n", " c.execute(swaption_sql_string, (position_date, position_date))\n", " swaption_trades = c.fetchall()\n", - " #Get Index/deltas Trade Ids\n", " c.execute(index_sql_string, (position_date,))\n", - " index_trade_ids = [dealid for dealid, ntl in c if ntl != 0]\n", + " index_trades = [[dealid, f\"{folder}_{dealid}\"] for dealid, folder, ntl in c if ntl != 0]\n", " \n", - "portf = Portfolio([DualCorrTranche.from_tradeid(dealid) for dealid in t_trade_ids],\n", - " ['trn_'+ str(a) for a in t_trade_ids])\n", - "for row in swaption_trades:\n", - " option_delta = CreditIndex(row[1].split()[1], row[1].split()[3][1:], '5yr', position_date)\n", - " option_delta.mark()\n", - " portf.add_trade(BlackSwaption.from_tradeid(row[0], option_delta), 'opt_' + str(row[0]))\n", - "for index_id in index_trade_ids:\n", - " portf.add_trade(CreditIndex.from_tradeid(index_id), 'index_' + str(index_id))\n", + "portf = Portfolio([DualCorrTranche.from_tradeid(dealid) for dealid, _ in t_trades],\n", + " [trade_id for _, trade_id in t_trades])\n", + "for trade_id, desc, strat in swaption_trades:\n", + " portf.add_trade(BlackSwaption.from_tradeid(trade_id), str(strat) + \"_\" + str(trade_id))\n", + "for trade_id, name in index_trades:\n", + " portf.add_trade(CreditIndex.from_tradeid(trade_id), name)\n", " \n", "#get bond risks:\n", "rmbs_pos = go.rmbs_pos(position_date)\n", + "clo_pos = go.clo_pos(position_date)\n", "r = serenitasdb.execute(\"select duration from on_the_run where index = 'HY' and date = %s\",\n", " shock_date)\n", "duration, = next(r)\n", "rmbs_pos['hy_equiv'] = rmbs_pos['delta_yield']/duration * 100\n", - "notional\n", - "portf.add_trade(CreditIndex('HY', on_the_run('HY'), '5yr', value_date = shock_date, \n", - " notional = rmbs_pos['hy_equiv'].sum()), 'rmbs_bond')\n", + "notional = rmbs_pos['hy_equiv'].sum() + clo_pos['hy_equiv'].sum()\n", + "temp = CreditIndex('HY', on_the_run('HY'), '5yr', value_date = shock_date, notional = notional)\n", + "temp.direction = 'Seller'\n", + "portf.add_trade(temp, 'rmbs_bond')\n", " \n", "portf.value_date = shock_date\n", "portf.mark(interp_method=\"bivariate_linear\")\n", @@ -190,7 +189,7 @@ "for trade in portf.swaptions:\n", " vs = BlackSwaptionVolSurface(trade.index.index_type, trade.index.series, \n", " value_date=shock_date, interp_method = \"bivariate_linear\")\n", - " vol_surface[trade.index.index_type + trade.index.series] = vs[vs.list(option_type='payer')[-1]]\n", + " vol_surface[(trade.index.index_type, trade.index.series)] = vs[vs.list(option_type='payer')[-1]]\n", "vol_shock = [0]\n", "corr_shock = [0]\n", "spread_shock = tighten + [0] + widen\n", @@ -281,9 +280,9 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.1" + "version": "3.8.0" } }, "nbformat": 4, - "nbformat_minor": 2 + "nbformat_minor": 4 } |
