程式
使用 QuantLib Python 在利率掉期估值中指定浮動邊的固定天數
我正在嘗試使用 QuantLib Python 為利率掉期定價,一切似乎都很好。但是,我似乎無法理解在哪裡可以指定修復天數。
以下是我的程式碼。請注意,用於貼現和美國 Libor 的曲線是假設的,僅用於說明目的。
import QuantLib as ql valuationDate = ql.Date(30, 6, 2020) ql.Settings.instance().evaluationDate = valuationDate dayConvention = ql.Actual360() calendar = ql.UnitedStates() businessConvention = ql.Following settlementDays = 3 settlementDate = calendar.advance(valuationDate, ql.Period(settlementDays, ql.Days)) zeroCurve = ql.ZeroCurve([ql.Date(30, 6, 2020), ql.Date(30, 6, 2021), ql.Date(30, 6, 2022), ql.Date(30, 7, 2025)], [0.05, 0.06, 0.06, 0.07], dayConvention, calendar, ql.Linear(), ql.Compounded) handle = ql.YieldTermStructureHandle(zeroCurve) fixedFrequency = ql.Annual floatFrequency = ql.Semiannual floatIndex = ql.USDLibor(ql.Period(floatFrequency), handle) floatIndex.addFixing(ql.Date(30, 12, 2019), 0.01) floatIndex.addFixing(ql.Date(29, 6, 2020), 0.02) issueDate = ql.Date(1, 1, 2019) maturityDate = ql.Date(1, 1, 2023) fixedLegTenor = ql.Period(fixedFrequency) fixedSchedule = ql.Schedule(settlementDate, maturityDate, fixedLegTenor, calendar, businessConvention, businessConvention, ql.DateGeneration.Forward, True) floatLegTenor = ql.Period(floatFrequency) floatSchedule = ql.Schedule(settlementDate, maturityDate, floatLegTenor, calendar, businessConvention, businessConvention, ql.DateGeneration.Forward, True) notional = 34000 fixedRate = 0.03 fixedLegDayCount = ql.Actual365Fixed() floatSpread = 0.01 floatLegDayCount = ql.Actual360() irs = ql.VanillaSwap(ql.VanillaSwap.Payer, notional, fixedSchedule, fixedRate, fixedLegDayCount, floatSchedule, floatIndex, floatSpread, floatLegDayCount) discounting = ql.DiscountingSwapEngine(handle) irs.setPricingEngine(discounting)
從 QuantLib 中,固定天數為 2,如下所示:
for i, cf in enumerate(irs.leg(1)): c = ql.as_floating_rate_coupon(cf) if c: print(c.fixingDays())
返回
2
2
2
2
2
有沒有辦法可以指定另一個固定天數,比如 1?
謝謝你的幫助!
VanillaSwap
模擬簡單的交換,所以它沒有很多花里胡哨。要獲得更多控制,您可以分別創建兩條腿並使用Swap
類。在你的情況下:fixed_leg = ql.FixedRateLeg(fixedSchedule, fixedLegDayCount, [notional], [fixedRate]) floating_leg = ql.IborLeg([notional], floatSchedule, floatIndex, floatLegDayCount, spreads=[floatSpread], fixingDays=[1]) irs = ql.Swap(fixed_leg, floating_leg) irs.setPricingEngine(discounting)
它的建構子
Swap
需要兩條腿,假設第一條是付費的,第二條是收到的。至於固定天數:
for cf in floating_leg: c = ql.as_floating_rate_coupon(cf) if c: print(c.fixingDays())
輸出是:
1 1 1 1 1