Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
settings = Settings()
settings.evaluation_date = DtSettlement
calendar = TARGET()
if df_rates is None:
df_tmp = DataFrame.filter(df_option, items=['dtExpiry', 'IR', 'IDIV'])
grouped = df_tmp.groupby('dtExpiry')
df_rates = grouped.agg(lambda x: x[0])
# convert data frame (date/value) into zero curve
# expect the index to be a date, and 1 column of values
risk_free_ts = df_to_zero_curve(df_rates['R'], dtTrade)
dividend_ts = df_to_zero_curve(df_rates['D'], dtTrade)
# back out the spot from any forward
iRate = df_option['R'][0]
iDiv = df_option['D'][0]
TTM = df_option['T'][0]
Fwd = df_option['F'][0]
spot = SimpleQuote(Fwd * np.exp(-(iRate - iDiv) * TTM))
print('Spot: %f risk-free rate: %f div. yield: %f' % \
(spot.value, iRate, iDiv))
# loop through rows in option data frame, construct
# helpers for bid/ask
oneDay = datetime.timedelta(days=1)
dtExpiry = [dtTrade + int(t * 365) * oneDay for t in df_option['T']]
df_option['dtExpiry'] = dtExpiry
DtSettlement = pydate_to_qldate(dtTrade)
settings = Settings()
settings.evaluation_date = DtSettlement
calendar = TARGET()
if df_rates is None:
df_tmp = DataFrame.filter(df_option, items=['dtExpiry', 'IR', 'IDIV'])
grouped = df_tmp.groupby('dtExpiry')
df_rates = grouped.agg(lambda x: x[0])
# convert data frame (date/value) into zero curve
# expect the index to be a date, and 1 column of values
risk_free_ts = df_to_zero_curve(df_rates['R'], dtTrade)
dividend_ts = df_to_zero_curve(df_rates['D'], dtTrade)
# back out the spot from any forward
iRate = df_option['R'][0]
iDiv = df_option['D'][0]
TTM = df_option['T'][0]
Fwd = df_option['F'][0]
spot = SimpleQuote(Fwd * np.exp(-(iRate - iDiv) * TTM))
print('Spot: %f risk-free rate: %f div. yield: %f' % \
(spot.value, iRate, iDiv))
# loop through rows in option data frame, construct
# helpers for bid/ask
oneDay = datetime.timedelta(days=1)
dtExpiry = [dtTrade + int(t * 365) * oneDay for t in df_option['T']]
def bates_calibration(df_option, dtTrade=None, df_rates=None, ival=None):
# array of option helpers
hh = heston_helpers(df_option, dtTrade, df_rates, ival)
options = hh['options']
spot = hh['spot']
risk_free_ts = df_to_zero_curve(df_rates['R'], dtTrade)
dividend_ts = df_to_zero_curve(df_rates['D'], dtTrade)
v0 = .02
if ival is None:
ival = {'v0': v0, 'kappa': 3.7, 'theta': v0,
'sigma': 1.0, 'rho': -.6, 'lambda': .1,
'nu': -.5, 'delta': 0.3}
process = BatesProcess(
risk_free_ts, dividend_ts, spot, ival['v0'], ival['kappa'],
ival['theta'], ival['sigma'], ival['rho'],
ival['lambda'], ival['nu'], ival['delta'])
model = BatesModel(process)
engine = BatesEngine(model, 64)
def heston_calibration(df_option, dtTrade=None, df_rates=None, ival=None):
"""
calibrate heston model
"""
# array of option helpers
print df_option, df_rates, ival
hh = heston_helpers(df_option, dtTrade, df_rates, ival)
options = hh['options']
spot = hh['spot']
risk_free_ts = df_to_zero_curve(df_rates['R'], dtTrade)
dividend_ts = df_to_zero_curve(df_rates['D'], dtTrade)
if ival is None:
ival = {'v0': 0.1, 'kappa': 1.0, 'theta': 0.1,
'sigma': 0.5, 'rho': -.5}
process = HestonProcess(
risk_free_ts, dividend_ts, spot, ival['v0'], ival['kappa'],
ival['theta'], ival['sigma'], ival['rho'])
model = HestonModel(process)
engine = AnalyticHestonEngine(model, 64)
for option in options:
option.set_pricing_engine(engine)
om = LevenbergMarquardt(1e-8, 1e-8, 1e-8)
def heston_calibration(df_option, dtTrade=None, df_rates=None, ival=None):
"""
calibrate heston model
"""
# array of option helpers
print df_option, df_rates, ival
hh = heston_helpers(df_option, dtTrade, df_rates, ival)
options = hh['options']
spot = hh['spot']
risk_free_ts = df_to_zero_curve(df_rates['R'], dtTrade)
dividend_ts = df_to_zero_curve(df_rates['D'], dtTrade)
if ival is None:
ival = {'v0': 0.1, 'kappa': 1.0, 'theta': 0.1,
'sigma': 0.5, 'rho': -.5}
process = HestonProcess(
risk_free_ts, dividend_ts, spot, ival['v0'], ival['kappa'],
ival['theta'], ival['sigma'], ival['rho'])
model = HestonModel(process)
engine = AnalyticHestonEngine(model, 64)
for option in options:
option.set_pricing_engine(engine)
def batesdetjump_calibration(df_option, dtTrade=None,
df_rates=None, ival=None):
# array of option helpers
hh = heston_helpers(df_option, dtTrade, df_rates, ival)
options = hh['options']
spot = hh['spot']
risk_free_ts = df_to_zero_curve(df_rates['R'], dtTrade)
dividend_ts = df_to_zero_curve(df_rates['D'], dtTrade)
v0 = .02
if ival is None:
ival = {'v0': v0, 'kappa': 3.7, 'theta': v0,
'sigma': 1.0, 'rho': -.6, 'lambda': .1,
'nu': -.5, 'delta': 0.3}
process = BatesProcess(
risk_free_ts, dividend_ts, spot, ival['v0'], ival['kappa'],
ival['theta'], ival['sigma'], ival['rho'],
ival['lambda'], ival['nu'], ival['delta'])
model = BatesDetJumpModel(process)
engine = BatesDetJumpEngine(model, 64)
def heston_pricer(trade_date, options, params, rates, spot):
"""
Price a list of European options with heston model.
"""
spot = SimpleQuote(spot)
risk_free_ts = df_to_zero_curve(rates[nm.INTEREST_RATE], trade_date)
dividend_ts = df_to_zero_curve(rates[nm.DIVIDEND_YIELD], trade_date)
process = HestonProcess(risk_free_ts, dividend_ts, spot, **params)
model = HestonModel(process)
engine = AnalyticHestonEngine(model, 64)
settlement_date = pydate_to_qldate(trade_date)
settings = Settings()
settings.evaluation_date = settlement_date
modeled_values = np.zeros(len(options))
for index, row in options.T.iteritems():