def simpleToContinuousRates(self, simpleRates, dates, day_counter=ql.Actual360()):
     continousRates = simpleRates.copy()
     for i in range(len(simpleRates) - 1):
         r_simple = ql.InterestRate(
             simpleRates[i + 1], ql.Actual360(), ql.Simple, ql.Once
         )
         t = day_counter.yearFraction(dates[i], dates[i + 1])
         r_continuous = r_simple.equivalentRate(ql.Continuous, ql.NoFrequency, t)
         continousRates[i + 1] = r_continuous.rate()
     return continousRates
Exemple #2
0
def create_swaption_helpers(data, index, term_structure, engine):
    swaptions = []
    fixed_leg_tenor = ql.Period(1, ql.Years)
    fixed_leg_daycounter = ql.Actual360()
    floating_leg_daycounter = ql.Actual360()
    for d in data:
        vol_handle = ql.QuoteHandle(ql.SimpleQuote(d.volatility))
        helper = ql.SwaptionHelper(ql.Period(d.start, ql.Years),
                                   ql.Period(d.length,
                                             ql.Years), vol_handle, index,
                                   fixed_leg_tenor, fixed_leg_daycounter,
                                   floating_leg_daycounter, term_structure)
        helper.setPricingEngine(engine)
        swaptions.append(helper)
    return swaptions
Exemple #3
0
def build_volatility_curve(volatility):
    """Create volatility curve"""
    volatility = ql.SimpleQuote(volatility)
    volatility_curve = ql.BlackConstantVol(0, ql.TARGET(),
                                           ql.QuoteHandle(volatility),
                                           ql.Actual360())
    return volatility, volatility_curve
Exemple #4
0
def to_ql_day_counter(arg):
    """Converts a string with day_counter name to the corresponding QuantLib object.

    Parameters
    ----------
    arg: str

    Returns
    -------
    QuantLib.DayCounter

    """
    if arg.upper() == "THIRTY360E":
        return ql.Thirty360(ql.Thirty360.European)
    elif arg.upper() == "THIRTY360":
        return ql.Thirty360()
    elif arg.upper() == "ACTUAL360":
        return ql.Actual360()
    elif arg.upper() == "ACTUAL365":
        return ql.Actual365Fixed()
    elif arg.upper() == "ACTUALACTUAL":
        return ql.ActualActual(ql.ActualActual.ISMA)
    elif arg.upper() == "ACTUALACTUALISMA":
        return ql.ActualActual(ql.ActualActual.ISMA)
    elif arg.upper() == "ACTUALACTUALISDA":
        return ql.ActualActual(ql.ActualActual.ISDA)
    elif arg.upper() == "BUSINESS252":
        return ql.Business252()
    else:
        raise ValueError(
            "Unable to convert {} to a QuantLib day counter".format(arg))
def testPiecewiseeFlatForwardCurve(asOfDate, basis, curveId):
    curve = builder.PiecewiseFlatForwardCurve(asOfDate, basis, curveId)
    # print(curve.dayCounter())
    today = curve.referenceDate()
    end = today + ql.Period(2, ql.Years)
    dates = [
        ql.Date(serial) for serial in range(today.serialNumber(),
                                            end.serialNumber() + 1)
    ]

    rates_c = [
        curve.forwardRate(d,
                          ql.TARGET().advance(d, 1, ql.Days), ql.Actual360(),
                          ql.Simple).rate() for d in dates
    ]
    pydt = [
        datetime.datetime(d.year(), d.month(), d.dayOfMonth()) for d in dates
    ]
    fig, ax = plt.subplots()
    fig.set_size_inches(10.5, 9.5)
    ax.yaxis.set_major_formatter(
        FuncFormatter(lambda r, pos: '{:.2%}'.format(r)))
    plt.subplots_adjust(bottom=0.2)
    l, = plt.plot(pydt, rates_c)
    plt.show()
Exemple #6
0
def make_swap(start,
              maturity,
              notional,
              fixed_rate,
              index,
              typ=Qlb.VanillaSwap.Payer):
    end = Qlb.TARGET().advance(start, maturity)
    fixed_leg_tenor = Qlb.Period('6m')
    fixed_leg_bdc = Qlb.ModifiedFollowing
    fixed_leg_dc = Qlb.Actual360()
    spread = 0.0
    fixed_leg_schedule = Qlb.Schedule(start, end, fixed_leg_tenor,
                                      index.fixingCalendar(), fixed_leg_bdc,
                                      fixed_leg_bdc,
                                      Qlb.DateGeneration.Backward, False)
    floater_leg_schedule = Qlb.Schedule(start, end, index.tenor(),
                                        index.fixingCalendar(),
                                        index.businessDayConvention(),
                                        index.businessDayConvention(),
                                        Qlb.DateGeneration.Backward, False)
    swap = Qlb.VanillaSwap(typ, notional, fixed_leg_schedule, fixed_rate,
                           fixed_leg_dc, floater_leg_schedule, index, spread,
                           index.dayCounter())

    return swap, [index.fixingDate(x) for x in floater_leg_schedule][:-1]
def jpy_3m_example():
    calendar = objects.get('JAPAN')
    start = ql.Date(15, 3, 2020)
    maturity = ql.Date(15, 6, 2020)

    fixedSchedule = ql.MakeSchedule(start,
                                    maturity,
                                    ql.Period('3M'),
                                    calendar=calendar)
    floatSchedule = ql.MakeSchedule(start,
                                    maturity,
                                    ql.Period('3M'),
                                    calendar=calendar)

    jpy_3m_crv = curves.get('JPY.3M')
    jpy_3m_yts = ql.YieldTermStructureHandle(jpy_3m_crv)

    jpy_libor_3m = objects.get('JPY.3M').clone(jpy_3m_yts)

    jpy_yts = ql.YieldTermStructureHandle(curves.get('JPY.OIS'))
    engine = ql.DiscountingSwapEngine(jpy_yts)

    swap = ql.VanillaSwap(ql.VanillaSwap.Receiver, 1e9, fixedSchedule,
                          -0.15 / 100, ql.Actual365Fixed(), floatSchedule,
                          jpy_libor_3m, 0, ql.Actual360())
    swap.setPricingEngine(engine)

    print(f"Swap NPV  : {swap.NPV():,.2f}")
    print(f"Swap Rate  : {swap.fairRate() * 100:,.6f}")
def uf_list(date_dt, infla, yieldcurve_clp, yieldcurve_uf):
    #Quantlib day_count, compounding and frequency convention
    day_count = ql.Actual360()
    compounding = ql.Simple
    freq = ql.Annual
    #Todays UF
    actual_uf = infla[infla['Dates'] == date_dt]['UF'].iloc[0]
    #Today date in quantlib format from datetime format
    today_date_ql = ql.Date(date_dt.day, date_dt.month, date_dt.year)
    size = len(infla)
    dates = []
    ufs = []
    i = 0
    for i in range(7300 - size):
        #next date to calculate UF
        i += 1
        calc_date = infla['Dates'].iloc[-1] + Day(i, normalize=True)
        aux_date_ql = ql.Date(calc_date.day, calc_date.month, calc_date.year)
        yrs = day_count.yearFraction(today_date_ql, aux_date_ql)

        rate_uf = yieldcurve_uf.zeroRate(yrs, compounding, freq).rate()
        rate_clp = yieldcurve_clp.zeroRate(yrs, compounding, freq).rate()
        next_uf = (1 + rate_clp * yrs) * actual_uf / (1 + rate_uf * yrs)

        dates.append(calc_date)
        ufs.append(next_uf)

    tmp = pd.DataFrame.from_dict({'Dates': dates, 'UF': ufs})
    final = pd.concat([infla, tmp], ignore_index=True)
    return final
def usd_1m_example():
    calendar = ql.UnitedStates()
    start = ql.Date(17, 11, 2019)
    maturity = calendar.advance(start, ql.Period('5y'))

    fixedSchedule = ql.MakeSchedule(start, maturity, ql.Period('6M'))
    floatSchedule = ql.MakeSchedule(start, maturity, ql.Period('1M'))

    usd_1m_crv = curves['USD.1M']
    usd_1m_yts = ql.YieldTermStructureHandle(usd_1m_crv)
    usd_libor_1m = ql.USDLibor(ql.Period('1M'), usd_1m_yts)

    usd_yts = ql.YieldTermStructureHandle(curves['USD.OIS'])
    engine = ql.DiscountingSwapEngine(usd_yts)

    fixedRate = 0.354500 / 100
    swap = ql.VanillaSwap(ql.VanillaSwap.Receiver, 10e6, fixedSchedule,
                          fixedRate, ql.Thirty360(), floatSchedule,
                          usd_libor_1m, 0, ql.Actual360())
    swap.setPricingEngine(engine)

    bbg_mtm = -573192.53
    print(f"Swap NPV  : {swap.NPV():,.2f}")
    print(f"BBG  NPV  : {bbg_mtm:,.2f}")
    print(f"Swap PV01 : {swap.fixedLegBPS():,.2f}")
    dif = bbg_mtm - swap.NPV()
    print(f"Diff      : {dif:,.2f}")
    print(f"Diff (bps): {dif / swap.fixedLegBPS():,.2f}")
Exemple #10
0
    def day_count_fraction(self, dcf):
        """
        day_count_fraction takes a string that is used in the
        conventions DataFrame, and returns the QuantLib-equivalent
        day-count object. This function takes all day-counters that are
        currently allowed in QuantLib.

        Currently, the function takes the following strings:
        Act360, Act365Fixed, ActAct, Bus252, 30360

        Args:
            dcf (str): day count fraction string

        Returns:
            object: QuantLib day-count-fraction object
        """
        if dcf == 'Act360':
            return qlib.Actual360()
        elif dcf == 'Act365Fixed':
            return qlib.Actual365Fixed()
        elif dcf == 'ActAct':
            return qlib.ActualActual()
        elif dcf == 'Bus252':
            return qlib.Business252()
        elif dcf == '30360':
            return qlib.Thirty360()
Exemple #11
0
def CURVE(today, ticker, quote):

    depo = quote[quote['InstType'] == 'CASH']
    futures = quote[quote['InstType'] == 'FUTURE']
    swap = quote[quote['InstType'] == 'SWAP']

    todays_date = ql.Date(today.day, today.month, today.year)
    ql.Settings.instance().evaluationDate = todays_date

    if ticker == 'USDIRS':
        calendar = ql.UnitedStates()
        dayCounter = ql.Actual360()
        adjustment = ql.ModifiedFollowing
        settlementDays = 2
        frequency = ql.Semiannual

    elif ticker == 'KRWIRS':
        calendar = ql.SouthKorea()
        dayCounter = ql.Actual365Fixed()
        adjustment = ql.ModifiedFollowing
        settlementDays = 2
        frequency = ql.Quarterly

    # Build Rate Helpers
    # 1. Deposit Rate Helper
    depositHelpers = [
        ql.DepositRateHelper(ql.QuoteHandle(ql.SimpleQuote(rate / 100)),
                             ql.Period(int(day), ql.Days), settlementDays,
                             calendar, adjustment, False, dayCounter)
        for day, rate in zip(depo['DaysToMaturity'], depo['Market.Mid'])
    ]

    # 2. Fra Rate Helper
    futuresHelpers = []
    for i, price in enumerate(futures['Market.Mid']):
        iborStartDate = ql.Date(futures['Maturity'][i].day,
                                futures['Maturity'][i].month,
                                futures['Maturity'][i].year)

        futuresHelper = ql.FuturesRateHelper(
            ql.QuoteHandle(ql.SimpleQuote(price)), iborStartDate, 3, calendar,
            adjustment, False, dayCounter)

        futuresHelpers.append(futuresHelper)

    # 3. Swap Rate Helper
    swapHelpers = [
        ql.SwapRateHelper(ql.QuoteHandle(ql.SimpleQuote(rate / 100)),
                          ql.Period(int(day), ql.Days), calendar, frequency,
                          adjustment, dayCounter, ql.Euribor3M())
        for day, rate in zip(swap['DaysToMaturity'], swap['Market.Mid'])
    ]

    # Curve Construction
    helpers = depositHelpers + futuresHelpers + swapHelpers
    depoFuturesSwapCurve = ql.PiecewiseLinearZero(todays_date, helpers,
                                                  dayCounter)

    return depoFuturesSwapCurve
Exemple #12
0
def Hazard_Rate_Ex2(interest_rate, matrix, LGD, Maturity):
    """
    Purpose:
        Compute the expected hazard rate through Stripping
    Input:
        interest_rate   compound interest rates
        matrix          the matrix of maturity and CDS Spread
        LGD             Loss Given Default
        Maturity        Vector of the CDS Maturities 
    
    Return value:   
        Fwd              The forward hazard rate
    """

    recoveryRate = 1 - LGD
    settlementDate = ql.Date().todaysDate()
    yts = ql.FlatForward(2, ql.TARGET(), interest_rate,
                         ql.Actual360())  #Rate of changing

    #Maturities
    CDS_tenors = [ql.Period(1, ql.Years), ql.Period(3, ql.Years), ql.Period(5, ql.Years), ql.Period(7, ql.Years), \
        ql.Period(10, ql.Years)]
    #Spread
    Spread = [160, 180, 200, 210, 250]

    CDSHelpers_ctpy = [ql.SpreadCdsHelper((CDS_spread / 10000.0), CDS_tenor, 0, ql.TARGET(), ql.Quarterly, ql.Following, \
        ql.DateGeneration.TwentiethIMM, ql.Actual360(), recoveryRate, ql.YieldTermStructureHandle(yts))
    for CDS_spread, CDS_tenor in zip(Spread, CDS_tenors)]

    Fwd_Hazard_rate = ql.PiecewiseFlatHazardRate(settlementDate,
                                                 CDSHelpers_ctpy,
                                                 ql.Thirty360())

    #pd_curve are tuples
    print("Calibrated hazard rate values: ")
    for x in Fwd_Hazard_rate.nodes():
        print("fwd hazard rate on %s is %.7f" % x)

    test = list(Fwd_Hazard_rate.nodes())
    Fwd = []
    for i in range(len(test)):
        Fwd.append(list(test[i]))
    Fwd.pop(0)
    Fwd = np.array(Fwd)

    return Fwd
Exemple #13
0
def build_interest_curve(rate_risk_free):
    """Create interest curve"""
    rate_risk_free = ql.SimpleQuote(rate_risk_free)
    rate_risk_free_curve = ql.FlatForward(0, ql.TARGET(),
                                          ql.QuoteHandle(rate_risk_free),
                                          ql.Actual360())

    return rate_risk_free_curve
def testOrStmt():
    basis = 'Act/360'
    # basis = ql.Actual360()
    day_count = None
    print(day_count, type(day_count))
    day_count = basis if isinstance(basis, ql.DayCounter) else ql.Actual360()
    print(day_count, type(day_count))
    print(isinstance(day_count, ql.DayCounter))
Exemple #15
0
class DayCount:
    ACT360 = ql.Actual360()
    ACT365Fixed = ql.Actual365Fixed()
    _30360BB = ql.Thirty360(ql.Thirty360.BondBasis)
    _30E360 = ql.Thirty360(ql.Thirty360.EurobondBasis)
    _30360US = ql.Thirty360(ql.Thirty360.USA)
    ACT365NL = ql.Actual365NoLeap()
    ACTACT = ql.ActualActual()
 def to_dayCounter(s):
     if (s.upper() == 'ACTUAL360'): return ql.Actual360()
     if (s.upper() == 'ACTUAL365FIXED'): return ql.Actual365Fixed()
     if (s.upper() == 'ACTUALACTUAL'): return ql.ActualActual()
     if (s.upper() == 'ACTUAL365NOLEAP'): return ql.Actual365NoLeap()
     if (s.upper() == 'BUSINESS252'): return ql.Business252()
     if (s.upper() == 'ONEDAYCOUNTER'): return ql.OneDayCounter()
     if (s.upper() == 'SIMPLEDAYCOUNTER'): return ql.SimpleDayCounter()
     if (s.upper() == 'THIRTY360'): return ql.Thirty360()
Exemple #17
0
    def makeInstrument(self, swapType, notional, start, maturity, index1,
                       index2, spread):
        schedule1 = ql.MakeSchedule(start, maturity, index1.tenor())

        leg1 = ql.IborLeg([notional], schedule1, index1)
        schedule2 = ql.MakeSchedule(start, maturity, index1.tenor())
        leg2 = ql.OvernightLeg([notional], schedule2, index2, ql.Actual360(),
                               ql.Following, [1], [spread], True)
        return ql.Swap(leg1, leg2)
 def defineParameters(self):
     self.calendar = CalendarCl(2001,50)
     self.day_counter = ql.Actual360()
     self.settlement_days = 2
     self.fixing_days = 0
     self.ICP = ql.OvernightIndex("ICP",
                                 self.settlement_days,
                                 ql.CLPCurrency(),
                                 self.calendar,
                                 self.day_counter)
Exemple #19
0
    def __init__(self, ccy):
        self.ccy = ccy
        if self.ccy == 'USD':
            self.calendar = ql.UnitedStates()
            self.dayCounter = ql.Actual360()
            self.depo = {
                'settlementDays': 2,
                'dayCounterConvention': ql.Actual360(),
                'businessDayConvention': ql.ModifiedFollowing,
                'EOM': False
            }
            self.futures = {
                'settlementDays': 2,
                'months': 3,
                'dayCounterConvention': ql.Actual360(),
                'businessDayConvention': ql.ModifiedFollowing,
                'EOM': True
            }
            self.swap = {
                'settlementDays': 2,
                'fixedLegFrequency': ql.Semiannual,
                'fixedLegTenor': ql.Period(6, ql.Months),
                'fixedLegAdjustment': ql.Unadjusted,
                'fixedLegDayCounter': ql.Actual360(),
                'floatingLegFrequency': ql.Quarterly,
                'floatingLegTenor': ql.Period(3, ql.Months),
                'floatingLegAdjustment': ql.ModifiedFollowing,
                'index': ql.USDLibor(ql.Period(3, ql.Months)),
                'EOM': False
            }

            self.swaption = {
                'fittable': [('1Y', '1Y'), ('1Y', '2Y'), ('1Y', '5Y'),
                             ('1Y', '10Y'), ('1Y', '20Y'), ('2Y', '1Y'),
                             ('2Y', '2Y'), ('2Y', '5Y'), ('2Y', '10Y'),
                             ('2Y', '20Y'), ('5Y', '1Y'), ('5Y', '2Y'),
                             ('5Y', '5Y'), ('5Y', '10Y'), ('5Y', '20Y'),
                             ('10Y', '1Y'), ('10Y', '2Y'), ('10Y', '5Y'),
                             ('10Y', '10Y'), ('10Y', '15Y'), ('20Y', '1Y'),
                             ('20Y', '2Y'), ('20Y', '5Y'), ('15Y', '10Y')]
            }
Exemple #20
0
 def timesteps(self):
     if self._timesteps is None:
         months = range(3, 12 * self.nb_years + 1, 3)
         s_periods = [str(month) + "m" for month in months]
         dates = [as_of_date
                  ] + [as_of_date + ql.Period(s) for s in s_periods]
         T = [0] + [
             ql.Actual360().yearFraction(as_of_date, dates[i])
             for i in range(1, len(dates))
         ]
         self._timesteps = np.array(T)
     return self._timesteps
Exemple #21
0
    def setUp(self):
        self.calendar = ql.TARGET()
        today = self.calendar.adjust(ql.Date.todaysDate())
        self.settlementDays = 2
        settlement = self.calendar.advance(today, self.settlementDays, ql.Days)
        deposits = [
            ql.DepositRateHelper(
                ql.QuoteHandle(ql.SimpleQuote(rate / 100)),
                ql.Period(n, units),
                self.settlementDays,
                self.calendar,
                ql.ModifiedFollowing,
                False,
                ql.Actual360(),
            ) for (n, units, rate) in [
                (1, ql.Months, 4.581),
                (2, ql.Months, 4.573),
                (3, ql.Months, 4.557),
                (6, ql.Months, 4.496),
                (9, ql.Months, 4.490),
            ]
        ]
        swaps = [
            ql.SwapRateHelper(
                ql.QuoteHandle(ql.SimpleQuote(rate / 100)),
                ql.Period(years, ql.Years),
                self.calendar,
                ql.Annual,
                ql.Unadjusted,
                ql.Thirty360(),
                ql.Euribor6M(),
            ) for (years,
                   rate) in [(1, 4.54), (5, 4.99), (10,
                                                    5.47), (20,
                                                            5.89), (30, 5.96)]
        ]

        self.termStructure = ql.PiecewiseFlatForward(settlement,
                                                     deposits + swaps,
                                                     ql.Actual360())
Exemple #22
0
def str2dc(s):
    s = s.lower()
    if s=="actual365fixed" or s=="act365":
        return ql.Actual365Fixed()
    elif s=="actualactual" or s=="actact":
        return ql.ActualActual()
    elif s=="actual360" or s=="act360":
        return ql.Actual360()
    elif s=="Thirty360" or s=="30360":
        return ql.Thirty360()
    else:
        print("Period String Error")
        
def plot(curve):
    today = curve.referenceDate()
    end = today + ql.Period(2, ql.Years)
    dates = [
        ql.Date(serial) for serial in range(today.serialNumber(),
                                            end.serialNumber() + 1)
    ]

    rates = [
        curve.forwardRate(d,
                          ql.TARGET().advance(d, 1, ql.Days), ql.Actual360(),
                          ql.Simple).rate() for d in dates
    ]
    plotCurve(dates, rates)
Exemple #24
0
 def _calculate_cc(self, date, spot_price, currency_curve, di_curve):
     maturity_date = self._maturity_on_the_run(date)
     future_price = currency_curve.exchange_rate_to_date(
         date, maturity_date)
     di = di_curve.zero_rate_to_date(date, maturity_date, ql.Compounded,
                                     ql.Annual)
     di_rate = ql.InterestRate(di, ql.Business252(), ql.Compounded,
                               ql.Annual)
     date = to_ql_date(date)
     compound = spot_price * di_rate.compoundFactor(
         date, maturity_date) / future_price
     rate = ql.InterestRate.impliedRate(compound, ql.Actual360(), ql.Simple,
                                        ql.Annual, date, maturity_date)
     return rate.rate()
def Prepare_BSM_Option(spot,
                       strike,
                       rd,
                       rf,
                       vol,
                       init_date,
                       exp_date,
                       type='call'):
    today = qt.Date(init_date.day, init_date.month, init_date.year)
    qt.Settings.instance().evaluationDate = today
    if type == 'call':
        option = qt.EuropeanOption(
            qt.PlainVanillaPayoff(qt.Option.Call, strike),
            qt.EuropeanExercise(
                qt.Date(exp_date.day, exp_date.month, exp_date.year)))
    else:
        option = qt.EuropeanOption(
            qt.PlainVanillaPayoff(qt.Option.Put, strike),
            qt.EuropeanExercise(
                qt.Date(exp_date.day, exp_date.month, exp_date.year)))

    u = qt.SimpleQuote(spot)
    rd = qt.SimpleQuote(rd)
    rf = qt.SimpleQuote(rf)
    sigma = qt.SimpleQuote(vol)
    rf = qt.FlatForward(0, qt.TARGET(), qt.QuoteHandle(rf), qt.Actual360())
    rd = qt.FlatForward(0, qt.TARGET(), qt.QuoteHandle(rd), qt.Actual360())
    volatility = qt.BlackConstantVol(0, qt.TARGET(), qt.QuoteHandle(sigma),
                                     qt.Actual360())
    process = qt.GarmanKohlagenProcess(
        qt.QuoteHandle(u), qt.YieldTermStructureHandle(rf),
        qt.YieldTermStructureHandle(rd),
        qt.BlackVolTermStructureHandle(volatility))
    engine = qt.AnalyticEuropeanEngine(process)
    option.setPricingEngine(engine)
    return option
def plot3(curve):
    today = curve.referenceDate()
    dates = [today + ql.Period(i, ql.Months) for i in range(0, 12 * 60 + 1)]
    rates = [
        curve.forwardRate(d,
                          ql.TARGET().advance(d, 1, ql.Days), ql.Actual360(),
                          ql.Simple).rate() for d in dates
    ]
    pydt = [
        datetime.datetime(d.year(), d.month(), d.dayOfMonth()) for d in dates
    ]
    fig, ax = plt.subplots()
    fig.set_size_inches(12.5, 10.5)
    l, = plt.plot(pydt, rates, '.', lw=2)
    plt.show()
def testPiecewiseLinearZeroCurve(asOfDate, basis, curveId):
    crv = builder.PiecewiseLinearZeroCurve(asOfDate, basis, curveId)
    print(crv.dayCounter())
    for node in crv.nodes():
        print(node)
    dates, rates = zip(*crv.nodes())
    crv2 = ql.ZeroCurve(dates, rates, ql.Actual360())
    spot = crv.referenceDate()
    sample_dates = [spot + ql.Period(i, ql.Weeks) for i in range(15 * 52)]
    z1 = [
        crv.zeroRate(d, ql.Actual360(), ql.Continuous).rate()
        for d in sample_dates
    ]
    z2 = [
        crv2.zeroRate(d, ql.Actual360(), ql.Continuous).rate()
        for d in sample_dates
    ]

    fig = plt.figure(figsize=(12, 6))
    ax = fig.add_subplot(1, 1, 1)
    ax.plot_date([d.to_date() for d in sample_dates], z1, '.')
    ax.plot_date([d.to_date() for d in sample_dates], z2, '-')

    plt.show()
def testPiecewiseLogCubicCurve(asOfDate, basis, curveId):
    curve = builder.PiecewiseLogCubicDiscountCurve(asOfDate, basis, curveId)
    today = curve.referenceDate()
    end = today + ql.Period(2, ql.Years)
    dates = [
        ql.Date(serial) for serial in range(today.serialNumber(),
                                            end.serialNumber() + 1)
    ]

    rates = [
        curve.forwardRate(d,
                          ql.TARGET().advance(d, 1, ql.Days), ql.Actual360(),
                          ql.Simple).rate() for d in dates
    ]
    plotCurve(dates, rates, '.')
Exemple #29
0
    def makeSwap(self,
                 optionTenor,
                 swapTenor,
                 swapRate=None,
                 spread=0.0,
                 notional=1.0,
                 fixedLegTenor=ql.Period('6M'),
                 fixedLegDayCounter=ql.Actual360(),
                 endOfMonth=False):
        self.optionTenor = optionTenor
        self.swapTenor = swapTenor
        self.notional = notional
        self.strike = swapRate
        self.fixedLegTenor = fixedLegTenor
        self.fixedLegDayCounter = fixedLegDayCounter
        self.endOfMonth = endOfMonth
        self.exerciseDate = self.cal.advance(self.baseDate, optionTenor)
        self.effectiveDate = self.cal.advance(self.exerciseDate,
                                              self.settlementDays)
        self.maturity = self.cal.advance(self.effectiveDate, swapTenor)

        # schedule generation
        self.fixedSchedule = ql.Schedule(self.effectiveDate, self.maturity,
                                         self.fixedLegTenor, self.cal,
                                         ql.ModifiedFollowing,
                                         ql.ModifiedFollowing,
                                         ql.DateGeneration.Forward, endOfMonth)
        self.floatSchedule = ql.Schedule(self.effectiveDate, self.maturity,
                                         self.iborIndex.tenor(), self.cal,
                                         ql.ModifiedFollowing,
                                         ql.ModifiedFollowing,
                                         ql.DateGeneration.Forward, endOfMonth)

        # make vanilla swap
        self.payerSwap = ql.VanillaSwap(ql.VanillaSwap.Payer, notional,
                                        self.fixedSchedule, swapRate,
                                        self.fixedLegDayCounter,
                                        self.floatSchedule, self.iborIndex,
                                        spread, self.iborIndex.dayCounter())
        self.receiverSwap = ql.VanillaSwap(ql.VanillaSwap.Receiver, notional,
                                           self.fixedSchedule, swapRate,
                                           self.fixedLegDayCounter,
                                           self.floatSchedule, self.iborIndex,
                                           spread, self.iborIndex.dayCounter())
        swapEngine = ql.DiscountingSwapEngine(self.discountTermStructure)
        self.payerSwap.setPricingEngine(swapEngine)
        self.receiverSwap.setPricingEngine(swapEngine)
        self.fairRate = self.payerSwap.fairRate()
Exemple #30
0
    def getForwardCurve(spotCurve):
        today = spotCurve.referenceDate()
        end = today + ql.Period(50, ql.Years)

        dates = [
            ql.Date(serial) for serial in range(today.serialNumber(),
                                                end.serialNumber() + 1)
        ]

        rates_c = [
            spotCurve.forwardRate(d,
                                  ql.TARGET().advance(d, 1, ql.Days),
                                  ql.Actual360(), ql.Simple).rate()
            for d in dates
        ]
        return rates_c