def test_get_possible_defenses(self): a1 = AssetFundNetwork.Asset(price=1, daily_volume=10, symbol='a1') a2 = AssetFundNetwork.Asset(price=2, daily_volume=4, symbol='a2') a3 = AssetFundNetwork.Asset(price=7, daily_volume=2, symbol='a3') f1 = Fund('f1', {'a1': 10, 'a2': 10, 'a3': 10}, 100, 1, 1) f2 = Fund('f2', {'a1': 10}, 100, 1, 1) SysConfig.set("STEP_ORDER_SIZE", 0.5) network = AssetFundNetwork.AssetFundsNetwork( funds={ 'f1': f1, 'f2': f2 }, assets={ 'a1': a1, 'a2': a2, 'a3': a3 }, mi_calc=MockMarketImpactTestCalculator()) actual_orders = get_possible_defenses(network, 10) expected_orders = [] expected_orders.append(([Buy('a1', 5)], 5)) expected_orders.append(([Buy('a2', 2)], 4)) expected_orders.append(([Buy('a3', 1)], 7)) expected_orders.append(([Buy('a1', 5), Buy('a2', 2)], 9)) expected_orders.append(([], 0)) actual_orders.sort(key=operator.itemgetter(1)) expected_orders.sort(key=operator.itemgetter(1)) self.assertEqual(len(actual_orders), len(expected_orders)) for i in range(0, len(actual_orders)): actual_orders[i][0].sort(key=lambda x: x.asset_symbol) expected_orders[i][0].sort(key=lambda x: x.asset_symbol) self.assertListEqual(actual_orders[i][0], expected_orders[i][0]) self.assertEqual(actual_orders[i][1], expected_orders[i][1])
def test_constructor(self): a1 = AssetFundNetwork.Asset(price=1, daily_volume=100, symbol='a1') a2 = AssetFundNetwork.Asset(price=2, daily_volume=200, symbol='a2') a3 = AssetFundNetwork.Asset(price=3, daily_volume=300, symbol='a3') assets = {'a1': a1, 'a2': a2, 'a3': a3} mgr = ActionsManager(assets, 0.1) sell_all_assets = [Sell('a1', 10), Sell('a2', 20), Sell('a3', 30)] portfolio_dict = { 10: [Attack([Sell('a1', 10)], 10)], 40: [Attack([Sell('a2', 20)], 40)], 90: [Attack([Sell('a3', 30)], 90)], 50: [Attack([Sell('a1', 10), Buy('a2', 20)], 50)], 100: [Attack([Sell('a1', 10), Buy('a3', 30)], 100)], 130: [Attack([Sell('a2', 20), Buy('a3', 30)], 130)], 140: [Attack( [Sell('a1', 10), Sell('a2', 20), Buy('a3', 30)], 140)], 0: [Attack([], 0)] } self.assertEqual(mgr._ActionsManager__step_order_size, 0.1) self.assertDictEqual({ 1: 'a1', 2: 'a2', 3: 'a3' }, mgr._ActionsManager__id_to_sym) self.assertDictEqual(portfolio_dict, mgr._ActionsManager__portfolios_dict) self.assertListEqual([0, 10, 40, 50, 90, 100, 130, 140], list(mgr._ActionsManager__sorted_keys)) self.assertListEqual(sell_all_assets, mgr._ActionsManager__sell_all_assets)
def test_get_single_orders(self): assets = {'a1': Asset(10, 100, 'a1'), 'a2': Asset(20, 200, 'a2')} expected_sell_orders = [Sell('a1', 10), Sell('a2', 20)] expected_buy_orders = [Buy('a1', 10), Buy('a2', 20)] actions_mgr = ActionsManager(assets, 0.1) actual_buy_orders = actions_mgr._ActionsManager__get_single_orders( assets, ActionsManager._ActionsManager__gen_buy_order) actual_sell_orders = actions_mgr._ActionsManager__get_single_orders( assets, ActionsManager._ActionsManager__gen_sell_order) self.assertListEqual(expected_sell_orders, actual_sell_orders) self.assertListEqual(expected_buy_orders, actual_buy_orders)
def test_get_possible_defenses_integ(self): a1 = AssetFundNetwork.Asset(price=1, daily_volume=100, symbol='a1') a2 = AssetFundNetwork.Asset(price=2, daily_volume=100, symbol='a2') a3 = AssetFundNetwork.Asset(price=3, daily_volume=100, symbol='a3') a4 = AssetFundNetwork.Asset(price=1, daily_volume=100, symbol='a4') f1 = Fund('f1', {'a1': 10, 'a2': 10, 'a3': 10}, 100, 1, 1) f2 = Fund('f2', {'a4': 10}, 100, 1, 1) network = AssetFundNetwork.AssetFundsNetwork( funds={ 'f1': f1, 'f2': f2 }, assets={ 'a1': a1, 'a2': a2, 'a3': a3, 'a4': a4 }, mi_calc=MockMarketImpactTestCalculator()) actions_mgr = ActionsManager(network.assets, 0.1) history = {BUY: {'a1': 2}, SELL: {'a1': 1, 'a2': 2}} budget = 20 actions_mgr._ActionsManager__funds_under_risk = MagicMock( return_value=['f1']) actual_defenses = actions_mgr.get_possible_defenses( network, budget, history) self.assertListEqual(actual_defenses, [([Buy('a2', 10)], 20), ([], 0)]) actions_mgr._ActionsManager__funds_under_risk.assert_called_once_with( network)
def test_simulate_trade_sell_orders_in_buy_command(self): a1 = AssetFundNetwork.Asset(price=1, daily_volume=1, symbol='a1') a2 = AssetFundNetwork.Asset(price=2, daily_volume=1, symbol='a2') f1 = Fund('f1', {'a1': 10}, 100, 1, 1) f2 = Fund('f2', {'a2': 10}, 100, 1, 1) mi_calc = MarketImpactCalculator() mi_calc.get_updated_price = MagicMock() network = AssetFundNetwork.AssetFundsNetwork(funds={ 'f1': f1, 'f2': f2 }, assets={ 'a1': a1, 'a2': a2 }, mi_calc=mi_calc, limit_trade_step=False) exception = False try: network.submit_buy_orders([Buy('a1', 2), Sell('a1', 2)]) except TypeError: exception = True mi_calc.get_updated_price.assert_not_called() self.assertTrue(exception)
def test_simulate_trade_sell_more_than_buy(self): a1 = AssetFundNetwork.Asset(price=1, daily_volume=1, symbol='a1') a2 = AssetFundNetwork.Asset(price=2, daily_volume=1, symbol='a2') f1 = Fund('f1', {'a1': 10}, 100, 1, 1) f2 = Fund('f2', {'a2': 10}, 100, 1, 1) mi_calc = MarketImpactCalculator() mi_calc.get_updated_price = MagicMock(return_value=0.5) network = AssetFundNetwork.AssetFundsNetwork(funds={ 'f1': f1, 'f2': f2 }, assets={ 'a1': a1, 'a2': a2 }, mi_calc=mi_calc, limit_trade_step=False) network.submit_buy_orders([Buy('a1', 2)]) network.submit_sell_orders([Sell('a1', 3)]) log = network.simulate_trade() self.assertDictEqual({'a1': '1->0.5'}, log) mi_calc.get_updated_price.assert_called_once_with(1, a1, -1) self.assertFalse(network.buy_orders) self.assertFalse(network.sell_orders) self.assertTrue(a1.price == 0.5) self.assertTrue(a2.price == 2)
def test_simulate_trade_buy_equals_sell(self): a1 = AssetFundNetwork.Asset(price=1, daily_volume=1, symbol='a1') a2 = AssetFundNetwork.Asset(price=2, daily_volume=1, symbol='a2') f1 = Fund('f1', {'a1': 10}, 100, 1, 1) f2 = Fund('f2', {'a2': 10}, 100, 1, 1) mi_calc = MarketImpactCalculator() mi_calc.get_updated_price = MagicMock() network = AssetFundNetwork.AssetFundsNetwork(funds={ 'f1': f1, 'f2': f2 }, assets={ 'a1': a1, 'a2': a2 }, mi_calc=mi_calc, limit_trade_step=False) network.submit_buy_orders([Buy('a1', 3)]) network.submit_sell_orders([Sell('a1', 3)]) log = network.simulate_trade() mi_calc.get_updated_price.assert_not_called() self.assertFalse(log) self.assertFalse(network.buy_orders) self.assertFalse(network.sell_orders) self.assertTrue(a1.price == 1) self.assertTrue(a2.price == 2)
def test_simulate_trade_mix_trades(self): a1 = AssetFundNetwork.Asset(price=1, daily_volume=1, symbol='a1') a2 = AssetFundNetwork.Asset(price=2, daily_volume=1, symbol='a2') f1 = Fund('f1', {'a1': 10}, 100, 1, 1) f2 = Fund('f2', {'a2': 10}, 100, 1, 1) mi_calc = MarketImpactCalculator() mi_calc.get_updated_price = MagicMock(return_value=1.5) network = AssetFundNetwork.AssetFundsNetwork(funds={ 'f1': f1, 'f2': f2 }, assets={ 'a1': a1, 'a2': a2 }, mi_calc=mi_calc, limit_trade_step=False) network.submit_sell_orders([Sell('a2', 2)]) network.submit_buy_orders([Buy('a1', 2)]) log = network.simulate_trade() calls = [call(2, a2, -1), call(2, a1, 1)] mi_calc.get_updated_price.assert_has_calls(calls, any_order=True) self.assertDictEqual({'a1': '1->1.5', 'a2': '2->1.5'}, log) self.assertFalse(network.buy_orders) self.assertFalse(network.sell_orders)
def dont_test_get_possible_defenses(self): a1 = AssetFundNetwork.Asset(price=1, daily_volume=10, symbol='a1') a2 = AssetFundNetwork.Asset(price=2, daily_volume=4, symbol='a2') a3 = AssetFundNetwork.Asset(price=7, daily_volume=2, symbol='a3') f1 = Fund('f1', {'a1': 10, 'a2': 10, 'a3': 10}, 100, 1, 1) f2 = Fund('f2', {'a1': 10}, 100, 1, 1) SysConfig.set("ORDER_SIZES", [0.5, 1]) network = AssetFundNetwork.AssetFundsNetwork( funds={ 'f1': f1, 'f2': f2 }, assets={ 'a1': a1, 'a2': a2, 'a3': a3 }, mi_calc=MockMarketImpactTestCalculator()) actual_orders = get_possible_defenses(network, 10) expected_orders = [] expected_orders.append(([Buy('a1', 5)], 5)) expected_orders.append(([Buy('a1', 10)], 10)) expected_orders.append(([Buy('a2', 2)], 4)) expected_orders.append(([Buy('a2', 4)], 8)) expected_orders.append(([Buy('a3', 1)], 7)) expected_orders.append(([Buy('a1', 5), Buy('a2', 2)], 9)) expected_orders.append(([], 0)) self.assertEqual(actual_orders, expected_orders)
def test_filter_from_history(self): sell_action1 = Attack([Sell('a1', 1), Sell('a2', 1)], 10) sell_action2 = Attack([Sell('a1', 1), Sell('a3', 1)], 10) buy_action1 = Attack([Buy('a1', 1), Buy('a2', 1)], 10) buy_action2 = Attack([Buy('a2', 1)], 10) history = {BUY: {'a1': 2}, SELL: {'a1': 1, 'a2': 2}} self.assertTrue( ActionsManager._ActionsManager__filter_from_history( sell_action1, history, SELL)) self.assertFalse( ActionsManager._ActionsManager__filter_from_history( sell_action2, history, SELL)) self.assertTrue( ActionsManager._ActionsManager__filter_from_history( buy_action1, history, BUY)) self.assertFalse( ActionsManager._ActionsManager__filter_from_history( buy_action2, history, BUY))
def test_get_defenses_in_budget(self): a1 = AssetFundNetwork.Asset(price=1, daily_volume=100, symbol='a1') a2 = AssetFundNetwork.Asset(price=2, daily_volume=200, symbol='a2') a3 = AssetFundNetwork.Asset(price=2, daily_volume=300, symbol='a3') assets = {'a1': a1, 'a2': a2, 'a3': a3} single_asset_orders = [Buy('a1', 10), Buy('a2', 20), Buy('a3', 30)] expected_defenses = [([Buy('a1', 10)], 10), ([Buy('a2', 20)], 40), ([Buy('a3', 30)], 60), ([Buy('a1', 10), Buy('a2', 20)], 50)] actual_defenses = ActionsManager._ActionsManager__get_defenses_in_budget( assets, single_asset_orders, lambda a: a.price, 60) self.assertListEqual(expected_defenses, actual_defenses)
def test_get_all_attacks(self): a1 = AssetFundNetwork.Asset(price=1, daily_volume=100, symbol='a1') a2 = AssetFundNetwork.Asset(price=2, daily_volume=200, symbol='a2') a3 = AssetFundNetwork.Asset(price=3, daily_volume=300, symbol='a3') assets = {'a1': a1, 'a2': a2, 'a3': a3} expected_attacks = [ Attack([Sell('a1', 10)], 10), Attack([Sell('a2', 20)], 40), Attack([Sell('a3', 30)], 90), Attack([Sell('a1', 10), Buy('a2', 20)], 50), Attack([Sell('a1', 10), Buy('a3', 30)], 100), Attack([Sell('a2', 20), Buy('a3', 30)], 130), Attack( [Sell('a1', 10), Sell('a2', 20), Buy('a3', 30)], 140), Attack([], 0) ] mgr = ActionsManager(assets, 0.1) actual_attacks = mgr._ActionsManager__get_all_attacks(assets, 3) actual_attacks.sort(key=lambda a: a.cost) expected_attacks.sort(key=lambda a: a.cost) self.assertListEqual(actual_attacks, expected_attacks)
def test_simulate_trade_limit_trade_step(self): a1 = AssetFundNetwork.Asset(price=1, daily_volume=1000, symbol='a1') f1 = Fund('f1', {'a1': 10}, 100, 1, 1) mi_calc = MarketImpactCalculator() mi_calc.get_updated_price = MagicMock(return_value=1.5) network = AssetFundNetwork.AssetFundsNetwork(funds={'f1': f1}, assets={'a1': a1}, mi_calc=mi_calc) SysConfig.set('TIME_STEP_MINUTES', 1) SysConfig.set('DAILY_PORTION_PER_MIN', 0.001) network.submit_buy_orders([Buy('a1', 2)]) log = network.simulate_trade() self.assertDictEqual({'a1': '1->1.5'}, log) mi_calc.get_updated_price.assert_called_once_with(1, a1, 1) self.assertEqual(network.buy_orders['a1'], 1)
def test_reset_books(self): a1 = AssetFundNetwork.Asset(price=1, daily_volume=1, symbol='a1') a2 = AssetFundNetwork.Asset(price=2, daily_volume=1, symbol='a2') f1 = Fund('f1', {'a1': 10}, 100, 1, 1) f2 = Fund('f2', {'a2': 10}, 100, 1, 1) network = AssetFundNetwork.AssetFundsNetwork( funds={ 'f1': f1, 'f2': f2 }, assets={ 'a1': a1, 'a2': a2 }, mi_calc=MockMarketImpactTestCalculator(), limit_trade_step=True) network.submit_buy_orders([Buy('a1', 2)]) network.submit_sell_orders([Sell('a1', 2)]) self.assertTrue(network.sell_orders) self.assertTrue(network.buy_orders) network.reset_order_books() self.assertFalse(network.sell_orders) self.assertFalse(network.buy_orders)
def dont_test_get_possible_defenses_unit(self): a1 = AssetFundNetwork.Asset(price=1, daily_volume=100, symbol='a1') a2 = AssetFundNetwork.Asset(price=2, daily_volume=100, symbol='a2') a3 = AssetFundNetwork.Asset(price=3, daily_volume=100, symbol='a3') a4 = AssetFundNetwork.Asset(price=1, daily_volume=100, symbol='a4') f1 = Fund('f1', {'a1': 10, 'a2': 10, 'a3': 10}, 100, 1, 1) f2 = Fund('f2', {'a4': 10}, 100, 1, 1) network = AssetFundNetwork.AssetFundsNetwork( funds={ 'f1': f1, 'f2': f2 }, assets={ 'a1': a1, 'a2': a2, 'a3': a3, 'a4': a4 }, mi_calc=MockMarketImpactTestCalculator()) actions_mgr = ActionsManager(network.assets, 0.1) actions_mgr.funds_under_risk = MagicMock(return_value=['f1']) actions_mgr.get_single_orders = MagicMock( return_value=[Buy('a1', 10), Buy('a2', 10), Buy('a3', 10)]) expected_order_set = [[Buy('a1', 10)]] actions_mgr.get_defenses_in_budget = MagicMock( return_value=[(expected_order_set, 10)]) history = {BUY: {'a1': 2}, SELL: {'a1': 1, 'a2': 2}} budget = 60 actual_defenses = actions_mgr.get_possible_defenses( network, budget, history) self.assertListEqual(actual_defenses, [(expected_order_set, 10), ([], 0)]) actions_mgr.funds_under_risk.assert_called_once_with(network) defense_assets = { 'a2': a2, 'a1': a1, 'a3': a3, } actions_mgr.get_single_orders.assert_called_once_with(defense_assets) filtered_defenses = [Buy('a2', 10), Buy('a3', 10)] actions_mgr.get_defenses_in_budget.assert_called_once_with( defense_assets, filtered_defenses)
def test_market_impact_buy_sqrt(self): calc = SqrtMarketImpactCalculator(0.5) order = Buy('a1', 10) a = Asset(price=2, daily_volume=1000, volatility=1.5, symbol='a1') mi = calc.get_market_impact(order, a, ) npt.assert_almost_equal(0.075, mi, decimal=4)
def test_market_impact_buy_order_exp(self): calc = ExponentialMarketImpactCalculator(2) order = Buy('a1', 10) a = Asset(price=2, daily_volume=100, volatility=1.5, symbol='a1') mi = calc.get_market_impact(order, a) npt.assert_almost_equal(1.2214, mi, decimal=4)
def gen_tree(self, ): sell = str([Sell('a1', 50)]) buy = str([Buy('a1', 50)]) nope = str([]) sell_actions = [nope, sell] buy_actions = [nope, buy] root = { 'name': 'root', 'to_move': CHANCE, 'actions': ['40', '30'], 'inf_set': '.', 'terminal': False } price_drop_log = "{'a1': '1->0.6'}" price_bump_log = "{'a1': '1->1.5'}" price_bump_log_06 = "{'a1': '0.6->1.5'}" empty_log = '{}' node_1_0 = self.fill_dict(name='node_1_0', to_move=ATTACKER, actions=sell_actions, history_assets_dict={ BUY: {}, SELL: {} }, budget=Budget(attacker=50, defender=50), actions_history={ BUY: [], SELL: [], SIM_TRADE: [] }, inf_set='.50.A_HISTORY:[]') node_1_0_0 = self.fill_dict(name='node_1_0_0', to_move=DEFENDER, actions=buy_actions, history_assets_dict={ BUY: {}, SELL: { 'a1': 1 } }, budget=Budget(attacker=0, defender=50), actions_history={ BUY: [], SELL: [sell], SIM_TRADE: [] }, inf_set='.50.D_HISTORY:[]') node_1_0_1 = self.fill_dict(name='node_1_0_1', to_move=DEFENDER, actions=buy_actions, history_assets_dict={ BUY: {}, SELL: {} }, budget=Budget(attacker=50, defender=50), actions_history={ BUY: [], SELL: [nope], SIM_TRADE: [] }, inf_set='.50.D_HISTORY:[]') node_1_0_1_0 = self.fill_dict( name='node_1_0_1_0', to_move=MARKET, actions=[], history_assets_dict={ BUY: {}, SELL: {} }, budget=Budget(attacker=50, defender=50), actions_history={ BUY: [nope], SELL: [nope], SIM_TRADE: [] }, inf_set=".MARKET_HISTORY:[].BUY:{}.SELL:{}", terminal=True, eval=0) node_1_0_1_1 = self.fill_dict( name='node_1_0_1_1', to_move=MARKET, actions=[price_bump_log], history_assets_dict={ BUY: { 'a1': 1 }, SELL: {} }, budget=Budget(attacker=50, defender=0), actions_history={ BUY: [buy], SELL: [nope], SIM_TRADE: [] }, inf_set=".MARKET_HISTORY:[].BUY:{'a1': 50}.SELL:{}") node_1_0_1_1_0 = self.fill_dict( name='node_1_0_1_1_0', to_move=ATTACKER, actions=['[]'], history_assets_dict={ BUY: {}, SELL: { 'a1': 1 } }, budget=Budget(attacker=0, defender=50), actions_history={ BUY: [nope, price_drop_log], SELL: [sell, price_drop_log], SIM_TRADE: [price_drop_log] }, inf_set=".0.A_HISTORY:['[Sell a1 50]', \"{'a1': '1->0.6'}\"]") node_1_0_1_1_0_0 = self.fill_dict( name='node_1_0_1_1_0_0', to_move=DEFENDER, actions=buy_actions, history_assets_dict={ BUY: {}, SELL: { 'a1': 1 } }, budget=Budget(attacker=0, defender=50), actions_history={ BUY: [nope, price_drop_log], SELL: [sell, price_drop_log, nope], SIM_TRADE: [price_drop_log] }, inf_set=".50.D_HISTORY:['[]', \"{'a1': '1->0.6'}\"]") node_1_0_1_1_0_0_0 = self.fill_dict( name='node_1_0_1_1_0_0_0', to_move=MARKET, actions=[], history_assets_dict={ BUY: {}, SELL: { 'a1': 1 } }, budget=Budget(attacker=0, defender=50), actions_history={ BUY: [nope, price_drop_log, nope], SELL: [sell, price_drop_log, nope], SIM_TRADE: [price_drop_log] }, inf_set=".MARKET_HISTORY:[\"{'a1': '1->0.6'}\"].BUY:{}.SELL:{}", terminal=True, eval=0) node_1_0_0_0 = self.fill_dict( name='node_1_0_0_0', to_move=MARKET, actions=[empty_log], history_assets_dict={ BUY: { 'a1': 1 }, SELL: { 'a1': 1 } }, budget=Budget(attacker=0, defender=0), actions_history={ BUY: [buy], SELL: [sell], SIM_TRADE: [] }, inf_set=".MARKET_HISTORY:[].BUY:{'a1': 50}.SELL:{'a1': 50}") node_1_0_0_1 = self.fill_dict( name='node_1_0_0_1', to_move=MARKET, actions=[price_drop_log], history_assets_dict={ BUY: {}, SELL: { 'a1': 1 } }, budget=Budget(attacker=0, defender=50), actions_history={ BUY: [nope], SELL: [sell], SIM_TRADE: [] }, inf_set=".MARKET_HISTORY:[].BUY:{}.SELL:{'a1': 50}") node_1_0_0_1_0 = self.fill_dict( name='node_1_0_0_1_0', to_move=ATTACKER, actions=['[]'], history_assets_dict={ BUY: {}, SELL: { 'a1': 1 } }, budget=Budget(attacker=0, defender=50), actions_history={ BUY: [nope, price_drop_log], SELL: [sell, price_drop_log], SIM_TRADE: [price_drop_log] }, inf_set=".0.A_HISTORY:['[Sell a1 50]', \"{'a1': '1->0.6'}\"]") node_1_0_0_1_0_0 = self.fill_dict( name='node_1_0_0_1_0_0', to_move=DEFENDER, actions=buy_actions, history_assets_dict={ BUY: {}, SELL: { 'a1': 1 } }, budget=Budget(attacker=0, defender=50), actions_history={ BUY: [nope, price_drop_log], SELL: [sell, price_drop_log, nope], SIM_TRADE: [price_drop_log] }, inf_set=".50.D_HISTORY:['[]', \"{'a1': '1->0.6'}\"]") node_1_0_0_1_0_0_0 = self.fill_dict( name='node_1_0_0_1_0_0_0', to_move=MARKET, actions=[], history_assets_dict={ BUY: {}, SELL: { 'a1': 1 } }, budget=Budget(attacker=0, defender=50), actions_history={ BUY: [nope, price_drop_log, nope], SELL: [sell, price_drop_log, nope], SIM_TRADE: [price_drop_log] }, inf_set=".MARKET_HISTORY:[\"{'a1': '1->0.6'}\"].BUY:{}.SELL:{}", terminal=True, eval=-1) node_1_0_0_1_0_0_1 = self.fill_dict( name='node_1_0_0_1_0_0_1', to_move=MARKET, actions=["{'a1': '0.6->1.5'}"], history_assets_dict={ BUY: { 'a1': 1 }, SELL: { 'a1': 1 } }, budget=Budget(attacker=0, defender=20), actions_history={ BUY: [nope, price_drop_log, buy], SELL: [sell, price_drop_log, nope], SIM_TRADE: [price_drop_log] }, inf_set= ".MARKET_HISTORY:[\"{'a1': '1->0.6'}\"].BUY:{'a1': 50}.SELL:{}") node_1_0_0_1_0_0_1_0 = self.fill_dict( name='node_1_0_0_1_0_0_1_0', to_move=ATTACKER, actions=['[]'], history_assets_dict={ BUY: { 'a1': 1 }, SELL: { 'a1': 1 } }, budget=Budget(attacker=0, defender=20), actions_history={ BUY: [nope, price_drop_log, buy, price_bump_log_06], SELL: [sell, price_drop_log, nope, price_bump_log_06], SIM_TRADE: [price_drop_log, price_bump_log_06] }, inf_set= ".0.A_HISTORY:['[Sell a1 50]', \"{'a1': '1->0.6'}\", '[]', \"{'a1': '0.6->1.5'}\"]" ) node_1_0_0_1_0_0_1_0_0 = self.fill_dict( name='node_1_0_0_1_0_0_1_0_0', to_move=DEFENDER, actions=['[]'], history_assets_dict={ BUY: { 'a1': 1 }, SELL: { 'a1': 1 } }, budget=Budget(attacker=0, defender=20), actions_history={ BUY: [nope, price_drop_log, buy, price_bump_log_06], SELL: [sell, price_drop_log, nope, price_bump_log_06, nope], SIM_TRADE: [price_drop_log, price_bump_log_06] }, inf_set= ".20.0.D_HISTORY:['[]', \"{'a1': '1->0.6'}\", '[Buy a1 50]', \"{'a1': '0.6->1.5'}\"]" ) node_1_0_0_1_0_0_1_0_0_0 = self.fill_dict( name='node_1_0_0_1_0_0_1_0_0_0', to_move=MARKET, actions=[], history_assets_dict={ BUY: { 'a1': 1 }, SELL: { 'a1': 1 } }, budget=Budget(attacker=0, defender=20), actions_history={ BUY: [nope, price_drop_log, buy, price_bump_log_06, nope], SELL: [sell, price_drop_log, nope, price_bump_log_06, nope], SIM_TRADE: [price_drop_log, price_bump_log_06] }, inf_set= ".MARKET_HISTORY:[\"{'a1': '1->0.6'}\", \"{'a1': '0.6->1.5'}\"].BUY:{}.SELL:{}", terminal=True, eval=0) node_1_0_0_0_0 = self.fill_dict( name='node_1_0_0_0_0', to_move=ATTACKER, actions=['[]'], history_assets_dict={ BUY: { 'a1': 1 }, SELL: { 'a1': 1 } }, budget=Budget(attacker=0, defender=0), actions_history={ BUY: [buy, empty_log], SELL: [sell, empty_log], SIM_TRADE: [empty_log] }, inf_set=".0.A_HISTORY:['[Sell a1 50]', '{}']") node_1_0_0_0_0_0 = self.fill_dict( name='node_1_0_0_0_0_0', to_move=DEFENDER, actions=['[]'], history_assets_dict={ BUY: { 'a1': 1 }, SELL: { 'a1': 1 } }, budget=Budget(attacker=0, defender=0), actions_history={ BUY: [buy, empty_log], SELL: [sell, empty_log, nope], SIM_TRADE: [empty_log] }, inf_set=".0.D_HISTORY:['[Buy a1 50]', '{}']") node_1_0_0_0_0_0_0 = self.fill_dict( name='node_1_0_0_0_0_0_0', to_move=MARKET, actions=[], history_assets_dict={ BUY: { 'a1': 1 }, SELL: { 'a1': 1 } }, budget=Budget(attacker=0, defender=0), actions_history={ BUY: [buy, empty_log, nope], SELL: [sell, empty_log, nope], SIM_TRADE: [empty_log] }, inf_set=".MARKET_HISTORY:['{}'].BUY:{}.SELL:{}", terminal=True, eval=0) ########### node_1_1 = self.fill_dict(name='node_1_1', to_move=ATTACKER, actions=[nope], history_assets_dict={ BUY: {}, SELL: {} }, budget=Budget(attacker=30, defender=50), actions_history={ BUY: [], SELL: [], SIM_TRADE: [] }, inf_set='.30.A_HISTORY:[]') node_1_1_0 = self.fill_dict(name='node_1_1_0', to_move=DEFENDER, actions=buy_actions, history_assets_dict={ BUY: {}, SELL: {} }, budget=Budget(attacker=30, defender=50), actions_history={ BUY: [], SELL: [nope], SIM_TRADE: [] }, inf_set='.50.D_HISTORY:[]') node_1_1_0_0 = self.fill_dict( name='node_1_1_0_0', to_move=MARKET, actions=[price_bump_log], history_assets_dict={ BUY: { 'a1': 1 }, SELL: {} }, budget=Budget(attacker=30, defender=0), actions_history={ BUY: [buy], SELL: [nope], SIM_TRADE: [] }, inf_set=".MARKET_HISTORY:[].BUY:{'a1': 50}.SELL:{}") node_1_1_0_0_0 = self.fill_dict( name='node_1_1_0_0_0', to_move=ATTACKER, actions=[nope], history_assets_dict={ BUY: { 'a1': 1 }, SELL: {} }, budget=Budget(attacker=30, defender=0), actions_history={ BUY: [buy, price_bump_log], SELL: [nope, price_bump_log], SIM_TRADE: [price_bump_log] }, inf_set=".30.A_HISTORY:['[]', \"{'a1': '1->1.5'}\"]") node_1_1_0_0_0_0 = self.fill_dict( name='node_1_1_0_0_0_0', to_move=DEFENDER, actions=[nope], history_assets_dict={ BUY: { 'a1': 1 }, SELL: {} }, budget=Budget(attacker=30, defender=0), actions_history={ BUY: [buy, price_bump_log], SELL: [nope, price_bump_log, nope], SIM_TRADE: [price_bump_log] }, inf_set=".0.D_HISTORY:['[Buy a1 50]', \"{'a1': '1->1.5'}\"]") node_1_1_0_0_0_0_0 = self.fill_dict( name='node_1_1_0_0_0_0_0', to_move=MARKET, actions=[], history_assets_dict={ BUY: { 'a1': 1 }, SELL: {} }, budget=Budget(attacker=30, defender=0), actions_history={ BUY: [buy, price_bump_log, nope], SELL: [nope, price_bump_log, nope], SIM_TRADE: [price_bump_log] }, terminal=True, eval=0, inf_set=".MARKET_HISTORY:[\"{'a1': '1->1.5'}\"].BUY:{}.SELL:{}") node_1_1_0_1 = self.fill_dict( name='node_1_1_0_1', to_move=MARKET, actions=[], history_assets_dict={ BUY: {}, SELL: {} }, budget=Budget(attacker=30, defender=50), actions_history={ BUY: [nope], SELL: [nope], SIM_TRADE: [] }, inf_set=".MARKET_HISTORY:[].BUY:{}.SELL:{}", terminal=True, eval=0) ########## root['children'] = {'50': node_1_0, '30': node_1_1} node_1_0['children'] = {sell: node_1_0_0, nope: node_1_0_1} node_1_0_0['children'] = {buy: node_1_0_0_0, nope: node_1_0_0_1} node_1_0_1['children'] = {nope: node_1_0_1_0, buy: node_1_0_1_1} node_1_0_0_0['children'] = {empty_log: node_1_0_0_0_0} node_1_0_0_1['children'] = {price_drop_log: node_1_0_0_1_0} node_1_0_0_0_0['children'] = {nope: node_1_0_0_0_0_0} node_1_0_0_0_0_0['children'] = {nope: node_1_0_0_0_0_0_0} node_1_0_0_0_0_0_0['children'] = {} node_1_0_1_0['children'] = {} node_1_0_1_1['children'] = {price_bump_log: node_1_0_1_1_0} node_1_0_0_1_0['children'] = {nope: node_1_0_0_1_0_0} node_1_1['children'] = {nope: node_1_1_0} node_1_1_0['children'] = {buy: node_1_1_0_0, nope: node_1_1_0_1} node_1_1_0_0['children'] = {price_bump_log: node_1_1_0_0_0} node_1_1_0_0_0['children'] = {nope: node_1_1_0_0_0_0} node_1_1_0_0_0_0['children'] = {nope: node_1_1_0_0_0_0_0} node_1_1_0_0_0_0_0['children'] = {} node_1_1_0_1['children'] = {} node_1_0_0_1_0_0['children'] = { nope: node_1_0_0_1_0_0_0, buy: node_1_0_0_1_0_0_1 } node_1_0_0_1_0_0_0['children'] = {} node_1_0_0_1_0_0_1['children'] = { price_bump_log_06: node_1_0_0_1_0_0_1_0 } node_1_0_0_1_0_0_1_0['children'] = {nope: node_1_0_0_1_0_0_1_0_0} node_1_0_0_1_0_0_1_0_0['children'] = {nope: node_1_0_0_1_0_0_1_0_0_0} node_1_0_0_1_0_0_1_0_0_0['children'] = {} return root
def gen_buy_order(asset, size): return Buy(asset.symbol, int(floor(size * asset.daily_volume)))
def test_updated_price_buy_sqrt(self): calc = SqrtMarketImpactCalculator(0.5) order = Buy('a1', 10) a = Asset(price=2, daily_volume=1000, volatility=1.5, symbol='a1') mi = calc.get_updated_price(order.num_shares, a, 1) npt.assert_almost_equal(2.075, mi, decimal=4)
def gen_tree(self, ): sell_order = Sell('a1', 50) sell = str([sell_order]) buy = str([Buy('a1', 50)]) nope = str([]) buy_actions = [nope, buy] root = { 'tree_size': 8, 'to_move': CHANCE, 'actions': ['p1', 'p2'], 'inf_set': '.', 'terminal': False } price_drop_log = "{'a1': '1->0.5'}" empty_log = '{}' node_1_0 = self.fill_dict(tree_size=6, to_move=ATTACKER, actions=[sell], history_assets_dict={ BUY: {}, SELL: {} }, players_info=PlayersHiddenInfo( attacker_attack=[sell_order], attacker_pid='p2', defender_budget=50), actions_history={ BUY: [], SELL: [], SIM_TRADE: [] }, inf_set='.p2.A_HISTORY:[]', terminal=False, eval=None) node_1_0_0 = self.fill_dict(tree_size=5, to_move=DEFENDER, actions=buy_actions, history_assets_dict={ BUY: {}, SELL: { 'a1': 1 } }, players_info=PlayersHiddenInfo( attacker_attack=[], attacker_pid='p2', defender_budget=50), actions_history={ BUY: [], SELL: [sell], SIM_TRADE: [] }, inf_set='.50.D_HISTORY:[]', terminal=False, eval=None) node_1_0_0_0 = self.fill_dict( tree_size=2, to_move=MARKET, actions=[empty_log], history_assets_dict={ BUY: { 'a1': 1 }, SELL: { 'a1': 1 } }, players_info=PlayersHiddenInfo(attacker_attack=[], attacker_pid='p2', defender_budget=0), actions_history={ BUY: [buy], SELL: [sell], SIM_TRADE: [] }, inf_set=".MARKET_HISTORY:[].BUY:{'a1': 50}.SELL:{'a1': 50}", terminal=False, eval=None) node_1_0_0_1 = self.fill_dict( tree_size=2, to_move=MARKET, actions=[price_drop_log], history_assets_dict={ BUY: {}, SELL: { 'a1': 1 } }, players_info=PlayersHiddenInfo(attacker_attack=[], attacker_pid='p2', defender_budget=50), actions_history={ BUY: [nope], SELL: [sell], SIM_TRADE: [] }, inf_set=".MARKET_HISTORY:[].BUY:{}.SELL:{'a1': 50}", terminal=False, eval=None) node_1_0_0_0_0 = self.fill_dict( tree_size=1, terminal=True, eval=0, to_move=ATTACKER, actions=[], history_assets_dict={ BUY: { 'a1': 1 }, SELL: { 'a1': 1 } }, players_info=PlayersHiddenInfo(attacker_attack=[], attacker_pid='p2', defender_budget=0), actions_history={ BUY: [buy, empty_log], SELL: [sell, empty_log], SIM_TRADE: [empty_log] }, inf_set=".p2.A_HISTORY:['[Sell a1 50]', '{}']") node_1_0_0_1_0 = self.fill_dict( tree_size=1, terminal=True, eval=-1, to_move=ATTACKER, actions=[], history_assets_dict={ BUY: { 'a1': 1 }, SELL: { 'a1': 1 } }, players_info=PlayersHiddenInfo(attacker_attack=[], attacker_pid='p2', defender_budget=50), actions_history={ BUY: [nope, price_drop_log], SELL: [sell, price_drop_log], SIM_TRADE: [price_drop_log] }, inf_set=".p2.A_HISTORY:['[Sell a1 50]', \"{'a1': '1->0.5'}\"]") ########### node_1_1 = self.fill_dict(tree_size=1, terminal=True, eval=0, to_move=ATTACKER, actions=[], history_assets_dict={ BUY: {}, SELL: {} }, players_info=PlayersHiddenInfo( attacker_attack=[], attacker_pid='p1', defender_budget=50), actions_history={ BUY: [], SELL: [], SIM_TRADE: [] }, inf_set='.p1.A_HISTORY:[]') ########## root['children'] = {'p2': node_1_0, 'p1': node_1_1} node_1_0['children'] = {sell: node_1_0_0} node_1_0_0['children'] = {buy: node_1_0_0_0, nope: node_1_0_0_1} node_1_0_0_0['children'] = {empty_log: node_1_0_0_0_0} node_1_0_0_1['children'] = {price_drop_log: node_1_0_0_1_0} node_1_0_0_0_0['children'] = {} node_1_0_0_1_0['children'] = {} node_1_1['children'] = {} return root
def gen_tree(self, ): buy = str([Buy('a1', 50)]) nope = str([]) buy_actions = [nope, buy] root = { 'tree_size': 15, 'name': 'root', 'to_move': CHANCE, 'actions': ['40', '30'], 'inf_set': '.', 'terminal': False } price_bump_log = "{'a1': '1->1.5'}" node_1_0 = self.fill_dict(tree_size=7, name='node_1_0', to_move=ATTACKER, actions=[nope], history_assets_dict={ BUY: {}, SELL: {} }, budget=Budget(attacker=40, defender=50), actions_history={ BUY: [], SELL: [], SIM_TRADE: [] }, inf_set='.40.A_HISTORY:[]') node_1_0_0 = self.fill_dict(tree_size=6, name='node_1_0_0', to_move=DEFENDER, actions=buy_actions, history_assets_dict={ BUY: {}, SELL: {} }, budget=Budget(attacker=40, defender=50), actions_history={ BUY: [], SELL: [nope], SIM_TRADE: [] }, inf_set='.50.D_HISTORY:[]') node_1_0_0_0 = self.fill_dict( tree_size=4, name='node_1_0_0_0', to_move=MARKET, actions=[price_bump_log], history_assets_dict={ BUY: { 'a1': 1 }, SELL: {} }, budget=Budget(attacker=40, defender=0), actions_history={ BUY: [buy], SELL: [nope], SIM_TRADE: [] }, inf_set=".MARKET_HISTORY:[].BUY:{'a1': 50}.SELL:{}") node_1_0_0_0_0 = self.fill_dict( tree_size=3, name='node_1_0_0_0_0', to_move=ATTACKER, actions=[nope], history_assets_dict={ BUY: { 'a1': 1 }, SELL: {} }, budget=Budget(attacker=40, defender=0), actions_history={ BUY: [buy, price_bump_log], SELL: [nope, price_bump_log], SIM_TRADE: [price_bump_log] }, inf_set=".40.A_HISTORY:['[]', \"{'a1': '1->1.5'}\"]") node_1_0_0_0_0_0 = self.fill_dict( tree_size=2, name='node_1_0_0_0_0_0', to_move=DEFENDER, actions=[nope], history_assets_dict={ BUY: { 'a1': 1 }, SELL: {} }, budget=Budget(attacker=40, defender=0), actions_history={ BUY: [buy, price_bump_log], SELL: [nope, price_bump_log, nope], SIM_TRADE: [price_bump_log] }, inf_set=".0.D_HISTORY:['[Buy a1 50]', \"{'a1': '1->1.5'}\"]") node_1_0_0_0_0_0_0 = self.fill_dict( tree_size=1, name='node_1_0_0_0_0_0_0', to_move=MARKET, actions=[], history_assets_dict={ BUY: { 'a1': 1 }, SELL: {} }, budget=Budget(attacker=40, defender=0), actions_history={ BUY: [buy, price_bump_log, nope], SELL: [nope, price_bump_log, nope], SIM_TRADE: [price_bump_log] }, terminal=True, eval=0, inf_set=".MARKET_HISTORY:[\"{'a1': '1->1.5'}\"].BUY:{}.SELL:{}") node_1_0_0_1 = self.fill_dict( tree_size=1, name='node_1_0_0_1', to_move=MARKET, actions=[], history_assets_dict={ BUY: {}, SELL: {} }, budget=Budget(attacker=40, defender=50), actions_history={ BUY: [nope], SELL: [nope], SIM_TRADE: [] }, inf_set=".MARKET_HISTORY:[].BUY:{}.SELL:{}", terminal=True, eval=0) ########### node_1_1 = self.fill_dict(tree_size=7, name='node_1_1', to_move=ATTACKER, actions=[nope], history_assets_dict={ BUY: {}, SELL: {} }, budget=Budget(attacker=30, defender=50), actions_history={ BUY: [], SELL: [], SIM_TRADE: [] }, inf_set='.30.A_HISTORY:[]') node_1_1_0 = self.fill_dict(tree_size=6, name='node_1_1_0', to_move=DEFENDER, actions=buy_actions, history_assets_dict={ BUY: {}, SELL: {} }, budget=Budget(attacker=30, defender=50), actions_history={ BUY: [], SELL: [nope], SIM_TRADE: [] }, inf_set='.50.D_HISTORY:[]') node_1_1_0_0 = self.fill_dict( tree_size=4, name='node_1_1_0_0', to_move=MARKET, actions=[price_bump_log], history_assets_dict={ BUY: { 'a1': 1 }, SELL: {} }, budget=Budget(attacker=30, defender=0), actions_history={ BUY: [buy], SELL: [nope], SIM_TRADE: [] }, inf_set=".MARKET_HISTORY:[].BUY:{'a1': 50}.SELL:{}") node_1_1_0_0_0 = self.fill_dict( tree_size=3, name='node_1_1_0_0_0', to_move=ATTACKER, actions=[nope], history_assets_dict={ BUY: { 'a1': 1 }, SELL: {} }, budget=Budget(attacker=30, defender=0), actions_history={ BUY: [buy, price_bump_log], SELL: [nope, price_bump_log], SIM_TRADE: [price_bump_log] }, inf_set=".30.A_HISTORY:['[]', \"{'a1': '1->1.5'}\"]") node_1_1_0_0_0_0 = self.fill_dict( tree_size=2, name='node_1_1_0_0_0_0', to_move=DEFENDER, actions=[nope], history_assets_dict={ BUY: { 'a1': 1 }, SELL: {} }, budget=Budget(attacker=30, defender=0), actions_history={ BUY: [buy, price_bump_log], SELL: [nope, price_bump_log, nope], SIM_TRADE: [price_bump_log] }, inf_set=".0.D_HISTORY:['[Buy a1 50]', \"{'a1': '1->1.5'}\"]") node_1_1_0_0_0_0_0 = self.fill_dict( tree_size=1, name='node_1_1_0_0_0_0_0', to_move=MARKET, actions=[], history_assets_dict={ BUY: { 'a1': 1 }, SELL: {} }, budget=Budget(attacker=30, defender=0), actions_history={ BUY: [buy, price_bump_log, nope], SELL: [nope, price_bump_log, nope], SIM_TRADE: [price_bump_log] }, terminal=True, eval=0, inf_set=".MARKET_HISTORY:[\"{'a1': '1->1.5'}\"].BUY:{}.SELL:{}") node_1_1_0_1 = self.fill_dict( tree_size=1, name='node_1_1_0_1', to_move=MARKET, actions=[], history_assets_dict={ BUY: {}, SELL: {} }, budget=Budget(attacker=30, defender=50), actions_history={ BUY: [nope], SELL: [nope], SIM_TRADE: [] }, inf_set=".MARKET_HISTORY:[].BUY:{}.SELL:{}", terminal=True, eval=0) ########## root['children'] = {'40': node_1_0, '30': node_1_1} node_1_0['children'] = {nope: node_1_0_0} node_1_0_0['children'] = {buy: node_1_0_0_0, nope: node_1_0_0_1} node_1_0_0_0['children'] = {price_bump_log: node_1_0_0_0_0} node_1_0_0_0_0['children'] = {nope: node_1_0_0_0_0_0} node_1_0_0_0_0_0['children'] = {nope: node_1_0_0_0_0_0_0} node_1_0_0_0_0_0_0['children'] = {} node_1_0_0_1['children'] = {} node_1_1['children'] = {nope: node_1_1_0} node_1_1_0['children'] = {buy: node_1_1_0_0, nope: node_1_1_0_1} node_1_1_0_0['children'] = {price_bump_log: node_1_1_0_0_0} node_1_1_0_0_0['children'] = {nope: node_1_1_0_0_0_0} node_1_1_0_0_0_0['children'] = {nope: node_1_1_0_0_0_0_0} node_1_1_0_0_0_0_0['children'] = {} node_1_1_0_1['children'] = {} return root