def test_relinkable_structures(self): discounting_term_structure = YieldTermStructure(relinkable=True) settlement_days = 3 flat_term_structure = FlatForward(settlement_days=settlement_days, forward=0.044, calendar=NullCalendar(), daycounter=Actual360()) discounting_term_structure.link_to(flat_term_structure) evaluation_date = Settings().evaluation_date +100 self.assertEquals( flat_term_structure.discount(evaluation_date), discounting_term_structure.discount(evaluation_date) ) another_flat_term_structure = FlatForward(settlement_days=10, forward=0.067, calendar=NullCalendar(), daycounter=Actual365Fixed()) discounting_term_structure.link_to(another_flat_term_structure) self.assertEquals( another_flat_term_structure.discount(evaluation_date), discounting_term_structure.discount(evaluation_date) ) self.assertNotEquals( flat_term_structure.discount(evaluation_date), discounting_term_structure.discount(evaluation_date) )
def test_excel_example_with_zero_coupon_bond(self): todays_date = Date(25, August, 2011) settlement_days = 3 face_amount = 100 calendar = TARGET() maturity_date = Date(26, February, 2024) bond = ZeroCouponBond( settlement_days, calendar, face_amount, maturity_date, Following, 100., todays_date ) discounting_term_structure = YieldTermStructure(relinkable=True) flat_term_structure = FlatForward( settlement_days = 1, forward = 0.044, calendar = NullCalendar(), daycounter = Actual365Fixed(), compounding = Continuous, frequency = Annual) discounting_term_structure.link_to(flat_term_structure) engine = DiscountingBondEngine(discounting_term_structure) bond.set_pricing_engine(engine) self.assertEquals( calendar.advance(todays_date, 3, Days), bond.settlement_date() ) self.assertEquals(0., bond.accrued_amount(bond.settlement_date())) self.assertAlmostEquals(57.6915, bond.clean_price, 4)
def test_create_libor_index(self): settings = Settings.instance() # Market information calendar = UnitedStates(LiborImpact) # 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('USDLibor', Period(6, Months), settlement_days, USDCurrency(), calendar, Actual360(), term_structure) default_libor = USDLibor(Period(6, Months)) for attribute in [ "business_day_convention", "end_of_month", "fixing_calendar", "joint_calendar", "tenor", "fixing_days", "day_counter", "family_name", "name" ]: self.assertEqual(getattr(index, attribute), getattr(default_libor, attribute))
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_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_excel_example_with_zero_coupon_bond(self): todays_date = Date(25, August, 2011) settlement_days = 3 face_amount = 100 calendar = TARGET() maturity_date = Date(26, February, 2024) bond = ZeroCouponBond( settlement_days, calendar, face_amount, maturity_date, Following, 100., todays_date ) discounting_term_structure = YieldTermStructure(relinkable=True) flat_term_structure = FlatForward( settlement_days = 1, forward = 0.044, calendar = NullCalendar(), daycounter = Actual365Fixed(), compounding = Continuous, frequency = Annual) discounting_term_structure.link_to(flat_term_structure) bond.set_pricing_engine(discounting_term_structure) self.assertEquals( calendar.advance(todays_date, 3, Days), bond.settlement_date() ) self.assertEquals(0., bond.accrued_amount(bond.settlement_date())) self.assertAlmostEquals(57.6915, bond.clean_price, 4)
def test_relinkable_structures(self): discounting_term_structure = YieldTermStructure(relinkable=True) settlement_days = 3 flat_term_structure = FlatForward(settlement_days=settlement_days, forward=0.044, calendar=NullCalendar(), daycounter=Actual360()) discounting_term_structure.link_to(flat_term_structure) evaluation_date = Settings().evaluation_date +100 self.assertEqual( flat_term_structure.discount(evaluation_date), discounting_term_structure.discount(evaluation_date) ) another_flat_term_structure = FlatForward(settlement_days=10, forward=0.067, calendar=NullCalendar(), daycounter=Actual365Fixed()) discounting_term_structure.link_to(another_flat_term_structure) self.assertEqual( another_flat_term_structure.discount(evaluation_date), discounting_term_structure.discount(evaluation_date) ) self.assertNotEqual( flat_term_structure.discount(evaluation_date), discounting_term_structure.discount(evaluation_date) )
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_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_create_libor_index(self): settings = Settings.instance() # Market information calendar = UnitedStates(LiborImpact) # 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('USDLibor', Period(6, Months), settlement_days, USDCurrency(), calendar, Actual360(), term_structure) default_libor = USDLibor(Period(6, Months)) for attribute in ["business_day_convention", "end_of_month", "fixing_calendar", "joint_calendar", "tenor", "fixing_days", "day_counter", "family_name", "name"]: self.assertEqual(getattr(index, attribute), getattr(default_libor, attribute))
def test_creation(self): settlement_date = Date(1, January, 2014) term_structure = YieldTermStructure() term_structure.link_to(FlatForward(settlement_date, 0.05, Actual365Fixed())) index = USDLibor(Period(3, Months), term_structure) self.assertEqual(index.name, 'USDLibor3M Actual/360')
def test_creation(self): settlement_date = Date(1, January, 2014) term_structure = YieldTermStructure() term_structure.link_to( FlatForward(settlement_date, 0.05, Actual365Fixed())) index = USDLibor(Period(3, Months), term_structure) self.assertEqual(index.name, 'USDLibor3M Actual/360')
def test_creation(self): settlement_date = Date(1, January, 2014) term_structure = YieldTermStructure() term_structure.link_to(FlatForward(settlement_date, 0.05, Actual365Fixed())) # Makes sure the constructor does not segfault anymore ;-) index = Euribor6M(term_structure) self.assertEqual(index.name, 'Euribor6M Actual/360')
def test_creation(self): settlement_date = Date(1, January, 2014) term_structure = YieldTermStructure(relinkable=True) term_structure.link_to( FlatForward(settlement_date, 0.05, Actual365Fixed())) # Makes sure the constructor does not segfault anymore ;-) index = Euribor6M(term_structure) self.assertEquals(index.name, 'Euribor6M Actual/360')
def test_excel_example_with_fixed_rate_bond(self): """Port the QuantLib Excel adding bond example to Python. """ todays_date = Date(25, 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( 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, ) discounting_term_structure = YieldTermStructure(relinkable=True) flat_term_structure = FlatForward( settlement_days=1, forward=0.044, calendar=NullCalendar(), daycounter=Actual365Fixed(), compounding=Continuous, frequency=Annual, ) discounting_term_structure.link_to(flat_term_structure) engine = DiscountingBondEngine(discounting_term_structure) bond.set_pricing_engine(engine) self.assertEquals(Date(10, Jul, 2016), termination_date) self.assertEquals(calendar.advance(todays_date, 3, Days), bond.settlement_date()) self.assertEquals(Date(11, Jul, 2016), bond.maturity_date) self.assertAlmostEqual(0.6849, bond.accrued_amount(bond.settlement_date()), 4) self.assertAlmostEqual(102.1154, bond.clean_price, 4)
def test_excel_example_with_fixed_rate_bond(self): '''Port the QuantLib Excel adding bond example to Python. ''' todays_date = Date(25, 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, Backward) issue_date = effective_date bond = FixedRateBond(settlement_days, face_amount, fixed_bond_schedule, [coupon_rate], ActualActual(ISMA), Following, redemption, issue_date) discounting_term_structure = YieldTermStructure() flat_term_structure = FlatForward(settlement_days=1, forward=0.044, calendar=NullCalendar(), daycounter=Actual365Fixed(), compounding=Continuous, frequency=Annual) discounting_term_structure.link_to(flat_term_structure) engine = DiscountingBondEngine(discounting_term_structure) bond.set_pricing_engine(engine) self.assertEqual(Date(10, Jul, 2016), termination_date) self.assertEqual(calendar.advance(todays_date, 3, Days), bond.settlement_date()) self.assertEqual(Date(11, Jul, 2016), bond.maturity_date) self.assertAlmostEqual(0.6849, bond.accrued_amount(bond.settlement_date()), 4) self.assertAlmostEqual(102.1154, bond.clean_price, 4)
def test_create_swap_index(self): term_structure = YieldTermStructure() term_structure.link_to(FlatForward(forward=0.05, daycounter=Actual365Fixed(), settlement_days=2, calendar=UnitedStates())) ibor_index = USDLibor(Period(3, Months), term_structure) index = SwapIndex( 'UsdLiborSwapIsdaFixAm', Period(10, Years), 2, USDCurrency(), UnitedStates(GovernmentBond), Period(6, Months), ModifiedFollowing, Thirty360(), ibor_index) index2 = UsdLiborSwapIsdaFixAm(Period(10, Years), term_structure) for attr in ['name', 'family_name', 'fixing_calendar', 'tenor', 'day_counter', 'currency']: self.assertEqual(getattr(index, attr), getattr(index2, attr))
def test_create_swap_index(self): term_structure = YieldTermStructure() term_structure.link_to( FlatForward(forward=0.05, daycounter=Actual365Fixed(), settlement_days=2, calendar=UnitedStates())) ibor_index = USDLibor(Period(3, Months), term_structure) index = SwapIndex('UsdLiborSwapIsdaFixAm', Period(10, Years), 2, USDCurrency(), UnitedStates(GovernmentBond), Period(6, Months), ModifiedFollowing, Thirty360(), ibor_index) index2 = UsdLiborSwapIsdaFixAm(Period(10, Years), term_structure) for attr in [ 'name', 'family_name', 'fixing_calendar', 'tenor', 'day_counter', 'currency' ]: self.assertEqual(getattr(index, attr), getattr(index2, attr))
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)
class IborMarket(FixedIncomeMarket): def __init__(self, name, market, **kwargs): params = swap_params(market) params = params._replace(**kwargs) self._params = params self._name = name self._market = market # floating rate index index = IborIndex.from_name(market, **kwargs) self._floating_rate_index = index self._deposit_daycount = params.floating_leg_daycount self._termstructure_daycount = 'ACT/365' self._eval_date = None self._quotes = None self._termstructure = None self._discount_term_structure = None self._forecast_term_structure = None self._rate_helpers = [] self._quotes = [] def _set_evaluation_date(self, dt_obs): if not isinstance(dt_obs, Date): dt_obs = pydate_to_qldate(dt_obs) settings = Settings() calendar = JointCalendar(UnitedStates(), UnitedKingdom()) # must be a business day eval_date = calendar.adjust(dt_obs) settings.evaluation_date = eval_date self._eval_date = eval_date return eval_date def set_quotes(self, dt_obs, quotes): self._quotes.extend(quotes) eval_date = self._set_evaluation_date(dt_obs) for quote in quotes: # construct rate helper helper = make_rate_helper(self, quote, eval_date) self._rate_helpers.append(helper) def set_bonds(self, dt_obs, quotes): """ Supply the market with a set of bond quotes. The `quotes` parameter must be a list of quotes of the form (clean_price, coupons, tenor, issue_date, maturity). For more information about the format of the individual fields, see the documentation for :meth:`add_bond_quote`. """ self._quotes.extend(quotes) self._set_evaluation_date(dt_obs) for quote in quotes: self.add_bond_quote(*quote) def add_bond_quote(self, clean_price, coupons, tenor, issue_date, maturity): """ Add a bond quote to the market. Parameters ---------- clean_price : real Clean price of the bond. coupons : real or list(real) Interest rates paid by the bond. tenor : str Tenor of the bond. issue_date, maturity : Date instance Issue date and maturity of the bond. """ if not isinstance(coupons, (list, tuple)): coupons = [coupons] helper = make_eurobond_helper( self, clean_price, coupons, tenor, issue_date, maturity) self._rate_helpers.append(helper) @property def calendar(self): return self._params.calendar @property def settlement_days(self): return self._params.settlement_days @property def fixed_rate_frequency(self): return self._params.fixed_rate_frequency @property def fixed_rate_convention(self): return self._params.fixed_instrument_convention @property def fixed_rate_daycounter(self): return self._params.fixed_rate_daycounter @property def termstructure_daycounter(self): return self._termstructure_daycounter @property def reference_date(self): return 0 @property def max_date(self): return 0 def __str__(self): output = \ "Ibor Market %s\n" % self._name + \ "Number of settlement days: %d\n" % self._params.settlement_days +\ "Fixed rate frequency: %s\n" % self._params.fixed_rate_frequency +\ "Fixed rate convention: %s\n" % self._params.fixed_instrument_convention +\ "Fixed rate daycount: %s\n" % self._params.fixed_instrument_daycounter +\ "Term structure daycount: %s\n" % self._termstructure_daycount + \ "Floating rate index: %s\n" % self._floating_rate_index + \ "Deposit daycount: %s\n" % self._deposit_daycount + \ "Calendar: %s\n" % self._params.calendar return output def bootstrap_term_structure(self, interpolator='loglinear'): 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 = PiecewiseYieldCurve( 'discount', interpolator, 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._forecast_term_structure = YieldTermStructure(relinkable=True) self._forecast_term_structure.link_to(ts) return ts def discount(self, date_maturity, extrapolate=True): return self._discount_term_structure.discount(date_maturity) 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._forecast_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 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 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(relinkable=True) 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 ) bfs=bf.BondFunctions() d=bfs.startDate(bond) bfs.display() zspd=bfs.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(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( 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( 'discount', 'loglinear', settlement_date, rate_helpers, ts_day_counter, tolerance) pyc_zspd=bfs.zSpread(bond,102.0,ts,ActualActual(ISDA), Compounded,Semiannual,settlement_date,1e-6,100,0.5) pyc_zspd_disco=bfs.zSpread(bond,95.0,ts,ActualActual(ISDA), Compounded,Semiannual,settlement_date,1e-6,100,0.5) # tests #self.assertTrue(Date(27, January, 2011), bond.issue_date) #self.assertTrue(Date(31, August, 2020), bond.maturity_date) #self.assertTrue(settings.evaluation_date, bond.valuation_date) print d self.assertTrue(Date(27, January, 2011), d) print 'Yield: {:.15%}'.format(bond_yield) #self.assertTrue(bond_yield, print 'Yield: {:.4%}'.format(bond_yield) print 'z-spread: {:.4%}'.format(zspd) print 'premium z-spread using pwyc: {:.4%}'.format(pyc_zspd) print 'discount z-spread using pwyc: {:.4%}'.format(pyc_zspd_disco)
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 report(swap, name): print(format % (name, formatPrice(swap.npv,2), formatRate(swap.fair_spread,4), formatRate(swap.fair_rate,4))) print(dblrule) print("5-year market swap-rate = %s" % formatRate(swaps[(5,Years)])) print(dblrule) # price on two different term structures print(tab + "5-years swap paying %s" % formatRate(fixedRate)) print(separator.join(headers)) print(rule) discountTermStructure.link_to(depoFuturesSwapCurve) forecastTermStructure.link_to(depoFuturesSwapCurve) report(spot,'depo-fut-swap') discountTermStructure.link_to(depoFraSwapCurve) forecastTermStructure.link_to(depoFraSwapCurve) report(spot,'depo-FRA-swap') print(rule) # price the 1-year forward swap print(tab + "5-years, 1-year forward swap paying %s" % formatRate(fixedRate)) print(rule) discountTermStructure.link_to(depoFuturesSwapCurve)
class TestOvernightIndexedSwap(unittest.TestCase): def setUp(self): self.today = Date(5, 2, 2009) self.settlement_days = 2 self.nominal = 100 self.settings = Settings().__enter__() self.settings.evaluation_date = self.today self.eonia_term_structure = YieldTermStructure() self.eonia_index = Eonia(self.eonia_term_structure) self.calendar = self.eonia_index.fixing_calendar self.settlement = self.calendar.advance(self.today, self.settlement_days, Following) self.eonia_term_structure.link_to(flat_rate(0.05)) def make_swap(self, length, fixed_rate, spread, telescopic_value_dates, effective_date=None, payment_lag=0, averaging_method=RateAveraging.Compound): return (MakeOIS( length, self.eonia_index, fixed_rate, 0 * Days).with_effective_date( self.settlement if effective_date is None else effective_date ).with_overnight_leg_spread(spread).with_nominal( self.nominal).with_payment_lag( payment_lag).with_discounting_term_structure( self.eonia_term_structure).with_telescopic_value_dates( telescopic_value_dates).with_averaging_method( averaging_method))() def test_fair_rate(self): lengths = [1 * Years, 2 * Years, 5 * Years, 10 * Years, 20 * Years] spreads = [-0.001, -.01, 0.0, 0.01, 0.001] for length, spread in product(lengths, spreads): with self.subTest(length=length, spread=spread): swap = self.make_swap(length, 0.0, spread, False) swap2 = self.make_swap(length, 0.0, spread, True) self.assertAlmostEqual(swap.fair_rate, swap2.fair_rate) swap = self.make_swap(length, swap.fair_rate, spread, False) self.assertAlmostEqual(swap.npv, 0.0) swap = self.make_swap(length, swap.fair_rate, spread, True) self.assertAlmostEqual(swap.npv, 0.0) def test_fair_spread(self): lengths = [1 * Years, 2 * Years, 5 * Years, 10 * Years, 20 * Years] rates = [0.04, 0.05, 0.06, 0.07] for length, rate in product(lengths, rates): with self.subTest(length=length, rate=rate): swap = self.make_swap(length, rate, 0.0, False) swap2 = self.make_swap(length, rate, 0.0, True) fair_spread = swap.fair_spread fair_spread2 = swap.fair_spread self.assertAlmostEqual(swap.fair_spread, swap2.fair_spread) swap = self.make_swap(length, rate, fair_spread, False) self.assertAlmostEqual(swap.npv, 0.0) swap = self.make_swap(length, rate, fair_spread, True) self.assertAlmostEqual(swap.npv, 0.0) def test_cached_value(self): flat = 0.05 self.eonia_term_structure.link_to( flat_rate(flat, Actual360(), reference_date=self.settlement)) fixed_rate = math.exp(flat) - 1 swap = self.make_swap(1 * Years, fixed_rate, 0.0, False) swap2 = self.make_swap(1 * Years, fixed_rate, 0.0, True) cached_npv = 0.001730450147 self.assertAlmostEqual(swap.npv, cached_npv) self.assertAlmostEqual(swap2.npv, cached_npv) def tearUp(self): self.settings.__exit__(None, None, None)
class IborMarket(FixedIncomeMarket): def __init__(self, name, market, **kwargs): params = SwapData.params(market) params = params._replace(**kwargs) self._params = params self._name = name self._market = market # floating rate index index = IborIndex.from_name(market, **kwargs) self._floating_rate_index = index self._deposit_daycount = params.floating_leg_daycount self._termstructure_daycount = 'ACT/365' self._eval_date = None self._quotes = None self._termstructure = None self._discount_term_structure = None self._forecast_term_structure = None def __str__(self): return 'Fixed Income Market: %s' % self._name def set_quotes(self, dt_obs, quotes): self._quotes = quotes if(~isinstance(dt_obs, Date)): dt_obs = pydate_to_qldate(dt_obs) settings = Settings() calendar = JointCalendar(UnitedStates(), UnitedKingdom()) # must be a business day eval_date = calendar.adjust(dt_obs) settings.evaluation_date = eval_date self._eval_date = eval_date self._rate_helpers = [] for quote in quotes: # construct rate helper helper = make_rate_helper(self, quote, eval_date) self._rate_helpers.append(helper) @property def calendar(self): return self._params.calendar @property def settlement_days(self): return self._params.settlement_days @property def fixed_rate_frequency(self): return self._params.fixed_rate_frequency @property def fixed_rate_convention(self): return self._params.fixed_instrument_convention @property def fixed_rate_daycounter(self): return self._params.fixed_rate_daycounter @property def termstructure_daycounter(self): return self._termstructure_daycounter @property def reference_date(self): return 0 @property def max_date(self): return 0 def to_str(self): str = \ "Ibor Market %s\n" % self._name + \ "Number of settlement days: %d\n" % self._params.settlement_days +\ "Fixed rate frequency: %s\n" % self._params.fixed_rate_frequency +\ "Fixed rate convention: %s\n" % self._params.fixed_instrument_convention +\ "Fixed rate daycount: %s\n" % self._params.fixed_instrument_daycounter +\ "Term structure daycount: %s\n" % self._termstructure_daycount + \ "Floating rate index: %s\n" % self._floating_rate_index + \ "Deposit daycount: %s\n" % self._deposit_daycount + \ "Calendar: %s\n" % self._params.calendar return str 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 discount(self, date_maturity, extrapolate=True): return self._discount_term_structure.discount(date_maturity) 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
class TestOvernightIndexedCoupon(unittest.TestCase): def setUp(self): self.today = Date(23, November, 2021) Settings().evaluation_date = self.today self.forecast_curve = YieldTermStructure() self.notional = 10_000 self.sofr = Sofr(self.forecast_curve) self.dates = [ Date(18, October, 2021), Date(19, October, 2021), Date(20, October, 2021), Date(21, October, 2021), Date(22, October, 2021), Date(25, October, 2021), Date(26, October, 2021), Date(27, October, 2021), Date(28, October, 2021), Date(29, October, 2021), Date(1, November, 2021), Date(2, November, 2021), Date(3, November, 2021), Date(4, November, 2021), Date(5, November, 2021), Date(8, November, 2021), Date(9, November, 2021), Date(10, November, 2021), Date(12, November, 2021), Date(15, November, 2021), Date(16, November, 2021), Date(17, November, 2021), Date(18, November, 2021), Date(19, November, 2021), Date(22, November, 2021) ] self.past_rates = [ 0.0008, 0.0009, 0.0008, 0.0010, 0.0012, 0.0011, 0.0013, 0.0012, 0.0012, 0.0008, 0.0009, 0.0010, 0.0011, 0.0014, 0.0013, 0.0011, 0.0009, 0.0008, 0.0007, 0.0008, 0.0008, 0.0007, 0.0009, 0.0010, 0.0009 ] self.sofr.add_fixings(self.dates, self.past_rates) def make_coupon(self, start_date, end_date): return OvernightIndexedCoupon(end_date, self.notional, start_date, end_date, self.sofr) def test_past_coupon_rate(self): """Testing rate for past overnight-indexed coupon""" past_coupon = self.make_coupon(Date(18, October, 2021), Date(18, November, 2021)) expected_rate = 0.000987136104 expected_amount = self.notional * expected_rate * 31.0 / 360 self.assertAlmostEqual(past_coupon.rate, expected_rate) self.assertAlmostEqual(past_coupon.amount, expected_amount) def test_current_coupon_rate(self): """Testing rate for current overnight-indexed coupon""" self.forecast_curve.link_to(flat_rate(0.0010, Actual360())) current_coupon = self.make_coupon(Date(10, November, 2021), Date(10, December, 2021)) expected_rate = 0.000926701551 expected_amount = self.notional * expected_rate * 30.0 / 360 self.assertAlmostEqual(current_coupon.rate, expected_rate) self.assertAlmostEqual(current_coupon.amount, expected_amount) self.sofr.add_fixing(Date(23, November, 2021), 0.0007) expected_rate = 0.000916700760 expected_amount = self.notional * expected_rate * 30.0 / 360 self.assertAlmostEqual(current_coupon.rate, expected_rate) self.assertAlmostEqual(current_coupon.amount, expected_amount) self.sofr.clear_fixings() self.sofr.add_fixings(self.dates, self.past_rates) def test_future_coupon_rate(self): """Testing rate for future overnight-indexed coupon""" self.forecast_curve.link_to(flat_rate(0.0010, Actual360())) future_coupon = self.make_coupon(Date(10, December, 2021), Date(10, January, 2022)) expected_rate = 0.001000043057 expected_amount = self.notional * expected_rate * 31.0 / 360 self.assertAlmostEqual(future_coupon.rate, expected_rate) self.assertAlmostEqual(future_coupon.amount, expected_amount) def test_rate_when_today_is_holiday(self): """Testing rate for overnight-indexed coupon when today is a holiday""" with Settings() as s: s.evaluation_date = Date(20, November, 2021) self.forecast_curve.link_to(flat_rate(0.0010, Actual360())) coupon = self.make_coupon(Date(10, November, 2021), Date(10, December, 2021)) expected_rate = 0.000930035180 expected_amount = self.notional * expected_rate * 30.0 / 360 self.assertAlmostEqual(coupon.rate, expected_rate) self.assertAlmostEqual(coupon.amount, expected_amount) def test_accrued_amount_in_the_past(self): """Testing accrued amount in the past for overnight-indexed coupon""" coupon = self.make_coupon(Date(18, October, 2021), Date(18, January, 2022)) expected_amount = self.notional * 0.000987136104 * 31.0 / 360 self.assertAlmostEqual(coupon.accrued_amount(Date(18, November, 2021)), expected_amount) def test_accrued_amount_spanning_today(self): """Testing accrued amount spanning today for current overnight-indexed coupon""" self.forecast_curve.link_to(flat_rate(0.0010, Actual360())) coupon = self.make_coupon(Date(10, November, 2021), Date(10, January, 2022)) expected_amount = self.notional * 0.000926701551 * 30.0 / 360 self.assertAlmostEqual(coupon.accrued_amount(Date(10, December, 2021)), expected_amount) self.sofr.add_fixing(Date(23, November, 2021), 0.0007) expected_amount = self.notional * 0.000916700760 * 30.0 / 360 self.assertAlmostEqual(coupon.accrued_amount(Date(10, December, 2021)), expected_amount) self.sofr.clear_fixings() self.sofr.add_fixings(self.dates, self.past_rates) def test_accrued_amount_in_the_futures(self): """Testing accrued amount in the future for overnight-indexed coupon""" self.forecast_curve.link_to(flat_rate(0.0010, Actual360())) coupon = self.make_coupon(Date(10, December, 2021), Date(10, March, 2022)) accrual_date = Date(10, January, 2022) expected_rate = 0.001000043057 expected_amount = self.notional * expected_rate * 31.0 / 360 self.assertAlmostEqual(coupon.accrued_amount(accrual_date), expected_amount)
def test_excel_example_with_floating_rate_bond(self): todays_date = Date(25, 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_date = calendar.adjust(Date(28, January, 2011)) settlement_days = 3 #1 face_amount = 13749769.27 #2 coupon_rate = 0.05 redemption = 100.0 float_bond_schedule = Schedule(effective_date, termination_date, Period(Annual), calendar, ModifiedFollowing, ModifiedFollowing, Backward) #3 flat_discounting_term_structure = YieldTermStructure(relinkable=True) forecastTermStructure = YieldTermStructure(relinkable=True) dc = Actual360() ibor_index = Euribor6M(forecastTermStructure) #5 fixing_days = 2 #6 gearings = [1, 0.0] #7 spreads = [1, 0.05] #8 caps = [] #9 floors = [] #10 pmt_conv = ModifiedFollowing #11 issue_date = effective_date float_bond = FloatingRateBond(settlement_days, face_amount, float_bond_schedule, ibor_index, dc, fixing_days, gearings, spreads, caps, floors, pmt_conv, redemption, issue_date) flat_term_structure = FlatForward(settlement_days=1, forward=0.055, calendar=NullCalendar(), daycounter=Actual365Fixed(), compounding=Continuous, frequency=Annual) flat_discounting_term_structure.link_to(flat_term_structure) forecastTermStructure.link_to(flat_term_structure) engine = DiscountingBondEngine(flat_discounting_term_structure) float_bond.set_pricing_engine(engine) cons_option_vol = ConstantOptionletVolatility(settlement_days, UnitedStates(SETTLEMENT), pmt_conv, 0.95, Actual365Fixed()) coupon_pricer = BlackIborCouponPricer(cons_option_vol) set_coupon_pricer(float_bond, coupon_pricer) self.assertEquals(Date(10, Jul, 2016), termination_date) self.assertEquals(calendar.advance(todays_date, 3, Days), float_bond.settlement_date()) self.assertEquals(Date(11, Jul, 2016), float_bond.maturity_date) self.assertAlmostEqual( 0.6944, float_bond.accrued_amount(float_bond.settlement_date()), 4) self.assertAlmostEqual(98.2485, float_bond.dirty_price, 4) self.assertAlmostEqual(13500805.2469, float_bond.npv, 4)
def test_bucketanalysis_bond(self): settings = Settings() calendar = TARGET() settlement_date = calendar.adjust(Date(28, January, 2011)) simple_quotes = [] fixing_days = 1 settlement_days = 1 todays_date = calendar.advance( settlement_date, -fixing_days, Days ) settings.evaluation_date = todays_date face_amount = 100.0 redemption = 100.0 issue_date = Date(27, January, 2011) maturity_date = Date(1, January, 2021) coupon_rate = 0.055 bond_yield = 0.034921 flat_discounting_term_structure = YieldTermStructure(relinkable=True) flat_term_structure = FlatForward( reference_date = settlement_date, forward = bond_yield, daycounter = Actual365Fixed(), compounding = Compounded, frequency = Semiannual) flat_discounting_term_structure.link_to(flat_term_structure) 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 ) zspd=bf.zSpread(bond, 100.0, flat_term_structure, Actual365Fixed(), Compounded, Semiannual, settlement_date, 1e-6, 100, 0.5) 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) sq_rate = SimpleQuote(rate/100) helper = DepositRateHelper(sq_rate, tenor, settlement_days, calendar, ModifiedFollowing, end_of_month, Actual360()) simple_quotes.append(sq_rate) 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: sq_rate = SimpleQuote(rate/100) helper = SwapRateHelper.from_tenor( sq_rate, Period(m, Years), calendar, Annual, Unadjusted, Thirty360(), liborIndex, spread, fwdStart ) simple_quotes.append(sq_rate) 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) discounting_term_structure = YieldTermStructure(relinkable=True) discounting_term_structure.link_to(ts) pricing_engine = DiscountingBondEngine(discounting_term_structure) bond.set_pricing_engine(pricing_engine) self.assertAlmostEqual(bond.npv, 100.83702940160767) ba = bucket_analysis([simple_quotes], [bond], [1], 0.0001, 1) self.assertTrue(2, ba) self.assertTrue(type(tuple), ba) self.assertEqual(len(simple_quotes), len(ba[0][0])) self.assertEqual(0, ba[0][0][8])
def test_bucketanalysis_bond(self): settings = Settings() calendar = TARGET() settlement_date = calendar.adjust(Date(28, January, 2011)) simple_quotes = [] fixing_days = 1 settlement_days = 1 todays_date = calendar.advance(settlement_date, -fixing_days, Days) settings.evaluation_date = todays_date face_amount = 100.0 redemption = 100.0 issue_date = Date(27, January, 2011) maturity_date = Date(1, January, 2021) coupon_rate = 0.055 bond_yield = 0.034921 flat_discounting_term_structure = YieldTermStructure() flat_term_structure = FlatForward(reference_date=settlement_date, forward=bond_yield, daycounter=Actual365Fixed(), compounding=Compounded, frequency=Semiannual) flat_discounting_term_structure.link_to(flat_term_structure) fixed_bond_schedule = Schedule.from_rule( 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) zspd = bf.zSpread(bond, 100.0, flat_term_structure, Actual365Fixed(), Compounded, Semiannual, settlement_date, 1e-6, 100, 0.5) 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) sq_rate = SimpleQuote(rate / 100) helper = DepositRateHelper(sq_rate, tenor, settlement_days, calendar, ModifiedFollowing, end_of_month, Actual360()) simple_quotes.append(sq_rate) 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: sq_rate = SimpleQuote(rate / 100) helper = SwapRateHelper.from_tenor(sq_rate, Period(m, Years), calendar, Annual, Unadjusted, Thirty360(), liborIndex, spread, fwdStart) simple_quotes.append(sq_rate) 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) discounting_term_structure = YieldTermStructure() discounting_term_structure.link_to(ts) pricing_engine = DiscountingBondEngine(discounting_term_structure) bond.set_pricing_engine(pricing_engine) self.assertAlmostEqual(bond.npv, 100.83702940160767) ba = bucket_analysis([simple_quotes], [bond], [1], 0.0001, 1) self.assertTrue(2, ba) self.assertTrue(type(tuple), ba) self.assertEqual(len(simple_quotes), len(ba[0][0])) self.assertEqual(0, ba[0][0][8])
def test_pricing_bond(self): '''Inspired by the C++ code from http://quantcorner.wordpress.com/.''' 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 discounting_term_structure = YieldTermStructure(relinkable=True) 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 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) bond.set_pricing_engine(discounting_term_structure) # tests self.assertTrue(Date(27, January, 2011), bond.issue_date) self.assertTrue(Date(31, August, 2020), bond.maturity_date) self.assertTrue(settings.evaluation_date, bond.valuation_date) # the following assertion fails but must be verified self.assertAlmostEqual(101.1, bond.clean_price, 1) self.assertAlmostEqual(101.1, bond.net_present_value, 1) self.assertAlmostEqual(101.1, bond.dirty_price) self.assertAlmostEqual(0.009851, bond.accrued_amount()) print(settings.evaluation_date) print('Principal: {}'.format(face_amount)) print('Issuing date: {} '.format(bond.issue_date)) print('Maturity: {}'.format(bond.maturity_date)) print('Coupon rate: {:.4%}'.format(coupon_rate)) print('Yield: {:.4%}'.format(bond_yield)) print('Net present value: {:.4f}'.format(bond.net_present_value)) print('Clean price: {:.4f}'.format(bond.clean_price)) print('Dirty price: {:.4f}'.format(bond.dirty_price)) print('Accrued coupon: {:.6f}'.format(bond.accrued_amount())) print('Accrued coupon: {:.6f}'.format( bond.accrued_amount(Date(1, March, 2011))))
def test_pricing_bond(): '''Inspired by the C++ code from http://quantcorner.wordpress.com/.''' 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 discounting_term_structure = YieldTermStructure(relinkable=True) 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 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 ) bond.set_pricing_engine(discounting_term_structure) return bond
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)
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) discounting_term_structure = YieldTermStructure(relinkable=True) flat_term_structure = FlatForward(settlement_days=1, forward=0.044, calendar=NullCalendar(), daycounter=Actual365Fixed(), compounding=Continuous, frequency=Annual) discounting_term_structure.link_to(flat_term_structure) pricing_engine = DiscountingBondEngine(discounting_term_structure) bond.set_pricing_engine(pricing_engine) print('Settlement date: ', bond.settlement_date()) print('Maturity date:', bond.maturity_date) print('Accrued amount: ', bond.accrued_amount(bond.settlement_date())) print('Clean price:', bond.clean_price)
def test_pricing_bond(self): '''Inspired by the C++ code from http://quantcorner.wordpress.com/.''' 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 discounting_term_structure = YieldTermStructure(relinkable=True) 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 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 ) bond.set_pricing_engine(discounting_term_structure) # tests self.assertTrue(Date(27, January, 2011), bond.issue_date) self.assertTrue(Date(31, August, 2020), bond.maturity_date) self.assertTrue(settings.evaluation_date, bond.valuation_date) # the following assertion fails but must be verified self.assertAlmostEqual(101.1, bond.clean_price, 1) self.assertAlmostEqual(101.1, bond.net_present_value, 1) self.assertAlmostEqual(101.1, bond.dirty_price) self.assertAlmostEqual(0.009851, bond.accrued_amount()) print settings.evaluation_date print 'Principal: {}'.format(face_amount) print 'Issuing date: {} '.format(bond.issue_date) print 'Maturity: {}'.format(bond.maturity_date) print 'Coupon rate: {:.4%}'.format(coupon_rate) print 'Yield: {:.4%}'.format(bond_yield) print 'Net present value: {:.4f}'.format(bond.net_present_value) print 'Clean price: {:.4f}'.format(bond.clean_price) print 'Dirty price: {:.4f}'.format(bond.dirty_price) print 'Accrued coupon: {:.6f}'.format(bond.accrued_amount()) print 'Accrued coupon: {:.6f}'.format( bond.accrued_amount(Date(1, March, 2011)) )
bond = FixedRateBond( settlement_days, face_amount, fixed_bond_schedule, [coupon_rate], ActualActual(ISMA), Following, redemption, issue_date ) discounting_term_structure = YieldTermStructure(relinkable=True) flat_term_structure = FlatForward( settlement_days = 1, forward = 0.044, calendar = NullCalendar(), daycounter = Actual365Fixed(), compounding = Continuous, frequency = Annual) discounting_term_structure.link_to(flat_term_structure) pricing_engine = DiscountingBondEngine(discounting_term_structure) bond.set_pricing_engine(pricing_engine) print('Settlement date: ', bond.settlement_date()) print('Maturity date:', bond.maturity_date) print('Accrued amount: ', bond.accrued_amount(bond.settlement_date())) print('Clean price:', bond.clean_price)
class IborMarket(FixedIncomeMarket): def __init__(self, name, market, **kwargs): params = swap_params(market) params = params._replace(**kwargs) self._params = params self._name = name self._market = market # floating rate index index = IborIndex.from_name(market, **kwargs) self._floating_rate_index = index self._deposit_daycount = params.floating_leg_daycount self._termstructure_daycount = 'ACT/365' self._eval_date = None self._quotes = None self._termstructure = None self._discount_term_structure = None self._forecast_term_structure = None self._rate_helpers = [] self._quotes = [] def _set_evaluation_date(self, dt_obs): if not isinstance(dt_obs, Date): dt_obs = pydate_to_qldate(dt_obs) settings = Settings() calendar = JointCalendar(UnitedStates(), UnitedKingdom()) # must be a business day eval_date = calendar.adjust(dt_obs) settings.evaluation_date = eval_date self._eval_date = eval_date return eval_date def set_quotes(self, dt_obs, quotes): self._quotes.extend(quotes) eval_date = self._set_evaluation_date(dt_obs) for quote in quotes: # construct rate helper helper = make_rate_helper(self, quote, eval_date) self._rate_helpers.append(helper) def set_bonds(self, dt_obs, quotes): """ Supply the market with a set of bond quotes. The `quotes` parameter must be a list of quotes of the form (clean_price, coupons, tenor, issue_date, maturity). For more information about the format of the individual fields, see the documentation for :meth:`add_bond_quote`. """ self._quotes.extend(quotes) self._set_evaluation_date(dt_obs) for quote in quotes: self.add_bond_quote(*quote) def add_bond_quote(self, clean_price, coupons, tenor, issue_date, maturity): """ Add a bond quote to the market. Parameters ---------- clean_price : real Clean price of the bond. coupons : real or list(real) Interest rates paid by the bond. tenor : str Tenor of the bond. issue_date, maturity : Date instance Issue date and maturity of the bond. """ if not isinstance(coupons, (list, tuple)): coupons = [coupons] helper = make_eurobond_helper(self, clean_price, coupons, tenor, issue_date, maturity) self._rate_helpers.append(helper) @property def calendar(self): return self._params.calendar @property def settlement_days(self): return self._params.settlement_days @property def fixed_leg_period(self): return self._params.fixed_leg_period @property def fixed_leg_convention(self): return self._params.fixed_leg_convention @property def fixed_leg_daycount(self): return self._params.fixed_leg_daycount @property def termstructure_daycounter(self): return self._termstructure_daycounter @property def reference_date(self): return 0 @property def max_date(self): return 0 def __str__(self): output = \ "Ibor Market %s\n" % self._name + \ "Number of settlement days: %d\n" % self._params.settlement_days +\ "Fixed leg period: %s\n" % self._params.fixed_leg_period +\ "Fixed leg convention: %s\n" % self._params.fixed_leg_convention +\ "Fixed leg daycount: %s\n" % self._params.fixed_leg_daycount +\ "Term structure daycount: %s\n" % self._termstructure_daycount + \ "Floating rate index: %s\n" % self._floating_rate_index + \ "Deposit daycount: %s\n" % self._deposit_daycount + \ "Calendar: %s\n" % self._params.calendar return output def bootstrap_term_structure(self, interpolator=Interpolator.LogLinear): 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 = PiecewiseYieldCurve.from_reference_date( BootstrapTrait.Discount, interpolator, settlement_date, self._rate_helpers, DayCounter.from_name(self._termstructure_daycount), tolerance) self._term_structure = ts self._discount_term_structure = YieldTermStructure() self._discount_term_structure.link_to(ts) self._forecast_term_structure = YieldTermStructure() self._forecast_term_structure.link_to(ts) return ts def discount(self, date_maturity, extrapolate=True): return self._discount_term_structure.discount(date_maturity) 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._forecast_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 = \ Period(_params.fixed_leg_period) floating_frequency = Period(_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.from_rule(settlement_date, maturity, fixed_frequency, calendar, fixed_convention, fixed_convention, Rule.Forward, False) float_schedule = Schedule.from_rule(settlement_date, maturity, floating_frequency, calendar, floating_convention, floating_convention, Rule.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, settlement_date=settlement_date, npv_date=settlement_date) swap.set_pricing_engine(engine) return swap
def test_excel_example_with_floating_rate_bond(self): todays_date = Date(25, 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_date = calendar.adjust(Date(28, January, 2011)) settlement_days = 3 #1 face_amount = 13749769.27 #2 coupon_rate = 0.05 redemption = 100.0 float_bond_schedule = Schedule( effective_date, termination_date, Period(Annual), calendar, ModifiedFollowing, ModifiedFollowing, Backward )#3 flat_discounting_term_structure = YieldTermStructure(relinkable=True) forecastTermStructure = YieldTermStructure(relinkable=True) dc = Actual360() ibor_index = Euribor6M(forecastTermStructure) #5 fixing_days = 2 #6 gearings = [1,0.0] #7 spreads = [1,0.05] #8 caps = [] #9 floors = [] #10 pmt_conv = ModifiedFollowing #11 issue_date = effective_date float_bond = FloatingRateBond(settlement_days, face_amount, float_bond_schedule, ibor_index, dc, fixing_days, gearings, spreads, caps, floors, pmt_conv, redemption, issue_date) flat_term_structure = FlatForward( settlement_days = 1, forward = 0.055, calendar = NullCalendar(), daycounter = Actual365Fixed(), compounding = Continuous, frequency = Annual) flat_discounting_term_structure.link_to(flat_term_structure) forecastTermStructure.link_to(flat_term_structure) engine = DiscountingBondEngine(flat_discounting_term_structure) float_bond.set_pricing_engine(engine) cons_option_vol = ConstantOptionletVolatility(settlement_days, UnitedStates(SETTLEMENT), pmt_conv, 0.95, Actual365Fixed()) coupon_pricer = BlackIborCouponPricer(cons_option_vol) set_coupon_pricer(float_bond,coupon_pricer) self.assertEquals(Date(10, Jul, 2016), termination_date) self.assertEquals( calendar.advance(todays_date, 3, Days), float_bond.settlement_date() ) self.assertEquals(Date(11, Jul, 2016), float_bond.maturity_date) self.assertAlmostEqual( 0.6944, float_bond.accrued_amount(float_bond.settlement_date()), 4 ) self.assertAlmostEqual(98.2485, float_bond.dirty_price, 4) self.assertAlmostEqual(13500805.2469, float_bond.npv,4)
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)
] eonia_term_structure = PiecewiseYieldCurve[Discount, Cubic].from_reference_date( todays_date, eonia_instruments, termStructureDayCounter) eonia_term_structure.extrapolation = True # Term structures that will be used for pricing: # the one used for discounting cash flows discountingTermStructure = YieldTermStructure() # the one used for forward rate forecasting forecastingTermStructure = YieldTermStructure() discountingTermStructure.link_to(eonia_term_structure) # /********************* # ** EURIBOR 6M ** # *********************/ euribor6M = Euribor6M() # deposits d6MRate = SimpleQuote(0.00312) # FRAs fra_rates = [ 0.002930, 0.002720, 0.002600, 0.002560, 0.002520, 0.002480, 0.002540, 0.002610, 0.002670, 0.002790, 0.002910, 0.003030, 0.003180, 0.003350, 0.003520, 0.003710, 0.003890, 0.004090 ]