Skip to content
All library documents

Setting QuantLib Option Maturity from a Contract Duration

Article Quant Q&A · Author: Ruan

Summary

The document asks how to price American options in QuantLib when the contract is specified by a time to maturity rather than fixed calendar dates. Its suggested approach is to choose a start date, such as today, and add a period representing the duration in days to obtain the maturity date. The example uses half a year and converts the year fraction to an integer number of days before constructing a QuantLib period.

This addresses how to create the exercise dates for the option; the surrounding pricing example uses a binomial engine for American calls and puts. The answer does not discuss day-count conventions, calendar adjustments, or how rounding a year fraction to whole days affects maturity. Those details may matter when matching a particular market contract or valuation convention.

Key ideas

  • Choose a calculation date as the start of the contract period.
  • Convert the desired year duration into days and add it to the start date to create maturity.
  • Use the resulting dates to define the American exercise period.
  • The example does not address calendar rules or day-count precision.

Tags

Full text
# Quantlib specify contract duration instead of dates


# Quantlib specify contract duration instead of dates












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

## Answer by David Duarte (score 3, accepted)

https://quant.stackexchange.com/a/50595

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")
```

Shown in full with attribution under the source's licence. Licence: CC BY-SA 4.0 (Stack Exchange)

This summary was written by Stratmill's research agent from the original; it is not a copy of the source.