def zbt_libor_yield(instruments, yields, pricing_date, basis='Actual/Actual (Bond)', compounding_freq='Continuous', maturity_dates=None): """ Bootstrap a zero-coupon curve from libor rates and swap yields Args: instruments: list of instruments, of the form Libor?M for Libor rates and Swap?Y for swap rates yields: market rates pricing_date: the date where market data is observed. Settlement is by default 2 days after pricing_date Optional: compounding_frequency: ... of zero-coupon rates. By default: 'Continuous' Returns: zero_rate: zero-coupon rate maturity_date: ... of corresponding rate """ calendar = TARGET() settings = Settings() # must be a business day eval_date = calendar.adjust(pydate_to_qldate(pricing_date)) settings.evaluation_date = eval_date rates = dict(zip(instruments, yields)) ts = make_term_structure(rates, pricing_date) cnt = DayCounter.from_name(basis) if maturity_dates is None: # schedule of maturity dates from settlement date to last date on # the term structure s = Schedule(effective_date=ts.reference_date, termination_date=ts.max_date, tenor=Period(1, Months), calendar=TARGET()) maturity_dates = [qldate_to_pydate(dt) for dt in s.dates()] cp_freq = Compounding[compounding_freq] zc = [ ts.zero_rate(pydate_to_qldate(dt), day_counter=cnt, compounding=cp_freq).rate for dt in maturity_dates ] return (maturity_dates, zc)
class ScheduleMethodTestCase(unittest.TestCase): def setUp(self): self.from_date = Date(1, Jan, 2011) self.to_date = Date(31, Dec, 2011) self.tenor = Period(4, Weeks) self.calendar = UnitedKingdom() self.convention = Following self.termination_convention = Preceding self.rule = Twentieth self.schedule = Schedule(self.from_date, self.to_date, self.tenor, self.calendar, self.convention, self.termination_convention, self.rule) def test_size(self): self.assertEquals(15, self.schedule.size()) def test_dates(self): expected_dates_length = self.schedule.size() dates = list(self.schedule.dates()) self.assertEquals(expected_dates_length, len(dates)) def test_at(self): expected_date = self.calendar.adjust(self.from_date, Following) self.assertTrue(expected_date == self.schedule.at(0)) next_date = self.calendar.adjust(self.from_date + Period(4, Weeks), Following) expected_date = Date(20, next_date.month, next_date.year) self.assertTrue(expected_date == self.schedule.at(1)) def test_previous_next_reference_date(self): from_date = Date(3, Sep, 2011) to_date = Date(15, Dec, 2011) tenor = Period(1, Months) calendar = UnitedKingdom() convention = Following termination_convention = Following rule = Forward fwd_schedule = Schedule(from_date, to_date, tenor, calendar, convention, termination_convention, rule) expected_date = Date(5, Sep, 2011) self.assert_(expected_date == fwd_schedule.next_date(from_date)) rule = Backward bwd_schedule = Schedule(from_date, to_date, tenor, calendar, convention, termination_convention, rule) expected_date = Date(15, Nov, 2011) self.assert_(expected_date == bwd_schedule.previous_date(to_date))
def zbt_libor_yield(instruments, yields, pricing_date, basis='Actual/Actual (Bond)', compounding_freq='Continuous', maturity_dates=None): """ Bootstrap a zero-coupon curve from libor rates and swap yields Args: insruments: list of instruments, of the form Libor?M for Libor rates and Swap?Y for swap rates yields: market rates pricing_date: the date where market data is observed. Settlement is by default 2 days after pricing_date Optional: compounding_frequency: ... of zero-coupon rates. By default: 'Continuous' Returns: zero_rate: zero-coupon rate maturity_date: ... of corresponding rate """ calendar = TARGET() settings = Settings() # must be a business day eval_date = calendar.adjust(pydate_to_qldate(pricing_date)) settings.evaluation_date = eval_date rates = dict(zip(instruments, yields)) ts = make_term_structure(rates, pricing_date) cnt = DayCounter.from_name(basis) if maturity_dates is None: # schedule of maturity dates from settlement date to last date on # the term structure s = Schedule(effective_date=ts.reference_date, termination_date=ts.max_date, tenor=Period(1, Months), calendar=TARGET()) maturity_dates = [qldate_to_pydate(dt) for dt in s.dates()] cp_freq = compounding_from_name(compounding_freq) zc = [ts.zero_rate(date=pydate_to_qldate(dt), day_counter=cnt, compounding=cp_freq).rate for dt in maturity_dates] return (maturity_dates, zc)
def test_create_schedule(self): from_date = Date(1, Jan, 2011) to_date = Date(31, Dec, 2011) tenor = Period(3, Weeks) calendar = UnitedKingdom() convention = Following termination_convention = Following rule = Forward schedule = Schedule(from_date, to_date, tenor, calendar, convention, termination_convention, rule) for date in schedule.dates(): print date self.assert_(schedule is not None) # Constructor using the defaults for the different conventions schedule = Schedule(from_date, to_date, tenor, calendar) self.assert_(schedule is not None)
def test_create_schedule(self): from_date = Date(1, Jan, 2011) to_date = Date(31, Dec, 2011) tenor = Period(3, Weeks) calendar = UnitedKingdom() convention = Following termination_convention = Following rule = Forward schedule = Schedule(from_date, to_date, tenor, calendar, convention, termination_convention, rule) self.assertIsNotNone(schedule) for date in schedule.dates(): pass # Constructor using the defaults for the different conventions schedule = Schedule(from_date, to_date, tenor, calendar) self.assertIsNotNone(schedule)
class ScheduleMethodTestCase(unittest.TestCase): def setUp(self): self.from_date = Date(1, Jan, 2011) self.to_date = Date(31, Dec, 2011) self.tenor = Period(4, Weeks) self.calendar = UnitedKingdom() self.convention = Following self.termination_convention = Preceding self.rule = Twentieth self.schedule = Schedule( self.from_date, self.to_date, self.tenor, self.calendar, self.convention, self.termination_convention, self.rule ) def test_size(self): self.assertEquals(15, self.schedule.size()) def test_dates(self): expected_dates_length = self.schedule.size() dates = list(self.schedule.dates()) self.assertEquals(expected_dates_length, len(dates)) def test_iter_dates(self): expected_dates_length = self.schedule.size() dates= [date for date in self.schedule] self.assertEqual(expected_dates_length, len(dates)) def test_at(self): expected_date = self.calendar.adjust(self.from_date, Following) self.assertTrue(expected_date == self.schedule.at(0)) next_date = self.calendar.adjust( self.from_date + Period(4, Weeks), Following ) expected_date = Date(20, next_date.month, next_date.year) self.assertTrue(expected_date == self.schedule.at(1)) def test_previous_next_reference_date(self): from_date = Date(3, Sep, 2011) to_date = Date(15, Dec, 2011) tenor = Period(1, Months) calendar = UnitedKingdom() convention = Following termination_convention = Following rule = Forward fwd_schedule = Schedule(from_date, to_date, tenor, calendar, convention, termination_convention, rule) expected_date = Date(5, Sep, 2011) self.assert_(expected_date == fwd_schedule.next_date(from_date)) rule = Backward bwd_schedule = Schedule(from_date, to_date, tenor, calendar, convention, termination_convention, rule) expected_date = Date(15, Nov, 2011) self.assert_(expected_date == bwd_schedule.previous_date(to_date))