QuantLib-Python Basics

以下是设置带有评估日期的 Quant Lib 的命令。一切都以“评估日期”开始,这意味着您想要评估工具的日期。考虑到您想要对 2020 年 9 月 16 日的“Swap”进行估值,您将首先在 QuantLib 中设置评估日期。 Underhood C++ quant 库是使用 SWIg 打包的,python 更像是一个调用 C++ 库的 API。

#import the Quant Lib
import QuantLib as ql

# Let the today date whenwe want to value a instrument be
today = ql.Date(15,6,2020)

# we can set evaluationDate in QL as
ql.Settings.instance().evaluationDate = today
print(ql.Settings.instance().evaluationDate);
# prints..June 15th, 2020

# or you can do
today = ql.Date(15,12,2021);
ql.Settings.instance().setEvaluationDate(today)
print(ql.Settings.instance().evaluationDate)
# prints..December 15th, 2021

移动引用曲线的日期:Following 返回基于 FlatForward 的期限结构

settlementDays = 2

# Holiday calendar of united states
calendar = ql.UnitedStates()

forwardRate = 0.05

"""Day Counter provides methods for determining the length of a time period according to given market convention,
both as a number of days and as a year fraction."""
dayCounter = ql.Actual360()

# Construct flat forward rate term structure
flatForwardTermStructure = ql.FlatForward(settlementDays, calendar, forwardRate, dayCounter)

flatForwardTermStructure.referenceDate()

print("Max Date: ", flatForwardTermStructure.maxDate())

更改评估日期计算:以下显示使用评估或评估日期。让我们构建一个用于创建一条腿的时间表,然后我们将计算这条腿的利率。

today = ql.Date(15,6,2020)
ql.Settings.instance().evaluationDate = today

effectiveDate = ql.Date(15, 6, 2020)
terminationDate = ql.Date(15, 6, 2022)

创建时间表

schedule = ql.MakeSchedule(effectiveDate, terminationDate, ql.Period('6M'))

使用帮助类构建固定利率优惠券序列创建固定利率边

notional = [100.0]
rate = [0.05]
leg = ql.FixedRateLeg(schedule, dayCounter, notional, rate)

利率类封装了利率复利代数。它管理计日惯例、复利惯例、不同惯例之间的转换、折扣/复利因子计算以及隐含/等价率计算。

dayCounter = ql.Thirty360()
rate = 0.03

"""
ql/Compounding.hpp
    //! Interest rate compounding rule
    enum Compounding { Simple = 0,          //!< \f$ 1+rt \f$
                       Compounded = 1,      //!< \f$ (1+r)^t \f$
                       Continuous = 2,      //!< \f$ e^{rt} \f$
                       SimpleThenCompounded, //!< Simple up to the first period then Compounded
                       CompoundedThenSimple //!< Compounded up to the first period then Simple
    };
"""

compoundingType = ql.Compounded

"""
ql/time/frequency.hpp
enum Frequency { NoFrequency = -1,     //!< null frequency
                     Once = 0,             //!< only once, e.g., a zero-coupon
                     Annual = 1,           //!< once a year
                     Semiannual = 2,       //!< twice a year
                     EveryFourthMonth = 3, //!< every fourth month
                     Quarterly = 4,        //!< every third month
                     Bimonthly = 6,        //!< every second month
                     Monthly = 12,         //!< once a month
                     EveryFourthWeek = 13, //!< every fourth week
                     Biweekly = 26,        //!< every second week
                     Weekly = 52,          //!< once a week
                     Daily = 365,          //!< once a day
                     OtherFrequency = 999  //!< some other unknown frequency
    };
"""

frequency = ql.Annual
interestRate = ql.InterestRate(rate, dayCounter, compoundingType, frequency)

4.958531764309427

ql/cashflows/Cashflows.hpp NPV 是现金流的总和,每个现金流都根据给定的固定利率贴现。结果受利率复利选择以及相对频率和日期计数器的影响。

ql.Settings.instance().evaluationDate = ql.Date(15,12,2020)
print( ql.CashFlows.npv(leg, rate, False) )

2.4906934531375144

猜你喜欢

转载自blog.csdn.net/sinat_37574187/article/details/127469957