How to use the quantlib.time.api.Period function in QuantLib

To help you get started, we’ve selected a few QuantLib examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github enthought / pyql / examples / scripts / make_zero_coupon.py View on Github external
swapData = [[1, Years, 'Swap1Y'],
                [2, Years, 'Swap2Y'],
                [3, Years, 'Swap3Y'],
                [4, Years, 'Swap4Y'],
                [5, Years, 'Swap5Y'],
                [7, Years, 'Swap7Y'],
                [10, Years, 'Swap10Y'],
                [30, Years, 'Swap30Y']]

    rate_helpers = []

    end_of_month = True

    for m, period, label in depositData:
        tenor = Period(m, Months)
        rate = df_libor.get_value(dtObs, label)
        helper = DepositRateHelper(SimpleQuote(rate / 100.0), tenor,
                                   settlement_days,
                                   calendar, ModifiedFollowing,
                                   end_of_month,
                                   Actual360())

        rate_helpers.append(helper)

    liborIndex = Libor('USD Libor', Period(3, Months),
                       settlement_days,
                       USDCurrency(), calendar,
                       Actual360())

    spread = SimpleQuote(0)
    fwdStart = Period(0, Days)
github enthought / pyql / examples / make_zc.py View on Github external
endOfMonth = True

    liborIndex = Libor('USD Libor', Period(6, Months),
                       settlement_days,
                       USDCurrency(), calendar,
                       ModifiedFollowing,
                       endOfMonth, Actual360())

    spread = SimpleQuote(0)
    fwdStart = Period(0, Days)

    for m, period, label in swapData:
        rate = df_libor.get_value(dtObs, label)
        helper = SwapRateHelper(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 = term_structure_factory(
        'discount', 'loglinear', settlement_date, rate_helpers,
        ts_day_counter, tolerance)

    return ts
github enthought / pyql / quantlib / market / market.py View on Github external
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
github enthought / pyql / examples / option_valuation.py View on Github external
UnitedStates(), Actual360()
    )

    print("Description of the Swap used for the Yield Curve construction")
    print("Tenor of the fixed leg:			", Swap_fixedLegTenor)
    print("Index of the floated leg: 		", Swap_iborIndex.name)
    print("Maturity		Rate				")

    swapRates = []
    swapRatesTenor = []
    # INPUT : all the following data are input : the rate and the corresponding tenor
    #		You could make the choice of more or less data
    #		--> However you have tho choice the instruments with different maturities
    swapRates = [0.005681, 0.006970, 0.009310, 0.012010, 0.014628, 0.016881, 0.018745,
                 0.020260, 0.021545]
    swapRatesTenor = [Period(i, Years) for i in range(2, 11)]
    
    for tenor, rate in zip(swapRatesTenor, swapRates):
        print(tenor, "\t\t\t", rate)
    
    # ++++++++++++++++++++ Creation of the vector of RateHelper (need for the Yield Curve construction)
    # ++++++++++++++++++++ Libor 
    LiborFamilyName = currency.name + "Libor"
    instruments = []
    for rate, tenor in zip(liborRates, liborRatesTenor):
        # Index description ___ creation of a Libor index
        liborIndex =  Libor(LiborFamilyName, tenor, settlement_days, currency, calendar,
                Libor_dayCounter)
        # Initialize rate helper	___ the DepositRateHelper link the recording rate with the Libor index													
        instruments.append(DepositRateHelper(rate, index=liborIndex))

    # +++++++++++++++++++++ Swap
github enthought / pyql / quantlib / market / market.py View on Github external
"""

        _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,
github enthought / pyql / examples / swap.py View on Github external
futuresHelpers = [ FuturesRateHelper(futures[d],
                                     d, months,
                                     calendar, ModifiedFollowing,
                                     True, day_counter)
                   for d in futures.keys() ]

settlementDays = 2
fixedLegFrequency = Annual
fixedLegTenor = Period(1,Years)
fixedLegAdjustment = Unadjusted
fixedLegDayCounter = Thirty360()
floatingLegFrequency = Semiannual
floatingLegTenor = Period(6,Months)
floatingLegAdjustment = ModifiedFollowing
swapHelpers = [ SwapRateHelper.from_tenor(swaps[(n,unit)],
                               Period(n,unit), calendar,
                               fixedLegFrequency, fixedLegAdjustment,
                               fixedLegDayCounter, Euribor6M())
                for n, unit in swaps.keys() ]

### Curve building 

ts_daycounter = ActualActual(ISDA)

# term-structure construction
helpers = depositHelpers + swapHelpers
depoSwapCurve = PiecewiseYieldCurve.from_reference_date(
    BootstrapTrait.Discount, Interpolator.LogLinear, settlementDate, helpers, ts_daycounter
)

helpers = depositHelpers[:2] + futuresHelpers + swapHelpers[1:]
depoFuturesSwapCurve = PiecewiseYieldCurve.from_reference_date(
github enthought / pyql / quantlib / util / rates.py View on Github external
settlement_days = 2
    settlement_date = calendar.advance(eval_date, settlement_days, Days)
    # must be a business day
    settlement_date = calendar.adjust(settlement_date)
    end_of_month = True

    if((rate_type == 'SWAP') & (period == 'Y')):
        liborIndex = Libor(
            'USD Libor', Period(6, Months), settlement_days,
            USDCurrency(), calendar, Actual360()
        )
        spread = SimpleQuote(0)
        fwdStart = Period(0, Days)
        helper = SwapRateHelper.from_tenor(
            SimpleQuote(rate),
            Period(tenor, Years),
            calendar, Annual,
            Unadjusted, Thirty360(),
            liborIndex, spread, fwdStart)
    elif((rate_type == 'LIBOR') & (period == 'M')):
        helper = DepositRateHelper(SimpleQuote(rate),
                                   Period(tenor, Months),
                                   settlement_days,
                                   calendar,
                                   ModifiedFollowing,
                                   end_of_month,
                                   Actual360())
    else:
        raise Exception("Rate type %s not supported" % label)

    return (helper)
github enthought / pyql / examples / calibrate_heston.py View on Github external
# loop through rows in option data frame, construct
    # helpers for bid/ask

    oneDay = datetime.timedelta(days=1)
    dtExpiry = [dtTrade + int(t * 365) * oneDay for t in df_option['T']]
    df_option['dtExpiry'] = dtExpiry

    options = []
    for index, row in df_option.T.iteritems():

        strike = row['K']
        if (strike / spot.value > 1.3) | (strike / spot.value < .7):
            continue

        days = int(365 * row['T'])
        maturity = Period(days, Days)

        options.append(
                HestonModelHelper(
                    maturity, calendar, spot.value,
                    strike, SimpleQuote(row['VB']),
                    risk_free_ts, dividend_ts,
                    ImpliedVolError))

        options.append(
                HestonModelHelper(
                    maturity, calendar, spot.value,
                    strike, SimpleQuote(row['VA']),
                    risk_free_ts, dividend_ts,
                    ImpliedVolError))

    return {'options': options, 'spot': spot}
github enthought / pyql / quantlib / market / market.py View on Github external
spread = SimpleQuote(0)
        fwdStart = Period(0, Days)
        helper = SwapRateHelper.from_tenor(
            quote_value,
            Period(tenor),
            market._floating_rate_index.fixing_calendar,
            Period(market._params.fixed_leg_period).frequency,
            BusinessDayConvention.from_name(
                market._params.fixed_leg_convention),
            DayCounter.from_name(market._params.fixed_leg_daycount),
            libor_index, spread, fwdStart)
    elif rate_type == 'DEP':
        end_of_month = True
        helper = DepositRateHelper(
            quote_value,
            Period(tenor),
            market._params.settlement_days,
            market._floating_rate_index.fixing_calendar,
            market._floating_rate_index.business_day_convention,
            end_of_month,
            DayCounter.from_name(market._deposit_daycount))
    elif rate_type == 'ED':
        if reference_date is None:
            raise Exception("Reference date needed with ED Futures data")

        forward_date = next_imm_date(reference_date, tenor)

        helper = FuturesRateHelper(
            price =SimpleQuote(quote_value),
            imm_date = qldate_from_pydate(forward_date),
            length_in_months = 3,
            calendar = market._floating_rate_index.fixing_calendar,
github enthought / pyql / examples / scripts / make_zero_coupon.py View on Github external
rate_helpers = []

    end_of_month = True

    for m, period, label in depositData:
        tenor = Period(m, Months)
        rate = df_libor.get_value(dtObs, label)
        helper = DepositRateHelper(SimpleQuote(rate / 100.0), tenor,
                                   settlement_days,
                                   calendar, ModifiedFollowing,
                                   end_of_month,
                                   Actual360())

        rate_helpers.append(helper)

    liborIndex = Libor('USD Libor', Period(3, Months),
                       settlement_days,
                       USDCurrency(), calendar,
                       Actual360())

    spread = SimpleQuote(0)
    fwdStart = Period(0, Days)

    for m, period, label in swapData:
        rate = df_libor.get_value(dtObs, label)
        helper = SwapRateHelper.from_tenor(
            SimpleQuote(rate / 100.0),
            Period(m, Years),
            calendar, Semiannual,
            ModifiedFollowing, Thirty360(),
            liborIndex, spread, fwdStart)