2
$\begingroup$

I use the following code in Python to price American put/call options. It's simple code since I'm new to using Quantlib. I would like to specify the contract duration (i.e. T=1, T=2, etc.) instead of specifying the calculation and maturity dates. Is it possible to do this? If so, how can I modify the code below to achieve this?

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)

I would appreciate any help. Thanks

$\endgroup$

1 Answer 1

3
$\begingroup$

How about just defining the maturity date as todays date (or any other start date) ajusted by a period of T x 365 days? Here is an example:

T = 0.5
today = ql.Date().todaysDate()
maturity = today + ql.Period(f"{int(T*365)}d")
$\endgroup$
2
  • $\begingroup$ Hello David. Thank you, it works perfectly. I appreciate your help. $\endgroup$
    – Ruan
    Jan 8, 2020 at 9:32
  • 2
    $\begingroup$ ql.Period(int(T*365), ql.Days) has the same effect and might be a bit less cryptic. $\endgroup$ Jan 8, 2020 at 11:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.