简体   繁体   English

QuantLib:面值掉期率计算

[英]QuantLib: par swap rate calculation

I would like to calculate the par swap rates (ie, the fixed leg rates), for swaps traded at par (ie market value = 0), given a zero-coupon curve with observed maturities ranging from 3 months to 120 months.我想计算以面值交易的掉期(即市场价值 = 0)的面值掉期利率(即固定腿利率),给定观察到的到期期限为 3 个月至 120 个月的零息票曲线。

Here's what I did:这是我所做的:

# 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())

However, this is to obtain the market value of a swap with observed fixed rate = FIXED_RATE但是,这是为了获得观察到的固定利率 = FIXED_RATE的掉期市场价值

Instead, I want the rate for a given observed market value (zero).相反,我想要给定观察到的市场价值(零)的汇率。

Many thanks in advance for your help.非常感谢您的帮助。

Calling irs.fairRate() is correct: it ignores the passed fixed rate and gives you the rate corresponding to value = 0. Instead, irs.NPV() would give you the market rate given the fixed rate.调用irs.fairRate()是正确的:它忽略传递的固定利率,并为您提供与 value = 0 对应的利率。相反, irs.NPV()会给您提供固定利率的市场利率。

However, the way you're creating the swaps is not correct.但是,您创建交换的方式不正确。 The second loop should be something like:第二个循环应该是这样的:

for maturity_date in maturities:

which will give you the correct maturity date for each swap.这将为您提供每次掉期的正确到期日。 Currently, you're iterating again over tenor but you don't redefine maturity_date , so you're reusing over and over again its current value, which happens to be the last value from the previous loop.目前,您正在对tenor再次进行迭代,但您没有重新定义maturity_date日期,因此您一遍又一遍地重用其当前值,而这恰好是前一个循环中的最后一个值。

Inside the loop, fixedLegTenor should be set to the length of a fixed-rate coupon (I'm guessing ql.Period(3, ql.Months) like the floating-rate coupons?)在循环内部, fixedLegTenor应该设置为固定利率优惠券的长度(我猜ql.Period(3, ql.Months)就像浮动利率优惠券一样?)

Finally: yes, you can use any interest-rate term structure for Libor, including ql.ZeroCurve .最后:是的,您可以为 Libor 使用任何利率期限结构,包括ql.ZeroCurve

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM