diff options
Diffstat (limited to 'python/tests')
| -rw-r--r-- | python/tests/test_sabr_quantlib.py | 19 | ||||
| -rw-r--r-- | python/tests/test_swap_index.py | 19 | ||||
| -rw-r--r-- | python/tests/test_upfront_cds.py | 102 |
3 files changed, 140 insertions, 0 deletions
diff --git a/python/tests/test_sabr_quantlib.py b/python/tests/test_sabr_quantlib.py new file mode 100644 index 00000000..67a33ccb --- /dev/null +++ b/python/tests/test_sabr_quantlib.py @@ -0,0 +1,19 @@ +from quantlib.termstructures.volatility.sabr_interpolated_smilesection import SabrInterpolatedSmileSection +from quantlib.time.api import Date, Period, Months +from quantlib.quotes import SimpleQuote +from quantlib.settings import Settings +import numpy as np + +strikes = np.array([50, 55, 57.5, 60, 62.5, 65, 67.5, 70, 75, 80, 85]) +pvs = np.array([53.65, 37.75, 31.55, 26.45, 22.25, 18.85, 16.15, 13.95, 10.55, + 8.05, 6.15, 4.65, 3.65, 2.75]) * 1e-4 +option_date = Settings().instance().evaluation_date + Period(3, Months) +forward = SimpleQuote(58.71e-4) +strikes = np.array([50, 55, 57.5, 60, 62.5, 65, 67.5, 70, 75, 80, 85, 90, 95, 100]) * 1e-4 +vol = np.array([28.5, 31.6, 33.7, 36.1, 38.7, 41.5, 44.1, + 46.5, 50.8, 54.4, 57.3, 59.8, 61.8, 63.6]) * 1e-2 +vol_quotes = [SimpleQuote(q) for q in vol] + +section = SabrInterpolatedSmileSection(option_date, forward, strikes, False, + SimpleQuote(0.4), vol_quotes, 0.1, 1, 0.1, 0.5, + is_beta_fixed=True) diff --git a/python/tests/test_swap_index.py b/python/tests/test_swap_index.py new file mode 100644 index 00000000..f2816bac --- /dev/null +++ b/python/tests/test_swap_index.py @@ -0,0 +1,19 @@ +import unittest +import sys + +from quantlib.indexes.swap.usd_libor_swap import UsdLiborSwapIsdaFixAm +from quantlib.time.api import Date, Period, Years +from yieldcurve import YC + +class UsdLiborSwap(unittest.TestCase): + def test_creation(self): + yc = YC() + yc.extrapolation = True + USISDA30 = UsdLiborSwapIsdaFixAm(Period(30, Years), forwarding=yc, discounting=yc) + USISDA30.add_fixing(Date(25, 1, 2018), 0.02781) + USISDA02 = UsdLiborSwapIsdaFixAm(Period(2, Years), forwarding=yc, discounting=yc) + USISDA02.add_fixing(Date(25, 1, 2018), 0.02283) + USFS022 = USISDA02.underlying_swap(Date(27, 1, 2020)) + USFS0230 = USISDA30.underlying_swap(Date(27, 1, 2020)) + self.assertEqual(USFS022.fair_rate, USISDA02.fixing(Date(27, 1, 2020))) + self.assertEqual(USFS0230.fair_rate, USISDA30.fixing(Date(27, 1, 2020))) diff --git a/python/tests/test_upfront_cds.py b/python/tests/test_upfront_cds.py new file mode 100644 index 00000000..1782ddc5 --- /dev/null +++ b/python/tests/test_upfront_cds.py @@ -0,0 +1,102 @@ +from quantlib.time.api import (WeekendsOnly, today, Years, Months, + Period, Date, Actual365Fixed, Actual360, + Quarterly, Following, Unadjusted, Schedule, + CDS, pydate_from_qldate) +from quantlib.instruments.api import CreditDefaultSwap, SELLER, BUYER +from quantlib.pricingengines.credit.isda_cds_engine import ( + IsdaCdsEngine, ForwardsInCouponPeriod, NumericalFix, AccrualBias) +from quantlib.termstructures.default_term_structure import DefaultProbabilityTermStructure +from quantlib.termstructures.credit.api import ( + UpfrontCdsHelper, SpreadCdsHelper, PiecewiseDefaultCurve, FlatHazardRate) +from quantlib.settings import Settings +from yieldcurve import YC, rate_helpers, getMarkitIRData +import pandas as pd +from pyisda.curve import YieldCurve, BadDay, SpreadCurve +from pyisda.utils import build_yc +from pyisda.cdsone import upfront_charge +from pyisda.legs import ContingentLeg, FeeLeg +import datetime +import array +import math +import numpy as np + +def snac_pv(spread, term_date, fixed_coupon=0.01, recovery=0.4, ts=YC()): + settings = Settings() + calendar = WeekendsOnly() + cds_helper = SpreadCdsHelper(spread, Period(57, Months), 1, calendar, + Quarterly, Following, CDS, Actual360(), recovery, ts, + lastperiod = Actual360(True)) + cds_helper.set_isda_engine_parameters(int(NumericalFix.Taylor), int(AccrualBias.HalfDayBias), + int(ForwardsInCouponPeriod.Flat)) + pdc = PiecewiseDefaultCurve("SurvivalProbability", "LogLinear", + settings.evaluation_date, [cds_helper], Actual365Fixed()) + isda_pricer = IsdaCdsEngine(pdc, recovery, ts, False, + forwards_in_coupon_period=ForwardsInCouponPeriod.Piecewise, + accrual_bias=AccrualBias.HalfDayBias) + protect_start = settings.evaluation_date + 1 + cds_schedule = Schedule(protect_start, term_date, Period(Quarterly), calendar, + Following, Unadjusted, CDS) + cds_trade = CreditDefaultSwap(BUYER, 100, fixed_coupon, cds_schedule, Following, Actual360(), + protection_start = protect_start, + last_period_day_counter = Actual360(True)) + cds_trade.set_pricing_engine(isda_pricer) + return cds_trade, cds_helper, isda_pricer + +def jpmorgan_curves(trade_date, value_date, start_date, end_date, spread, recovery=0.4): + yc = build_yc(trade_date, True) + step_in_date = trade_date + datetime.timedelta(days=1) + spread = array.array('d', [spread]) + sc = SpreadCurve(trade_date, yc, start_date, step_in_date, + value_date, [term_date], spread, recovery, True) + return yc, sc + +if __name__=="__main__": + settings = Settings() + settings.evaluation_date = Date(21, 5, 2009) + yield_helpers = rate_helpers() + ts = YC(helpers = yield_helpers) + tenor = Period(5, Years) + trade_date = datetime.date(2009, 5, 21) + stepin_date = trade_date + datetime.timedelta(days=1) + value_date = datetime.date(2009, 5, 26) + term_date = datetime.date(2019, 6, 20) + start_date = datetime.date(2009, 3, 20) + spread = 0.001 + yc, sc = jpmorgan_curves(trade_date, value_date, start_date, term_date, spread, recovery = 0.4) + sc_data = sc.inspect()['data'] + hazard_rate = math.log(1 + sc_data[0][1]) + contingent_leg = ContingentLeg(start_date, term_date, 10000000) + fee_leg = FeeLeg(start_date, term_date, True, 10000000, 0.01) + + flat_curve = FlatHazardRate(0, WeekendsOnly(), hazard_rate, Actual365Fixed()) + cds_schedule = Schedule(Date.from_datetime(trade_date), Date.from_datetime(term_date), + Period(Quarterly), WeekendsOnly(), + Following, Unadjusted, CDS) + cds_trade = CreditDefaultSwap.from_upfront(BUYER, 10000000, 0., 0.01, cds_schedule, Following, Actual360(), + protection_start = Date.from_datetime(trade_date) + 1, + last_period_day_counter = Actual360(True)) + isda_pricer = IsdaCdsEngine(flat_curve, 0.4, ts, accrual_bias=AccrualBias.HalfDayBias, + forwards_in_coupon_period = ForwardsInCouponPeriod.Piecewise) + #795915.9787 + cds_trade.set_pricing_engine(isda_pricer) + + cds_trade2 = CreditDefaultSwap(BUYER, 10000000, spread, cds_schedule, Following, Actual360(), + protection_start = Date.from_datetime(trade_date) + 1, + last_period_day_counter = Actual360(True)) + #h = cds_trade2.implied_hazard_rate(0., ts) + h = 0.00168276528775 + flat_curve2 = FlatHazardRate(0, WeekendsOnly(), h, Actual365Fixed()) + isda_pricer2 = IsdaCdsEngine(flat_curve2, 0.4, ts) + cds_trade.set_pricing_engine(isda_pricer2) + print(cds_trade.fair_upfront) + #hazard_rate = 0.12649393489974806 + + # cds_trade.set_pricing_engine(isda_pricer) + # cds_trade2 = CreditDefaultSwap.from_upfront(BUYER, 10000000, 0., 0.01, cds_schedule, + # Following, Actual360(), + # protection_start = Date.from_datetime(trade_date) + 1, + # last_period_day_counter = Actual360(True)) + # cds_trade3 = CreditDefaultSwap(BUYER, 10000000, 0.05, cds_schedule, + # Following, Actual360(), + # protection_start = Date.from_datetime(trade_date) + 1, + # last_period_day_counter = Actual360(True)) |
