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