def test_analytical_delta(self): while self.tdi.has_next(): row = self.tdi.next_row() S, K, t, r, sigma = row['S'], row['K'], row['t'], row['R'], row[ 'v'] self.assertAlmostEqual(analytical.delta('c', S, K, t, r, sigma, q), row['CD'], delta=0.000001) self.assertAlmostEqual(analytical.delta('p', S, K, t, r, sigma, q), row['PD'], delta=0.000001)
def test_analytical_delta(self): S = 49 K = 50 sigma = .2 r = .05 t = 0.3846 q = 0.2 flag = 'p' c_put = c_analytical.delta(flag, S, K, t, r, sigma, q) py_put = py_analytical.delta(flag, S, K, t, r, sigma, q) self.assertTrue(almost_equal(c_put, py_put)) flag = 'c' c_call = c_analytical.delta(flag, S, K, t, r, sigma, q) py_call = py_analytical.delta(flag, S, K, t, r, sigma, q) self.assertTrue(almost_equal(c_call, py_call))
def delta(self, S, vol, t0=None): if t0 is None: if self.exp not in option.time_exp: option.time_exp[self.exp] = self.timetoexp() te = option.time_exp[self.exp] else: te = self.timetoexp(t0) if te <= 0: te = 0 option.time_exp[self.exp] = te return bsg.delta(self.flag, S, self.K, te, self.rate, vol, 0.)
def test_delta(self): deltas = [ delta(flag, S, K, t, r, sigma, q) for flag, S, K, t, r, sigma, q in self.arg_combinations ] ndeltas = [ ndelta(flag, S, K, t, r, sigma, q) for flag, S, K, t, r, sigma, q in self.arg_combinations ] self.assertTrue(self.diff_mean(deltas, ndeltas) < self.epsilon)
def get_malliavin_greeks_bs_flat(s0: float, t: float, no_steps: int, no_paths: int, r: float, q: float, sigma: float, payoff: Callable[[ndarray], ndarray], euler_scheme: EULER_SCHEME_TYPE): shift_delta = 0.01 shift_vega = 0.01 z = np.random.standard_normal(size=(no_paths, no_steps - 1)) drift_t = partial(bs_drift_flat, rate_t=r, dividend_t=q) sigma_t = partial(bs_sigma_flat, sigma_t=sigma) sigma_t_shift = partial(bs_sigma_flat, sigma_t=sigma + shift_vega) s_t = sde_euler_simulation(0.0, t, s0, no_steps, no_paths, z, drift_t, sigma_t, euler_scheme) s_t_shift_delta = sde_euler_simulation(0.0, t, s0 + shift_delta, no_steps, no_paths, z, drift_t, sigma_t, euler_scheme) s_t_shift_vega = sde_euler_simulation(0.0, t, s0, no_steps, no_paths, z, drift_t, sigma_t_shift, euler_scheme) phi_t = payoff(s_t[:, -1]) mc_price = np.average(phi_t) * np.exp(-r * t) mc_price_delta = np.average(payoff(s_t_shift_delta[:, -1])) * np.exp( -r * t) mc_price_vega = np.average(payoff(s_t_shift_vega[:, -1])) * np.exp(-r * t) delta = analytical.delta('c', s0, 90, t, r, sigma, q) price = black_scholes_merton('c', s0, 90, t, r, sigma, q) vega = analytical.vega('c', s0, 90, t, r, sigma, q) * 100 mc_delta = (mc_price_delta - mc_price) / shift_delta malliavin_delta = get_malliavin_delta_bs_flat(phi_t, s_t[:, -1], s0, sigma, r, q, t) mc_vega = (mc_price_vega - mc_price) / shift_vega malliavin_vega = get_malliavin_vega_bs_flat(phi_t, s_t[:, -1], s0, sigma, r, q, t) return mc_delta, malliavin_delta
pos = api.position() pos_ticker = list(pos[pos != 0].index) sum_delta = 0 for opt_ticker in pos_ticker: if 'C' in opt_ticker: flag = 'c' elif 'P' in opt_ticker: flag = 'p' else: sum_delta += pos[opt_ticker] * 1 continue K = int(re.findall(r'\d+', opt_ticker)[0]) if opt_ticker in call_list: sigma = iv_s[opt_ticker] elif opt_ticker in put_list: sigma = iv_s[opt_ticker] opt_delta = delta(flag, S_last, K, t, r, sigma, q) sum_delta += pos[opt_ticker] * 100 * opt_delta if sum_delta > 0: while sum_delta > 10000: api.market_sell("RTM", 10000) sum_delta -= 10000 api.market_sell("RTM", sum_delta) elif sum_delta < 0: while sum_delta < -10000: api.market_buy("RTM", 10000) sum_delta += 10000 api.market_buy("RTM", -sum_delta) pass
t_i_s = np.linspace(0.0, T, no_time_steps) diff_t_i_s = np.diff(t_i_s) # We will do the dynamic hedge of option call under BS model. k = 100.0 spot = 120.0 r = 0.02 q = 0.00 sigma = 0.7 delta_time = T notional = 1000.0 portfolio_t_i = np.zeros(no_time_steps) option_t_i = np.zeros(no_time_steps) alpha_t_i_1 = analytical.delta('c', spot, k, delta_time, r, sigma, q) * notional option_t_i[0] = black_scholes_merton('c', spot, k, delta_time, r, sigma, q) * notional beta_t = option_t_i[0] - alpha_t_i_1 * spot portfolio_t_i[0] = (alpha_t_i_1 * spot + beta_t) s_t_i_1 = spot s_t_i = 0.0 alpha_t_i = 0.0 rebalanced_index = list( map(lambda x: int(x), list(np.arange(0.0, no_time_steps, 30)))) # rebalanced_index = np.arange(1, no_time_steps) rng = RNG.RndGenerator(123)