Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
from quantlib.termstructures.yields.zero_curve import ZeroCurve
def flat_rate(forward, daycounter):
return FlatForward(
quote = SimpleQuote(forward),
settlement_days = 0,
calendar = NullCalendar(),
daycounter = daycounter
)
DtSettlement = today()
settings = Settings()
settings.evaluation_date = DtSettlement
daycounter = ActualActual()
calendar = NullCalendar()
iRate = .1
iDiv = .04
risk_free_ts = flat_rate(iRate, daycounter)
dividend_ts = flat_rate(iDiv, daycounter)
s0 = SimpleQuote(32.0)
# Bates model
v0 = 0.05
kappa = 5.0;
theta = 0.05;
sigma = 1.0e-4;
endOfMonth, Actual360())
spread = SimpleQuote(0)
fwdStart = Period(0, Days)
for m, period, label in swapData:
rate = df_libor.get_value(dtObs, label)
helper = SwapRateHelper(SimpleQuote(rate/100),
Period(m, Years),
calendar, Annual,
Unadjusted, Thirty360(),
liborIndex, spread, fwdStart)
rate_helpers.append(helper)
ts_day_counter = ActualActual(ISDA)
tolerance = 1.0e-15
ts = term_structure_factory(
'discount', 'loglinear', settlement_date, rate_helpers,
ts_day_counter, tolerance)
return ts
from quantlib.time.api import today, NullCalendar, ActualActual
def flat_rate(forward, daycounter):
return FlatForward(
forward = SimpleQuote(forward),
settlement_days = 0,
calendar = NullCalendar(),
daycounter = daycounter
)
settings = Settings.instance()
settlement_date = today()
settings.evaluation_date = settlement_date
daycounter = ActualActual()
calendar = NullCalendar()
interest_rate = .1
dividend_yield = .04
risk_free_ts = flat_rate(interest_rate, daycounter)
dividend_ts = flat_rate(dividend_yield, daycounter)
s0 = SimpleQuote(32.0)
# Heston model
v0 = 0.05
kappa = 5.0
theta = 0.05
sigma = 1.0e-4
process = HestonProcess(
risk_free_ts, dividend_ts, spot, params['v0'], params['kappa'],
params['theta'], params['sigma'], params['rho'])
model = HestonModel(process)
engine = AnalyticHestonEngine(model, 64)
DtSettlement = dateToQLDate(dtTrade)
settings = Settings()
settings.evaluation_date = DtSettlement
calendar = TARGET()
model_value = np.zeros(len(df_option))
daycounter = ActualActual()
for index, row in df_option.T.iteritems():
dtExpiry = row['dtExpiry']
strike = row['Strike']
cp = Call if row['Type'] == 'C' else Put
payoff = PlainVanillaPayoff(cp, strike)
dtExpiry = dateToQLDate(row['dtExpiry'])
exercise = EuropeanExercise(dtExpiry)
option = VanillaOption(payoff, exercise)
spread = SimpleQuote(0)
fwdStart = Period(0, Days)
for m, period, label in swapData:
rate = df_libor.get_value(dtObs, label)
helper = SwapRateHelper.from_tenor(
SimpleQuote(rate / 100.0),
Period(m, Years),
calendar, Semiannual,
ModifiedFollowing, Thirty360(),
liborIndex, spread, fwdStart)
rate_helpers.append(helper)
ts_day_counter = ActualActual(ISDA)
tolerance = 1.0e-15
ts = PiecewiseYieldCurve.from_reference_date(BootstrapTrait.Discount,
Interpolator.LogLinear,
settlement_date,
rate_helpers,
ts_day_counter,
tolerance)
ts.extrapolation = True
return ts
Actual360())
spread = SimpleQuote(0)
fwdStart = Period(0, Days)
for m, period, label in swapData:
rate = df_libor.get_value(dtObs, label)
helper = SwapRateHelper.from_tenor(rate/100.,
Period(m, Years),
calendar, Annual,
Unadjusted, Thirty360(),
liborIndex, spread, fwdStart)
rate_helpers.append(helper)
ts_day_counter = ActualActual(ISDA)
tolerance = 1.0e-15
ts = term_structure_factory('discount', 'loglinear',
settlement_date, rate_helpers,
ts_day_counter, tolerance)
return ts
def _blsprice(spot, strike, risk_free_rate, time, volatility,
option_type='Call', dividend=0.0, calc='price'):
"""
Black-Scholes option pricing model + greeks.
"""
_spot = SimpleQuote(spot)
daycounter = ActualActual(ISMA)
risk_free_ts = FlatForward(today(), risk_free_rate, daycounter)
dividend_ts = FlatForward(today(), dividend, daycounter)
volatility_ts = BlackConstantVol(today(), NullCalendar(),
volatility, daycounter)
process = BlackScholesMertonProcess(_spot, dividend_ts,
risk_free_ts, volatility_ts)
exercise_date = today() + Period(time * 365, Days)
exercise = EuropeanExercise(exercise_date)
payoff = PlainVanillaPayoff(option_type, strike)
option = EuropeanOption(payoff, exercise)
engine = AnalyticEuropeanEngine(process)
option.set_pricing_engine(engine)
def _blsimpv(price, spot, strike, risk_free_rate, time,
option_type='Call', dividend=0.0):
spot = SimpleQuote(spot)
daycounter = ActualActual()
risk_free_ts = FlatForward(today(), risk_free_rate, daycounter)
dividend_ts = FlatForward(today(), dividend, daycounter)
volatility_ts = BlackConstantVol(today(), NullCalendar(),
.3, daycounter)
process = BlackScholesMertonProcess(spot, dividend_ts,
risk_free_ts, volatility_ts)
exercise_date = today() + Period(time * 365, Days)
exercise = EuropeanExercise(exercise_date)
payoff = PlainVanillaPayoff(option_type, strike)
option = EuropeanOption(payoff, exercise)
engine = AnalyticEuropeanEngine(process)
option.set_pricing_engine(engine)
from quantlib.models.equity.heston_model import HestonModel
from quantlib.quotes import SimpleQuote
from quantlib.settings import Settings
from quantlib.time.api import today, NullCalendar, ActualActual
from quantlib.util.rates import flat_rate
import pylab as pl
from quantlib.sim.simulate import simulate_model
# The Heston Process
# ------------------
settings = Settings.instance()
settlement_date = today()
settings.evaluation_date = settlement_date
daycounter = ActualActual()
calendar = NullCalendar()
interest_rate = .1
dividend_yield = .04
risk_free_ts = flat_rate(interest_rate, daycounter)
dividend_ts = flat_rate(dividend_yield, daycounter)
s0 = SimpleQuote(100.0)
# Heston model
v0 = 0.05
kappa = 5.0
theta = 0.05
sigma = 1.0e-4