def make_test_data(F, sigma, r, t, noise): """Returns a test option chain with up to 100 strikes surrounding the futures price F. """ K = range(max(1, F - 50), F + 50) calls, puts = [], [] for Ki in K: P, C = black('p', F, Ki, t, r, sigma), black('c', F, Ki, t, r, sigma) P += numpy.random.rand() * noise * numpy.random.choice([-1, 1]) C += numpy.random.rand() * noise * numpy.random.choice([-1, 1]) calls.append(C) puts.append(P) return K, calls, puts, t
def make_test_data(F, sigma, r, t, noise): """Returns a test option chain with up to 100 strikes surrounding the futures price F. """ K= range(max(1,F-50),F+50) calls, puts = [],[] for Ki in K: P, C = black('p', F, Ki, t, r, sigma), black('c', F, Ki, t, r, sigma) P += numpy.random.rand() * noise * numpy.random.choice([-1,1]) C += numpy.random.rand() * noise * numpy.random.choice([-1,1]) calls.append(C) puts.append(P) return K, calls, puts, t
def rho(flag, F, K, t, r, sigma): """Returns the Black rho of an option. :param flag: 'c' or 'p' for call or put. :type flag: str :param F: underlying futures price :type F: float :param K: strike price :type K: float :param t: time to expiration in years :type t: float :param r: annual risk-free interest rate :type r: float :param sigma: volatility :type sigma: float :returns: float :: ========================================================== The text book analytical formula does not multiply by .01, but in practice rho is defined as the change in price for each 1 percent change in r, hence we multiply by 0.01. ========================================================== >>> F = 49 >>> K = 50 >>> r = .05 >>> t = 0.3846 >>> sigma = 0.2 >>> flag = 'c' >>> v1 = rho(flag, F, K, t, r, sigma) >>> v2 = -0.0074705380059582258 >>> abs(v1-v2) < .000001 True >>> flag = 'p' >>> v1 = rho(flag, F, K, t, r, sigma) >>> v2 = -0.011243286001308292 >>> abs(v1-v2) < .000001 True """ return -t * black(flag, F, K, t, r, sigma) * 0.01
def rho(flag, F, K, t, r, sigma): """Returns the Black rho of an option. :param flag: 'c' or 'p' for call or put. :type flag: str :param F: underlying futures price :type F: float :param K: strike price :type K: float :param t: time to expiration in years :type t: float :param r: annual risk-free interest rate :type r: float :param sigma: volatility :type sigma: float :returns: float :: ========================================================== The text book analytical formula does not multiply by .01, but in practice rho is defined as the change in price for each 1 percent change in r, hence we multiply by 0.01. ========================================================== >>> F = 49 >>> K = 50 >>> r = .05 >>> t = 0.3846 >>> sigma = 0.2 >>> flag = 'c' >>> v1 = rho(flag, F, K, t, r, sigma) >>> v2 = -0.0074705380059582258 >>> abs(v1-v2) < .000001 True >>> flag = 'p' >>> v1 = rho(flag, F, K, t, r, sigma) >>> v2 = -0.011243286001308292 >>> abs(v1-v2) < .000001 True """ return -t * black(flag, F, K, t, r, sigma) * .01
def plot_numerical_vs_analytical(greek, flag): f_analytical, f_numerical = { 'd': (adelta, delta), 'g': (agamma, gamma), 't': (atheta, theta), 'v': (avega, vega), 'r': (arho, rho), }[greek] S_vals = numpy.linspace(10, 250, 2000) K = 100 t = 1.0 r = .1 sigma = 0.3 price, analytical, numerical = [], [], [] for S in S_vals: analytical.append(f_analytical(flag, S, K, t, r, sigma)) numerical.append(f_numerical(flag, S, K, t, r, sigma)) price.append(black(flag, S, K, t, r, sigma)) plt.figure(1) plt.subplot(211) plt.plot(S_vals, analytical, label='Analytical') plt.plot(S_vals, numerical, label='Numerical') min_a, max_a = min(analytical), max(analytical) h = max_a - min_a plt.ylim(min_a - h * .05, max_a + h * .05) plt.grid() plt.legend(loc='best') plt.subplot(212) plt.plot(S_vals, price, label='Price') plt.ylim(min(min(price), -10), max(max(price), 10)) plt.grid() plt.legend(loc='best') plt.show()
import numpy from vollib.black import black from vollib.helper.numerical_greeks import delta as numerical_delta from vollib.helper.numerical_greeks import vega as numerical_vega from vollib.helper.numerical_greeks import theta as numerical_theta from vollib.helper.numerical_greeks import rho as numerical_rho from vollib.helper.numerical_greeks import gamma as numerical_gamma from vollib.black.greeks.analytical import gamma as agamma from vollib.black.greeks.analytical import delta as adelta from vollib.black.greeks.analytical import vega as avega from vollib.black.greeks.analytical import rho as arho from vollib.black.greeks.analytical import theta as atheta f = lambda flag, F, K, t, r, sigma, b: black(flag, F, K, t, r, sigma) def delta(flag, F, K, t, r, sigma): """Returns the Black delta of an option. :param flag: 'c' or 'p' for call or put. :type flag: str :param F: underlying futures price :type F: float :param K: strike price :type K: float :param t: time to expiration in years :type t: float :param r: annual risk-free interest rate :type r: float