Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Avatar for Software 20.04.
Download
394 views
ubuntu2004-dev
Kernel: Python 3 (system-wide)
import QuantLib as ql import matplotlib.pyplot as plt
ql.__version__
'1.24'

Demonstrates how to price European options using QuantLib Python. Methods using Black-Scholes-Merton formula and binomial tree will be discussed.

# option data maturity_date = ql.Date(15, 1, 2016) spot_price = 127.62 strike_price = 130 volatility = 0.20 # the historical vols for a year dividend_rate = 0.0163 option_type = ql.Option.Call risk_free_rate = 0.001 day_count = ql.Actual365Fixed() calendar = ql.UnitedStates() calculation_date = ql.Date(8, 5, 2015) ql.Settings.instance().evaluationDate = calculation_date
# construct the European Option payoff = ql.PlainVanillaPayoff(option_type, strike_price) exercise = ql.EuropeanExercise(maturity_date) european_option = ql.VanillaOption(payoff, exercise)

The Black-Scholes-Merto process is constructed here

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)) bsm_process = ql.BlackScholesMertonProcess(spot_handle, dividend_yield, flat_ts, flat_vol_ts)
european_option.setPricingEngine(ql.AnalyticEuropeanEngine(bsm_process)) bs_price = european_option.NPV() print(f"The theoretical price is {bs_price}")
The theoretical price is 6.749271812460607
def binomial_price(bsm_process, steps): binomial_engine = ql.BinomialVanillaEngine(bsm_process, "crr", steps) european_option.setPricingEngine(binomial_engine) return european_option.NPV() steps = range(2, 100, 1) prices = [binomial_price(bsm_process, step) for step in steps]
plt.plot(steps, prices, label="Binomial Tree Price", lw=2, alpha=0.6) plt.plot([0, 100], [bs_price, bs_price], "r--", label="BSM Price", lw=2, alpha=0.6) plt.xlabel("Steps") plt.ylabel("Price") plt.title("Binomial Tree Price For Varying Steps") plt.legend()
<matplotlib.legend.Legend at 0x7f25d6afbdc0>
Image in a Jupyter notebook