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_clean_price(self): notional = 1000000.0 fixed_rates = [0.1] fixed_day_count = Actual365Fixed() fixed_calendar = self.calendar fixed_index = self.ii contractObservationLag = Period(3, Months) observationInterpolation = InterpolationType.Flat settlement_days = 3 growth_only = True baseCPI = 206.1 fixed_schedule = Schedule.from_rule(Date(2, 10, 2007), Date(2, 10, 2052), Period(6, Months), fixed_calendar, Unadjusted, Rule.Backward) cpi_bond = CPIBond(settlement_days, notional, growth_only, baseCPI, contractObservationLag, fixed_index, observationInterpolation, fixed_schedule, fixed_rates, fixed_day_count, ModifiedFollowing) engine = DiscountingBondEngine(self.yts) cpi_bond.set_pricing_engine(engine) set_coupon_pricer(cpi_bond.cashflows, CPICouponPricer(self.yts)) storedPrice = 383.01816406 calculated = cpi_bond.clean_price self.assertAlmostEqual(storedPrice, calculated)
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.from_rule( issue_date, maturity, Period(tenor), index.fixing_calendar, index.business_day_convention, index.business_day_convention, Rule.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 test_clean_price(self): notional = 1000000.0; fixed_rates = [0.1] fixed_day_count = Actual365Fixed() fixed_calendar = self.calendar fixed_index = self.ii contractObservationLag = Period(3, Months); observationInterpolation = InterpolationType.Flat; settlement_days = 3 growth_only = True baseCPI = 206.1 fixed_schedule = Schedule.from_rule(Date(2, 10, 2007), Date(2, 10, 2052), Period(6, Months), fixed_calendar, Unadjusted, Rule.Backward) cpi_bond = CPIBond(settlement_days, notional, growth_only, baseCPI, contractObservationLag, fixed_index, observationInterpolation, fixed_schedule, fixed_rates, fixed_day_count, ModifiedFollowing) engine = DiscountingBondEngine(self.yts) cpi_bond.set_pricing_engine(engine) storedPrice = 383.01816406 calculated = cpi_bond.clean_price self.assertAlmostEqual(storedPrice, calculated)
def test_bond_schedule_anotherday(self): '''Test date calculations and role of settings when evaluation date set to arbitrary date. This test is known to fail with boost 1.42. ''' todays_date = Date(30, August, 2011) settings = Settings() settings.evaluation_date = todays_date calendar = TARGET() effective_date = Date(10, Jul, 2006) termination_date = calendar.advance( effective_date, 10, Years, convention=Unadjusted) settlement_days = 3 face_amount = 100.0 coupon_rate = 0.05 redemption = 100.0 fixed_bond_schedule = Schedule.from_rule( effective_date, termination_date, Period(Annual), calendar, ModifiedFollowing, ModifiedFollowing, Rule.Backward ) issue_date = effective_date bond = FixedRateBond( settlement_days, face_amount, fixed_bond_schedule, [coupon_rate], ActualActual(ISMA), Following, redemption, issue_date ) self.assertEqual( calendar.advance(todays_date, 3, Days), bond.settlement_date())
def example02(): print("example 2:\n") todays_date = Date(25, 9, 2014) Settings.instance().evaluation_date = todays_date calendar = TARGET() term_date = calendar.adjust(todays_date + Period(2, Years), Following) cds_schedule = Schedule(todays_date, term_date, Period(Quarterly), WeekendsOnly(), ModifiedFollowing, ModifiedFollowing, date_generation_rule=Rule.CDS) for date in cds_schedule: print(date) print() todays_date = Date(21, 10, 2014) Settings.instance().evaluation_date = todays_date quotes = [0.00006, 0.00045, 0.00081, 0.001840, 0.00256, 0.00337] tenors = [1, 2, 3, 6, 9, 12] deps = [DepositRateHelper(q, Period(t, Months), 2, calendar, ModifiedFollowing, False, Actual360()) for q, t in zip(quotes, tenors)] tenors = [2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 15, 20, 30] quotes = [0.00223, 0.002760, 0.003530, 0.004520, 0.005720, 0.007050, 0.008420, 0.009720, 0.010900, 0.012870, 0.014970, 0.017, 0.01821] swaps = [SwapRateHelper.from_tenor(q, Period(t, Years), calendar, Annual, ModifiedFollowing, Thirty360(), Euribor6M(), SimpleQuote(0)) for q, t in zip(quotes, tenors)] helpers = deps + swaps YC = PiecewiseYieldCurve.from_reference_date(BootstrapTrait.Discount, Interpolator.LogLinear, todays_date, helpers, Actual365Fixed()) YC.extrapolation = True print("ISDA rate curve:") for h in helpers: print("{0}: {1:.6f}\t{2:.6f}".format(h.latest_date, YC.zero_rate(h.latest_date, Actual365Fixed(), 2).rate, YC.discount(h.latest_date))) defaultTs0 = FlatHazardRate(0, WeekendsOnly(), 0.016739207493630, Actual365Fixed()) cds_schedule = Schedule.from_rule(Date(22, 9, 2014), Date(20, 12, 2019), Period(3, Months), WeekendsOnly(), Following, Unadjusted, Rule.CDS, False) nominal = 100000000 trade = CreditDefaultSwap(Side.Buyer, nominal, 0.01, cds_schedule, Following, Actual360(), True, True, Date(22, 10, 2014), Actual360(True), True) engine = IsdaCdsEngine(defaultTs0, 0.4, YC, False) trade.set_pricing_engine(engine) print("reference trade NPV = {0}\n".format(trade.npv))
def setUp(self): calendar = TARGET() today_date = today() Settings().evaluation_date = today_date hazard_rate = SimpleQuote(0.01234) probability_curve = FlatHazardRate(0, calendar, hazard_rate, Actual360()) discount_curve = FlatForward(today_date, 0.06, Actual360()) issue_date = today_date #calendar.advance(today_date, -1, Years) maturity = calendar.advance(issue_date, 10, Years) self.convention = Following self.schedule = Schedule.from_rule(issue_date, maturity, Period("3M"), calendar, self.convention, self.convention, Rule.TwentiethIMM) recovery_rate = 0.4 self.engine = MidPointCdsEngine(probability_curve, recovery_rate, discount_curve, True)
def 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.from_rule( issue_date, maturity, Period(tenor), index.fixing_calendar, index.business_day_convention, index.business_day_convention, Rule.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
floatingLegConvention = ModifiedFollowing fixedLegDayCounter = Thirty360(European) fixedRate = 0.007 floatingLegDayCounter = Actual360() # // floating leg floatingLegFrequency = Semiannual euriborIndex = Euribor6M(forecastingTermStructure) spread = 0.0 lengthInYears = 5 swapType = SwapType.Payer maturity = settlement_date + lengthInYears * Years fixedSchedule = Schedule.from_rule(settlement_date, maturity, Period(fixedLegFrequency), calendar, fixedLegConvention, fixedLegConvention, Forward, False) floatSchedule = Schedule.from_rule(settlement_date, maturity, Period(floatingLegFrequency), calendar, floatingLegConvention, floatingLegConvention, Forward, False) spot5YearSwap = VanillaSwap(swapType, nominal, fixedSchedule, fixedRate, fixedLegDayCounter, floatSchedule, euriborIndex, spread, floatingLegDayCounter) fwdStart = calendar.advance(settlement_date, 1, Years) fwdMaturity = fwdStart + lengthInYears * Years fwdFixedSchedule = Schedule.from_rule(fwdStart, fwdMaturity, Period(fixedLegFrequency), calendar, fixedLegConvention, fixedLegConvention, Forward, False)
def example01(): #********************* #*** MARKET DATA *** #********************* calendar = TARGET() todays_date = Date(15, May, 2007) # must be a business day todays_date = calendar.adjust(todays_date) Settings.instance().evaluation_date = todays_date # dummy curve ts_curve = FlatForward( reference_date=todays_date, forward=0.01, daycounter=Actual365Fixed() ) # In Lehmans Brothers "guide to exotic credit derivatives" # p. 32 there's a simple case, zero flat curve with a flat CDS # curve with constant market spreads of 150 bp and RR = 50% # corresponds to a flat 3% hazard rate. The implied 1-year # survival probability is 97.04% and the 2-years is 94.18% # market recovery_rate = 0.5 quoted_spreads = [0.0150, 0.0150, 0.0150, 0.0150 ] tenors = [Period(i, Months) for i in [3, 6, 12, 24]] maturities = [ calendar.adjust(todays_date + tenors[i], Following) for i in range(4) ] instruments = [] for i in range(4): helper = SpreadCdsHelper( quoted_spreads[i], tenors[i], 0, calendar, Quarterly, Following, Rule.TwentiethIMM, Actual365Fixed(), recovery_rate, ts_curve ) instruments.append(helper) # Bootstrap hazard rates hazard_rate_structure = PiecewiseDefaultCurve.from_reference_date( ProbabilityTrait.HazardRate, Interpolator.BackwardFlat, todays_date, instruments, Actual365Fixed() ) #vector<pair<Date, Real> > hr_curve_data = hazardRateStructure->nodes(); #cout << "Calibrated hazard rate values: " << endl ; #for (Size i=0; i<hr_curve_data.size(); i++) { # cout << "hazard rate on " << hr_curve_data[i].first << " is " # << hr_curve_data[i].second << endl; #} #cout << endl; target = todays_date + Period(1, Years) print(target) print("Some survival probability values: ") print("1Y survival probability: {:%}".format( hazard_rate_structure.survival_probability(target) )) print(" expected: {:%}".format(0.9704)) print("2Y survival probability: {:%}".format( hazard_rate_structure.survival_probability(todays_date + Period(2, Years)) )) print(" expected: {:%}".format(0.9418)) # reprice instruments nominal = 1000000.0; #Handle<DefaultProbabilityTermStructure> probability(hazardRateStructure); engine = MidPointCdsEngine(hazard_rate_structure, recovery_rate, ts_curve) cds_schedule = Schedule.from_rule( todays_date, maturities[0], Period(Quarterly), calendar, termination_date_convention=Unadjusted, date_generation_rule=Rule.TwentiethIMM ) cds_3m = CreditDefaultSwap( Side.Seller, nominal, quoted_spreads[0], cds_schedule, Following, Actual365Fixed() ) cds_schedule = Schedule.from_rule( todays_date, maturities[1], Period(Quarterly), calendar, termination_date_convention=Unadjusted, date_generation_rule=Rule.TwentiethIMM ) cds_6m = CreditDefaultSwap( Side.Seller, nominal, quoted_spreads[1], cds_schedule, Following, Actual365Fixed() ) cds_schedule = Schedule.from_rule( todays_date, maturities[2], Period(Quarterly), calendar, termination_date_convention=Unadjusted, date_generation_rule=Rule.TwentiethIMM ) cds_1y = CreditDefaultSwap( Side.Seller, nominal, quoted_spreads[2], cds_schedule, Following, Actual365Fixed() ) cds_schedule = Schedule.from_rule( todays_date, maturities[3], Period(Quarterly), calendar, termination_date_convention=Unadjusted, date_generation_rule=Rule.TwentiethIMM ) cds_2y = CreditDefaultSwap( Side.Seller, nominal, quoted_spreads[3], cds_schedule, Following, Actual365Fixed() ) cds_3m.set_pricing_engine(engine); cds_6m.set_pricing_engine(engine); cds_1y.set_pricing_engine(engine); cds_2y.set_pricing_engine(engine); print("Repricing of quoted CDSs employed for calibration: ") print("3M fair spread: {}".format(cds_3m.fair_spread)) print(" NPV: ", cds_3m.net_present_value) print(" default leg: ", cds_3m.default_leg_npv) print(" coupon leg: ", cds_3m.coupon_leg_npv) print("6M fair spread: {}".format(cds_6m.fair_spread)) print(" NPV: ", cds_6m.net_present_value) print(" default leg: ", cds_6m.default_leg_npv) print(" coupon leg: ", cds_6m.coupon_leg_npv) print("1Y fair spread: {}".format(cds_1y.fair_spread)) print(" NPV: ", cds_1y.net_present_value) print(" default leg: ", cds_1y.default_leg_npv) print(" coupon leg: ", cds_1y.coupon_leg_npv) print("2Y fair spread: {}".format(cds_2y.fair_spread)) print(" NPV: ", cds_2y.net_present_value) print(" default leg: ", cds_2y.default_leg_npv) print(" coupon leg: ", cds_2y.coupon_leg_npv) print()
def setUp(self): self.calendar = UnitedKingdom() today = Date(25, 11, 2009) evaluation_date = self.calendar.adjust(today) Settings().evaluation_date = evaluation_date day_counter = ActualActual() rpi_schedule = Schedule.from_rule(Date(20, 7, 2007), Date(20, 11, 2009), Period(1, Months), self.calendar, ModifiedFollowing) self.cpi_ts = ZeroInflationTermStructure() self.yts = FlatForward(evaluation_date, 0.05, day_counter) self.ii = UKRPI(False, self.cpi_ts) fix_data = [ 206.1, 207.3, 208.0, 208.9, 209.7, 210.9, 209.8, 211.4, 212.1, 214.0, 215.1, 216.8, 216.5, 217.2, 218.4, 217.7, 216, 212.9, 210.1, 211.4, 211.3, 211.5, 212.8, 213.4, 213.4, 213.4, 214.4 ] for date, data in zip(rpi_schedule, fix_data): self.ii.add_fixing(date, data) dates = [ Date(25, 11, 2010), Date(25, 11, 2011), Date(26, 11, 2012), Date(25, 11, 2013), Date(25, 11, 2014), Date(25, 11, 2015), Date(25, 11, 2016), Date(25, 11, 2017), Date(25, 11, 2018), Date(25, 11, 2019), Date(25, 11, 2021), Date(25, 11, 2024), Date(26, 11, 2029), Date(27, 11, 2034), Date(25, 11, 2039), Date(25, 11, 2049), Date(25, 11, 2059) ] rates = [ 3.0495, 2.93, 2.9795, 3.029, 3.1425, 3.211, 3.2675, 3.3625, 3.405, 3.48, 3.576, 3.649, 3.751, 3.77225, 3.77, 3.734, 3.714 ] observation_lag = Period('2M') self.helpers = [ZeroCouponInflationSwapHelper( SimpleQuote(r / 100), observation_lag, maturity, self.calendar, ModifiedFollowing, day_counter, self.ii, self.yts) \ for maturity, r in zip(dates, rates)] base_zero_rate = rates[0] / 100 self.cpi_ts.link_to( PiecewiseZeroInflationCurve(Interpolator.Linear, evaluation_date, self.calendar, day_counter, observation_lag, self.ii.frequency, self.ii.interpolated, base_zero_rate, self.helpers))
def setUp(self): self.calendar = UnitedKingdom() today = Date(25, 11, 2009) evaluation_date = self.calendar.adjust(today) Settings().evaluation_date = evaluation_date day_counter = ActualActual() rpi_schedule = Schedule.from_rule(Date(20, 7, 2007), Date(20, 11, 2009), Period(1, Months), self.calendar, ModifiedFollowing) self.cpi_ts = ZeroInflationTermStructure() self.yts = FlatForward(evaluation_date, 0.05, day_counter) self.ii = UKRPI(False, self.cpi_ts) fix_data = [206.1, 207.3, 208.0, 208.9, 209.7, 210.9, 209.8, 211.4, 212.1, 214.0, 215.1, 216.8, 216.5, 217.2, 218.4, 217.7, 216, 212.9, 210.1, 211.4, 211.3, 211.5, 212.8, 213.4, 213.4, 213.4, 214.4] for date, data in zip(rpi_schedule, fix_data): self.ii.add_fixing(date, data) dates = [Date(25, 11, 2010), Date(25, 11, 2011), Date(26, 11, 2012), Date(25, 11, 2013), Date(25, 11, 2014), Date(25, 11, 2015), Date(25, 11, 2016), Date(25, 11, 2017), Date(25, 11, 2018), Date(25, 11, 2019), Date(25, 11, 2021), Date(25, 11, 2024), Date(26, 11, 2029), Date(27, 11, 2034), Date(25, 11, 2039), Date(25, 11, 2049), Date(25, 11, 2059)] rates = [3.0495, 2.93, 2.9795, 3.029, 3.1425, 3.211, 3.2675, 3.3625, 3.405, 3.48, 3.576, 3.649, 3.751, 3.77225, 3.77, 3.734, 3.714] observation_lag = Period('2M') self.helpers = [ZeroCouponInflationSwapHelper( SimpleQuote(r / 100), observation_lag, maturity, self.calendar, ModifiedFollowing, day_counter, self.ii) \ for maturity, r in zip(dates, rates)] base_zero_rate = rates[0] / 100 self.cpi_ts.link_to( PiecewiseZeroInflationCurve(Interpolator.Linear, evaluation_date, self.calendar, day_counter, observation_lag, self.ii.frequency, self.ii.interpolated, base_zero_rate, self.yts, self.helpers))
payFixed = True fixedLegFrequency = Annual fixedLegAdjustment = Unadjusted fixedLegDayCounter = Thirty360() fixedRate = 0.04 floatingLegFrequency = Semiannual spread = 0.0 fixingDays = 2 index = Euribor6M(forecastTermStructure) floatingLegAdjustment = ModifiedFollowing floatingLegDayCounter = index.day_counter fixedSchedule = Schedule.from_rule(settlementDate, maturity, fixedLegTenor, calendar, fixedLegAdjustment, fixedLegAdjustment, Forward, False) floatingSchedule = Schedule.from_rule(settlementDate, maturity, floatingLegTenor, calendar, floatingLegAdjustment, floatingLegAdjustment, Forward, False) swapEngine = DiscountingSwapEngine(discountTermStructure) spot = VanillaSwap(Payer, nominal, fixedSchedule, fixedRate, fixedLegDayCounter, floatingSchedule, index, spread, floatingLegDayCounter) spot.set_pricing_engine(swapEngine) forwardStart = calendar.advance(settlementDate,1,Years) forwardEnd = calendar.advance(forwardStart,length,Years)