aboutsummaryrefslogtreecommitdiffstats
path: root/python/analytics
diff options
context:
space:
mode:
Diffstat (limited to 'python/analytics')
-rw-r--r--python/analytics/basket_index.py12
-rw-r--r--python/analytics/index.py22
2 files changed, 24 insertions, 10 deletions
diff --git a/python/analytics/basket_index.py b/python/analytics/basket_index.py
index f41494fc..e099ef5d 100644
--- a/python/analytics/basket_index.py
+++ b/python/analytics/basket_index.py
@@ -1,6 +1,6 @@
from index_data import (get_index_quotes, get_singlenames_curves,
get_tranche_quotes, _serenitas_engine)
-from tranche_functions import credit_schedule
+from .tranche_functions import credit_schedule
from dateutil.relativedelta import relativedelta
from pyisda.credit_index import CreditIndex
from typing import List
@@ -35,6 +35,14 @@ class BasketIndex(CreditIndex):
index_col='tenor',
params=(index_type, series),
parse_dates=['maturity', 'issue_date'])
+ self._index_version = pd.read_sql_query("SELECT lastdate, indexfactor, cumulativeloss " \
+ "FROM index_version " \
+ "WHERE index=%s AND series = %s" \
+ "ORDER BY lastdate",
+ _serenitas_engine,
+ index_col='lastdate',
+ params=(index_type, series),
+ parse_dates=['lastdate'])
self.issue_date = self.index_desc.issue_date[0]
maturities = self.index_desc.maturity.sort_values().dt.to_pydatetime()
self.index_desc = self.index_desc.reset_index().set_index('maturity')
@@ -128,7 +136,7 @@ class TrancheBasket(BasketIndex):
self.maturity = index_desc.loc[tenor].maturity
self.start_date, self.cs = credit_schedule(trade_date, tenor[:-1], 1, self.yc)
self.K_orig = [0] + [q['detach'] for q in self.tranche_quotes]
- self.K = adjust_attachments(self.K_orig, self
+ #self.K = adjust_attachments(self.K_orig, self
def _get_quotes(self):
refprice = self.tranche_quotes[0]['indexrefprice']
diff --git a/python/analytics/index.py b/python/analytics/index.py
index 789bf692..ae570c10 100644
--- a/python/analytics/index.py
+++ b/python/analytics/index.py
@@ -7,7 +7,6 @@ import pandas as pd
import warnings
from dateutil.relativedelta import relativedelta
-
from pyisda.legs import ContingentLeg, FeeLeg
from quantlib.settings import Settings
from quantlib.time.api import Date, Actual365Fixed
@@ -52,7 +51,7 @@ def _key_from_index(index):
class Index(object):
""" minimal class to represent a credit index """
__slots__ = ['fixed_rate', 'notional', '_start_date', '_end_date',
- 'recovery', 'factor', '_fee_leg', '_default_leg',
+ 'recovery', '_factor', '_fee_leg', '_default_leg',
'_trade_date', '_yc', '_sc', '_risky_annuity', '_spread',
'_price', 'name', 'issue_date', '_quote_is_price',
'_direction', 'currency', '_step_in_date', '_accrued',
@@ -75,7 +74,7 @@ class Index(object):
self._start_date = start_date
self._end_date = end_date
self.recovery = recovery
- self.factor = 1
+ self._factor = ()
self._fee_leg = FeeLeg(self._start_date, end_date, True, 1., 1.)
self._default_leg = ContingentLeg(self._start_date, end_date, True)
@@ -360,6 +359,13 @@ class Index(object):
(index_type, series, tenor, self.trade_date))
rec = run.fetchone()
self.spread = rec.closespread
+ @property
+ def factor(self):
+ for lastdate, factor in getattr(self, '_factor'):
+ if lastdate >= self.trade_date:
+ return factor
+ else:
+ return 1
@classmethod
def from_name(cls, index=None, series=None, tenor=None, trade_date=datetime.date.today(),
@@ -378,7 +384,8 @@ class Index(object):
raise ValueError("Not enough information to load the index.")
try:
df = pd.read_sql_query(sql_str,
- _engine, parse_dates=['lastdate', 'issue_date'],
+ _engine,
+ parse_dates=['lastdate', 'issue_date'],
params=params)
maturity = df.maturity[0]
coupon = df.coupon[0]
@@ -386,9 +393,7 @@ class Index(object):
tenor = df.tenor[0]
index_type = index.upper() if index else df.loc[0,'index']
series = series if series else df.series[0]
- df.loc[df.lastdate.isnull(),'lastdate'] = maturity
- factor = df.loc[df.lastdate >= pd.Timestamp(trade_date),
- 'indexfactor'].iat[0]/100
+ df.loc[df.lastdate.isnull(), 'lastdate'] = maturity
except exc.DataError as e:
print(e)
return None
@@ -396,7 +401,8 @@ class Index(object):
recovery = 0.4 if index_type == "IG" else 0.3
instance = cls(trade_date, maturity, recovery, coupon, notional,
index_type=="HY", df.issue_date[0])
- instance.factor = factor
+ instance._factor = tuple((ld.date(), factor / 100) for ld, factor in \
+ df[['lastdate', 'indexfactor']].itertuples(index=False))
instance.direction = "Buyer"
tenor = tenor.upper()
if tenor.endswith("R"):