def test_bond_schedule_anotherday(self): '''Test date calculations and role of settings when evaluation date set to arbitrary date. This test is known to fail with boost 1.42. ''' todays_date = Date(30, August, 2011) settings = Settings() settings.evaluation_date = todays_date calendar = TARGET() effective_date = Date(10, Jul, 2006) termination_date = calendar.advance( effective_date, 10, Years, convention=Unadjusted) settlement_days = 3 face_amount = 100.0 coupon_rate = 0.05 redemption = 100.0 fixed_bond_schedule = Schedule.from_rule( effective_date, termination_date, Period(Annual), calendar, ModifiedFollowing, ModifiedFollowing, Rule.Backward ) issue_date = effective_date bond = FixedRateBond( settlement_days, face_amount, fixed_bond_schedule, [coupon_rate], ActualActual(ISMA), Following, redemption, issue_date ) self.assertEqual( calendar.advance(todays_date, 3, Days), bond.settlement_date())
def test_bond_schedule_anotherday(self): '''Test date calculations and role of settings when evaluation date set to arbitrary date. This test is known to fail with boost 1.42. ''' todays_date = Date(30, August, 2011) settings = Settings() settings.evaluation_date = todays_date calendar = TARGET() effective_date = Date(10, Jul, 2006) termination_date = calendar.advance( effective_date, 10, Years, convention=Unadjusted) settlement_days = 3 face_amount = 100.0 coupon_rate = 0.05 redemption = 100.0 fixed_bond_schedule = Schedule.from_rule( effective_date, termination_date, Period(Annual), calendar, ModifiedFollowing, ModifiedFollowing, Rule.Backward ) issue_date = effective_date bond = FixedRateBond( settlement_days, face_amount, fixed_bond_schedule, [coupon_rate], ActualActual(ISMA), Following, redemption, issue_date ) self.assertEqual( calendar.advance(todays_date, 3, Days), bond.settlement_date())
def test_extrapolation(self): rate_helpers = build_helpers() settings = Settings() calendar = TARGET() # must be a business Days dtObs = date(2007, 4, 27) eval_date = calendar.adjust(pydate_to_qldate(dtObs)) settings.evaluation_date = eval_date settlement_days = 2 settlement_date = calendar.advance(eval_date, settlement_days, Days) # must be a business day settlement_date = calendar.adjust(settlement_date) print('dt Obs: %s\ndt Eval: %s\ndt Settle: %s' % (dtObs, eval_date, settlement_date)) ts_day_counter = ActualActual(ISDA) tolerance = 1.0e-2 ts = PiecewiseYieldCurve('discount', 'loglinear', settlement_date, rate_helpers, ts_day_counter, tolerance) # max_date raises an exception without extrapolaiton... self.assertFalse(ts.extrapolation) with self.assertRaisesRegexp( RuntimeError, "1st iteration: failed at 2nd alive instrument"): dtMax = ts.max_date
def test_create_swap_index(self): settings = Settings.instance() # Market information calendar = TARGET() # must be a business day eval_date = calendar.adjust(today()) settings.evaluation_date = eval_date settlement_days = 2 settlement_date = calendar.advance(eval_date, settlement_days, Days) # must be a business day settlement_date = calendar.adjust(settlement_date) term_structure = YieldTermStructure(relinkable=True) term_structure.link_to( FlatForward(settlement_date, 0.05, Actual365Fixed())) ibor_index = Libor('USD Libor', Period(6, Months), settlement_days, USDCurrency(), calendar, Actual360(), term_structure) index = SwapIndex('family name', Period(3, Months), 10, USDCurrency(), TARGET(), Period(12, Months), Following, Actual360(), ibor_index) self.assertIsNotNone(index)
class FlatHazardRateTestCase(unittest.TestCase): def setUp(self): self.calendar = TARGET() todays_date = Date(15, May, 2007) self.todays_date = self.calendar.adjust(todays_date) self.d = self.todays_date + Period(3, Years) def test_create_flat_hazard(self): Settings.instance().evaluation_date = self.todays_date flat_curve = FlatHazardRate(2, self.calendar, 0.05, Actual365Fixed()) flat_curve_from_reference_date = FlatHazardRate.from_reference_date( self.calendar.advance(self.todays_date, 2, Days), 0.05, Actual365Fixed()) self.assertIsNotNone(flat_curve) self.assertIsNotNone(flat_curve_from_reference_date) self.assertEqual(flat_curve.time_from_reference(self.d), flat_curve_from_reference_date.time_from_reference(self.d)) self.assertAlmostEqual(flat_curve.hazard_rate(self.d), 0.05) self.assertAlmostEqual(flat_curve.survival_probability(self.d), math.exp(-0.05*flat_curve.time_from_reference(self.d))) def test_flat_hazard_with_quote(self): Settings.instance().evaluation_date = self.todays_date hazard_rate = SimpleQuote() flat_curve = FlatHazardRate(2, self.calendar, hazard_rate, Actual365Fixed()) for h in [0.01, 0.02, 0.03]: hazard_rate.value = h self.assertAlmostEqual(flat_curve.survival_probability(self.d), math.exp(-h * flat_curve.time_from_reference(self.d)))
def test_extrapolation(self): rate_helpers = build_helpers() settings = Settings() calendar = TARGET() # must be a business Days dtObs = date(2007, 4, 27) eval_date = calendar.adjust(pydate_to_qldate(dtObs)) settings.evaluation_date = eval_date settlement_days = 2 settlement_date = calendar.advance(eval_date, settlement_days, Days) # must be a business day settlement_date = calendar.adjust(settlement_date) print('dt Obs: %s\ndt Eval: %s\ndt Settle: %s' % (dtObs, eval_date, settlement_date)) ts_day_counter = ActualActual(ISDA) tolerance = 1.0e-2 ts = PiecewiseYieldCurve.from_reference_date(BootstrapTrait.Discount, Interpolator.LogLinear, settlement_date, rate_helpers, ts_day_counter, tolerance) # max_date raises an exception without extrapolaiton... self.assertFalse(ts.extrapolation) with self.assertRaises(RuntimeError) as ctx: ts.discount(ts.max_date + 1) self.assertTrue(str(ctx.exception) in ( "time (30.011) is past max curve time (30.0082)", "1st iteration: failed at 2nd alive instrument"))
def test_extrapolation(self): rate_helpers = build_helpers() settings = Settings() calendar = TARGET() # must be a business Days dtObs = date(2007, 4, 27) eval_date = calendar.adjust(pydate_to_qldate(dtObs)) settings.evaluation_date = eval_date settlement_days = 2 settlement_date = calendar.advance(eval_date, settlement_days, Days) # must be a business day settlement_date = calendar.adjust(settlement_date) print('dt Obs: %s\ndt Eval: %s\ndt Settle: %s' % (dtObs, eval_date, settlement_date)) ts_day_counter = ActualActual(ISDA) tolerance = 1.0e-2 ts = PiecewiseYieldCurve('discount', 'loglinear', settlement_date, rate_helpers, ts_day_counter, tolerance) # max_date raises an exception without extrapolaiton... self.assertFalse(ts.extrapolation) with self.assertRaisesRegexp(RuntimeError, "1st iteration: failed at 2nd alive instrument"): dtMax = ts.max_date
class FlatHazardRateTestCase(unittest.TestCase): def setUp(self): self.calendar = TARGET() todays_date = Date(15, May, 2007) self.todays_date = self.calendar.adjust(todays_date) self.d = self.todays_date + Period(3, Years) def test_create_flat_hazard(self): Settings.instance().evaluation_date = self.todays_date flat_curve = FlatHazardRate(2, self.calendar, 0.05, Actual365Fixed()) flat_curve_from_reference_date = FlatHazardRate.from_reference_date( self.calendar.advance(self.todays_date, 2, Days), 0.05, Actual365Fixed()) self.assertIsNotNone(flat_curve) self.assertIsNotNone(flat_curve_from_reference_date) self.assertEqual( flat_curve.time_from_reference(self.d), flat_curve_from_reference_date.time_from_reference(self.d)) self.assertAlmostEqual(flat_curve.hazard_rate(self.d), 0.05) self.assertAlmostEqual( flat_curve.survival_probability(self.d), math.exp(-0.05 * flat_curve.time_from_reference(self.d))) def test_flat_hazard_with_quote(self): Settings.instance().evaluation_date = self.todays_date hazard_rate = SimpleQuote() flat_curve = FlatHazardRate(2, self.calendar, hazard_rate, Actual365Fixed()) for h in [0.01, 0.02, 0.03]: hazard_rate.value = h self.assertAlmostEqual( flat_curve.survival_probability(self.d), math.exp(-h * flat_curve.time_from_reference(self.d)))
def test_extrapolation(self): rate_helpers = build_helpers() settings = Settings() calendar = TARGET() # must be a business Days dtObs = date(2007, 4, 27) eval_date = calendar.adjust(pydate_to_qldate(dtObs)) settings.evaluation_date = eval_date settlement_days = 2 settlement_date = calendar.advance(eval_date, settlement_days, Days) # must be a business day settlement_date = calendar.adjust(settlement_date) print('dt Obs: %s\ndt Eval: %s\ndt Settle: %s' % (dtObs, eval_date, settlement_date)) ts_day_counter = ActualActual(ISDA) tolerance = 1.0e-2 ts = PiecewiseYieldCurve.from_reference_date(BootstrapTrait.Discount, Interpolator.LogLinear, settlement_date, rate_helpers, ts_day_counter, tolerance) # max_date raises an exception without extrapolaiton... self.assertFalse(ts.extrapolation)
def test_zero_curve(self): rate_helpers = build_helpers() settings = Settings() calendar = TARGET() # must be a business Days dtObs = date(2007, 4, 27) eval_date = calendar.adjust(pydate_to_qldate(dtObs)) settings.evaluation_date = eval_date settlement_days = 2 settlement_date = calendar.advance(eval_date, settlement_days, Days) # must be a business day settlement_date = calendar.adjust(settlement_date) ts_day_counter = ActualActual(ISDA) tolerance = 1.0e-2 ts = PiecewiseYieldCurve.from_reference_date(BootstrapTrait.Discount, Interpolator.LogLinear, settlement_date, rate_helpers, ts_day_counter, tolerance) # max_date raises an exception... ts.extrapolation = True zr = ts.zero_rate(Date(10, 5, 2027), ts_day_counter, 2) self.assertAlmostEqual(zr.rate, 0.0539332, 6)
def test_create_libor_index(self): settings = Settings.instance() # Market information calendar = TARGET() # must be a business day eval_date = calendar.adjust(today()) settings.evaluation_date = eval_date settlement_days = 2 settlement_date = calendar.advance(eval_date, settlement_days, Days) # must be a business day settlement_date = calendar.adjust(settlement_date) term_structure = YieldTermStructure(relinkable=True) term_structure.link_to( FlatForward(settlement_date, 0.05, Actual365Fixed())) index = Libor('USD Libor', Period(6, Months), settlement_days, USDCurrency(), calendar, Actual360(), term_structure) t = index.tenor self.assertEquals(t.length, 6) self.assertEquals(t.units, 2) self.assertEquals('USD Libor6M Actual/360', index.name)
def test_zero_curve(self): rate_helpers = build_helpers() settings = Settings() calendar = TARGET() # must be a business Days dtObs = date(2007, 4, 27) eval_date = calendar.adjust(pydate_to_qldate(dtObs)) settings.evaluation_date = eval_date settlement_days = 2 settlement_date = calendar.advance(eval_date, settlement_days, Days) # must be a business day settlement_date = calendar.adjust(settlement_date) ts_day_counter = ActualActual(ISDA) tolerance = 1.0e-2 ts = PiecewiseYieldCurve('discount', 'loglinear', settlement_date, rate_helpers, ts_day_counter, tolerance) # max_date raises an exception... ts.extrapolation = True zr = ts.zero_rate(Date(10, 5, 2027), ts_day_counter, 2) self.assertAlmostEqual(zr.rate, 0.0539332)
def test_create_swap_index(self): settings = Settings.instance() # Market information calendar = TARGET() # must be a business day eval_date = calendar.adjust(today()) settings.evaluation_date = eval_date settlement_days = 2 settlement_date = calendar.advance(eval_date, settlement_days, Days) # must be a business day settlement_date = calendar.adjust(settlement_date) term_structure = YieldTermStructure(relinkable=True) term_structure.link_to(FlatForward(settlement_date, 0.05, Actual365Fixed())) ibor_index = Libor('USD Libor', Period(6, Months), settlement_days, USDCurrency(), calendar, Actual360(), term_structure) index = SwapIndex( 'family name', Period(3, Months), 10, USDCurrency(), TARGET(), Period(12, Months), Following, Actual360(), ibor_index) self.assertIsNotNone(index)
def test_extrapolation(self): rate_helpers = build_helpers() settings = Settings() calendar = TARGET() # must be a business Days dtObs = date(2007, 4, 27) eval_date = calendar.adjust(pydate_to_qldate(dtObs)) settings.evaluation_date = eval_date settlement_days = 2 settlement_date = calendar.advance(eval_date, settlement_days, Days) # must be a business day settlement_date = calendar.adjust(settlement_date) print('dt Obs: %s\ndt Eval: %s\ndt Settle: %s' % (dtObs, eval_date, settlement_date)) ts_day_counter = ActualActual(ISDA) tolerance = 1.0e-2 ts = PiecewiseYieldCurve.from_reference_date(BootstrapTrait.Discount, Interpolator.LogLinear, settlement_date, rate_helpers, ts_day_counter, tolerance) # max_date raises an exception without extrapolaiton... self.assertFalse(ts.extrapolation) with self.assertRaises(RuntimeError) as ctx: ts.discount(ts.max_date + 1) self.assertTrue( str(ctx.exception) in ( "time (30.011) is past max curve time (30.0082)", "1st iteration: failed at 2nd alive instrument"))
def test_create_swap_index(self): settings = Settings.instance() # Market information calendar = TARGET() # must be a business day eval_date = calendar.adjust(today()) settings.evaluation_date = eval_date settlement_days = 2 settlement_date = calendar.advance(eval_date, settlement_days, Days) # must be a business day settlement_date = calendar.adjust(settlement_date); ibor_index = Libor('USD Libor', Period(6, Months), settlement_days, USDCurrency(), calendar, Actual360()) index = SwapIndex( 'family name', Period(3, Months), 10, USDCurrency(), TARGET(), Period(12, Months), Following, Actual360(), ibor_index) self.assertIsNotNone(index)
def test_create_libor_index(self): settings = Settings.instance() # Market information calendar = TARGET() # must be a business day eval_date = calendar.adjust(today()) settings.evaluation_date = eval_date settlement_days = 2 settlement_date = calendar.advance(eval_date, settlement_days, Days) # must be a business day settlement_date = calendar.adjust(settlement_date) term_structure = YieldTermStructure(relinkable=True) term_structure.link_to(FlatForward(settlement_date, 0.05, Actual365Fixed())) index = Libor('USD Libor', Period(6, Months), settlement_days, USDCurrency(), calendar, Actual360(), term_structure) t = index.tenor self.assertEqual(t.length, 6) self.assertEqual(t.units, 2) self.assertEqual('USD Libor6M Actual/360', index.name)
def test_bond_schedule_today(self): '''Test date calculations and role of settings when evaluation date set to current date. ''' todays_date = today() settings = Settings() settings.evaluation_date = todays_date calendar = TARGET() effective_date = Date(10, Jul, 2006) termination_date = calendar.advance( effective_date, 10, Years, convention=Unadjusted) settlement_days = 3 face_amount = 100.0 coupon_rate = 0.05 redemption = 100.0 fixed_bond_schedule = Schedule( effective_date, termination_date, Period(Annual), calendar, ModifiedFollowing, ModifiedFollowing, Backward ) issue_date = effective_date bond = FixedRateBond( settlement_days, face_amount, fixed_bond_schedule, [coupon_rate], ActualActual(ISMA), Following, redemption, issue_date ) self.assertEquals( calendar.advance(todays_date, 3, Days), bond.settlement_date())
def zero_curve(ts, days, dtObs): calendar = TARGET() dtMat = [calendar.advance(dateToDate(dtObs), d, Days) for d in days] df = np.array([ts.discount(dt) for dt in dtMat]) dtMat = [QLDateTodate(dt) for dt in dtMat] dtToday = QLDateTodate(dtObs) dt = np.array([(d-dtToday).days/365.0 for d in dtMat]) zc = -np.log(df) / dt return (dtMat, zc)
def zero_curve(ts, days, dtObs): calendar = TARGET() dtMat = [calendar.advance(dateToDate(dtObs), d, Days) for d in days] df = np.array([ts.discount(dt) for dt in dtMat]) dtMat = [QLDateTodate(dt) for dt in dtMat] dtToday = QLDateTodate(dtObs) dt = np.array([(d - dtToday).days / 365.0 for d in dtMat]) zc = -np.log(df) / dt return (dtMat, zc)
def test_relative_yieldcurve(self): settings = Settings() settings.evaluation_date = Date(6, 10, 2016) # Market information calendar = TARGET() quotes = [0.0096, 0.0145, 0.0194] tenors = [3, 6, 12] deposit_day_counter = Actual365Fixed() convention = ModifiedFollowing end_of_month = True fixing_days = 3 rate_helpers = [ DepositRateHelper(quote, Period(month, Months), fixing_days, calendar, convention, end_of_month, deposit_day_counter) for quote, month in zip(quotes, tenors) ] ts_day_counter = ActualActual(ISDA) tolerance = 1.0e-15 ts_relative = PiecewiseYieldCurve(BootstrapTrait.Discount, Interpolator.LogLinear, 2, calendar, rate_helpers, ts_day_counter, tolerance) self.assertEqual( ts_relative.reference_date, calendar.advance(settings.evaluation_date, period=Period(2, Days))) settings.evaluation_date = Date(10, 10, 2016) settlement_date = calendar.advance(settings.evaluation_date, period=Period(2, Days)) self.assertEqual(ts_relative.reference_date, settlement_date) ts_absolute = PiecewiseYieldCurve.from_reference_date( BootstrapTrait.Discount, Interpolator.LogLinear, settlement_date, rate_helpers, ts_day_counter, tolerance) self.assertEqual(ts_absolute.data, ts_relative.data) self.assertEqual(ts_absolute.dates, ts_relative.dates) self.assertEqual(ts_absolute.times, ts_relative.times)
def test_relative_yieldcurve(self): settings = Settings() settings.evaluation_date = Date(6, 10, 2016) # Market information calendar = TARGET() quotes = [0.0096, 0.0145, 0.0194] tenors = [3, 6, 12] deposit_day_counter = Actual365Fixed() convention = ModifiedFollowing end_of_month = True fixing_days = 3 rate_helpers = [DepositRateHelper( quote, Period(month, Months), fixing_days, calendar, convention, end_of_month, deposit_day_counter) for quote, month in zip(quotes, tenors)] ts_day_counter = ActualActual(ISDA) tolerance = 1.0e-15 ts_relative = PiecewiseYieldCurve( BootstrapTrait.Discount, Interpolator.LogLinear, 2, calendar, rate_helpers, ts_day_counter, tolerance ) self.assertEqual(ts_relative.reference_date, calendar.advance(settings.evaluation_date, period = Period(2, Days))) settings.evaluation_date = Date(10, 10, 2016) settlement_date = calendar.advance(settings.evaluation_date, period = Period(2, Days)) self.assertEqual(ts_relative.reference_date, settlement_date) ts_absolute = PiecewiseYieldCurve.from_reference_date( BootstrapTrait.Discount, Interpolator.LogLinear, settlement_date, rate_helpers, ts_day_counter, tolerance ) self.assertEqual(ts_absolute.data, ts_relative.data) self.assertEqual(ts_absolute.dates, ts_relative.dates) self.assertEqual(ts_absolute.times, ts_relative.times)
def test_bond_schedule_today(self): '''Test date calculations and role of settings when evaluation date set to current date. ''' todays_date = today() settings = Settings() settings.evaluation_date = todays_date calendar = TARGET() effective_date = Date(10, Jul, 2006) termination_date = calendar.advance(effective_date, 10, Years, convention=Unadjusted) settlement_days = 3 face_amount = 100.0 coupon_rate = 0.05 redemption = 100.0 fixed_bond_schedule = Schedule(effective_date, termination_date, Period(Annual), calendar, ModifiedFollowing, ModifiedFollowing, Backward) issue_date = effective_date bond = FixedRateBond(settlement_days, face_amount, fixed_bond_schedule, [coupon_rate], ActualActual(ISMA), Following, redemption, issue_date) self.assertEquals(calendar.advance(todays_date, 3, Days), bond.settlement_date())
def zero_curve(ts, dtObs): dtMax = ts.max_date calendar = TARGET() days = range(10, 365 * 20, 30) dtMat = [min(dtMax, calendar.advance(dateToDate(dtObs), d, Days)) for d in days] # largest dtMat < dtMax, yet QL run time error df = np.array([ts.discount(dt, extrapolate=True) for dt in dtMat]) dtMat = [QLDateTodate(dt) for dt in dtMat] dtToday = QLDateTodate(dtObs) dt = np.array([(d - dtToday).days / 365.0 for d in dtMat]) zc = -np.log(df) / dt return (dtMat, zc)
def zero_curve(ts, dtObs): dtMax = ts.max_date calendar = TARGET() days = range(10, 365 * 20, 30) dtMat = [min(dtMax, calendar.advance(dateToDate(dtObs), d, Days)) for d in days] # largest dtMat < dtMax, yet QL run time error df = np.array([ts.discount(dt, extrapolate=True) for dt in dtMat]) dtMat = [QLDateTodate(dt) for dt in dtMat] dtToday = QLDateTodate(dtObs) dt = np.array([(d - dtToday).days / 365.0 for d in dtMat]) zc = -np.log(df) / dt return (dtMat, zc)
def setUp(self): calendar = TARGET() today_date = today() Settings().evaluation_date = today_date hazard_rate = SimpleQuote(0.01234) probability_curve = FlatHazardRate(0, calendar, hazard_rate, Actual360()) discount_curve = FlatForward(today_date, 0.06, Actual360()) issue_date = today_date #calendar.advance(today_date, -1, Years) maturity = calendar.advance(issue_date, 10, Years) self.convention = Following self.schedule = Schedule(issue_date, maturity, Period("3M"), calendar, self.convention, self.convention, Rule.TwentiethIMM) recovery_rate = 0.4 self.engine = MidPointCdsEngine(probability_curve, recovery_rate, discount_curve, True)
def test_create_libor_index(self): settings = Settings.instance() # Market information calendar = TARGET() # must be a business day eval_date = calendar.adjust(today()) settings.evaluation_date = eval_date settlement_days = 2 settlement_date = calendar.advance(eval_date, settlement_days, Days) # must be a business day settlement_date = calendar.adjust(settlement_date) index = Libor("USD Libor", Period(6, Months), settlement_days, USDCurrency(), calendar, Actual360()) self.assertEquals("USD Libor6M Actual/360", index.name)
def test_create_libor_index(self): settings = Settings.instance() # Market information calendar = TARGET() # must be a business day eval_date = calendar.adjust(today()) settings.evaluation_date = eval_date settlement_days = 2 settlement_date = calendar.advance(eval_date, settlement_days, Days) # must be a business day settlement_date = calendar.adjust(settlement_date) index = Libor('USD Libor', Period(6, Months), settlement_days, USDCurrency(), calendar, Actual360()) self.assertEquals('USD Libor6M Actual/360', index.name)
from quantlib.time.daycounter import Actual365Fixed from quantlib.time.daycounters.actual_actual import ActualActual, ISMA from quantlib.time.schedule import Schedule, Backward from quantlib.settings import Settings from quantlib.termstructures.yields.api import (FlatForward, YieldTermStructure) todays_date = Date(25, August, 2011) settings = Settings.instance() settings.evaluation_date = todays_date calendar = TARGET() effective_date = Date(10, Jul, 2006) termination_date = calendar.advance(effective_date, 10, Years, convention=Unadjusted) settlement_days = 3 face_amount = 100.0 coupon_rate = 0.05 redemption = 100.0 fixed_bond_schedule = Schedule(effective_date, termination_date, Period(Annual), calendar, ModifiedFollowing, ModifiedFollowing, Backward) issue_date = effective_date bond = FixedRateBond(settlement_days, face_amount, fixed_bond_schedule, [coupon_rate], ActualActual(ISMA), Following, redemption, issue_date)
def get_term_structure(df_libor, dtObs): settings = Settings() # Market information calendar = TARGET() # must be a business day eval_date = calendar.adjust(Date.from_datetime(dtObs)) settings.evaluation_date = eval_date settlement_days = 2 settlement_date = calendar.advance(eval_date, settlement_days, Days) # must be a business day settlement_date = calendar.adjust(settlement_date) depositData = [[1, Months, 'Libor1M'], [3, Months, 'Libor3M'], [6, Months, 'Libor6M']] swapData = [[1, Years, 'Swap1Y'], [2, Years, 'Swap2Y'], [3, Years, 'Swap3Y'], [4, Years, 'Swap4Y'], [5, Years, 'Swap5Y'], [7, Years, 'Swap7Y'], [10, Years, 'Swap10Y'], [30, Years, 'Swap30Y']] rate_helpers = [] end_of_month = True for m, period, label in depositData: tenor = Period(m, Months) rate = df_libor.get_value(dtObs, label) helper = DepositRateHelper(SimpleQuote(rate / 100.0), tenor, settlement_days, calendar, ModifiedFollowing, end_of_month, Actual360()) rate_helpers.append(helper) liborIndex = Libor('USD Libor', Period(3, Months), settlement_days, USDCurrency(), calendar, Actual360()) spread = SimpleQuote(0) fwdStart = Period(0, Days) for m, period, label in swapData: rate = df_libor.get_value(dtObs, label) helper = SwapRateHelper.from_tenor(SimpleQuote(rate / 100.0), Period(m, Years), calendar, Semiannual, ModifiedFollowing, Thirty360(), liborIndex, spread, fwdStart) rate_helpers.append(helper) ts_day_counter = ActualActual(ISDA) tolerance = 1.0e-15 ts = PiecewiseYieldCurve.from_reference_date(BootstrapTrait.Discount, Interpolator.LogLinear, settlement_date, rate_helpers, ts_day_counter, tolerance) ts.extrapolation = True return ts
def test_swap_from_market(self): """ Test that a swap with fixed coupon = fair rate has an NPV=0 Create from market """ eval_date = Date(2, January, 2014) settings = Settings() settings.evaluation_date = eval_date calendar = TARGET() settlement_date = calendar.advance(eval_date, 2, Days) # must be a business day settlement_date = calendar.adjust(settlement_date) length = 5 fixed_rate = .05 floating_spread = 0.0 m = libor_market('USD(NY)') quotes = [('DEP', '1W', SimpleQuote(0.0382)), ('DEP', '1M', SimpleQuote(0.0372)), ('DEP', '3M', SimpleQuote(0.0363)), ('DEP', '6M', SimpleQuote(0.0353)), ('DEP', '9M', SimpleQuote(0.0348)), ('DEP', '1Y', SimpleQuote(0.0345)), ('SWAP', '2Y', SimpleQuote(0.037125)), ('SWAP', '3Y', SimpleQuote(0.0398)), ('SWAP', '5Y', SimpleQuote(0.0443)), ('SWAP', '10Y', SimpleQuote(0.05165)), ('SWAP', '15Y', SimpleQuote(0.055175))] m.set_quotes(eval_date, quotes) m.bootstrap_term_structure() dt = Date(2, January, 2015) df = m.discount(dt) print('discount factor for %s (USD Libor): %f' % (dt, df)) swap = m.create_fixed_float_swap(settlement_date, length, fixed_rate, floating_spread) fixed_l = swap.fixed_leg float_l = swap.floating_leg f = swap.fair_rate print('fair rate: %f' % f) p = swap.net_present_value print('NPV: %f' % p) fixed_npv = swap.fixed_leg_npv float_npv = swap.floating_leg_npv # verify calculation by discounting both legs tot = 0.0 for frc in fixed_l: df = m.discount(frc.date) tot += frc.amount * df print('fixed npv: %f discounted cf: %f' % (fixed_npv, tot)) self.assertAlmostEqual(fixed_npv, -tot) tot = 0.0 for ic in float_l: df = m.discount(ic.date) tot += ic.amount * df print('float npv: %f discounted cf: %f' % (float_npv, tot)) self.assertAlmostEqual(float_npv, tot)
def test_swap_QL(self): """ Test that a swap with fixed coupon = fair rate has an NPV=0 Create from QL objects """ nominal = 100.0 fixedConvention = Unadjusted floatingConvention = ModifiedFollowing fixedFrequency = Annual floatingFrequency = Semiannual fixedDayCount = Thirty360() floatDayCount = Thirty360() calendar = TARGET() settlement_days = 2 eval_date = Date(2, January, 2014) settings = Settings() settings.evaluation_date = eval_date settlement_date = calendar.advance(eval_date, settlement_days, Days) # must be a business day settlement_date = calendar.adjust(settlement_date) termStructure = YieldTermStructure(relinkable=True) termStructure.link_to(FlatForward(settlement_date, 0.05, Actual365Fixed())) index = Libor('USD Libor', Period(6, Months), settlement_days, USDCurrency(), calendar, Actual360(), termStructure) length = 5 fixedRate = .05 floatingSpread = 0.0 maturity = calendar.advance(settlement_date, length, Years, convention=floatingConvention) fixedSchedule = Schedule(settlement_date, maturity, Period(fixedFrequency), calendar, fixedConvention, fixedConvention, Rule.Forward, False) floatSchedule = Schedule(settlement_date, maturity, Period(floatingFrequency), calendar, floatingConvention, floatingConvention, Rule.Forward, False) engine = DiscountingSwapEngine(termStructure, False, settlement_date, settlement_date) for swap_type in [Payer, Receiver]: swap = VanillaSwap(swap_type, nominal, fixedSchedule, fixedRate, fixedDayCount, floatSchedule, index, floatingSpread, floatDayCount, fixedConvention) swap.set_pricing_engine(engine) fixed_leg = swap.fixed_leg floating_leg = swap.floating_leg f = swap.fair_rate print('fair rate: %f' % f) p = swap.net_present_value print('NPV: %f' % p) swap = VanillaSwap(swap_type, nominal, fixedSchedule, f, fixedDayCount, floatSchedule, index, floatingSpread, floatDayCount, fixedConvention) swap.set_pricing_engine(engine) p = swap.net_present_value print('NPV: %f' % p) self.assertAlmostEqual(p, 0)
class TestPiecewiseZeroSpreadedTermStructure(unittest.TestCase): def setUp(self): self.calendar = TARGET() self.today = Date(9, 6, 2009) settlement_date = self.calendar.advance(self.today, 2, Days) Settings().evaluation_date = self.today rates = [0.035, 0.035, 0.033, 0.034, 0.034, 0.036, 0.037, 0.039, 0.04] ts = [13, 41, 75, 165, 256, 345, 524, 703] dates = [settlement_date] + [self.calendar.advance(self.today, d, Days) for d in ts] self.day_counter = Actual360() self.term_structure = ZeroCurve(dates, rates, self.day_counter) self.spreads = [SimpleQuote(0.02), SimpleQuote(0.03)] self.spread_dates = [self.calendar.advance(self.today, 8, Months), self.calendar.advance(self.today, 15, Months)] self.spreaded_term_structure = PiecewiseZeroSpreadedTermStructure( self.term_structure, self.spreads, self.spread_dates) self.spreaded_term_structure.extrapolation = True def test_flat_interpolation_left(self): interpolation_date = self.calendar.advance(self.today, 6, Months) t = self.day_counter.year_fraction(self.today, interpolation_date) interpolated_zero_rate = (self.spreaded_term_structure. zero_rate(t, compounding=Continuous).rate) expected_rate = self.term_structure.zero_rate(t, compounding=Continuous).rate + \ self.spreads[0].value self.assertAlmostEqual(interpolated_zero_rate, expected_rate) def test_flat_interpolation_right(self): interpolation_date = self.calendar.advance(self.today, 20, Months) t = self.day_counter.year_fraction(self.today, interpolation_date) interpolated_zero_rate = (self.spreaded_term_structure. zero_rate(t, compounding=Continuous).rate) expected_rate = self.term_structure.zero_rate(t, compounding=Continuous).rate + \ self.spreads[1].value self.assertAlmostEqual(interpolated_zero_rate, expected_rate) def test_linear_interpolation(self): interpolation_date = self.calendar.advance(self.today, 12, Months) t = self.term_structure.time_from_reference(interpolation_date) t1 = self.term_structure.time_from_reference(self.spread_dates[0]) t2 = self.term_structure.time_from_reference(self.spread_dates[1]) interpolated_zero_rate = (self.spreaded_term_structure. zero_rate(t, compounding=Continuous).rate) zero_rate = (self.term_structure. zero_rate(t, compounding=Continuous, frequency=NoFrequency)) expected_rate = zero_rate.rate + \ (t - t1) / (t2 - t1 ) * self.spreads[1].value + \ (t2 - t) / (t2 - t1) * self.spreads[0].value spreaded_rate = InterestRate(expected_rate, zero_rate.day_counter, zero_rate.compounding, zero_rate.frequency) self.assertAlmostEqual(interpolated_zero_rate, spreaded_rate.equivalent_rate(Continuous, NoFrequency, t).rate)
from quantlib.termstructures.yields.piecewise_yield_curve import PiecewiseYieldCurve from quantlib.termstructures.yields.bootstraptraits import Discount from quantlib.termstructures.yields.api import YieldTermStructure from quantlib.math.interpolation import Cubic from quantlib.indexes.ibor.eonia import Eonia from quantlib.indexes.api import Euribor6M from quantlib.instruments.swap import SwapType from quantlib.instruments.vanillaswap import VanillaSwap from quantlib.pricingengines.swap import DiscountingSwapEngine calendar = TARGET() todays_date = Date(11, 12, 2012) Settings().evaluation_date = todays_date fixing_days = 2 settlement_date = calendar.advance(todays_date, fixing_days, Days) settlement_date = calendar.adjust(settlement_date) # deposits dONRate = SimpleQuote(0.0004) dTNRate = SimpleQuote(0.0004) dSNRate = SimpleQuote(0.0004) # OIS ois1WRate = SimpleQuote(0.00070) ois2WRate = SimpleQuote(0.00069) ois3WRate = SimpleQuote(0.00078) ois1MRate = SimpleQuote(0.00074) # Dated OIS oisDated1Rate = SimpleQuote(0.000460)
def test_deposit_swap(self): settings = Settings() # Market information calendar = TARGET() todays_date = Date(1, Mar, 2012) # must be a business day eval_date = calendar.adjust(todays_date) settings.evaluation_date = eval_date settlement_days = 2 settlement_date = calendar.advance(eval_date, settlement_days, Days) # must be a business day settlement_date = calendar.adjust(settlement_date) depositData = [ [1, Months, 4.581], [2, Months, 4.573], [3, Months, 4.557], [6, Months, 4.496], [9, Months, 4.490], ] swapData = [[1, Years, 4.54], [5, Years, 4.99], [10, Years, 5.47], [20, Years, 5.89], [30, Years, 5.96]] rate_helpers = [] end_of_month = True for m, period, rate in depositData: tenor = Period(m, Months) helper = DepositRateHelper( SimpleQuote(rate / 100), tenor, settlement_days, calendar, ModifiedFollowing, end_of_month, Actual360() ) rate_helpers.append(helper) liborIndex = Libor("USD Libor", Period(6, Months), settlement_days, USDCurrency(), calendar, Actual360()) spread = SimpleQuote(0) fwdStart = Period(0, Days) for m, period, rate in swapData: helper = SwapRateHelper.from_tenor( SimpleQuote(rate / 100), Period(m, Years), calendar, Annual, Unadjusted, Thirty360(), liborIndex, spread, fwdStart, ) rate_helpers.append(helper) ts_day_counter = ActualActual(ISDA) tolerance = 1.0e-15 ts = PiecewiseYieldCurve.from_reference_date( BootstrapTrait.Discount, Interpolator.LogLinear, settlement_date, rate_helpers, ts_day_counter, tolerance ) self.assertEqual(settlement_date, ts.reference_date) # this is not a real test ... self.assertAlmostEqual(0.9103, ts.discount(calendar.advance(todays_date, 2, Years)), 3) self.assertAlmostEqual(0.7836, ts.discount(calendar.advance(todays_date, 5, Years)), 3) self.assertAlmostEqual(0.5827, ts.discount(calendar.advance(todays_date, 10, Years)), 3) self.assertAlmostEqual(0.4223, ts.discount(calendar.advance(todays_date, 15, Years)), 3)
def test_deposit_swap(self): settings = Settings() # Market information calendar = TARGET() # must be a business day eval_date = calendar.adjust(today()) settings.evaluation_date = eval_date settlement_days = 2 settlement_date = calendar.advance(eval_date, settlement_days, Days) # must be a business day settlement_date = calendar.adjust(settlement_date); depositData = [[ 1, Months, 4.581 ], [ 2, Months, 4.573 ], [ 3, Months, 4.557 ], [ 6, Months, 4.496 ], [ 9, Months, 4.490 ]] swapData = [[ 1, Years, 4.54 ], [ 5, Years, 4.99 ], [ 10, Years, 5.47 ], [ 20, Years, 5.89 ], [ 30, Years, 5.96 ]] rate_helpers = [] end_of_month = True for m, period, rate in depositData: tenor = Period(m, Months) helper = DepositRateHelper(rate/100, tenor, settlement_days, calendar, ModifiedFollowing, end_of_month, Actual360()) rate_helpers.append(helper) liborIndex = Libor('USD Libor', Period(6, Months), settlement_days, USDCurrency(), calendar, Actual360()) spread = SimpleQuote(0) fwdStart = Period(0, Days) for m, period, rate in swapData: rate = SimpleQuote(rate/100) helper = SwapRateHelper(rate, Period(m, Years), calendar, Annual, Unadjusted, Thirty360(), liborIndex, spread, fwdStart) rate_helpers.append(helper) ts_day_counter = ActualActual(ISDA) tolerance = 1.0e-15 ts = term_structure_factory( 'discount', 'loglinear', settlement_date, rate_helpers, ts_day_counter, tolerance) # this is not a real test ... self.assertAlmostEquals(0.9103, ts.discount(calendar.advance(today(), 2, Years)),3) self.assertAlmostEquals(0.7836, ts.discount(calendar.advance(today(), 5, Years)),3) self.assertAlmostEquals(0.5827, ts.discount(calendar.advance(today(), 10, Years)),3) self.assertAlmostEquals(0.4223, ts.discount(calendar.advance(today(), 15, Years)),3)
from quantlib.time.schedule import Schedule, Backward from quantlib.settings import Settings from quantlib.termstructures.yields.api import ( FlatForward, YieldTermStructure ) todays_date = Date(25, August, 2011) settings = Settings.instance() settings.evaluation_date = todays_date calendar = TARGET() effective_date = Date(10, Jul, 2006) termination_date = calendar.advance( effective_date, 10, Years, convention=Unadjusted ) settlement_days = 3 face_amount = 100.0 coupon_rate = 0.05 redemption = 100.0 fixed_bond_schedule = Schedule.from_rule( effective_date, termination_date, Period(Annual), calendar, ModifiedFollowing, ModifiedFollowing,
def test_display(self): settings = Settings() # Date setup calendar = TARGET() # Settlement date settlement_date = calendar.adjust(Date(28, January, 2011)) # Evaluation date fixing_days = 1 settlement_days = 1 todays_date = calendar.advance( settlement_date, -fixing_days, Days ) settings.evaluation_date = todays_date # Bound attributes face_amount = 100.0 redemption = 100.0 issue_date = Date(27, January, 2011) maturity_date = Date(31, August, 2020) coupon_rate = 0.03625 bond_yield = 0.034921 flat_discounting_term_structure = YieldTermStructure() flat_term_structure = FlatForward( reference_date = settlement_date, forward = bond_yield, daycounter = Actual365Fixed(), #actual_actual.ActualActual(actual_actual.Bond), compounding = Compounded, frequency = Semiannual) # have a look at the FixedRateBondHelper to simplify this # construction flat_discounting_term_structure.link_to(flat_term_structure) #Rate fixed_bond_schedule = Schedule( issue_date, maturity_date, Period(Semiannual), UnitedStates(market=GOVERNMENTBOND), Unadjusted, Unadjusted, Backward, False); bond = FixedRateBond( settlement_days, face_amount, fixed_bond_schedule, [coupon_rate], ActualActual(Bond), Unadjusted, redemption, issue_date ) d=bf.startDate(bond) zspd=bf.zSpread(bond, 100.0, flat_term_structure, Actual365Fixed(), Compounded, Semiannual, settlement_date, 1e-6, 100, 0.5) #Also need a test case for a PiecewiseTermStructure... depositData = [[ 1, Months, 4.581 ], [ 2, Months, 4.573 ], [ 3, Months, 4.557 ], [ 6, Months, 4.496 ], [ 9, Months, 4.490 ]] swapData = [[ 1, Years, 4.54 ], [ 5, Years, 4.99 ], [ 10, Years, 5.47 ], [ 20, Years, 5.89 ], [ 30, Years, 5.96 ]] rate_helpers = [] end_of_month = True for m, period, rate in depositData: tenor = Period(m, Months) helper = DepositRateHelper(SimpleQuote(rate/100), tenor, settlement_days, calendar, ModifiedFollowing, end_of_month, Actual360()) rate_helpers.append(helper) liborIndex = Libor('USD Libor', Period(6, Months), settlement_days, USDCurrency(), calendar, Actual360(), YieldTermStructure(relinkable=False)) spread = SimpleQuote(0) fwdStart = Period(0, Days) for m, period, rate in swapData: helper = SwapRateHelper.from_tenor( SimpleQuote(rate/100), Period(m, Years), calendar, Annual, Unadjusted, Thirty360(), liborIndex, spread, fwdStart ) rate_helpers.append(helper) ts_day_counter = ActualActual(ISDA) tolerance = 1.0e-15 ts = PiecewiseYieldCurve.from_reference_date( BootstrapTrait.Discount, Interpolator.LogLinear, settlement_date, rate_helpers, ts_day_counter, tolerance) pyc_zspd=bf.zSpread(bond, 102.0, ts, ActualActual(ISDA), Compounded, Semiannual, Date(1, April, 2015), 1e-6, 100, 0.5) pyc_zspd_disco=bf.zSpread(bond, 95.0, ts, ActualActual(ISDA), Compounded, Semiannual, settlement_date, 1e-6, 100, 0.5) yld = bf.yld(bond, 102.0, ActualActual(ISDA), Compounded, Semiannual, settlement_date, 1e-6, 100, 0.5) dur = bf.duration(bond, yld, ActualActual(ISDA), Compounded, Semiannual, 2, settlement_date) yld_disco = bf.yld(bond, 95.0, ActualActual(ISDA), Compounded, Semiannual, settlement_date, 1e-6, 100, 0.5) dur_disco = bf.duration(bond, yld_disco, ActualActual(ISDA), Compounded, Semiannual, 2, settlement_date) self.assertEqual(round(zspd, 6), 0.001281) self.assertEqual(round(pyc_zspd, 4), -0.0264) self.assertEqual(round(pyc_zspd_disco, 4), -0.0114) self.assertEqual(round(yld, 4), 0.0338) self.assertEqual(round(yld_disco, 4), 0.0426) self.assertEqual(round(dur, 4), 8.0655) self.assertEqual(round(dur_disco, 4), 7.9702)
def test_swap_from_market(self): """ Test that a swap with fixed coupon = fair rate has an NPV=0 Create from market """ eval_date = Date(2, January, 2014) settings = Settings() settings.evaluation_date = eval_date calendar = TARGET() settlement_date = calendar.advance(eval_date, 2, Days) # must be a business day settlement_date = calendar.adjust(settlement_date) length = 5 fixed_rate = .05 floating_spread = 0.0 m = libor_market('USD(NY)') quotes = [('DEP', '1W', SimpleQuote(0.0382)), ('DEP', '1M', SimpleQuote(0.0372)), ('DEP', '3M', SimpleQuote(0.0363)), ('DEP', '6M', SimpleQuote(0.0353)), ('DEP', '9M', SimpleQuote(0.0348)), ('DEP', '1Y', SimpleQuote(0.0345)), ('SWAP', '2Y', SimpleQuote(0.037125)), ('SWAP', '3Y', SimpleQuote(0.0398)), ('SWAP', '5Y', SimpleQuote(0.0443)), ('SWAP', '10Y', SimpleQuote(0.05165)), ('SWAP', '15Y', SimpleQuote(0.055175))] m.set_quotes(eval_date, quotes) m.bootstrap_term_structure() dt = Date(2, January, 2015) df = m.discount(dt) print('discount factor for %s (USD Libor): %f' % (dt, df)) swap = m.create_fixed_float_swap(settlement_date, length, fixed_rate, floating_spread) fixed_l = swap.fixed_leg float_l = swap.floating_leg f = swap.fair_rate print('fair rate: %f' % f) p = swap.net_present_value print('NPV: %f' % p) fixed_npv = swap.fixed_leg_npv float_npv = swap.floating_leg_npv # verify calculation by discounting both legs tot = 0.0 for frc in fixed_l: df = m.discount(frc.date) tot += frc.amount * df print('fixed npv: %f discounted cf: %f' % (fixed_npv, tot)) self.assertAlmostEqual(fixed_npv, -tot) tot = 0.0 for ic in float_l: df = m.discount(ic.date) tot += ic.amount * df print('float npv: %f discounted cf: %f' % (float_npv, tot)) self.assertAlmostEqual(float_npv, tot)
def test_swap_QL(self): """ Test that a swap with fixed coupon = fair rate has an NPV=0 Create from QL objects """ nominal = 100.0 fixedConvention = Unadjusted floatingConvention = ModifiedFollowing fixedFrequency = Annual floatingFrequency = Semiannual fixedDayCount = Thirty360() floatDayCount = Thirty360() calendar = TARGET() settlement_days = 2 eval_date = Date(2, January, 2014) settings = Settings() settings.evaluation_date = eval_date settlement_date = calendar.advance(eval_date, settlement_days, Days) # must be a business day settlement_date = calendar.adjust(settlement_date) termStructure = YieldTermStructure(relinkable=True) termStructure.link_to( FlatForward(settlement_date, 0.05, Actual365Fixed())) index = Libor('USD Libor', Period(6, Months), settlement_days, USDCurrency(), calendar, Actual360(), termStructure) length = 5 fixedRate = .05 floatingSpread = 0.0 maturity = calendar.advance(settlement_date, length, Years, convention=floatingConvention) fixedSchedule = Schedule(settlement_date, maturity, Period(fixedFrequency), calendar, fixedConvention, fixedConvention, Rule.Forward, False) floatSchedule = Schedule(settlement_date, maturity, Period(floatingFrequency), calendar, floatingConvention, floatingConvention, Rule.Forward, False) engine = DiscountingSwapEngine(termStructure, False, settlement_date, settlement_date) for swap_type in [Payer, Receiver]: swap = VanillaSwap(swap_type, nominal, fixedSchedule, fixedRate, fixedDayCount, floatSchedule, index, floatingSpread, floatDayCount, fixedConvention) swap.set_pricing_engine(engine) fixed_leg = swap.fixed_leg floating_leg = swap.floating_leg f = swap.fair_rate print('fair rate: %f' % f) p = swap.net_present_value print('NPV: %f' % p) swap = VanillaSwap(swap_type, nominal, fixedSchedule, f, fixedDayCount, floatSchedule, index, floatingSpread, floatDayCount, fixedConvention) swap.set_pricing_engine(engine) p = swap.net_present_value print('NPV: %f' % p) self.assertAlmostEqual(p, 0)
def get_term_structure(df_libor, dtObs): settings = Settings() # Market information calendar = TARGET() # must be a business day eval_date = calendar.adjust(dateToDate(dtObs)) settings.evaluation_date = eval_date settlement_days = 2 settlement_date = calendar.advance(eval_date, settlement_days, Days) # must be a business day settlement_date = calendar.adjust(settlement_date); depositData =[[1, Months, 'Libor1M'], [3, Months, 'Libor3M'], [6, Months, 'Libor6M']] swapData = [[ 1, Years, 'Swap1Y'], [ 2, Years, 'Swap2Y'], [ 3, Years, 'Swap3Y'], [ 4, Years, 'Swap4Y'], [ 5, Years, 'Swap5Y'], [ 7, Years, 'Swap7Y'], [ 10, Years,'Swap10Y'], [ 30, Years,'Swap30Y']] rate_helpers = [] end_of_month = True for m, period, label in depositData: tenor = Period(m, Months) rate = df_libor.get_value(dtObs, label) helper = DepositRateHelper(float(rate/100), tenor, settlement_days, calendar, ModifiedFollowing, end_of_month, Actual360()) rate_helpers.append(helper) endOfMonth = True liborIndex = Libor('USD Libor', Period(6, Months), settlement_days, USDCurrency(), calendar, ModifiedFollowing, endOfMonth, Actual360()) spread = SimpleQuote(0) fwdStart = Period(0, Days) for m, period, label in swapData: rate = df_libor.get_value(dtObs, label) helper = SwapRateHelper(SimpleQuote(rate/100), Period(m, Years), calendar, Annual, Unadjusted, Thirty360(), liborIndex, spread, fwdStart) rate_helpers.append(helper) ts_day_counter = ActualActual(ISDA) tolerance = 1.0e-15 ts = term_structure_factory('discount', 'loglinear', settlement_date, rate_helpers, ts_day_counter, tolerance) return ts
def test_zero_curve(self): try: settings = Settings() calendar = TARGET() # must be a business Days dtObs = date(2007, 4, 27) eval_date = calendar.adjust(pydate_to_qldate(dtObs)) settings.evaluation_date = eval_date settlement_days = 2 settlement_date = calendar.advance(eval_date, settlement_days, Days) # must be a business day settlement_date = calendar.adjust(settlement_date) print('dt Obs: %s\ndt Eval: %s\ndt Settle: %s' % (dtObs, eval_date, settlement_date)) depositData = [[1, Months, 'Libor1M', 5.32], [3, Months, 'Libor3M', 5.35], [6, Months, 'Libor6M', 5.35]] swapData = [[1, Years, 'Swap1Y', 5.31], [2, Years, 'Swap2Y', 5.06], [3, Years, 'Swap3Y', 5.00], [4, Years, 'Swap4Y', 5.01], [5, Years, 'Swap5Y', 5.04], [7, Years, 'Swap7Y', 5.12], [10, Years, 'Swap10Y', 5.22], [30, Years, 'Swap30Y', 5.44]] rate_helpers = [] end_of_month = True for m, period, label, rate in depositData: tenor = Period(m, Months) helper = DepositRateHelper(SimpleQuote(rate / 100.0), tenor, settlement_days, calendar, ModifiedFollowing, end_of_month, Actual360()) rate_helpers.append(helper) liborIndex = Libor('USD Libor', Period(3, Months), settlement_days, USDCurrency(), calendar, Actual360()) spread = SimpleQuote(0) fwdStart = Period(0, Days) for m, period, label, rate in swapData: helper = SwapRateHelper.from_tenor(SimpleQuote(rate / 100.0), Period(m, Years), calendar, Semiannual, ModifiedFollowing, Thirty360(), liborIndex, spread, fwdStart) rate_helpers.append(helper) ts_day_counter = ActualActual(ISDA) tolerance = 1.0e-2 ts = PiecewiseYieldCurve('discount', 'loglinear', settlement_date, rate_helpers, ts_day_counter, tolerance) # max_date raises an exception... dtMax = ts.max_date print('max date: %s' % dtMax) except RuntimeError as e: print('Exception (expected):\n%s' % e) self.assertTrue(True) except Exception: self.assertFalse()
def get_term_structure(df_libor, dtObs): settings = Settings() # Market information calendar = TARGET() # must be a business day eval_date = calendar.adjust(dateToDate(dtObs)) settings.evaluation_date = eval_date settlement_days = 2 settlement_date = calendar.advance(eval_date, settlement_days, Days) # must be a business day settlement_date = calendar.adjust(settlement_date) depositData = [[1, Months, 'Libor1M'], [3, Months, 'Libor3M'], [6, Months, 'Libor6M']] swapData = [[1, Years, 'Swap1Y'], [2, Years, 'Swap2Y'], [3, Years, 'Swap3Y'], [4, Years, 'Swap4Y'], [5, Years, 'Swap5Y'], [7, Years, 'Swap7Y'], [10, Years, 'Swap10Y'], [30, Years, 'Swap30Y']] rate_helpers = [] end_of_month = True for m, period, label in depositData: tenor = Period(m, Months) rate = df_libor.get_value(dtObs, label) helper = DepositRateHelper(SimpleQuote(rate / 100.0), tenor, settlement_days, calendar, ModifiedFollowing, end_of_month, Actual360()) rate_helpers.append(helper) liborIndex = Libor('USD Libor', Period(3, Months), settlement_days, USDCurrency(), calendar, Actual360()) spread = SimpleQuote(0) fwdStart = Period(0, Days) for m, period, label in swapData: rate = df_libor.get_value(dtObs, label) helper = SwapRateHelper.from_tenor( SimpleQuote(rate / 100.0), Period(m, Years), calendar, Semiannual, ModifiedFollowing, Thirty360(), liborIndex, spread, fwdStart) rate_helpers.append(helper) ts_day_counter = ActualActual(ISDA) tolerance = 1.0e-15 ts = PiecewiseYieldCurve('discount', 'loglinear', settlement_date, rate_helpers, ts_day_counter, tolerance) return ts
helpers = depositHelpers[:3] + fraHelpers + swapHelpers depoFraSwapCurve = PiecewiseYieldCurve.from_reference_date( BootstrapTrait.Discount, Interpolator.LogLinear, settlementDate, helpers, ts_daycounter ) # Term structures that will be used for pricing: discountTermStructure = YieldTermStructure() forecastTermStructure = YieldTermStructure() ### SWAPS TO BE PRICED nominal = 1000000 length = 5 maturity = calendar.advance(settlementDate,length,Years) payFixed = True fixedLegFrequency = Annual fixedLegAdjustment = Unadjusted fixedLegDayCounter = Thirty360() fixedRate = 0.04 floatingLegFrequency = Semiannual spread = 0.0 fixingDays = 2 index = Euribor6M(forecastTermStructure) floatingLegAdjustment = ModifiedFollowing floatingLegDayCounter = index.day_counter fixedSchedule = Schedule.from_rule(settlementDate, maturity,
def test_zero_curve(self): try: settings = Settings() calendar = TARGET() # must be a business Days dtObs = date(2007, 4, 27) eval_date = calendar.adjust(pydate_to_qldate(dtObs)) settings.evaluation_date = eval_date settlement_days = 2 settlement_date = calendar.advance(eval_date, settlement_days, Days) # must be a business day settlement_date = calendar.adjust(settlement_date) print('dt Obs: %s\ndt Eval: %s\ndt Settle: %s' % (dtObs, eval_date, settlement_date)) depositData = [[1, Months, 'Libor1M', 5.32], [3, Months, 'Libor3M', 5.35], [6, Months, 'Libor6M', 5.35]] swapData = [[1, Years, 'Swap1Y', 5.31], [2, Years, 'Swap2Y', 5.06], [3, Years, 'Swap3Y', 5.00], [4, Years, 'Swap4Y', 5.01], [5, Years, 'Swap5Y', 5.04], [7, Years, 'Swap7Y', 5.12], [10, Years, 'Swap10Y', 5.22], [30, Years, 'Swap30Y', 5.44]] rate_helpers = [] end_of_month = True for m, period, label, rate in depositData: tenor = Period(m, Months) helper = DepositRateHelper(SimpleQuote(rate / 100.0), tenor, settlement_days, calendar, ModifiedFollowing, end_of_month, Actual360()) rate_helpers.append(helper) liborIndex = Libor('USD Libor', Period(3, Months), settlement_days, USDCurrency(), calendar, Actual360()) spread = SimpleQuote(0) fwdStart = Period(0, Days) for m, period, label, rate in swapData: helper = SwapRateHelper.from_tenor( SimpleQuote(rate / 100.0), Period(m, Years), calendar, Semiannual, ModifiedFollowing, Thirty360(), liborIndex, spread, fwdStart) rate_helpers.append(helper) ts_day_counter = ActualActual(ISDA) tolerance = 1.0e-2 ts = PiecewiseYieldCurve('discount', 'loglinear', settlement_date, rate_helpers, ts_day_counter, tolerance) # max_date raises an exception... dtMax = ts.max_date print('max date: %s' % dtMax) except RuntimeError as e: print('Exception (expected):\n%s' % e) self.assertTrue(True) except Exception: self.assertFalse()
def test_deposit_swap(self): settings = Settings() # Market information calendar = TARGET() todays_date = Date(1, Mar, 2012) # must be a business day eval_date = calendar.adjust(todays_date) settings.evaluation_date = eval_date settlement_days = 2 settlement_date = calendar.advance(eval_date, settlement_days, Days) # must be a business day settlement_date = calendar.adjust(settlement_date); depositData = [[ 1, Months, 4.581 ], [ 2, Months, 4.573 ], [ 3, Months, 4.557 ], [ 6, Months, 4.496 ], [ 9, Months, 4.490 ]] swapData = [[ 1, Years, 4.54 ], [ 5, Years, 4.99 ], [ 10, Years, 5.47 ], [ 20, Years, 5.89 ], [ 30, Years, 5.96 ]] rate_helpers = [] end_of_month = True for m, period, rate in depositData: tenor = Period(m, Months) helper = DepositRateHelper(SimpleQuote(rate/100), tenor, settlement_days, calendar, ModifiedFollowing, end_of_month, Actual360()) rate_helpers.append(helper) liborIndex = Libor( 'USD Libor', Period(6, Months), settlement_days, USDCurrency(), calendar, Actual360() ) spread = SimpleQuote(0) fwdStart = Period(0, Days) for m, period, rate in swapData: helper = SwapRateHelper.from_tenor( SimpleQuote(rate/100), Period(m, Years), calendar, Annual, Unadjusted, Thirty360(), liborIndex, spread, fwdStart ) rate_helpers.append(helper) ts_day_counter = ActualActual(ISDA) tolerance = 1.0e-15 ts = PiecewiseYieldCurve.from_reference_date( BootstrapTrait.Discount, Interpolator.LogLinear, settlement_date, rate_helpers, ts_day_counter, tolerance ) self.assertEqual(settlement_date, ts.reference_date) # this is not a real test ... self.assertAlmostEqual(0.9103, ts.discount(calendar.advance(todays_date, 2, Years)),3) self.assertAlmostEqual(0.7836, ts.discount(calendar.advance(todays_date, 5, Years)),3) self.assertAlmostEqual(0.5827, ts.discount(calendar.advance(todays_date, 10, Years)),3) self.assertAlmostEqual(0.4223, ts.discount(calendar.advance(todays_date, 15, Years)),3)