aboutsummaryrefslogtreecommitdiffstats
path: root/python/analytics/index.py
diff options
context:
space:
mode:
Diffstat (limited to 'python/analytics/index.py')
-rw-r--r--python/analytics/index.py22
1 files changed, 14 insertions, 8 deletions
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"):