def create_fixed_float_swap(self, settlement_date, length, fixed_rate, floating_spread, **kwargs): """ Create a fixed-for-float swap given: - settlement date - length in years - additional arguments to modify market default parameters """ _params = self._params._replace(**kwargs) index = IborIndex.from_name(self._market, self._forecasting_term_structure, **kwargs) swap_type = Payer nominal = 100.0 fixed_convention = \ BusinessDayConvention.from_name(_params.fixed_leg_convention) floating_convention = \ BusinessDayConvention.from_name(_params.floating_leg_convention) fixed_frequency = \ code_to_frequency(_params.fixed_leg_period) floating_frequency = code_to_frequency(_params.floating_leg_period) fixed_daycount = DayCounter.from_name(_params.fixed_leg_daycount) float_daycount = DayCounter.from_name(_params.floating_leg_daycount) calendar = Calendar.from_name(_params.calendar) maturity = calendar.advance(settlement_date, length, Years, convention=floating_convention) fixed_schedule = Schedule(settlement_date, maturity, Period(fixed_frequency), calendar, fixed_convention, fixed_convention, Forward, False) float_schedule = Schedule(settlement_date, maturity, Period(floating_frequency), calendar, floating_convention, floating_convention, Forward, False) swap = VanillaSwap(swap_type, nominal, fixed_schedule, fixed_rate, fixed_daycount, float_schedule, index, floating_spread, float_daycount, fixed_convention) engine = DiscountingSwapEngine(self._discount_term_structure, False, settlementDate=settlement_date, npvDate=settlement_date) swap.set_pricing_engine(engine) return swap
def make_rate_helper(market, quote, reference_date=None): """ Wrapper for deposit and swaps rate helpers makers TODO: class method of RateHelper? """ rate_type, tenor, quote_value = quote if(rate_type == 'SWAP'): libor_index = market._floating_rate_index spread = SimpleQuote(0) fwdStart = Period(0, Days) helper = SwapRateHelper.from_tenor( quote_value, Period(tenor), market._floating_rate_index.fixing_calendar, code_to_frequency(market._params.fixed_leg_period), BusinessDayConvention.from_name( market._params.fixed_leg_convention), DayCounter.from_name(market._params.fixed_leg_daycount), libor_index, spread, fwdStart) elif(rate_type == 'DEP'): end_of_month = True helper = DepositRateHelper( quote_value, Period(tenor), market._params.settlement_days, market._floating_rate_index.fixing_calendar, market._floating_rate_index.business_day_convention, end_of_month, DayCounter.from_name(market._deposit_daycount)) elif(rate_type == 'ED'): if reference_date is None: raise Exception("Reference date needed with ED Futures data") forward_date = next_imm_date(reference_date, tenor) helper = FuturesRateHelper( rate = SimpleQuote(quote_value), imm_date = qldate_from_pydate(forward_date), length_in_months = 3, calendar = market._floating_rate_index.fixing_calendar, convention = market._floating_rate_index.business_day_convention, end_of_month = True, day_counter = DayCounter.from_name(market._params.floating_leg_daycount)) else: raise Exception("Rate type %s not supported" % rate_type) return (helper)
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)
def test_thirty360_from_name(self): for c in Convention: dc = Thirty360(c) try: dc2 = DayCounter.from_name(dc.name) except ValueError: pass else: self.assertEqual(dc, dc2)
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 _cfamounts(coupon_rate, pricing_date, maturity_date, period, basis): """ cash flow schedule """ _period = str_to_frequency(period) evaluation_date = pydate_to_qldate(pricing_date) settings = Settings() settings.evaluation_date = evaluation_date calendar = TARGET() termination_date = pydate_to_qldate(maturity_date) # effective date must be before settlement date, but do not # care about exact issuance date of bond effective_date = Date(termination_date.day, termination_date.month, evaluation_date.year) effective_date = calendar.advance( effective_date, -1, Years, convention=Unadjusted) face_amount = 100.0 redemption = 100.0 fixed_bond_schedule = Schedule( effective_date, termination_date, Period(_period), calendar, ModifiedFollowing, ModifiedFollowing, Backward ) issue_date = effective_date cnt = DayCounter.from_name(basis) settlement_days = 2 bond = FixedRateBond( settlement_days, face_amount, fixed_bond_schedule, [coupon_rate], cnt, Following, redemption, issue_date) res = zip(*bond.cashflows) return(res)
def test_create_simple_daycounter_from_name(self): type_vs_name = { 'Actual360': 'Actual/360', 'Actual/360': 'Actual/360', 'Actual365Fixed': 'Actual/365 (Fixed)', 'Actual/365': 'Actual/365 (Fixed)', 'OneDayCounter': '1/1', '1/1': '1/1', } for counter_type, expected_name in type_vs_name.items(): cnt = DayCounter.from_name(counter_type) self.assertEquals(cnt.name(), expected_name)
def test_create_simple_daycounter_from_name(self): type_vs_name = { 'Actual360' : 'Actual/360', 'Actual/360' : 'Actual/360', 'Actual365Fixed' : 'Actual/365 (Fixed)', 'Actual/365' : 'Actual/365 (Fixed)', 'OneDayCounter' : '1/1', '1/1' : '1/1', } for counter_type, expected_name in type_vs_name.items(): cnt = DayCounter.from_name(counter_type) self.assertEquals(cnt.name(), expected_name)
def test_create_daycounter_with_convention_from_name(self): type_vs_name = { 'Actual/Actual (Bond)': 'Actual/Actual (ISMA)', 'Actual/Actual (ISMA)': 'Actual/Actual (ISMA)', 'Actual/Actual (ISDA)': 'Actual/Actual (ISDA)', 'Actual/Actual (Historical)': 'Actual/Actual (ISDA)', 'Actual/Actual (Actual365)': 'Actual/Actual (ISDA)', 'Actual/Actual (AFB)': 'Actual/Actual (AFB)', 'Actual/Actual (Euro)': 'Actual/Actual (AFB)', } for counter_type, expected_name in type_vs_name.items(): cnt = DayCounter.from_name(counter_type) self.assertEquals(cnt.name(), expected_name)
def test_create_daycounter_with_convention_from_name(self): type_vs_name = { 'Actual/Actual (Bond)' : 'Actual/Actual (ISMA)', 'Actual/Actual (ISMA)' : 'Actual/Actual (ISMA)', 'Actual/Actual (ISDA)' : 'Actual/Actual (ISDA)', 'Actual/Actual (Historical)' : 'Actual/Actual (ISDA)', 'Actual/Actual (Actual365)' : 'Actual/Actual (ISDA)', 'Actual/Actual (AFB)' : 'Actual/Actual (AFB)', 'Actual/Actual (Euro)' : 'Actual/Actual (AFB)', } for counter_type, expected_name in type_vs_name.items(): cnt = DayCounter.from_name(counter_type) self.assertEquals(cnt.name(), expected_name)
def bootstrap_term_structure(self): tolerance = 1.0e-15 settings = Settings() calendar = JointCalendar(UnitedStates(), UnitedKingdom()) # must be a business day eval_date = self._eval_date settings.evaluation_date = eval_date settlement_days = self._params.settlement_days settlement_date = calendar.advance(eval_date, settlement_days, Days) # must be a business day settlement_date = calendar.adjust(settlement_date) ts = term_structure_factory( 'discount', 'loglinear', settlement_date, self._rate_helpers, DayCounter.from_name(self._termstructure_daycount), tolerance) self._term_structure = ts self._discount_term_structure = YieldTermStructure(relinkable=True) self._discount_term_structure.link_to(ts) self._forecasting_term_structure = YieldTermStructure(relinkable=True) self._forecasting_term_structure.link_to(ts) return 0
def make_eurobond_helper( market, clean_price, coupons, tenor, issue_date, maturity): """ Wrapper for bond helpers. FIXME: This convenience method has some conventions specifically hardcoded for Eurobonds. These should be moved to the market. """ # Create schedule based on market and bond parameters. index = market._floating_rate_index schedule = Schedule( issue_date, maturity, Period(tenor), index.fixing_calendar, index.business_day_convention, index.business_day_convention, Backward, # Date generation rule index.end_of_month, ) daycounter = DayCounter.from_name("Actual/Actual (Bond)") helper = FixedRateBondHelper( SimpleQuote(clean_price), market._params.settlement_days, 100.0, schedule, coupons, daycounter, Following, # Payment convention 100.0, issue_date) return helper
def _bndprice(bond_yield, coupon_rate, pricing_date, maturity_date, period, basis, compounding_frequency): """ Clean price and accrued interest of a bond """ _period = str_to_frequency(period) evaluation_date = pydate_to_qldate(pricing_date) settings = Settings() settings.evaluation_date = evaluation_date calendar = TARGET() termination_date = pydate_to_qldate(maturity_date) # effective date must be before settlement date, but do not # care about exact issuance date of bond effective_date = Date(termination_date.day, termination_date.month, evaluation_date.year) effective_date = calendar.advance(effective_date, -1, Years, convention=Unadjusted) settlement_date = calendar.advance(evaluation_date, 2, Days, convention=ModifiedFollowing) face_amount = 100.0 redemption = 100.0 fixed_bond_schedule = Schedule(effective_date, termination_date, Period(_period), calendar, ModifiedFollowing, ModifiedFollowing, Backward) issue_date = effective_date cnt = DayCounter.from_name(basis) settlement_days = 2 bond = FixedRateBond(settlement_days, face_amount, fixed_bond_schedule, [coupon_rate], cnt, Following, redemption, issue_date) discounting_term_structure = YieldTermStructure(relinkable=True) cnt_yield = DayCounter.from_name('Actual/Actual (Historical)') flat_term_structure = FlatForward(settlement_days=2, forward=bond_yield, calendar=NullCalendar(), daycounter=cnt_yield, compounding=Compounded, frequency=_period) discounting_term_structure.link_to(flat_term_structure) engine = DiscountingBondEngine(discounting_term_structure) bond.set_pricing_engine(engine) price = bond.clean_price ac = bond.accrued_amount(pydate_to_qldate(settlement_date)) return (price, ac)
def test_empty_daycounter(self): day_counter = DayCounter() with self.assertRaisesRegexp(RuntimeError, 'no implementation provided'): day_counter.name
def _bndprice(bond_yield, coupon_rate, pricing_date, maturity_date, period, basis, compounding_frequency): """ Clean price and accrued interest of a bond """ _period = str_to_frequency(period) evaluation_date = pydate_to_qldate(pricing_date) settings = Settings() settings.evaluation_date = evaluation_date calendar = TARGET() termination_date = pydate_to_qldate(maturity_date) # effective date must be before settlement date, but do not # care about exact issuance date of bond effective_date = Date(termination_date.day, termination_date.month, evaluation_date.year) effective_date = calendar.advance( effective_date, -1, Years, convention=Unadjusted) settlement_date = calendar.advance( evaluation_date, 2, Days, convention=ModifiedFollowing) face_amount = 100.0 redemption = 100.0 fixed_bond_schedule = Schedule( effective_date, termination_date, Period(_period), calendar, ModifiedFollowing, ModifiedFollowing, Backward ) issue_date = effective_date cnt = DayCounter.from_name(basis) settlement_days = 2 bond = FixedRateBond( settlement_days, face_amount, fixed_bond_schedule, [coupon_rate], cnt, Following, redemption, issue_date ) discounting_term_structure = YieldTermStructure(relinkable=True) cnt_yield = DayCounter.from_name('Actual/Actual (Historical)') flat_term_structure = FlatForward( settlement_days=2, forward=bond_yield, calendar=NullCalendar(), daycounter=cnt_yield, compounding=Compounded, frequency=_period) discounting_term_structure.link_to(flat_term_structure) engine = DiscountingBondEngine(discounting_term_structure) bond.set_pricing_engine(engine) price = bond.clean_price ac = bond.accrued_amount(pydate_to_qldate(settlement_date)) return (price, ac)
def make_rate_helper(market, quote, reference_date=None): """ Wrapper for deposit and swaps rate helpers makers TODO: class method of RateHelper? """ rate_type, tenor, quote_value = quote if(rate_type == 'SWAP'): libor_index = market._floating_rate_index spread = SimpleQuote(0) fwdStart = Period(0, Days) helper = SwapRateHelper.from_tenor( quote_value, Period(tenor), market._floating_rate_index.fixing_calendar, code_to_frequency(market._params.fixed_leg_period), BusinessDayConvention.from_name( market._params.fixed_leg_convention), DayCounter.from_name(market._params.fixed_leg_daycount), libor_index, spread, fwdStart) elif(rate_type == 'DEP'): end_of_month = True helper = DepositRateHelper( quote_value, Period(tenor), market._params.settlement_days, market._floating_rate_index.fixing_calendar, market._floating_rate_index.business_day_convention, end_of_month, DayCounter.from_name(market._deposit_daycount)) elif(rate_type == 'ED'): if reference_date is None: raise Exception("Reference date needed with ED Futures data") forward_date = next_imm_date(reference_date, tenor) helper = FuturesRateHelper( rate =SimpleQuote(quote_value), imm_date = qldate_from_pydate(forward_date), length_in_months = 3, calendar = market._floating_rate_index.fixing_calendar, convention = market._floating_rate_index.business_day_convention, end_of_month = True, day_counter = DayCounter.from_name( market._params.floating_leg_daycount)) elif rate_type.startswith('ER'): # TODO For Euribor futures, we found it useful to supply the `imm_date` # parameter directly, instead of as a number of periods from the # evaluation date, as for ED futures. To achieve this, we pass the # `imm_date` in the `tenor` field of the quote. helper = FuturesRateHelper( rate=SimpleQuote(quote_value), imm_date=tenor, length_in_months=3, calendar=market._floating_rate_index.fixing_calendar, convention=market._floating_rate_index.business_day_convention, end_of_month=True, day_counter=DayCounter.from_name( market._params.floating_leg_daycount)) else: raise Exception("Rate type %s not supported" % rate_type) return helper