程式
Quantlib:為什麼利率掉期估值會拋出“執行時錯誤負時間”?
# Set evaluation date spotRates = [0.02514, 0.026285, 0.027326299999999998, 0.0279, 0.029616299999999998, 0.026526, 0.026028, 0.0258695, 0.025958000000000002, 0.0261375, 0.026355, 0.0266255, 0.026898, 0.0271745, 0.02741, 0.027666, 0.028107000000000004, 0.028412000000000003, 0.028447, 0.0284165] spotPeriod = [Period(1, Weeks), Period(1, Months), Period(3, Months), Period(6, Months), Period(9, Months), Period(1, Years), Period(3, Years),Period(5, Years) Period(10, Years),Period(15, Years), Period(20, Years), Period(30, Years), Period(50, Years)]
不得不更改一些東西,因為您有 20 個即期匯率但只有 13 個即期期,並添加了一個虛假的定價,但這是一個有效的範例。
import QuantLib as ql import pandas as pd todaysDate = ql.Date(13, 9, 2019) calendar = ql.SouthAfrica() day_count = ql.Actual365Fixed() currency = ql.ZARCurrency() todaysDate = calendar.adjust(todaysDate) ql.Settings.instance().evaluationDate = todaysDate spotRates = [0.02514, 0.026285, 0.027326299999999998, 0.0279, 0.029616299999999998, 0.026526, 0.026028, 0.0258695, 0.025958000000000002, 0.0261375, 0.026355, 0.0266255, 0.026898, 0.0271745, 0.02741, 0.027666, 0.028107000000000004, 0.028412000000000003, 0.028447, 0.0284165] spotPeriod = [ql.Period(1, ql.Weeks), ql.Period(1, ql.Months), ql.Period(3, ql.Months), ql.Period(6, ql.Months), ql.Period(9, ql.Months), ql.Period(1, ql.Years), ql.Period(3, ql.Years), ql.Period(5, ql.Years), ql.Period(10, ql.Years), ql.Period(15, ql.Years), ql.Period(20, ql.Years), ql.Period(30, ql.Years), ql.Period(50, ql.Years)] dates = [calendar.advance(todaysDate, period) for period in spotPeriod] curve = ql.ZeroCurve(dates, spotRates[:13], day_count, calendar) yts = ql.YieldTermStructureHandle(curve) engine = ql.DiscountingSwapEngine(yts) issue_date = ql.Date(18, 4, 2018) maturity_date = ql.Date(18, 4, 2028) fixedRate = .09 index = ql.Jibar(ql.Period('3M'), yts) fix_payment_frequency = ql.Annual float_payment_frequency = ql.Quarterly fixedSchedule = ql.MakeSchedule(issue_date, maturity_date, ql.Period('1Y'), calendar=calendar) floatSchedule = ql.MakeSchedule(issue_date, maturity_date, ql.Period('3M'), calendar=calendar) swap = ql.VanillaSwap( ql.VanillaSwap.Payer, 100, fixedSchedule, fixedRate, day_count, floatSchedule, index, 0, index.dayCounter() ) index.addFixing(ql.Date(18,7,2019), 0.02) swap.setPricingEngine(engine) data = [] for cf in map(ql.as_coupon, swap.leg(1)): if cf.date() > todaysDate: data.append({ 'accuralStart': cf.accrualStartDate(), 'accrualEnd': cf.accrualEndDate(), 'amount': cf.amount(), 'rate': cf.rate(), 'discount': yts.discount(cf.date()) }) pd.DataFrame(data).head()
這將輸出:
我懷疑您試圖從曲線日期之前的日期獲得折扣因子。