diff options
Diffstat (limited to 'python/analytics/basket_index.py')
| -rw-r--r-- | python/analytics/basket_index.py | 29 |
1 files changed, 17 insertions, 12 deletions
diff --git a/python/analytics/basket_index.py b/python/analytics/basket_index.py index ece47cb7..038ba20a 100644 --- a/python/analytics/basket_index.py +++ b/python/analytics/basket_index.py @@ -10,6 +10,7 @@ from yieldcurve import get_curve import datetime import logging import numpy as np +import psycopg2.extensions import pandas as pd from math import exp from scipy.optimize import brentq @@ -48,19 +49,24 @@ class BasketIndex(CreditIndex): else: self.recovery = 0.4 conn = serenitas_pool.getconn() - with conn.cursor() as c: + with conn.cursor(cursor_factory=psycopg2.extensions.cursor) as c: c.execute( - "SELECT tenor, maturity, (coupon * 1e-4)::float AS coupon, " - "issue_date " + "SELECT tenor, maturity, (coupon * 1e-4)::float AS coupon " "FROM index_maturity " "WHERE index=%s AND series=%s AND tenor IN %s " "ORDER BY maturity", (index_type, series, tuple(tenors)), ) - self.index_desc = list(tuple(r) for r in c) - if not self.index_desc: - raise ValueError(f"Index {index_type} {series} doesn't exist") - with conn.cursor() as c: + self.index_desc = list(c) + c.execute( + "SELECT issue_date FROM index_maturity WHERE index=%s AND series=%s", + (index_type, series), + ) + try: + (self.issue_date,) = c.fetchone() + except TypeError: + raise ValueError(f"Index {index_type} {series} doesn't exist") + with conn.cursor(cursor_factory=psycopg2.extensions.cursor) as c: c.execute( "SELECT lastdate," " indexfactor/100 AS factor," @@ -71,11 +77,10 @@ class BasketIndex(CreditIndex): "ORDER BY lastdate", (index_type, series), ) - self._index_version = list(tuple(r) for r in c) + self._index_version = list(c) serenitas_pool.putconn(conn) self._update_factor(value_date) - self.issue_date = self.index_desc[0][3] - self.tenors = {t: m for t, m, _, _ in self.index_desc} + self.tenors = {t: m for t, m, _ in self.index_desc} self.coupons = [r[2] for r in self.index_desc] maturities = [r[1] for r in self.index_desc] curves = get_singlenames_curves_prebuilt(index_type, series, value_date) @@ -216,7 +221,7 @@ class BasketIndex(CreditIndex): def pv(self, maturity=None, epsilon=0.0, coupon=None): if maturity is None: r = [] - for _, m, coupon, _ in self.index_desc: + for _, m, coupon in self.index_desc: r.append( super().pv( self.step_in_date, @@ -294,7 +299,7 @@ class BasketIndex(CreditIndex): index_quotes = {} if maturity is None: r = [] - for _, m, coupon, _ in self.index_desc: + for _, m, coupon in self.index_desc: index_quote = index_quotes.get(m, np.nan) r.append( super().theta( |
