diff options
| -rw-r--r-- | python/analytics/curve_trades.py | 6 | ||||
| -rw-r--r-- | python/analytics/option.py | 11 | ||||
| -rw-r--r-- | python/analytics/portfolio.py | 6 | ||||
| -rw-r--r-- | python/analytics/sabr.py | 4 | ||||
| -rw-r--r-- | python/exploration/tranches.py | 2 | ||||
| -rw-r--r-- | python/tests/test_cds.py | 8 | ||||
| -rw-r--r-- | python/tests/test_index.py | 17 | ||||
| -rw-r--r-- | python/tests/test_scenarios.py | 18 | ||||
| -rw-r--r-- | python/tests/test_swaption.py | 21 |
9 files changed, 50 insertions, 43 deletions
diff --git a/python/analytics/curve_trades.py b/python/analytics/curve_trades.py index f76f6b88..b989bd69 100644 --- a/python/analytics/curve_trades.py +++ b/python/analytics/curve_trades.py @@ -1,6 +1,6 @@ from analytics.index_data import get_index_quotes, index_returns from db import dbengine -from analytics import Index, Portfolio +from analytics import CreditIndex, Portfolio from analytics.utils import roll_date from dateutil.relativedelta import relativedelta from analytics.basket_index import MarkitBasketIndex @@ -278,8 +278,8 @@ def curve_pos(value_date, index_type='IG'): df = pd.read_sql_query(sql_string, dawndb, params=[value_date, f'SER_{index_type}CURVE']) - portf = Portfolio([Index.from_name(row.index, row.series, row.tenor, - value_date, -row.notional) + portf = Portfolio([CreditIndex(row.index, row.series, row.tenor, + value_date, -row.notional) for row in df[['index', 'tenor', 'series', 'notional']]. itertuples(index=False)]) portf.mark() diff --git a/python/analytics/option.py b/python/analytics/option.py index c0989814..34f1b7a0 100644 --- a/python/analytics/option.py +++ b/python/analytics/option.py @@ -8,7 +8,7 @@ from db import dbengine from .black import black, Nx from .sabr import sabr from .utils import GHquad, build_table -from .index import g, ForwardIndex, Index +from .index import g, ForwardIndex, CreditIndex from .db import _engine from yieldcurve import roll_yc from pandas.tseries.offsets import BDay @@ -55,7 +55,7 @@ def ATMstrike(index, exercise_date): Parameters ---------- index : - Index object + CreditIndex object exercise_date : datetime.date expiration date. price : bool, defaults to False @@ -99,7 +99,7 @@ class BlackSwaption(ForwardIndex): if rec is None: return ValueError("trade_id doesn't exist") if index is None: - index = Index.from_name(redcode=rec.security_id, maturity=rec.maturity, value_date=rec.trade_date) + index = CreditIndex(redcode=rec.security_id, maturity=rec.maturity, value_date=rec.trade_date) index.ref = rec.index_ref instance = cls(index, rec.expiration_date, rec.strike, rec.swaption_type.lower(), direction="Long" if rec.buysell else "Short") @@ -519,6 +519,7 @@ class QuoteSurface(): r.append((quotedate, quotesource)) return r + class VolSurface(QuoteSurface): def __init__(self, index_type, series, tenor='5yr', value_date=datetime.date.today()): super().__init__(index_type, series, tenor, value_date) @@ -626,7 +627,7 @@ class ModelBasedVolSurface(VolSurface): def __init__(self, index_type, series, tenor='5yr', value_date=datetime.date.today(), interp_method='bivariate_spline'): super().__init__(index_type, series, tenor, value_date) - self._index = Index.from_name(index_type, series, tenor, value_date, notional=1.) + self._index = CreditIndex(index_type, series, tenor, value_date, notional=1.) self._surfaces = {} self._index_refs = {} self._quotes = self._quotes.assign( @@ -716,7 +717,7 @@ class ProbSurface(QuoteSurface): def __init__(self, index_type, series, tenor='5yr', value_date=datetime.date.today()): super().__init__(index_type, series, tenor, value_date) self._surfaces = {} - self._index = Index.from_name(index_type, series, tenor, value_date) + self._index = CreditIndex(index_type, series, tenor, value_date) def __getitem__(self, surface_id): if surface_id not in self._surfaces: diff --git a/python/analytics/portfolio.py b/python/analytics/portfolio.py index 004288f2..3b9e1a9a 100644 --- a/python/analytics/portfolio.py +++ b/python/analytics/portfolio.py @@ -1,4 +1,4 @@ -from .index import Index +from .index import CreditIndex from .option import BlackSwaption from warnings import warn import pandas as pd @@ -36,7 +36,7 @@ class Portfolio: def __init__(self, trades, trade_ids=None): self.trades = trades self.trade_ids = trade_ids - self.indices = [t for t in trades if isinstance(t, Index)] + self.indices = [t for t in trades if isinstance(t, CreditIndex)] self.swaptions = [t for t in trades if isinstance(t, BlackSwaption)] value_dates = set(t.value_date for t in self.trades) self._keys = set([(index.index_type, index.series, index.tenor) for index in self.indices]) @@ -158,7 +158,7 @@ class Portfolio: "Vega"] rec = [] for t in self.trades: - if isinstance(t, Index): + if isinstance(t, CreditIndex): name = f"{t.index_type}{t.series} {t.tenor}" r = ("Index", name, t.notional, t.ref, "N/A", t.direction, "N/A", "N/A", None, t.pv, 1., 0., t.theta, 0.) diff --git a/python/analytics/sabr.py b/python/analytics/sabr.py index eba32bac..4de15338 100644 --- a/python/analytics/sabr.py +++ b/python/analytics/sabr.py @@ -58,10 +58,10 @@ def sabr(alpha, beta, rho, nu, F, K, T): if __name__ == "__main__":
from analytics.option import BlackSwaption
- from analytics import Index
+ from analytics import CreditIndex
from scipy.optimize import least_squares
- underlying = Index.from_name("IG", 28, "5yr")
+ underlying = CreditIndex("IG", 28, "5yr")
underlying.spread = 67.5
exercise_date = datetime.date(2017, 9, 20)
option = BlackSwaption(underlying, exercise_date, 70)
diff --git a/python/exploration/tranches.py b/python/exploration/tranches.py index a8e8d05e..0a5f969a 100644 --- a/python/exploration/tranches.py +++ b/python/exploration/tranches.py @@ -7,7 +7,7 @@ import analytics.basket_index as idx_bkt import numpy as np import pandas as pd -from analytics import Swaption, BlackSwaption, Index, BlackSwaptionVolSurface, Portfolio, ProbSurface +from analytics import Swaption, BlackSwaption, CreditIndex, BlackSwaptionVolSurface, Portfolio, ProbSurface from analytics.scenarios import run_swaption_scenarios, run_index_scenarios, run_portfolio_scenarios, run_tranche_scenarios from scipy.interpolate import interp1d diff --git a/python/tests/test_cds.py b/python/tests/test_cds.py index 44832dea..350f722a 100644 --- a/python/tests/test_cds.py +++ b/python/tests/test_cds.py @@ -10,12 +10,12 @@ from quantlib.time.api import Date import sys sys.path.append('..') -from analytics import Index +from analytics import CreditIndex from yieldcurve import YC, ql_to_jp, get_curve class TestUpfront(unittest.TestCase): - index = Index.from_name("ig", 26, "5yr", - value_date=datetime.date(2016, 9, 21)) + index = CreditIndex("ig", 26, "5yr", + value_date=datetime.date(2016, 9, 21)) index.notional = 50e6 index.spread = 70 @@ -73,5 +73,5 @@ class TestSpreadCurve(unittest.TestCase): b = cl.pv(trade_date, step_in_date, cash_settle_date, yc, sc, True) self.assertAlmostEqual(a - b, upf) -if __name__=="__main__": +if __name__ == "__main__": unittest.main() diff --git a/python/tests/test_index.py b/python/tests/test_index.py index 59dbfe88..d9dbf868 100644 --- a/python/tests/test_index.py +++ b/python/tests/test_index.py @@ -5,13 +5,13 @@ import numpy as np from pyisda.cdsone import upfront_charge from pyisda.flat_hazard import pv_vec -from analytics import Index, ForwardIndex +from analytics import CreditIndex, ForwardIndex from analytics.index import g import pickle + class TestPickle(unittest.TestCase): - index = Index.from_name("ig", 26, "5yr", - value_date=datetime.date(2016, 7, 1)) + index = CreditIndex("ig", 26, "5yr", value_date=datetime.date(2016, 7, 1)) index.notional = 50e6 index.spread = 75 @@ -19,9 +19,9 @@ class TestPickle(unittest.TestCase): a = pickle.loads(pickle.dumps(self.index)) self.assertTrue(hash(a) == hash(self.index)) + class TestStrike(unittest.TestCase): - index = Index.from_name("ig", 26, "5yr", - value_date=datetime.date(2016, 7, 1)) + index = CreditIndex("ig", 26, "5yr", value_date=datetime.date(2016, 7, 1)) index.notional = 50_000_000. index.spread = 75 exercise_date = datetime.date(2016, 8, 19) @@ -55,9 +55,10 @@ class TestStrike(unittest.TestCase): self.index.price = self.index.price self.assertAlmostEqual(self.index.spread, 75) + class TestForwardIndex(unittest.TestCase): - index = Index.from_name("ig", 26, "5yr", - value_date=datetime.date(2016, 7, 1)) + index = CreditIndex("ig", 26, "5yr", + value_date=datetime.date(2016, 7, 1)) index.notional = 50_000_000. index.spread = 75 exercise_date = datetime.date(2016, 8, 19) @@ -74,5 +75,5 @@ class TestForwardIndex(unittest.TestCase): fi = ForwardIndex(self.index, self.index.value_date) self.assertAlmostEqual(fi.forward_pv, self.index.clean_pv / self.index.notional) -if __name__=="__main__": +if __name__ == "__main__": unittest.main() diff --git a/python/tests/test_scenarios.py b/python/tests/test_scenarios.py index d079b1c9..c22c8be3 100644 --- a/python/tests/test_scenarios.py +++ b/python/tests/test_scenarios.py @@ -1,18 +1,20 @@ import unittest -import datetime import numpy as np import pandas as pd -from analytics import Index, BlackSwaption, Portfolio, BlackSwaptionVolSurface +from analytics import CreditIndex, BlackSwaption, Portfolio, BlackSwaptionVolSurface from pandas.tseries.offsets import BDay -from analytics.scenarios import run_portfolio_scenarios, run_swaption_scenarios, run_index_scenarios +from analytics.scenarios import (run_portfolio_scenarios, + run_swaption_scenarios, run_index_scenarios) + class TestSenarios(unittest.TestCase): - option_delta = Index.from_tradeid(874) + option_delta = CreditIndex.from_tradeid(874) option1 = BlackSwaption.from_tradeid(7, option_delta) option2 = BlackSwaption.from_tradeid(8, option_delta) portf = Portfolio([option1, option2, option_delta]) - date_range = pd.bdate_range(option_delta.value_date, pd.Timestamp('2017-05-17') - BDay(), freq = '5B') + date_range = pd.bdate_range(option_delta.value_date, + pd.Timestamp('2017-05-17') - BDay(), freq='5B') def test_portfolio(self): """ check that run_portfolio_scenarios match the sum of the individual pieces""" @@ -21,7 +23,9 @@ class TestSenarios(unittest.TestCase): vs = BlackSwaptionVolSurface("IG", 28, value_date=self.option_delta.value_date) vol_surface = vs[vs.list(source="BAML")[-1]] df = run_portfolio_scenarios(self.portf, self.date_range, - spread_shock, vol_shock, vol_surface) + spread_shock=spread_shock, + vol_shock=vol_shock, + vol_surface=vol_surface) df = df.set_index(['spread', 'vol_shock'], append=True) df1 = run_swaption_scenarios(self.option1, self.date_range, @@ -38,5 +42,5 @@ class TestSenarios(unittest.TestCase): df_orig = df_orig.set_index('vol_shock', append=True) self.assertFalse(np.any((df-df_orig).values)) -if __name__=="__main__": +if __name__ == "__main__": unittest.main() diff --git a/python/tests/test_swaption.py b/python/tests/test_swaption.py index c270dd2d..4faea8fe 100644 --- a/python/tests/test_swaption.py +++ b/python/tests/test_swaption.py @@ -4,11 +4,12 @@ import datetime import sys sys.path.append('..') from analytics.index import g -from analytics import Index, Swaption, BlackSwaption +from analytics import CreditIndex, Swaption, BlackSwaption + class TestPutCallParity(unittest.TestCase): - index = Index.from_name("ig", 27, "5yr", - value_date= datetime.date(2016, 10, 25)) + index = CreditIndex("ig", 27, "5yr", + value_date= datetime.date(2016, 10, 25)) index.spread = 74 exercise_date = datetime.date(2017, 3, 15) strike = 82.5 @@ -42,8 +43,8 @@ class TestPutCallParity(unittest.TestCase): self.assertAlmostEqual(payer.sigma, 0.37648716) def test_hy(self): - index = Index.from_name("hy", 27, "5yr", - value_date=datetime.date(2016, 11, 8)) + index = CreditIndex("hy", 27, "5yr", + value_date=datetime.date(2016, 11, 8)) index.price = 103.875 exercise_date = datetime.date(2017, 3, 15) strike = 102.5 @@ -54,13 +55,13 @@ class TestPutCallParity(unittest.TestCase): class TestBreakeven(unittest.TestCase): exercise_date = datetime.date(2017, 3, 15) - hyindex = Index.from_name("hy", 27, "5yr", - value_date=datetime.date(2016, 11, 16)) + hyindex = CreditIndex("hy", 27, "5yr", + value_date=datetime.date(2016, 11, 16)) hyindex.price = 103.75 hystrike = 102.5 - igindex = Index.from_name("ig", 27, "5yr", - value_date=datetime.date(2016, 11, 18)) + igindex = CreditIndex("ig", 27, "5yr", + value_date=datetime.date(2016, 11, 18)) igindex.spread = 76.5 igstrike = 80 @@ -90,5 +91,5 @@ class TestBreakeven(unittest.TestCase): receiver.notional = 1e7 self.assertAlmostEqual(receiver.breakeven, 73.715942707132982) -if __name__=="__main__": +if __name__ == "__main__": unittest.main() |
