固定收益


远期合约

远期利率协议

class ql.ForwardRateAgreement(valueDate, maturityDate, position, strikeForward, notional, iborIndex, discountCurve=ql.YieldTermStructureHandle())
fra = ql.ForwardRateAgreement(
    ql.Date(15,6,2020),
    ql.Date(15,12,2020),
    ql.Position.Long,
    0.01,
    1e6,
    ql.Euribor6M(yts),
    yts
)
.NPV()
.businessDayConvention()
.calendar()
.dayCounter()
.discountCurve()
.fixingDate()
.forwardRate()
.forwardValue()
.impliedYield(underlyingSpotValue, forwardValue, settlementDate, compoundingConvention, dayCounter)
.incomeDiscountCurve()
.isExpired()
.settlementDate()
.spotIncome(yts)
.spotValue()

固定利率债券远期

class ql.FixedRateBondForward(valueDate, maturityDate, Position::Type, strike, settlementDays, dayCounter, calendar, businessDayConvention, FixedRateBond, yieldTermStructure=ql.YieldTermStructureHandle(), incomeDiscountCurve=ql.YieldTermStructureHandle())

持仓:

  • ql.Position.Long

  • ql.Position.Short

valueDate = ql.Date(24, 6, 2020)
maturityDate = ql.Date(31, 5, 2032)
position = ql.Position.Long
strike = 100
settlementDays = 2
dayCounter = ql.Actual360()
calendar = ql.TARGET()
businessDayConvention = ql.Following
bond = ql.FixedRateBond(2, ql.TARGET(), 100.0, ql.Date(31, 5, 2032), ql.Date(30, 5, 2035), ql.Period('1Y'), [0.05], ql.ActualActual())
bond.setPricingEngine(engine)
fwd = ql.FixedRateBondForward(
    valueDate, maturityDate, position, strike, settlementDays,
    dayCounter , calendar, businessDayConvention, bond, yts, yts)

债券

债券

赎回和到期日是根据票息数据计算的(如果有的话)。因此,传递的现金流中不得包含赎回金额。

class ql.Bond(settlementDays, calendar, issueDate, coupons)
start = ql.Date(15,12,2019)
maturity = ql.Date(15,12,2020)
schedule = ql.MakeSchedule(start, maturity, ql.Period('6M'))

interest = ql.FixedRateLeg(schedule, ql.Actual360(), [100.], [0.05])
bond = ql.Bond(0, ql.TARGET(), start, interest)
.bondYield(dayCounter, compounding, frequency, accuracy=1.0e-8, maxEvaluations=100)
.bondYield(cleanPrice, dayCounter, compounding, frequency, settlementDate=Date, accuracy=1.0e-8, maxEvaluations=100)
bond.bondYield(100, ql.Actual360(), ql.Compounded, ql.Annual)
.dirtyPrice()
bond.dirtyPrice()
.dirtyPrice(yield, dayCount, compounding, frequency)
bond.dirtyPrice(0.05, ql.Actual360(), ql.Compounded, ql.Annual)

零息债券

ql.ZeroCouponBond(settlementDays, calendar, faceAmount, maturityDate)
bond = ql.ZeroCouponBond(2, ql.TARGET(), 100, ql.Date(20,6,2020))

FixedRateBond

ql.FixedRateBond(settlementDays, calendar, faceAmount, startDate, maturityDate, tenor, coupon, paymentConvention)
ql.FixedRateBond(settlementDays, faceAmount, schedule, coupon, paymentConvention)
bond = ql.FixedRateBond(2, ql.TARGET(), 100.0, ql.Date(15,12,2019), ql.Date(15,12,2024), ql.Period('1Y'), [0.05], ql.ActualActual(ql.ActualActual.Bond))

分期偿还固定利率债券

ql.AmortizingFixedRateBond(settlementDays, notionals, schedule, coupons, accrualDayCounter, paymentConvention=Following, issueDate=Date())
notionals = [100,100,100,50]
schedule = ql.MakeSchedule(ql.Date(25,1,2018), ql.Date(25,1,2022), ql.Period('1y'))
bond = ql.AmortizingFixedRateBond(0, notionals, schedule, [0.03], ql.Thirty360(ql.Thirty360.USA))

浮动利率债券

ql.FloatingRateBond(settlementDays, faceAmount, schedule, index, dayCounter, paymentConvention)
schedule = ql.MakeSchedule(ql.Date(15,6,2020), ql.Date(15,6,2022), ql.Period('6m'))
index = ql.Euribor6M()
bond = ql.FloatingRateBond(2,100, schedule, index, ql.Actual360(), spreads=[0.01])

AmortizingFloatingRateBond

ql.FloatingRateBond(settlementDays, notionals, schedule, index, dayCounter)
notional = [100, 50]
schedule = ql.MakeSchedule(ql.Date(15,6,2020), ql.Date(15,6,2022), ql.Period('1Y'))
index = ql.Euribor6M()
bond = ql.AmortizingFloatingRateBond(2, notional, schedule, index, ql.ActualActual(ql.ActualActual.Bond))

CMS利率债券

ql.CmsRateBond(settlementDays, faceAmount, schedule, index, dayCounter, paymentConvention, fixingDays, gearings, spreads, caps, floors)
schedule = ql.MakeSchedule(ql.Date(15,6,2020), ql.Date(15,6,2022), ql.Period('1Y'))
index = ql.EuriborSwapIsdaFixA(ql.Period('10y'))
bond = ql.CmsRateBond(2, 100, schedule, index, ql.Actual360(), ql.ModifiedFollowing, fixingDays=2, gearings=[1], spreads=[0], caps=[], floors=[])

可赎回债券

ql.CallableFixedRateBond(settlementDays, faceAmount, schedule, coupons, accrualDayCounter, paymentConvention, redemption, issueDate, putCallSchedule)
schedule = ql.MakeSchedule(ql.Date(15,6,2020), ql.Date(15,6,2022), ql.Period('1Y'))
putCallSchedule = ql.CallabilitySchedule()

my_price  = ql.BondPrice(100, ql.BondPrice.Clean)

putCallSchedule.append(
    ql.Callability(my_price, ql.Callability.Call, ql.Date(15,6,2021))
)

bond = ql.CallableFixedRateBond(2, 100, schedule, [0.01], ql.Actual360(), ql.ModifiedFollowing, 100, ql.Date(15,6,2020), putCallSchedule)

可转换债券

债券函数

bond = ql.FixedRateBond(
    2, ql.TARGET(), 100.0,
    ql.Date(15,12,2019), ql.Date(15,12,2024), ql.Period('1Y'),
    [0.05], ql.ActualActual(ql.ActualActual.Bond))

日期检查器

ql.BondFunctions.startDate(bond)
ql.BondFunctions.maturityDate(bond)
ql.BondFunctions.isTradable(bond)

现金流检查器

ql.BondFunctions.previousCashFlowDate(bond)
ql.BondFunctions.previousCashFlowDate(bond, ql.Date(15,12,2020))
ql.BondFunctions.previousCashFlowAmount(bond)
ql.BondFunctions.previousCashFlowAmount(bond, ql.Date(15,12,2020))
ql.BondFunctions.nextCashFlowDate(bond)
ql.BondFunctions.nextCashFlowDate(bond, ql.Date(15,12,2020))
ql.BondFunctions.nextCashFlowAmount(bond)
ql.BondFunctions.nextCashFlowAmount(bond, ql.Date(15,12,2020))

息票检查器

ql.BondFunctions.previousCouponRate(bond)
ql.BondFunctions.nextCouponRate(bond)
ql.BondFunctions.accrualStartDate(bond)
ql.BondFunctions.accrualEndDate(bond)
ql.BondFunctions.accrualPeriod(bond)
ql.BondFunctions.accrualDays(bond)
ql.BondFunctions.accruedPeriod(bond)
ql.BondFunctions.accruedDays(bond)
ql.BondFunctions.accruedAmount(bond)

收益率期限结构

crv = ql.FlatForward(2, ql.TARGET(), 0.04, ql.Actual360())
ql.BondFunctions.cleanPrice(bond, crv)
ql.BondFunctions.bps(bond, crv)
ql.BondFunctions.atmRate(bond, crv)

收益率(又称内部收益率,即IRR)函数

rate = ql.InterestRate(0.05, ql.Actual360(), ql.Compounded, ql.Annual)
ql.BondFunctions.cleanPrice(bond, rate)
ql.BondFunctions.bps(bond, rate)
ql.BondFunctions.duration(bond, rate)
ql.BondFunctions.convexity(bond, rate)
ql.BondFunctions.basisPointValue(bond, rate)
ql.BondFunctions.yieldValueBasisPoint(bond, rate)

Z利差函数

crv = ql.FlatForward(2, ql.TARGET(), 0.04, ql.Actual360())
ql.BondFunctions.zSpread(bond, 101, crv, ql.Actual360(), ql.Compounded, ql.Annual)

互换

VanillaSwap

ql.VanillaSwap(type, nominal, fixedSchedule, fixedRate, fixedDayCount, floatSchedule, index, spread, floatingDayCount)

类型:

  • ql.VanillaSwap.Payer

  • ql.VanillaSwap.Receiver

calendar = ql.TARGET()
start = ql.Date(17,6,2019)
maturity = calendar.advance(start, ql.Period('5y'))

fixedSchedule = ql.MakeSchedule(start, maturity, ql.Period('1Y'))

floatSchedule = ql.MakeSchedule(start, maturity, ql.Period('6M'))

swap = ql.VanillaSwap(
    ql.VanillaSwap.Payer, 100,
    fixedSchedule, 0.01, ql.Thirty360(),
    floatSchedule, ql.Euribor6M(), 0, ql.Actual360()
)

互换

ql.Swap(firstLeg, secondLeg)
fixedSchedule = ql.MakeSchedule(start, maturity, ql.Period('1Y'))
fixedLeg = ql.FixedRateLeg(fixedSchedule, ql.Actual360(), [100], [0.01])

floatSchedule = ql.MakeSchedule(start, maturity, ql.Period('6M'))
floatLeg = ql.IborLeg([100], floatSchedule, ql.Euribor6M(), ql.Actual360())

swap = ql.Swap(fixedLeg, floatLeg)

创建普通利率互换

ql.MakeVanillaSwap(tenor, index, fixedRate, forwardStart)

可选参数:

  • 固定利率计息天数

  • 名义金额

  • receiveFixed,

  • 互换类型

  • 结算天数

  • 生效日期

  • 终止日期

  • 日期生成规则

  • 固定期限

  • 固定收益日历

  • 固定利率端惯例

  • 固定利率计息天数

  • 浮动利率期限

  • 浮动利率日历

  • 浮动利率惯例

  • 浮动利率计息天数

  • 浮动利率利差

  • 贴现期限结构

  • 定价引擎

  • 固定利率端终止日期惯例

  • 固定期限日期生成规则

  • 固定期限月末

  • 固定利率端首个日期

  • 固定利率端的倒数第二个日期,

  • 浮动利率终止日期惯例

  • 浮动利率计息日规则

  • 浮动利率端月末调整

  • 浮动利率首个计息日

  • 浮动利率倒数第二个日期

tenor = ql.Period('5y')
index = ql.Euribor6M()
fixedRate = 0.05
forwardStart = ql.Period("2D")

swap = ql.MakeVanillaSwap(tenor, index, fixedRate, forwardStart, Nominal=100)
swap = ql.MakeVanillaSwap(tenor, index, fixedRate, forwardStart, swapType=ql.VanillaSwap.Payer)

分期偿还互换

calendar = ql.TARGET()
start = ql.Date(17,6,2019)
maturity = calendar.advance(start, ql.Period('2y'))


fixedSchedule = ql.MakeSchedule(start, maturity, ql.Period('1Y'))
fixedLeg = ql.FixedRateLeg(fixedSchedule, ql.Actual360(), [100, 50], [0.01])

floatSchedule = ql.MakeSchedule(start, maturity, ql.Period('6M'))
floatLeg = ql.IborLeg([100, 100, 50, 50], floatSchedule, ql.Euribor6M(), ql.Actual360())

swap = ql.Swap(fixedLeg, floatLeg)

FloatFloatSwap

ql.FloatFloatSwap(ql.VanillaSwap.Payer,
                [notional] * (len(float3m)-1),
                [notional] * (len(float6m)-1),
                float3m,
                index3m,
                ql.Actual360(),
                float6m,
                index6m,
                ql.Actual360(), False, False,
                [1] * (len(float3m)-1),
                [spread] * (len(float3m)-1))

资产互换

ql.AssetSwap(payFixed, bond, cleanPrice, index, spread)
ql.AssetSwap(payFixed, bond, cleanPrice, index, spread, schedule, dayCount, bool)
payFixedRate = True
bond = ql.FixedRateBond(2, ql.TARGET(), 100.0, ql.Date(15,12,2019), ql.Date(15,12,2024),
    ql.Period('1Y'), [0.05], ql.ActualActual()
    )
bondCleanPrice = 100
index = ql.Euribor6M()
spread = 0.0
ql.AssetSwap(payFixedRate, bond, bondCleanPrice, index, spread, ql.Schedule(), ql.ActualActual(), True)

隔夜指数互换

ql.OvernightIndexedSwap(swapType, nominal, schedule, fixedRate, fixedDC, overnightIndex)

或名义金额数组

ql.OvernightIndexedSwap(swapType, nominals, schedule, fixedRate, fixedDC, overnightIndex)

可选参数:

  • 利差=0.0

  • 支付延迟=0

  • paymentAdjustment=ql.Following()

  • paymentCalendar=ql.Calendar()

  • telescopicValueDates=false

类型:

  • ql.OvernightIndexedSwap.Receiver

  • ql.OvernightIndexedSwap.Receiver

swapType = ql.OvernightIndexedSwap.Receiver
nominal = 100
schedule = ql.MakeSchedule(ql.Date(15,6,2020), ql.Date(15,6,2021), ql.Period('1Y'), calendar=ql.TARGET())
fixedRate = 0.01
fixedDC = ql.Actual360()
overnightIndex = ql.Eonia()
ois_swap = ql.OvernightIndexedSwap(swapType, nominal, schedule, fixedRate, fixedDC, overnightIndex)

MakeOIS

ql.MakeOIS(swapTenor, overnightIndex, fixedRate)

可选参数:

  • fwdStart=Period(0, Days)

  • receiveFixed=True,

  • swapType=OvernightIndexedSwap.Payer

  • 名义值=1.0

  • 结算天数=2

  • effectiveDate=None

  • terminationDate=None

  • dateGenerationRule=DateGeneration.Backward

  • 支付频率=每年

  • paymentAdjustmentConvention=Following

  • 支付延迟=0

  • 支付日历=None

  • endOfMonth=True

  • fixedLegDayCount=None

  • 隔夜利率利差=0.0

  • discountingTermStructure=None

  • telescopicValueDates=False

  • 定价引擎=无

swapTenor = ql.Period('1Y')
overnightIndex = ql.Eonia()
fixedRate = 0.01
ois_swap = ql.MakeOIS(swapTenor, overnightIndex, fixedRate)

非标准互换

ql.NonstandardSwap(swapType, fixedNominal, floatingNominal, fixedSchedule, fixedRate, fixedDayCount, floatingSchedule, iborIndex, gearing, spread, floatDayCount)

可选参数:

  • intermediateCapitalExchange = False

  • finalCapitalExchange = False,

  • paymentConvention = None

swapType = ql.VanillaSwap.Payer
fixedNominal = [100, 100]
floatingNominal  = [100] * 4
fixedSchedule = ql.MakeSchedule(ql.Date(15,6,2020), ql.Date(15,6,2022), ql.Period('1Y'))
fixedRate = [0.02] * 2
fixedDayCount = ql.Thirty360()
floatingSchedule = ql.MakeSchedule(ql.Date(15,6,2020), ql.Date(15,6,2022), ql.Period('6M'))
iborIndex = ql.Euribor6M()
gearing = [1.] * 4
spread = [0.] * 4
floatDayCount = iborIndex.dayCounter()
nonstandardSwap = ql.NonstandardSwap(
    swapType, fixedNominal, floatingNominal,
    fixedSchedule, fixedRate, fixedDayCount,
    floatingSchedule, iborIndex, gearing, spread, floatDayCount)

互换期权

练习

  • ql.EuropeanExercise(start)

  • ql.AmericanExercise(最早日期, 最晚日期)

  • ql.BermudanExercise(dates)

结算类型/方法

  • ql.Settlement.Cash
    • ql.Settlement.CollateralizedCashPrice

    • ql.Settlement.ParYieldCurve

  • ql.Settlement.Physical
    • ql.Settlement.PhysicalCleared

    • ql.Settlement.PhysicalOTC

利率互换期权

ql.Swaption(swap, exercise, settlementType=ql.Settlement.Physical, settlementMethod=ql.Settlement.PhysicalOTC)
calendar = ql.TARGET()
today = ql.Date().todaysDate()
exerciseDate = calendar.advance(today, ql.Period('5y'))
exercise = ql.EuropeanExercise(exerciseDate)
swap = ql.MakeVanillaSwap(ql.Period('5y'), ql.Euribor6M(), 0.05, ql.Period('5y'))
swaption = ql.Swaption(swap, exercise)

swaption = ql.Swaption(swap, exercise, ql.Settlement.Cash, ql.Settlement.ParYieldCurve)
swaption = ql.Swaption(swap, exercise, ql.Settlement.Physical, ql.Settlement.PhysicalCleared)

非标准互换期权

FloatFloatSwaption


利率上限与下限

上限

ql.Cap(floatingLeg, exerciseRates)
schedule = ql.MakeSchedule(ql.Date(15,6,2020), ql.Date(16,6,2022), ql.Period('6M'))
ibor_leg = ql.IborLeg([100], schedule, ql.Euribor6M())
strike = 0.01
cap = ql.Cap(ibor_leg, [strike])

利率下限

ql.Floor(floatingLeg, exerciseRates)
schedule = ql.MakeSchedule(ql.Date(15,6,2020), ql.Date(16,6,2022), ql.Period('6M'))
ibor_leg = ql.IborLeg([100], schedule, ql.Euribor6M())
strike = 0.00
floor = ql.Floor(ibor_leg, [strike])

领口期权

ql.Collar(floatingLeg, capRates, floorRates)
schedule = ql.MakeSchedule(ql.Date(15,6,2020), ql.Date(16,6,2022), ql.Period('6M'))
ibor_leg = ql.IborLeg([100], schedule, ql.Euribor6M())
capStrike = 0.02
floorStrike = 0.00
collar = ql.Collar(ibor_leg, [capStrike], [floorStrike])