【问题标题】:How to advance the day in Quantlib如何在 Quantlib 中提前一天
【发布时间】:2017-01-04 19:19:54
【问题描述】:

我的理解是,为了提前一天,你做这样的事情:

ql.Settings.instance().evaluation_date = calculation_date + 1

但是,当我执行以下代码时,我得到的选项值相同:

import QuantLib as ql     

# option data
maturity_date = ql.Date(15, 1, 2016)
spot_price = 127.62
strike_price = 130
volatility = 0.20 # the historical vols for a year
dividend_rate =  0.0163
option_type = ql.Option.Call

risk_free_rate = 0.001
day_count = ql.Actual365Fixed()
#calendar = ql.UnitedStates()
calendar = ql.TARGET()

calculation_date = ql.Date(8, 5, 2015)
ql.Settings.instance().evaluationDate = calculation_date

# construct the European Option
payoff = ql.PlainVanillaPayoff(option_type, strike_price)
exercise = ql.EuropeanExercise(maturity_date)
european_option = ql.VanillaOption(payoff, exercise)

spot_handle = ql.QuoteHandle(
    ql.SimpleQuote(spot_price)
)

flat_ts = ql.YieldTermStructureHandle(
    ql.FlatForward(calculation_date, risk_free_rate, day_count)
)

dividend_yield = ql.YieldTermStructureHandle(
    ql.FlatForward(calculation_date, dividend_rate, day_count)
)

flat_vol_ts = ql.BlackVolTermStructureHandle(
    ql.BlackConstantVol(calculation_date, calendar, volatility, day_count)
)

bsm_process = ql.BlackScholesMertonProcess(spot_handle, 
                                           dividend_yield, 
                                           flat_ts, 
                                           flat_vol_ts)

european_option.setPricingEngine(ql.AnalyticEuropeanEngine(bsm_process))
bs_price = european_option.NPV()
print "The theoretical European price is ", bs_price   

payoff = ql.PlainVanillaPayoff(option_type, strike_price)
settlement = calculation_date
am_exercise = ql.AmericanExercise(settlement, maturity_date)
american_option = ql.VanillaOption(payoff, am_exercise)

#Once you have the american option object you can value them using the binomial tree method:

binomial_engine = ql.BinomialVanillaEngine(bsm_process, "crr", 100)
american_option.setPricingEngine(binomial_engine)
print "The theoretical American price is ", american_option.NPV()                                  

ql.Settings.instance().evaluation_date = calculation_date + 1

print "The theoretical European price is ", european_option.NPV() 
print "The theoretical American price is ", american_option.NPV()

[idf@node3 python]$ python european_option.py 
The theoretical European price is  6.74927181246
The theoretical American price is  6.85858045945
The theoretical European price is  6.74927181246
The theoretical American price is  6.85858045945
[idf@node3 python]$ 

编辑

按照以下建议将代码更改为,但日期更改对计算没有影响。

[idf@node3 python]$ python advance_day.py 
The theoretical European price is  6.74927181246
The theoretical American price is  6.85858045945
The theoretical European price is  6.74927181246
The theoretical American price is  6.85858045945
[idf@node3 python]$ 

这里是根据建议的代码更改。

import QuantLib as ql     

# option data
maturity_date = ql.Date(15, 1, 2016)
spot_price = 127.62
strike_price = 130
volatility = 0.20 # the historical vols for a year
dividend_rate =  0.0163
option_type = ql.Option.Call

risk_free_rate = 0.001
day_count = ql.Actual365Fixed()
#calendar = ql.UnitedStates()
calendar = ql.TARGET()

calculation_date = ql.Date(8, 5, 2015)
ql.Settings.instance().evaluationDate = calculation_date

# construct the European Option
payoff = ql.PlainVanillaPayoff(option_type, strike_price)
exercise = ql.EuropeanExercise(maturity_date)
european_option = ql.VanillaOption(payoff, exercise)

spot_handle = ql.QuoteHandle(
    ql.SimpleQuote(spot_price)
)

flat_ts = ql.YieldTermStructureHandle(
    ql.FlatForward(0, calendar, risk_free_rate, day_count)
)

dividend_yield = ql.YieldTermStructureHandle(
    ql.FlatForward(0, calendar, dividend_rate, day_count)
)

flat_vol_ts = ql.BlackVolTermStructureHandle(
    ql.BlackConstantVol(0, calendar, volatility, day_count)
)

bsm_process = ql.BlackScholesMertonProcess(spot_handle, 
                                           dividend_yield, 
                                           flat_ts, 
                                           flat_vol_ts)

european_option.setPricingEngine(ql.AnalyticEuropeanEngine(bsm_process))
bs_price = european_option.NPV()
print "The theoretical European price is ", bs_price   

payoff = ql.PlainVanillaPayoff(option_type, strike_price)
settlement = calculation_date
am_exercise = ql.AmericanExercise(settlement, maturity_date)
american_option = ql.VanillaOption(payoff, am_exercise)

#Once you have the american option object you can value them using the binomial tree method:

binomial_engine = ql.BinomialVanillaEngine(bsm_process, "crr", 100)
american_option.setPricingEngine(binomial_engine)
print "The theoretical American price is ", american_option.NPV()                                  

ql.Settings.instance().evaluation_date = calculation_date + 1
# Also tried calendar.advance(calculation_date,1,ql.Days)

print "The theoretical European price is ", european_option.NPV() 
print "The theoretical American price is ", american_option.NPV()

【问题讨论】:

    标签: python quantlib quantlib-swig


    【解决方案1】:

    计算日期并不是全部。您正在设置曲线,以便它们的参考日期是固定的(也就是说,您正在调用采用日期的构造函数;有关详细信息,请参阅 this post,或以 this video 为例)。

    如果您指定参考日期,则该参考日期独立于计算日期使用;这是因为它们不一定相同(例如,您可能希望利率曲线基于即期日期而不是今天的日期)。因此,即使您更改计算日期,从曲线返回的波动率和利率仍将相对于它们的参考日期,而参考日期并未发生变化。

    为了获得您想要的效果,您可以创建曲线,使它们随着评估日期移动;例如,而不是

    ql.FlatForward(calculation_date, risk_free_rate, day_count)
    

    你可以使用

    ql.FlatForward(0, calendar, risk_free_rate, day_count)
    

    表示将参考日期指定为“计算日期后0个工作日”,即计算日期。波动率曲线有一个类似的构造函数。

    【讨论】:

    • 我没有关注。请参阅原始帖子中的编辑部分。当天推进前后的输出是一样的。
    • 当您设置新日期时,它应该是evaluationDate,而不是evaluation_date。我也错过了第一次。不幸的是,Python 并没有警告您您只是添加了一个新属性……不过,关于不固定曲线参考日期的事情仍然适用:使用您的原始脚本,选项值不会改变。跨度>
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-06
    • 1970-01-01
    • 1970-01-01
    • 2022-07-08
    • 2022-07-08
    相关资源
    最近更新 更多