Options

Quantlib 指定契約期限而不是日期

  • January 8, 2020

我在 Python 中使用以下程式碼為美式看跌/看漲期權定價。這是簡單的程式碼,因為我是使用 Quantlib 的新手。我想指定契約期限(即T=1T=2等),而不是指定計算和到期日。是否有可能做到這一點?如果是這樣,我該如何修改下面的程式碼來實現這一點?

import QuantLib as ql

def OptionPrices(T, r, sigma, K, S0, TimeSteps):
   maturity_date = ql.Date(31, 12, 2020)
   calculation_date = ql.Date(1, 1, 2020)
   ql.Settings.instance().evaluationDate = calculation_date    

   payoff = ql.PlainVanillaPayoff(ql.Option.Call, K)

   am_exercise = ql.AmericanExercise(calculation_date, maturity_date)
   american_option = ql.VanillaOption(payoff, am_exercise)

   spot_handle = ql.QuoteHandle(ql.SimpleQuote(S0))
   flat_ts = ql.YieldTermStructureHandle(ql.FlatForward(calculation_date, r, ql.Actual365Fixed()))
   dividend_yield = ql.YieldTermStructureHandle(ql.FlatForward(calculation_date, 0, ql.Actual365Fixed()))
   flat_vol_ts = ql.BlackVolTermStructureHandle(ql.BlackConstantVol(calculation_date, ql.UnitedStates(), sigma, ql.Actual365Fixed()))
   bsm_process = ql.BlackScholesMertonProcess(spot_handle, dividend_yield, flat_ts, flat_vol_ts)

   binomial_engine = ql.BinomialVanillaEngine(bsm_process, "crr", TimeSteps)
   american_option.setPricingEngine(binomial_engine)

   OptionPrices.AmCallPrices = [binomial_price(american_option, bsm_process, step) for step in range(2, TimeSteps+1, 1)]
   OptionPrices.AmCallPrice = american_option.NPV()


   payoff = ql.PlainVanillaPayoff(ql.Option.Put, K)

   american_option = ql.VanillaOption(payoff, am_exercise)

   OptionPrices.AmPutPrices = [binomial_price(american_option, bsm_process, step) for step in range(2, TimeSteps+1, 1)]
   OptionPrices.AmPutPrice = american_option.NPV()

def binomial_price(option, bsm_process, steps):
   binomial_engine = ql.BinomialVanillaEngine(bsm_process, "crr", steps)
   option.setPricingEngine(binomial_engine)
   return option.NPV()

OptionPrices(1, 0.06, 0.15, 100, 90, 200)
print(OptionPrices.AmPutPrice)

我將不勝感激任何幫助。謝謝

將到期日定義為今天的日期(或任何其他開始日期)並按T x 365天數調整如何?這是一個例子:

T = 0.5
today = ql.Date().todaysDate()
maturity = today + ql.Period(f"{int(T*365)}d")

引用自:https://quant.stackexchange.com/questions/50592