diff options
Diffstat (limited to 'python/analytics')
| -rw-r--r-- | python/analytics/cms_spread.py | 33 | ||||
| -rw-r--r-- | python/analytics/index.py | 4 | ||||
| -rw-r--r-- | python/analytics/tranche_basket.py | 42 |
3 files changed, 43 insertions, 36 deletions
diff --git a/python/analytics/cms_spread.py b/python/analytics/cms_spread.py index e29f9381..e7d68cdc 100644 --- a/python/analytics/cms_spread.py +++ b/python/analytics/cms_spread.py @@ -31,18 +31,10 @@ from scipy.integrate import quad from scipy.interpolate import RectBivariateSpline from scipy.special import roots_hermitenorm from yieldcurve import YC -from db import dbconn +from .db import dawn_engine, serenitas_pool __all__ = ["CmsSpread"] -_serenitasdb = dbconn('serenitasdb') -_dawndb = dbconn('dawndb') - -@atexit.register -def close_dbs(): - _serenitasdb.close() - _dawndb.close() - @vectorize([float64(float64, float64, float64, float64, float64, float64, float64, float64, float64)], cache=True, nopython=True) def h_call(z, K, S1, S2, mu_x, mu_y, sigma_x, sigma_y, rho): @@ -139,9 +131,11 @@ def get_swaption_vol_data(source="ICPL", vol_type=VolatilityType.ShiftedLognorma else: sql_str += "AND date=%s" params = (source, date) - with _serenitasdb.cursor() as c: + conn = serenitas_pool.getconn() + with conn.cursor() as c: c.execute(sql_str, params) surf_data = next(c) + serenitas_pool.putconn(conn) return surf_data[0], np.array(surf_data[1:-1], order='F', dtype='float64').T @@ -317,21 +311,20 @@ class CmsSpread: @staticmethod def from_tradeid(trade_id): - with _dawndb.cursor() as c: - c.execute("SELECT " - "amount, expiration_date, floating_rate_index, strike, trade_date " - "FROM capfloors WHERE id = %s", (trade_id,)) - r = c.fetchone() - m = re.match(r"USD(\d{1,2})-(\d{1,2})CMS", r['floating_rate_index']) + rec = dawn_engine.execute("SELECT " + "amount, expiration_date, floating_rate_index, strike, trade_date " + "FROM capfloors WHERE id = %s", (trade_id,)) + r = rec.fetchone() + m = re.match(r"USD(\d{1,2})-(\d{1,2})CMS", r.floating_rate_index) if m: tenor2, tenor1 = map(int, m.groups()) if trade_id == 3: - instance = CmsSpread(r['expiration_date'], tenor1, tenor2, r['strike'] * 0.01, - value_date=r['trade_date'], notional=r['amount'], + instance = CmsSpread(r.expiration_date, tenor1, tenor2, r.strike * 0.01, + value_date=r.trade_date, notional=r.amount, conditional1=0.025) else: - instance = CmsSpread(r['expiration_date'], tenor1, tenor2, r['strike'] * 0.01, - value_date=r['trade_date'], notional=r['amount']) + instance = CmsSpread(r.expiration_date, tenor1, tenor2, r.strike * 0.01, + value_date=r.trade_date, notional=r.amount) return instance @property diff --git a/python/analytics/index.py b/python/analytics/index.py index ac3c435b..73d7c9bb 100644 --- a/python/analytics/index.py +++ b/python/analytics/index.py @@ -105,7 +105,9 @@ class CreditIndex(CreditDefaultSwap): @classmethod def from_tradeid(cls, trade_id): r = dawn_engine.execute(""" - SELECT * FROM cds + SELECT index, series, tenor, trade_date, notional, security_desc, + protection, upfront + FROM cds LEFT JOIN index_desc ON security_id = redindexcode AND cds.maturity = index_desc.maturity WHERE id=%s""", (trade_id,)) diff --git a/python/analytics/tranche_basket.py b/python/analytics/tranche_basket.py index 8e484a32..65451e57 100644 --- a/python/analytics/tranche_basket.py +++ b/python/analytics/tranche_basket.py @@ -81,22 +81,25 @@ class DualCorrTranche(): @classmethod def from_tradeid(cls, trade_id): - r = dawn_engine.execute("SELECT * FROM cds LEFT JOIN index_desc " - "ON security_id = redindexcode AND " - "cds.maturity = index_desc.maturity " - "WHERE id=%s", (trade_id,)) + r = dawn_engine.execute( + "SELECT cds.*, index_desc.index, index_desc.series, " + "index_desc.tenor FROM cds " + "LEFT JOIN index_desc " + "ON security_id = redindexcode AND " + "cds.maturity = index_desc.maturity " + "WHERE id=%s", (trade_id,)) rec = r.fetchone() - instance = cls(rec['index'], rec['series'], rec['tenor'], - attach=rec['orig_attach'], - detach=rec['orig_detach'], - corr_attach=rec['corr_attach'], - corr_detach=rec['corr_detach'], - notional=rec['notional'], - tranche_running=rec['fixed_rate']*100, - value_date=rec['trade_date']) - instance.direction = rec['protection'] - if rec['index_ref'] is not None: - instance._index.tweak([rec['index_ref']]) + instance = cls(rec.index, rec.series, rec.tenor, + attach=rec.orig_attach, + detach=rec.orig_detach, + corr_attach=rec.corr_attach, + corr_detach=rec.corr_detach, + notional=rec.notional, + tranche_running=rec.fixed_rate*100, + value_date=rec.trade_date) + instance.direction = rec.protection + if rec.index_ref is not None: + instance._index.tweak([rec.index_ref]) instance.reset_pv() return instance @@ -387,6 +390,15 @@ class DualCorrTranche(): return (calc['bp'][1] - calc['bp'][2]) / \ (calc['indexbp'][1] - calc['indexbp'][2]) * factor + def tranche_thetas(self, complement=False, shortened=4, method='ATM'): + bp = self.tranche_pvs(complement=complement).bond_price + rho_saved = self.rho + self.rho = self.map_skew(self, method, shortened) + bpshort = self.tranche_pvs(complement=complement, shortened=shortened).bond_price + self.rho = rho_saved + thetas = bpshort - bp + self.tranche_running + return pd.Series(thetas, index=self._row_names, name='theta') + @property def gamma(self): calc = self._greek_calc() |
