def test_delta_greeks(self): ''' Q: (Delta Greeks) A future option, 6 month to expiry, the futures price is 105, the strike strike price is 100, risk free interest rate is 10% per year, volatility is 36% per year A: delta_call = 0.5946 delta_put = -0.3566 ''' bsg = BlackScholesGreeks('futures_option', 105, 100, 0.1, 0.5, 0.36) self.assertEqual(0.5946, bsg.get_delta_greeks(OptionType.CALL)) self.assertEqual(-0.3566, bsg.get_delta_greeks(OptionType.PUT))
def test_delta_greeks2(self): ''' Q: A commodity option with two years to expiration. The commodity price is 90, the strike price is 40, the risk-free interest rate is 3% per year, the cost-of-carry is 9% per year, and the volatility is 20%. What's the delta of a call option? A: delta_call = 1.1273 This implies that the call option price will increase/decrease 1.1273 USD if the spot price increase/decrease by 1 USD ''' bsg = BlackScholesGreeks(None, 90, 40, 0.03, 2, 0.2, cost_of_carry=0.09) self.assertEqual(1.1273, bsg.get_delta_greeks(OptionType.CALL)) # For every 1 dollar increase/decrease of the spot price, the option price increase/decrease 1.1273 opt_price = BlackScholes(None, 90, 40, 0.03, 2, 0.2, cost_of_carry=0.09).get_option_price( OptionType.CALL) self.assertAlmostEqual( opt_price + 1.1273, BlackScholes(None, 91, 40, 0.03, 2, 0.2, cost_of_carry=0.09).get_option_price(OptionType.CALL), 3) self.assertEqual( round(opt_price - 1.1273, 4), BlackScholes(None, 89, 40, 0.03, 2, 0.2, cost_of_carry=0.09).get_option_price(OptionType.CALL))
def test_delta_greeks2(self): ''' Q: A commodity option with two years to expiration. The commodity price is 90, the strike price is 40, the risk-free interest rate is 3% per year, the cost-of-carry is 9% per year, and the volatility is 20%. What's the delta of a call option? A: delta_call = 1.1273 This implies that the call option price will increase/decrease 1.1273 USD if the spot price increase/decrease by 1 USD ''' bsg = BlackScholesGreeks(None, 90, 40, 0.03, 2, 0.2, cost_of_carry=0.09) self.assertEqual(1.1273, bsg.get_delta_greeks(OptionType.CALL)) # For every 1 dollar increase/decrease of the spot price, the option price increase/decrease 1.1273 opt_price = BlackScholes(None, 90, 40, 0.03, 2, 0.2, cost_of_carry=0.09).get_option_price(OptionType.CALL) self.assertAlmostEqual(opt_price + 1.1273, BlackScholes(None, 91, 40, 0.03, 2, 0.2, cost_of_carry=0.09).get_option_price(OptionType.CALL), 3) self.assertEqual(round(opt_price - 1.1273, 4), BlackScholes(None, 89, 40, 0.03, 2, 0.2, cost_of_carry=0.09).get_option_price(OptionType.CALL))
plt.show() ''' fig = plt.figure(figsize=plt.figaspect(0.5)) ##### 1st subplot ##### X = np.arange(1, 180, 5) # days to maturity lenX = len(X) Y = np.arange(50, 150, 2) # spot price lenY = len(Y) X, Y = np.meshgrid(X, Y) # Spot delta call: X = 100, r = 7%, b = 4%, vol= 30% Z = [[ BlackScholesGreeks(None, Y[i][j], 100, 0.07, X[i][j] / 365.0, 0.3, cost_of_carry=0.04).get_delta_greeks( OptionType.CALL) for j in range(lenX) ] for i in range(lenY)] ax = fig.add_subplot(121, projection='3d') ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=cm.jet, linewidth=0) #, antialiased=False) #ax.plot_wireframe(X, Y, Z) # Draw a wireframe diagram without color ax.set_zlim(0, 1) ax.set_xlabel('Days to maturity') ax.set_ylabel('Asset spot price') ax.set_zlabel('Delta') ##### 2nd subplot ######