Trying to learn Quantlib with Python, please have a look at below code:
# option data
# AAPL US
maturity_date = ql.Date(26, 1, 2019)
spot_price = 180
strike_price = 180
volatility = 0.2198 # the historical vols or implied vols
option_type = ql.Option.Call
risk_free_rate = 0.025
day_count = ql.Actual365Fixed()
calendar = ql.UnitedStates()
calculation_date = ql.Date(1, 8, 2018)
ql.Settings.instance().evaluationDate = calculation_date
dividenddates = [ql.Date(10,8,2018), ql.Date(8, 11, 2018)]
dividends = [0.73,0.73]
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))
bs_process = ql.BlackScholesProcess(spot_handle,flat_ts, flat_vol_ts)
payoff = ql.PlainVanillaPayoff(option_type, strike_price)
settlement = calculation_date
am_exercise = ql.AmericanExercise(settlement, maturity_date)
american_option = ql.DividendVanillaOption(payoff, am_exercise, dividenddates, dividends)
engine = ql.FDDividendAmericanEngine(bs_process)
# engine = ql.FDDividendAmericanEngine(bs_process, timeSteps=500, gridPoints=500)
american_option.setPricingEngine(engine)
print(american_option.NPV())
I got 11.273456 as option value. However, when I try to change the spot price, or calculation_date and reprice the option, the NPV doesn't seem to change.
I tried:
spot_handle.setValue = 179
print(american_option.NPV())
or
ql.Settings.instance().evaluationDate = calculation_date + 1
print(american_option.NPV())
NPV still gives same value, unless I re-run through the first section code to re-create instrument with new inputs. I thought Quantlib is designed to allow us use the existing instrument to reprice with simply changing one or more inputs, anything I miss here?
Thanks for your help.