使用QuantLib计算FloatingRateBond与Floor的现金流量

时间:2017-02-13 02:30:16

标签: python finance quantlib quantlib-swig

对QuantLib来说很新,所以猜测这是一个新手的错误。很高兴了解这个强大的图书馆,所以感谢作者和贡献者!

如果没有底价参数,我能够为没有价格的FloatingRateBond生成现金流量,所以我不明白为什么包含底线参数需要一个价格。我认为增加地板只会为每个固定值提供一分钟。

想知道是否有人在使用楼层时获得了FloatingRateBond现金流量。而且,如果是这样,如果有人能够发现我误入歧途的地方。提前谢谢!

我在通过预先打包的安装程序(QuantLib-Python-1.8.win-amd64-py3.5.msi)安装的Windows上使用QuantLib 1.8。

发生错误的地方:

File "C:/src/misc/generate_cashflows.py", line 138, in generate_cashflow
    print(cf.amount())
  File "C:\lib\site-packages\QuantLib\QuantLib.py", line 8844, in amount
    return _QuantLib.CashFlow_amount(self)
RuntimeError: pricer not set

具体代码如下:

ql_first_day, ql_first_month, ql_first_year = first_payment_date.day, first_payment_date.month, first_payment_date.year

ql_first_date = QuantLib.Date(ql_first_day, ql_first_month, ql_first_year)  

maturity_month, maturity_day, maturity_year = maturity.month, maturity.day, maturity.year
ql_maturity_date = QuantLib.Date(maturity_day, maturity_month, maturity_year)



ql_issue_day, ql_issue_month, ql_issue_year = issue_date.day, issue_date.month, issue_date.year
q1_settle_date = QuantLib.Date(ql_issue_day, ql_issue_month, ql_issue_year)

fixing_days = 0

calendar = QuantLib.UnitedStates()
ql_settle_date = calendar.adjust(q1_settle_date)
todays_date = calendar.advance(ql_settle_date, -fixing_days, QuantLib.Days)
QuantLib.Settings.instance().evaluationDate = todays_date

ql_schedule = QuantLib.Schedule(ql_settle_date,
                    ql_maturity_date, QuantLib.Period(ql_frequency_enum),
                    QuantLib.UnitedStates(),
                    QuantLib.Following, QuantLib.Following,
                    QuantLib.DateGeneration.Forward, False, ql_first_date)

ql_forecast_curve = QuantLib.RelinkableYieldTermStructureHandle()
today = datetime.datetime.today()
calc_date = QuantLib.Date(today.day, today.month, today.year)
QuantLib.Settings.instance().evaluationDate = calc_date

day_count = QuantLib.Thirty360()

# setup swaps
calendar = QuantLib.UnitedStates()
swFixedLegFrequency = QuantLib.Annual
swFixedLegConvention = QuantLib.Unadjusted
swFixedLegDayCounter = QuantLib.Thirty360()
swFloatingLegIndex = QuantLib.USDLibor(QuantLib.Period(3, QuantLib.Months))

swap_raw = [
    (1, 0.01251),
    (2, 0.01505),
    (3, 0.01701),
    (5, 0.01972),
    (7, 0.02158)
]

swap_rates = []

for year, rate in swap_raw:
    swap_rates.append(QuantLib.SwapRateHelper(
        QuantLib.QuoteHandle(QuantLib.SimpleQuote(rate)),
        QuantLib.Period(year, QuantLib.Years),
        calendar,
        swFixedLegFrequency,
        swFixedLegConvention,
        swFixedLegDayCounter,
        swFloatingLegIndex
    ))

swap_curve = QuantLib.PiecewiseFlatForward(calc_date, swap_rates, day_count)

ql_forecast_curve.linkTo(swap_curve)

ql_index = QuantLib.USDLibor(period, ql_forecast_curve)

settlement_days = 0
face_amount = 100

ql_bond = QuantLib.FloatingRateBond(settlement_days, #settlementDays
    face_amount, # faceAmount
    ql_schedule,
    ql_index,
    QuantLib.Thirty360(),
    gearings = [],
    spreads = [libor_spread],
    caps = [],
    floors = [.01]
)

ql_discount_curve = QuantLib.RelinkableYieldTermStructureHandle()
settlement_date = QuantLib.Date(9, 2, 2017)
flatForward = QuantLib.FlatForward(
    settlement_date,
    .02,
    QuantLib.ActualActual(QuantLib.ActualActual.Bond),
    QuantLib.Compounded,
    QuantLib.Semiannual)
ql_discount_curve.linkTo(flatForward)
bondEngine = QuantLib.DiscountingBondEngine(ql_discount_curve)
ql_bond.setPricingEngine(bondEngine)

for cf in ql_bond.cashflows():
    c = QuantLib.as_floating_rate_coupon(cf)
    print(cf.amount())

1 个答案:

答案 0 :(得分:2)

理论第一:当用底价定价优惠券时,您不能从预测曲线中获取预期的LIBOR利率,并在该指数与最低限额之间取最小值。相反,您需要在费率和最低限额之间取最小值的预期值,不幸的是E[min(R,F)]min(E[R],F)不同。所以不,地板不能提供最低限度;你需要一个不同的公式来估计预期的收益。

QuantLib的含义是,简单的浮动利率优惠券可以(并且)设置一个默认的价格,只是从预测曲线中读取费率,但是带帽或地板的优惠券需要用户指定要使用的价格和向其提供任何其他所需数据;在您的情况下,这意味着至少是一个波动率期限结构,尽管可以指定更多的可选数据;有关详细信息,请参阅BlackIborCouponPricer类的构造函数。

通常情况下,波动率是按照大写字母和楼层的市场报价进行自举的,但创建它的过程有点复杂(请参阅these tests以获取C ++中的示例),我不确定是否所有所需的类将导出到Python,您最好在QuantLib mailing list上询问它。

如果您想验证优惠券是否有效,您可以使用不变的波动率,如:

volatility = 0.10;
vol = QuantLib.ConstantOptionletVolatility(settlement_days,
                                           calendar,
                                           QuantLib.ModifiedFollowing,
                                           volatility,
                                           day_count)

pricer = QuantLib.BlackIborCouponPricer(
    QuantLib.OptionletVolatilityStructureHandle(vol))
QuantLib.setCouponPricer(ql_bond.cashflows(), pricer)

以上应该可以让你得到一个结果;但当然我从帽子中剔除了15%的波动性,它不会给你实际的市场价值......