2017-01-04 9 views
0
에서 일을 진행하는 방법

나의 이해 날을 진행하기 위해, 당신은 같은 것을 할 것입니다 옵션 :Quantlib

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() 

[[email protected] 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 
[[email protected] python]$ 

편집

아래의 제안에 따라에 코드를 변경하지만, 일의 변화는 computat에서 차이가 없습니다 이온.

[[email protected] 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 
[[email protected] 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() 

답변

1

계산 날짜가 전부는 아닙니다. 참조 날짜가 고정되도록 커브를 설정합니다 (즉, 날짜가 걸리는 생성자를 호출합니다 (자세한 내용은 this post, 예는 this video 참조).

참조 날짜를 지정하면 계산 날짜와 별도로 사용됩니다. 왜냐하면 그것들은 반드시 동일하지 않기 때문입니다 (예를 들어, 이자율 곡선이 오늘 날짜 대신 현장 날짜를 기반으로 할 수 있기를 바랍니다). 따라서 계산 날짜를 변경하더라도 곡선에서 반환 된 변동성 및 비율은 이동하지 않은 참조 날짜에 상대적입니다.

원하는 효과를 얻으려면 평가 날짜와 함께 이동하도록 곡선을 만들 수 있습니다. 예, 대신

ql.FlatForward(calculation_date, risk_free_rate, day_count) 

의 당신은 기준 날짜 계산 날짜로 "0 일 계산 날짜 이후", 또는 다른 말로,로 지정되어 있음을 의미

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

을 사용할 수 있습니다 . 변동성 곡선은 유사한 생성자를가집니다.

+0

나는 따르고 있지 않습니다. 원래 게시물의 EDIT 섹션을 참조하십시오. 하루 전진 전후의 결과는 같습니다. – Ivan

+0

새 날짜를 설정할 때'evaluation_date'가 아니라'evaluationDate' 여야합니다. 나는 처음으로 그것을 놓쳤다. 불행히도 파이썬은 단지 새로운 속성을 추가하고 있다는 경고를하지 않습니다 ... 그러나 커브의 참조 날짜를 고치지 않는 것에 대한 문제는 여전히 적용됩니다. 원본 스크립트에서는 옵션 값이 변경되지 않습니다. –

+0

나는 그것을 작동시켰다. 감사! – Ivan