コード例 #1
0
ファイル: market.py プロジェクト: xie3ge/pyql
    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
コード例 #2
0
ファイル: test_swap.py プロジェクト: xie3ge/pyql
    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)
コード例 #3
0
                                      fixedLegConvention, fixedLegConvention,
                                      Forward, False)
fwdFloatSchedule = Schedule.from_rule(fwdStart, fwdMaturity,
                                      Period(floatingLegFrequency), calendar,
                                      floatingLegConvention,
                                      floatingLegConvention, Forward, False)
oneYearForward5YearSwap = VanillaSwap(swapType, nominal, fwdFixedSchedule,
                                      fixedRate, fixedLegDayCounter,
                                      fwdFloatSchedule, euriborIndex, spread,
                                      floatingLegDayCounter)

# /***************
# * SWAP PRICING *
# ****************/

swapEngine = DiscountingSwapEngine(discountingTermStructure)

spot5YearSwap.set_pricing_engine(swapEngine)
oneYearForward5YearSwap.set_pricing_engine(swapEngine)

headers = ["", "net present value", "fair spread", "fair fixed rate"]

s5yRate = swap_quotes[5 * Years]
NPV = spot5YearSwap.npv
fairSpread = spot5YearSwap.fair_spread
fairRate = spot5YearSwap.fair_rate

vals1 = [f"5-years swap paying {fixedRate:.2%}", NPV, fairSpread, fairRate]

# now let's price the 1Y forward 5Y swap
NPV = oneYearForward5YearSwap.npv