QuantLib:票面交换率计算

问题描述 投票:0回答:1

我想计算票面掉期利率(即固定边际利率),对于以票面价格交易(即市场价值= 0)的掉期,给定零息曲线,观察到的到期日为3个月至120个月。

这是我所做的:

# define constants
face_amount = 100
settlementDays = 0
calendar = ql.NullCalendar()
fixedLegAdjustment = ql.Unadjusted
floatingLegAdjustment = ql.Unadjusted
fixedLegDayCounter = ql.SimpleDayCounter()
floatingLegDayCounter = ql.SimpleDayCounter()
end_of_month = False
floating_rate = ql.IborIndex("MyIndex", ql.Period("3m"), settlementDays, ql.USDCurrency(), calendar, floatingLegAdjustment, end_of_month, floatingLegDayCounter)

# pre-allocate
irs = {}

# calculate dates
curve_date = ql.DateParser.parseFormatted("2020-05-26", "%Y-%m-%d")
ql.Settings.instance().evaluationDate = curve_date
spot_date = calendar.advance(curve_date, settlementDays, ql.Days)

# pre-allocate
irs_rate = []
tenors = []
maturity_dates = []
# loop over maturities
for tenor in np.arange(3, 120 + 1, 3):
    # maturity date
    maturity_date = calendar.advance(spot_date, ql.Period(int(tenor), ql.Months))
    # gather maturity dates
    maturity_dates.append(maturity_date)

# build zero coupon curve object
zero_curve = ql.YieldTermStructureHandle(ql.ZeroCurve(maturity_dates, zero_rates, fixedLegAdjustment, calendar))

# build swap curve
# loop over maturities
for tenor in np.arange(3, 120 + 1, 3):
    # fixed leg tenor
    fixedLegTenor = ql.Period(tenor, ql.Months)
    # fixed leg coupon schedule
    fixedLegSchedule = ql.Schedule(spot_date, maturity_date, 
                                 fixedLegTenor, calendar,
                                 fixedLegAdjustment, fixedLegAdjustment,
                                 ql.DateGeneration.Forward, end_of_month)

    # floating leg tenor
    floatingLegTenor = ql.Period(3, ql.Months)
    # floating leg coupon schedule
    floatingLegSchedule = ql.Schedule(spot_date, maturity_date,
                                  floatingLegTenor, calendar,
                                  floatingLegAdjustment, floatingLegAdjustment,
                                  ql.DateGeneration.Forward, end_of_month)

    # build swap pricer
    irs = ql.VanillaSwap(ql.VanillaSwap.Receiver, face_amount, fixedLegSchedule, FIXED_RATE, fixedLegDayCounter, floatingLegSchedule, floating_rate, 0, floatingLegDayCounter)

    # build swap curve
    swap_curve = ql.DiscountingSwapEngine(zero_curve)
    # get swap rate
    irs.setPricingEngine(swap_curve)
    # get par swap rate
    irs_rate.append(irs.fairRate())

但是,这是获得具有固定利率= FIXED_RATE的掉期的市场价值

相反,我想要给定观察到的市场价值的利率(零)。

非常感谢您的帮助。

swap quantlib
1个回答
0
投票

调用irs.fairRate()是正确的:它将忽略传递的固定汇率,并为您提供与value = 0相对应的汇率。相反,irs.NPV()将为您提供给定固定汇率的市场汇率。

但是,创建交换的方式不正确。第二个循环应类似于:

for maturity_date in maturities:

这将为您提供每次交换的正确到期日。当前,您正在tenor上进行迭代,但没有重新定义maturity_date,因此您将一遍又一遍地重用其当前值,该值恰好是上一个循环中的最后一个值。

在循环内,应将fixedLegTenor设置为固定利率优惠券的长度(我想ql.Period(3, ql.Months)就像浮动利率优惠券一样?]

最后:是的,您可以为Libor使用任何利率期限结构,包括ql.ZeroCurve

© www.soinside.com 2019 - 2024. All rights reserved.