Exemplo n.º 1
0
    def test_implied_volatility(self):
        self.assertAlmostEquals(0.30, Option.imply_volatility(10.10, opt_type=OptionType.PUT, spot0=90, strike=100, mat=1.0 / 12,
                                                              riskless_rate=0.10), delta=0.01)
        self.assertAlmostEquals(0.52, Option.imply_volatility(2.25, opt_type=OptionType.CALL, spot0=90, strike=100, mat=1.0 / 12,
                                                              riskless_rate=0.10), delta=0.01)

        premium = 10.10
        opt_kwargs = {
            'opt_type': OptionType.PUT,
            'spot0': 90,
            'strike': 100,
            'mat': 1.0 / 12,
            'riskless_rate': 0.10,
        }
        self.assertAlmostEquals(0.30, Option.imply_volatility(premium, **opt_kwargs), delta=0.01)
        opt_kwargs['vol'] = Option.imply_volatility(premium, **opt_kwargs)
        self.assertAlmostEquals(premium, Option(**opt_kwargs).run_model(model=OptionModel.BINOMIAL_TREE)[OptionMeasure.VALUE], delta=0.01)
Exemplo n.º 2
0
    def test_model_cache(self):
        opt = Option(opt_type=OptionType.CALL,
                     spot0=90,
                     strike=100,
                     mat=3.0 / 12,
                     vol=0.35,
                     riskless_rate=0.10,
                     yield_=0.025)

        self.assertEquals(0, len(opt.model_cache))
        self.assertAlmostEquals(
            3.19,
            opt.run_model(
                model=OptionModel.BINOMIAL_TREE)[OptionMeasure.VALUE],
            delta=0.01)
        self.assertEquals([OptionModel.BINOMIAL_TREE], opt.model_cache.keys())
        self.assertAlmostEquals(
            3.19,
            opt.run_model(
                model=OptionModel.BINOMIAL_TREE)[OptionMeasure.VALUE],
            delta=0.01)

        opt.model_cache[OptionModel.BINOMIAL_TREE][OptionMeasure.VALUE] = 42.42
        self.assertAlmostEquals(
            42.42,
            opt.run_model(
                model=OptionModel.BINOMIAL_TREE)[OptionMeasure.VALUE],
            delta=0.01)

        opt.vol = 0.55
        self.assertAlmostEquals(
            6.76,
            opt.run_model(
                model=OptionModel.BINOMIAL_TREE)[OptionMeasure.VALUE],
            delta=0.01)
Exemplo n.º 3
0
    def test_binomial_tree_discrete_dividend_valuation(self):
        mat = 9.0 / 12
        num_steps = 9 * 4
        self.assertAlmostEquals(13.93, Option(opt_type=OptionType.PUT, spot0=90, strike=100, mat=mat,
                                              vol=0.35, riskless_rate=0.10) \
                                .run_model(model=OptionModel.BINOMIAL_TREE, num_steps=num_steps)[OptionMeasure.VALUE], delta=0.01)
        self.assertAlmostEquals(13.93, Option(opt_type=OptionType.PUT, spot0=90, strike=100, mat=mat,
                                              vol=0.35, riskless_rate=0.10, yield_=lambda t1, t2: 0) \
                                .run_model(model=OptionModel.BINOMIAL_TREE, num_steps=num_steps)[OptionMeasure.VALUE], delta=0.01)
        self.assertAlmostEquals(14.77, Option(opt_type=OptionType.PUT, spot0=90, strike=100, mat=mat,
                                              vol=0.35, riskless_rate=0.10, yield_=0.04) \
                                .run_model(model=OptionModel.BINOMIAL_TREE, num_steps=num_steps)[OptionMeasure.VALUE], delta=0.01)

        def div1_fn(t1, t2):  # $2.50 dividend after about a one month
            n = t2 / (mat / num_steps)
            return 2.50 if n == 4 else 0
        self.assertAlmostEquals(15.45, Option(opt_type=OptionType.PUT, spot0=90, strike=100, mat=mat,
                                              vol=0.35, riskless_rate=0.10, yield_=div1_fn) \
                                .run_model(model=OptionModel.BINOMIAL_TREE, num_steps=num_steps)[OptionMeasure.VALUE], delta=0.01)

        def div_monthly_fn(t1, t2):  # $2.50 dividend every month
            n = t2 / (mat / num_steps)
            return 2.50 if (n % 4) == 0 else 0
        self.assertAlmostEquals(3.57, Option(opt_type=OptionType.CALL, spot0=90, strike=100, mat=mat,
                                             vol=0.35, riskless_rate=0.10, yield_=div_monthly_fn) \
                                .run_model(model=OptionModel.BINOMIAL_TREE, num_steps=num_steps)[OptionMeasure.VALUE], delta=0.01)
        self.assertAlmostEquals(24.51, Option(opt_type=OptionType.PUT, spot0=90, strike=100, mat=mat,
                                              vol=0.35, riskless_rate=0.10, yield_=div_monthly_fn) \
                                .run_model(model=OptionModel.BINOMIAL_TREE, num_steps=num_steps)[OptionMeasure.VALUE], delta=0.01)
Exemplo n.º 4
0
    def test_implied_volatility(self):
        self.assertAlmostEquals(0.30,
                                Option.imply_volatility(
                                    10.10,
                                    opt_type=OptionType.PUT,
                                    spot0=90,
                                    strike=100,
                                    mat=1.0 / 12,
                                    riskless_rate=0.10),
                                delta=0.01)
        self.assertAlmostEquals(0.52,
                                Option.imply_volatility(
                                    2.25,
                                    opt_type=OptionType.CALL,
                                    spot0=90,
                                    strike=100,
                                    mat=1.0 / 12,
                                    riskless_rate=0.10),
                                delta=0.01)

        premium = 10.10
        opt_kwargs = {
            'opt_type': OptionType.PUT,
            'spot0': 90,
            'strike': 100,
            'mat': 1.0 / 12,
            'riskless_rate': 0.10,
        }
        self.assertAlmostEquals(0.30,
                                Option.imply_volatility(premium, **opt_kwargs),
                                delta=0.01)
        opt_kwargs['vol'] = Option.imply_volatility(premium, **opt_kwargs)
        self.assertAlmostEquals(
            premium,
            Option(**opt_kwargs).run_model(
                model=OptionModel.BINOMIAL_TREE)[OptionMeasure.VALUE],
            delta=0.01)
Exemplo n.º 5
0
    def test_model_cache(self):
        opt = Option(opt_type=OptionType.CALL, spot0=90, strike=100, mat=3.0 / 12,
                     vol=0.35, riskless_rate=0.10, yield_=0.025)

        self.assertEquals(0, len(opt.model_cache))
        self.assertAlmostEquals(3.19, opt.run_model(model=OptionModel.BINOMIAL_TREE)[OptionMeasure.VALUE], delta=0.01)
        self.assertEquals([OptionModel.BINOMIAL_TREE], opt.model_cache.keys())
        self.assertAlmostEquals(3.19, opt.run_model(model=OptionModel.BINOMIAL_TREE)[OptionMeasure.VALUE], delta=0.01)

        opt.model_cache[OptionModel.BINOMIAL_TREE][OptionMeasure.VALUE] = 42.42
        self.assertAlmostEquals(42.42, opt.run_model(model=OptionModel.BINOMIAL_TREE)[OptionMeasure.VALUE], delta=0.01)

        opt.vol = 0.55
        self.assertAlmostEquals(6.76, opt.run_model(model=OptionModel.BINOMIAL_TREE)[OptionMeasure.VALUE], delta=0.01)
Exemplo n.º 6
0
    def test_monte_carlo_valuation(self):
        self.assertAlmostEquals(2.51, Option(opt_type=OptionType.CALL, spot0=90, strike=100, mat=3.0 / 12,
                                             vol=0.35, riskless_rate=0.10, yield_=0.05, exer_type=OptionExerciseType.EUROPEAN) \
                                .run_model(model=OptionModel.MONTE_CARLO, random_seed=12345)[OptionMeasure.VALUE], delta=0.01)
        self.assertAlmostEquals(3.60, Option(opt_type=OptionType.CALL, spot0=90, strike=100, mat=3.0 / 12,
                                             vol=0.35, riskless_rate=0.10, yield_=0.05, exer_type=OptionExerciseType.AMERICAN) \
                                .run_model(model=OptionModel.MONTE_CARLO, random_seed=12345)[OptionMeasure.VALUE], delta=0.01)

        mat = 9.0 / 12
        num_steps = 9 * 4

        def div_monthly_fn(t1, t2):  # $2.50 dividend every month
            n = t2 / (mat / num_steps)
            return 2.50 if (n % 4) == 0 else 0

        self.assertAlmostEquals(10.16, Option(opt_type=OptionType.PUT, spot0=90, strike=100, mat=mat,
                                              vol=0.35, riskless_rate=0.10, exer_type=OptionExerciseType.EUROPEAN) \
                                .run_model(model=OptionModel.MONTE_CARLO, num_steps=num_steps, random_seed=12345)[OptionMeasure.VALUE], delta=0.01)
        self.assertAlmostEquals(23.39, Option(opt_type=OptionType.PUT, spot0=90, strike=100, mat=mat,
                                              vol=0.35, riskless_rate=0.10, yield_=div_monthly_fn, exer_type=OptionExerciseType.EUROPEAN) \
                                .run_model(model=OptionModel.MONTE_CARLO, num_steps=num_steps, random_seed=12345)[OptionMeasure.VALUE], delta=0.01)
        self.assertAlmostEquals(1.40, Option(opt_type=OptionType.CALL, spot0=90, strike=100, mat=mat,
                                             vol=0.35, riskless_rate=0.10, yield_=div_monthly_fn, exer_type=OptionExerciseType.EUROPEAN) \
                                .run_model(model=OptionModel.MONTE_CARLO, num_steps=num_steps, random_seed=12345)[OptionMeasure.VALUE], delta=0.01)

        greek_put_opt_results = Option(opt_type=OptionType.PUT, spot0=90, strike=100, mat=3.0 / 12,
                                       vol=0.35, riskless_rate=0.10, yield_=0.05, exer_type=OptionExerciseType.EUROPEAN) \
            .run_model(model=OptionModel.MONTE_CARLO, random_seed=12345)
        self.assertAlmostEqual(-0.6442,
                               greek_put_opt_results[OptionMeasure.DELTA],
                               delta=0.0001)
        self.assertAlmostEqual(-6.7226,
                               greek_put_opt_results[OptionMeasure.THETA],
                               delta=0.0001)
        self.assertAlmostEqual(-17.0219,
                               greek_put_opt_results[OptionMeasure.RHO],
                               delta=0.0001)
        self.assertAlmostEqual(+15.2718,
                               greek_put_opt_results[OptionMeasure.VEGA],
                               delta=0.0001)
        self.assertAlmostEqual(+0.0254,
                               greek_put_opt_results[OptionMeasure.GAMMA],
                               delta=0.0001)

        greek_call_opt_results = Option(opt_type=OptionType.CALL, spot0=90, strike=100, mat=3.0 / 12,
                                       vol=0.35, riskless_rate=0.10, yield_=0.05, exer_type=OptionExerciseType.AMERICAN) \
            .run_model(model=OptionModel.MONTE_CARLO, random_seed=12345)
        self.assertAlmostEqual(+0.4217,
                               greek_call_opt_results[OptionMeasure.DELTA],
                               delta=0.0001)
        self.assertAlmostEqual(+60.4526,
                               greek_call_opt_results[OptionMeasure.THETA],
                               delta=0.0001)
        self.assertAlmostEqual(+6.4028,
                               greek_call_opt_results[OptionMeasure.RHO],
                               delta=0.0001)
        self.assertAlmostEqual(-55.4468,
                               greek_call_opt_results[OptionMeasure.VEGA],
                               delta=0.0001)
        self.assertAlmostEqual(+0.0163,
                               greek_call_opt_results[OptionMeasure.GAMMA],
                               delta=0.0001)
Exemplo n.º 7
0
    def test_black_scholes_valuation(self):
        self.assertAlmostEquals(
            13.70,
            Option(opt_type=OptionType.CALL,
                   spot0=100,
                   strike=95,
                   mat=3.0 / 12,
                   vol=0.50,
                   riskless_rate=0.10,
                   exer_type=OptionExerciseType.EUROPEAN).run_model(
                       model=OptionModel.BLACK_SCHOLES)[OptionMeasure.VALUE],
            delta=0.01)
        self.assertAlmostEquals(
            10.03,
            Option(opt_type=OptionType.PUT,
                   spot0=90,
                   strike=100,
                   mat=1.0 / 12,
                   vol=0.35,
                   riskless_rate=0.10,
                   exer_type=OptionExerciseType.EUROPEAN).run_model(
                       model=OptionModel.BLACK_SCHOLES)[OptionMeasure.VALUE],
            delta=0.01)
        self.assertAlmostEquals(
            3.45,
            Option(opt_type=OptionType.CALL,
                   spot0=90,
                   strike=100,
                   mat=3.0 / 12,
                   vol=0.35,
                   riskless_rate=0.10,
                   exer_type=OptionExerciseType.EUROPEAN).run_model(
                       model=OptionModel.BLACK_SCHOLES)[OptionMeasure.VALUE],
            delta=0.01)
        self.assertAlmostEquals(
            3.06,
            Option(opt_type=OptionType.CALL,
                   spot0=90,
                   strike=100,
                   mat=3.0 / 12,
                   vol=0.35,
                   riskless_rate=0.10,
                   yield_=0.05,
                   exer_type=OptionExerciseType.EUROPEAN).run_model(
                       model=OptionModel.BLACK_SCHOLES)[OptionMeasure.VALUE],
            delta=0.01)

        greek_call_opt = Option(opt_type=OptionType.CALL,
                                spot0=90,
                                strike=100,
                                mat=3.0 / 12,
                                vol=0.35,
                                riskless_rate=0.10,
                                yield_=0.05,
                                exer_type=OptionExerciseType.EUROPEAN)
        greek_put_opt = Option(opt_type=OptionType.PUT,
                               spot0=90,
                               strike=100,
                               mat=3.0 / 12,
                               vol=0.35,
                               riskless_rate=0.10,
                               yield_=0.05,
                               exer_type=OptionExerciseType.EUROPEAN)
        self.assertAlmostEquals(
            +0.3248,
            greek_call_opt.run_model(
                model=OptionModel.BLACK_SCHOLES)[OptionMeasure.DELTA],
            delta=0.0001)
        self.assertAlmostEquals(
            -0.6628,
            greek_put_opt.run_model(
                model=OptionModel.BLACK_SCHOLES)[OptionMeasure.DELTA],
            delta=0.0001)
        self.assertAlmostEquals(
            -12.4048,
            greek_call_opt.run_model(
                model=OptionModel.BLACK_SCHOLES)[OptionMeasure.THETA],
            delta=0.0001)
        self.assertAlmostEquals(
            -7.0958,
            greek_put_opt.run_model(
                model=OptionModel.BLACK_SCHOLES)[OptionMeasure.THETA],
            delta=0.0001)
        self.assertAlmostEquals(
            +6.5405,
            greek_call_opt.run_model(
                model=OptionModel.BLACK_SCHOLES)[OptionMeasure.RHO],
            delta=0.0001)
        self.assertAlmostEquals(
            -17.8422,
            greek_put_opt.run_model(
                model=OptionModel.BLACK_SCHOLES)[OptionMeasure.RHO],
            delta=0.0001)
        self.assertAlmostEquals(
            +16.0714,
            greek_call_opt.run_model(
                model=OptionModel.BLACK_SCHOLES)[OptionMeasure.VEGA],
            delta=0.0001)
        self.assertAlmostEquals(
            +16.0714,
            greek_put_opt.run_model(
                model=OptionModel.BLACK_SCHOLES)[OptionMeasure.VEGA],
            delta=0.0001)
        self.assertAlmostEquals(
            +0.0227,
            greek_call_opt.run_model(
                model=OptionModel.BLACK_SCHOLES)[OptionMeasure.GAMMA],
            delta=0.0001)
        self.assertAlmostEquals(
            +0.0227,
            greek_put_opt.run_model(
                model=OptionModel.BLACK_SCHOLES)[OptionMeasure.GAMMA],
            delta=0.0001)
Exemplo n.º 8
0
    def test_binomial_tree_valuation(self):
        self.assertAlmostEquals(
            10.32,
            Option(opt_type=OptionType.PUT,
                   spot0=90,
                   strike=100,
                   mat=1.0 / 12,
                   vol=0.35,
                   riskless_rate=0.10).run_model(
                       model=OptionModel.BINOMIAL_TREE)[OptionMeasure.VALUE],
            delta=0.01)
        self.assertAlmostEquals(
            10.01,
            Option(opt_type=OptionType.PUT,
                   spot0=90,
                   strike=100,
                   mat=1.0 / 12,
                   vol=0.35,
                   riskless_rate=0.10,
                   exer_type=OptionExerciseType.EUROPEAN).run_model(
                       model=OptionModel.BINOMIAL_TREE)[OptionMeasure.VALUE],
            delta=0.01)
        self.assertAlmostEquals(
            10.40,
            Option(opt_type=OptionType.PUT,
                   spot0=90,
                   strike=100,
                   mat=1.0 / 12,
                   vol=0.35,
                   riskless_rate=0.10,
                   yield_=0.025).run_model(
                       model=OptionModel.BINOMIAL_TREE)[OptionMeasure.VALUE],
            delta=0.01)
        self.assertAlmostEquals(
            3.19,
            Option(opt_type=OptionType.CALL,
                   spot0=90,
                   strike=100,
                   mat=3.0 / 12,
                   vol=0.35,
                   riskless_rate=0.10,
                   yield_=0.025).run_model(
                       model=OptionModel.BINOMIAL_TREE)[OptionMeasure.VALUE],
            delta=0.01)

        greek_call_opt = Option(opt_type=OptionType.CALL,
                                spot0=90,
                                strike=100,
                                mat=3.0 / 12,
                                vol=0.35,
                                riskless_rate=0.10,
                                yield_=0.05,
                                exer_type=OptionExerciseType.AMERICAN)
        greek_put_opt = Option(opt_type=OptionType.PUT,
                               spot0=90,
                               strike=100,
                               mat=3.0 / 12,
                               vol=0.35,
                               riskless_rate=0.10,
                               yield_=0.05,
                               exer_type=OptionExerciseType.AMERICAN)
        self.assertAlmostEquals(
            +0.3205,
            greek_call_opt.run_model(
                model=OptionModel.BINOMIAL_TREE)[OptionMeasure.DELTA],
            delta=0.0001)
        self.assertAlmostEquals(
            -0.6973,
            greek_put_opt.run_model(
                model=OptionModel.BINOMIAL_TREE)[OptionMeasure.DELTA],
            delta=0.0001)
        self.assertAlmostEquals(
            -12.6706,
            greek_call_opt.run_model(
                model=OptionModel.BINOMIAL_TREE)[OptionMeasure.THETA],
            delta=0.0001)
        self.assertAlmostEquals(
            -8.3641,
            greek_put_opt.run_model(
                model=OptionModel.BINOMIAL_TREE)[OptionMeasure.THETA],
            delta=0.0001)
        self.assertAlmostEquals(
            +6.4561,
            greek_call_opt.run_model(
                model=OptionModel.BINOMIAL_TREE)[OptionMeasure.RHO],
            delta=0.0001)
        self.assertAlmostEquals(
            -10.9031,
            greek_put_opt.run_model(
                model=OptionModel.BINOMIAL_TREE)[OptionMeasure.RHO],
            delta=0.0001)
        self.assertAlmostEquals(
            +14.1128,
            greek_call_opt.run_model(
                model=OptionModel.BINOMIAL_TREE)[OptionMeasure.VEGA],
            delta=0.0001)
        self.assertAlmostEquals(
            +13.7077,
            greek_put_opt.run_model(
                model=OptionModel.BINOMIAL_TREE)[OptionMeasure.VEGA],
            delta=0.0001)
        self.assertAlmostEquals(
            +0.0268,
            greek_call_opt.run_model(
                model=OptionModel.BINOMIAL_TREE)[OptionMeasure.GAMMA],
            delta=0.0001)
        self.assertAlmostEquals(
            +0.0178,
            greek_put_opt.run_model(
                model=OptionModel.BINOMIAL_TREE)[OptionMeasure.GAMMA],
            delta=0.0001)
Exemplo n.º 9
0
def modelling(scenario_path, opt_type, exer_type, strike, hedge_type,hedge_frequency, coverage_ratio, output_path):

	value_hedging = (hedge_type == 'value')

	# Dealing with parameters
	scenario = pd.read_csv(scenario_path + '.csv', sep = ';')

	price_scenario = list(scenario['PX_LAST'])
	vol_scenario = list(scenario['VOL'])
	N = len(price_scenario) # number of days
	

	# frequency with which delta hedging is going to be done
	hedge_window_list = hedge_frequency
	delta_hedge_cover_ratio = coverage_ratio # percent of delta stock bought

	# Creating dataframe for storing all data
	columns = ['Delta Hedge Window', 'Day' ,'Spot', 'Option Price', 'Delta', 'Stock Position', 'Position Value', 'PL', 'Column2','Column3','Column4']
	track = pd.DataFrame(np.random.randn(0, len(columns)), columns = columns)


	print 'Starting Simulation'
	for index, delta_hedge_frequency in enumerate(hedge_window_list):

		fix_spot = price_scenario[0] # fix spot price from which we are going to implement
									 # value hedging

		for day, spot in enumerate(price_scenario):
			option = Option(opt_type=opt_type, spot0 = spot, strike = strike, mat = (N - day)/252.0,
		                vol = vol_scenario[day], riskless_rate = 0.0, exer_type = exer_type)

			option_modelled = option.run_model(model = OptionModel.BLACK_SCHOLES)

			# Each delta_hedge_frequency days delta hedge portfolio
			if value_hedging:
				if abs(spot - fix_spot) >= delta_hedge_frequency or day == 0:
					fix_spot = spot # update relative value
					delta = option_modelled[OptionMeasure.DELTA]
					stock_position = - delta * delta_hedge_cover_ratio
				else:
					stock_position = track['Stock Position'][track.shape[0] - 1]
			else:
				if day % delta_hedge_frequency == 0:
					delta = option_modelled[OptionMeasure.DELTA]
					stock_position = - delta * delta_hedge_cover_ratio
				else:
					stock_position = track['Stock Position'][track.shape[0] - 1]

			position_value = option_modelled[OptionMeasure.VALUE] + stock_position * spot

			if day == 0:
				PL = 0
			else:
				PL = position_value - track['Position Value'][track.shape[0] - 1]

			to_append = pd.DataFrame([[delta_hedge_frequency, 
							   day,
							   spot,
							   option_modelled[OptionMeasure.VALUE],
							   option_modelled[OptionMeasure.DELTA],
							   stock_position,
							   position_value,PL,0,0,0]], columns = columns)

			track = track.append(to_append, ignore_index=True)
		print str(index + 1) + "/" + str(len(hedge_window_list))

	print "Simulation completed"


	matplotlib.style.use('ggplot')

	# Plotting spot on 1st subplot
	
	ax1 = fig.add_subplot(311)
	track_tmp = track[(track['Delta Hedge Window'] == hedge_window_list[0])]
	ax1.plot(track_tmp["Day"], track_tmp['Spot'], 'g-')
	ax1.set_xlabel('day')
	ax1.set_ylabel('Spot Price')

	# Plotting position value on 2nd subplot
	ax2 = fig.add_subplot(312)
	for h_freq in hedge_window_list:
		track_tmp = track[(track['Delta Hedge Window'] == h_freq)]	
		ax2.plot(track_tmp['Day'], track_tmp['Position Value'], '-', label = str(h_freq) + ' days', linewidth = 1.5)

	ax2.set_xlabel('day')
	ax2.set_ylabel('Position Value')

	# Plotting P&L on 3rd subplot
	ax3 = fig.add_subplot(313)
	for h_freq in hedge_window_list:
		track_tmp = track[(track['Delta Hedge Window'] == h_freq)]	
		ax3.plot(track_tmp['Day'], track_tmp['PL'],'-', label = 'P&L: ' + str(h_freq) + ' days')

	ax3.set_xlabel('day')
	ax3.set_ylabel('P&L')

	if value_hedging:
		title = 'Value Hedging: ' + option.exer_type + ' ' + option.opt_type + ', strike ' + str(option.strike)
	else:
		title = 'Time Hedging: ' + option.exer_type + ' ' + option.opt_type + ', strike ' + str(option.strike)

	plt.suptitle(title, fontsize = 14)
	ax1.legend()
	ax2.legend()
	ax3.legend()
	fig.set_size_inches(10, 12)
	fig.savefig(output_path + '.pdf')
	print "Figure saved"

	track.to_csv(output_path + '.csv')
	print "Data saved"
Exemplo n.º 10
0
    def test_binomial_tree_valuation(self):
        self.assertAlmostEquals(10.32, Option(opt_type=OptionType.PUT, spot0=90, strike=100, mat=1.0 / 12,
                                              vol=0.35, riskless_rate=0.10).run_model(model=OptionModel.BINOMIAL_TREE)[OptionMeasure.VALUE], delta=0.01)
        self.assertAlmostEquals(10.01, Option(opt_type=OptionType.PUT, spot0=90, strike=100, mat=1.0 / 12,
                                              vol=0.35, riskless_rate=0.10, exer_type=OptionExerciseType.EUROPEAN).run_model(model=OptionModel.BINOMIAL_TREE)[OptionMeasure.VALUE], delta=0.01)
        self.assertAlmostEquals(10.40, Option(opt_type=OptionType.PUT, spot0=90, strike=100, mat=1.0 / 12,
                                              vol=0.35, riskless_rate=0.10, yield_=0.025).run_model(model=OptionModel.BINOMIAL_TREE)[OptionMeasure.VALUE], delta=0.01)
        self.assertAlmostEquals(3.19, Option(opt_type=OptionType.CALL, spot0=90, strike=100, mat=3.0 / 12,
                                             vol=0.35, riskless_rate=0.10, yield_=0.025).run_model(model=OptionModel.BINOMIAL_TREE)[OptionMeasure.VALUE], delta=0.01)

        greek_call_opt = Option(opt_type=OptionType.CALL, spot0=90, strike=100, mat=3.0 / 12,
                                vol=0.35, riskless_rate=0.10, yield_=0.05, exer_type=OptionExerciseType.AMERICAN)
        greek_put_opt = Option(opt_type=OptionType.PUT, spot0=90, strike=100, mat=3.0 / 12,
                               vol=0.35, riskless_rate=0.10, yield_=0.05, exer_type=OptionExerciseType.AMERICAN)
        self.assertAlmostEquals(+0.3205,   greek_call_opt.run_model(model=OptionModel.BINOMIAL_TREE)[OptionMeasure.DELTA], delta=0.0001)
        self.assertAlmostEquals(-0.6973,   greek_put_opt.run_model(model=OptionModel.BINOMIAL_TREE) [OptionMeasure.DELTA], delta=0.0001)
        self.assertAlmostEquals(-12.6706,  greek_call_opt.run_model(model=OptionModel.BINOMIAL_TREE)[OptionMeasure.THETA], delta=0.0001)
        self.assertAlmostEquals(-8.3641,   greek_put_opt.run_model(model=OptionModel.BINOMIAL_TREE) [OptionMeasure.THETA], delta=0.0001)
        self.assertAlmostEquals(+6.4561,   greek_call_opt.run_model(model=OptionModel.BINOMIAL_TREE)[OptionMeasure.RHO],   delta=0.0001)
        self.assertAlmostEquals(-10.9031,  greek_put_opt.run_model(model=OptionModel.BINOMIAL_TREE) [OptionMeasure.RHO],   delta=0.0001)
        self.assertAlmostEquals(+14.1128,  greek_call_opt.run_model(model=OptionModel.BINOMIAL_TREE)[OptionMeasure.VEGA],  delta=0.0001)
        self.assertAlmostEquals(+13.7077,  greek_put_opt.run_model(model=OptionModel.BINOMIAL_TREE) [OptionMeasure.VEGA],  delta=0.0001)
        self.assertAlmostEquals(+0.0268,   greek_call_opt.run_model(model=OptionModel.BINOMIAL_TREE)[OptionMeasure.GAMMA], delta=0.0001)
        self.assertAlmostEquals(+0.0178,   greek_put_opt.run_model(model=OptionModel.BINOMIAL_TREE) [OptionMeasure.GAMMA], delta=0.0001)
Exemplo n.º 11
0
    def test_black_scholes_valuation(self):
        self.assertAlmostEquals(13.70, Option(opt_type=OptionType.CALL, spot0=100, strike=95, mat=3.0 / 12,
                                              vol=0.50, riskless_rate=0.10, exer_type=OptionExerciseType.EUROPEAN).run_model(model=OptionModel.BLACK_SCHOLES)[OptionMeasure.VALUE], delta=0.01)
        self.assertAlmostEquals(10.03, Option(opt_type=OptionType.PUT, spot0=90, strike=100, mat=1.0 / 12,
                                              vol=0.35, riskless_rate=0.10, exer_type=OptionExerciseType.EUROPEAN).run_model(model=OptionModel.BLACK_SCHOLES)[OptionMeasure.VALUE], delta=0.01)
        self.assertAlmostEquals(3.45, Option(opt_type=OptionType.CALL, spot0=90, strike=100, mat=3.0 / 12,
                                             vol=0.35, riskless_rate=0.10, exer_type=OptionExerciseType.EUROPEAN).run_model(model=OptionModel.BLACK_SCHOLES)[OptionMeasure.VALUE], delta=0.01)
        self.assertAlmostEquals(3.06, Option(opt_type=OptionType.CALL, spot0=90, strike=100, mat=3.0 / 12,
                                             vol=0.35, riskless_rate=0.10, yield_=0.05, exer_type=OptionExerciseType.EUROPEAN).run_model(model=OptionModel.BLACK_SCHOLES)[OptionMeasure.VALUE], delta=0.01)

        greek_call_opt = Option(opt_type=OptionType.CALL, spot0=90, strike=100, mat=3.0 / 12,
                                vol=0.35, riskless_rate=0.10, yield_=0.05, exer_type=OptionExerciseType.EUROPEAN)
        greek_put_opt = Option(opt_type=OptionType.PUT, spot0=90, strike=100, mat=3.0 / 12,
                               vol=0.35, riskless_rate=0.10, yield_=0.05, exer_type=OptionExerciseType.EUROPEAN)
        self.assertAlmostEquals(+0.3248,   greek_call_opt.run_model(model=OptionModel.BLACK_SCHOLES)[OptionMeasure.DELTA], delta=0.0001)
        self.assertAlmostEquals(-0.6628,   greek_put_opt.run_model(model=OptionModel.BLACK_SCHOLES) [OptionMeasure.DELTA], delta=0.0001)
        self.assertAlmostEquals(-12.4048,  greek_call_opt.run_model(model=OptionModel.BLACK_SCHOLES)[OptionMeasure.THETA], delta=0.0001)
        self.assertAlmostEquals(-7.0958,   greek_put_opt.run_model(model=OptionModel.BLACK_SCHOLES) [OptionMeasure.THETA], delta=0.0001)
        self.assertAlmostEquals(+6.5405,   greek_call_opt.run_model(model=OptionModel.BLACK_SCHOLES)[OptionMeasure.RHO],   delta=0.0001)
        self.assertAlmostEquals(-17.8422,  greek_put_opt.run_model(model=OptionModel.BLACK_SCHOLES) [OptionMeasure.RHO],   delta=0.0001)
        self.assertAlmostEquals(+16.0714,  greek_call_opt.run_model(model=OptionModel.BLACK_SCHOLES)[OptionMeasure.VEGA],  delta=0.0001)
        self.assertAlmostEquals(+16.0714,  greek_put_opt.run_model(model=OptionModel.BLACK_SCHOLES) [OptionMeasure.VEGA],  delta=0.0001)
        self.assertAlmostEquals(+0.0227,   greek_call_opt.run_model(model=OptionModel.BLACK_SCHOLES)[OptionMeasure.GAMMA], delta=0.0001)
        self.assertAlmostEquals(+0.0227,   greek_put_opt.run_model(model=OptionModel.BLACK_SCHOLES) [OptionMeasure.GAMMA], delta=0.0001)