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_gen_optimal_attacks(self):
        a1 = AssetFundNetwork.Asset(price=10, daily_volume=10, symbol='a1')
        a2 = AssetFundNetwork.Asset(price=20, daily_volume=10, symbol='a2')
        f1 = MockFund('f1', a1, 1)
        f2 = MockFund('f2', a2, 1)
        f2 = MockFund('f3', a2, 1)
        network = AssetFundNetwork.AssetFundsNetwork(
            funds={
                'f1': f1,
                'f2': f2
            },
            assets={
                'a1': a1,
                'a2': a2
            },
            mi_calc=MockMarketImpactTestCalculator())

        expected_portfolio = [
            ([], 0),
            ([Sell("a1", 5)], 50),
            ([Sell("a1", 10)], 100),
            ([Sell("a2", 5)], 100),
            ([Sell("a2", 10)], 200),
            ([Sell("a1", 5), Sell("a2", 5)], 150),
            ([Sell("a1", 10), Sell("a2", 5)], 200),
        ]
    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)
Exemple #4
0
    def test_get_attacks_in_budget(self):
        a1 = AssetFundNetwork.Asset(price=10, daily_volume=10, symbol='a1')
        a2 = AssetFundNetwork.Asset(price=20, daily_volume=10, symbol='a2')
        f1 = MockFund('f1', a1, 1)
        f2 = MockFund('f2', a2, 1)
        network = AssetFundNetwork.AssetFundsNetwork(
            funds={
                'f1': f1,
                'f2': f2
            },
            assets={
                'a1': a1,
                'a2': a2
            },
            mi_calc=MockMarketImpactTestCalculator())

        expected_portfolio = [
            ([], 0),
            ([Sell("a1", 5)], 50),
            ([Sell("a1", 10)], 100),
            ([Sell("a2", 5)], 100),
            ([Sell("a2", 10)], 200),
            ([Sell("a1", 5), Sell("a2", 5)], 150),
            ([Sell("a1", 10), Sell("a2", 5)], 200),
        ]
        solver = SingleAgentESSolver(network, 0.5, 2)
        actual_portfolio = solver.get_attacks_in_budget(200, True)
        expected_portfolio_str = sorted([(str(x), str(y))
                                         for (x, y) in expected_portfolio])
        actual_portfolio_str = sorted([(str(x), str(y))
                                       for (x, y) in actual_portfolio])
        self.assertEqual(expected_portfolio_str, actual_portfolio_str)
    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)
Exemple #6
0
 def test_funds_under_risk(self):
     a1 = AssetFundNetwork.Asset(price=1, daily_volume=100, symbol='a1')
     a2 = AssetFundNetwork.Asset(price=2, daily_volume=200, symbol='a2')
     f1 = Fund('f1', {'a1': 10, 'a2': 10, 'a3': 10}, 100, 1, 1)
     f2 = Fund('f2', {'a1': 10}, 100, 1, 1)
     network = AssetFundNetwork.AssetFundsNetwork(
         funds={
             'f1': f1,
             'f2': f2
         },
         assets={
             'a1': a1,
             'a2': a2
         },
         mi_calc=MockMarketImpactTestCalculator())
     network.reset_order_books = MagicMock()
     network.simulate_trade = MagicMock()
     network.submit_sell_orders = MagicMock()
     network.get_funds_in_margin_calls = MagicMock(return_value=['f1'])
     actions_mgr = ActionsManager(network.assets, 0.1)
     actual_funds = actions_mgr._ActionsManager__funds_under_risk(network)
     self.assertListEqual(actual_funds, ['f1'])
     network.reset_order_books.assert_called_once()
     network.submit_sell_orders.assert_called_once_with(
         [Sell('a1', 10), Sell('a2', 20)])
     network.simulate_trade.assert_called_once()
     network.get_funds_in_margin_calls.assert_called_once()
Exemple #7
0
 def test_get_possible_attacks(self):
     a1 = AssetFundNetwork.Asset(price=1, daily_volume=10, symbol='a1')
     a1.set_price(3)
     a2 = AssetFundNetwork.Asset(price=2, daily_volume=4, symbol='a2')
     a2.set_price(4)
     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_attacks(network, 10)
     expected_orders = []
     expected_orders.append(([Sell('a1', 5)], 5))
     expected_orders.append(([Sell('a2', 2)], 4))
     expected_orders.append(([Sell('a3', 1)], 7))
     expected_orders.append(([Sell('a1', 5), Sell('a2', 2)], 9))
     expected_orders.append(([], 0))
     self.assertEqual(actual_orders, expected_orders)
Exemple #8
0
 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)
Exemple #9
0
 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_single_agent(self):
        a1 = AssetFundNetwork.Asset(price=1, daily_volume=100, symbol='a1')
        a2 = AssetFundNetwork.Asset(price=1, daily_volume=100, symbol='a1')

        f1 = AssetFundNetwork.Fund('f1', {'a2': 10}, 100, 1, 1)
        f2 = AssetFundNetwork.Fund('f2', {'a1': 20}, 100, 1, 1)
        mi_calc = MarketImpactCalculator()
        mi_calc.get_updated_price = MagicMock()
        mi_calc.get_updated_price.side_effect = update_price_side_effects
        network = AssetFundNetwork.AssetFundsNetwork(funds={
            'f1': f1,
            'f2': f2
        },
                                                     assets={
                                                         'a1': a1,
                                                         'a2': a2
                                                     },
                                                     mi_calc=mi_calc,
                                                     limit_trade_step=True)

        f1.marginal_call = MagicMock(return_value=False)
        f2.marginal_call = MagicMock()
        f2.marginal_call.side_effect = f1_margin_call_side_effect
        actions_mgr = ActionsManager(network.assets, 0.1, 2)
        result = single_agent(actions_mgr, network, 200)
        print(result.value)
        print(result.actions)
        self.assertEqual(result.value, -1)
        self.assertListEqual(result.actions, [[Sell('a1', 20)], ['MARKET']])
    def test_store_file(self):
        a1 = AssetFundNetwork.Asset(price=10, daily_volume=1, symbol='a1')
        a2 = AssetFundNetwork.Asset(price=20, daily_volume=1, symbol='a2')
        f1 = MockFund('f1', a1, 1)
        f2 = MockFund('f2', a2, 1)
        network = AssetFundNetwork.AssetFundsNetwork(
            funds={
                'f1': f1,
                'f2': f2
            },
            assets={
                'a1': a1,
                'a2': a2
            },
            mi_calc=MockMarketImpactTestCalculator())

        solver = SingleAgentDynamicProgrammingSolver(network, 40, 1, 1)
        solver.store_solution('../../../resources/test_store_file.csv')
        expected_rows = []
        for budget in [40, 30, 20, 10]:
            expected_rows.append({
                'budget':
                str(budget),
                'value':
                str(solver.solutions[2][budget].value),
                'actions':
                str(solver.solutions[2][budget].actions)
            })
        i = 0
        with open('../../../resources/test_store_file.csv',
                  newline='') as csvfile:
            reader = csv.DictReader(csvfile)
            for row in reader:
                self.assertEqual(dict(row), expected_rows[i])
                i += 1
Exemple #12
0
 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_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)
Exemple #14
0
 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])
Exemple #15
0
 def test_get_possible_attacks_zero_budget(self):
     a1 = AssetFundNetwork.Asset(price=1, daily_volume=100, symbol='a1')
     a2 = AssetFundNetwork.Asset(price=2, daily_volume=200, symbol='a2')
     assets = {'a1': a1, 'a2': a2}
     mgr = ActionsManager(assets, 0.1)
     expected_attacks = [([], 0)]
     actual_attacks = mgr.get_possible_attacks(budget=0)
     actual_attacks.sort(key=lambda a: a[1])
     expected_attacks.sort(key=lambda a: a[1])
     self.assertListEqual(expected_attacks, actual_attacks)
Exemple #16
0
 def test_get_possible_attacks_no_budget(self):
     a1 = AssetFundNetwork.Asset(price=1, daily_volume=100, symbol='a1')
     a2 = AssetFundNetwork.Asset(price=2, daily_volume=200, symbol='a2')
     assets = {'a1': a1, 'a2': a2}
     history = {BUY: {'a1': 2}, SELL: {'a1': 1, 'a2': 2}}
     mgr = ActionsManager(assets, 0.1)
     expected_attacks = [([Sell('a1', 10)], 10), ([], 0)]
     actual_attacks = mgr.get_possible_attacks(history=history)
     actual_attacks.sort(key=lambda a: a[1])
     expected_attacks.sort(key=lambda a: a[1])
     self.assertListEqual(expected_attacks, actual_attacks)
Exemple #17
0
 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 not_test_solver_finds_best_attack_integration(self):
        a1 = AssetFundNetwork.Asset(50, 2, 'a1')
        a2 = AssetFundNetwork.Asset(20, 2, 'a2')
        a3 = AssetFundNetwork.Asset(30, 2, 'a3')
        f1 = AssetFundNetwork.Fund('f1', {'a1': 10}, {'a2': 10}, 1000, 1, 1)
        f2 = AssetFundNetwork.Fund('f2', {'a2': 10}, {'a3': 10}, 1000, 1, 1)

        network = AssetFundNetwork.AssetFundsNetwork(funds=[f1, f2],
                                                     assets=[a1, a2, a3])

        solver = SingleAgentDynamicProgrammingSolver(network, 10, 0.5)
        print(solver.results.value)
        print(solver.results.actions)
Exemple #19
0
    def test_get_portfolios_in_budget_dict(self):
        a1 = AssetFundNetwork.Asset(price=1, daily_volume=100, symbol='a1')
        a2 = AssetFundNetwork.Asset(price=2, daily_volume=200, symbol='a2')
        assets = {'a1': a1, 'a2': a2}
        mgr = ActionsManager(assets, 0.1, attacker_budgets=[5, 10])
        id2portfolios = mgr.get_portfolios()
        actual = mgr.get_portfolios_in_budget_dict()
        expected_attacks_b5 = [Attack([], 0)]
        expected_attacks_b10 = [Attack([], 0), Attack([Sell('a1', 10)], 10)]
        actual_attacks_b5 = [id2portfolios[x] for x in actual[5]]
        actual_attacks_b10 = [id2portfolios[x] for x in actual[10]]

        self.assertListEqual(expected_attacks_b5, actual_attacks_b5)
        self.assertListEqual(expected_attacks_b10, actual_attacks_b10)
    def test_tree(self):
        a1 = AssetFundNetwork.Asset(price=1, daily_volume=100, symbol='a1')
        f1 = Fund('f1', {'a1': 10}, 100, 1, 1)
        f2 = Fund('f2', {'a1': 20}, 100, 1, 1)
        mi_calc = MarketImpactCalculator()
        mi_calc.get_updated_price = MagicMock()
        mi_calc.get_updated_price.side_effect = update_price_side_effects
        network = AssetFundNetwork.AssetFundsNetwork(funds={
            'f1': f1,
            'f2': f2
        },
                                                     assets={'a1': a1},
                                                     mi_calc=mi_calc,
                                                     limit_trade_step=False)

        action_manager = ActionsManager(network.assets, 0.5, 1, [50, 30])
        SysConfig.set("STEP_ORDER_SIZE", 0.5)

        f1.marginal_call = MagicMock(return_value=False)
        f2.marginal_call = MagicMock()
        f2.marginal_call.side_effect = f1_margin_call_side_effect
        actual_tree = PortfolioFlashCrashRootChanceGameState(
            action_manager, af_network=network, defender_budget=50)
        expected_tree = self.gen_tree()
        self.assertEqual(actual_tree.chance_prob(), {'p1': 0.75, 'p2': 0.25})
        self.assertEqual(actual_tree.tree_size, 8)
        self.cmp_tree(expected_tree, actual_tree)
    def test_solution2(self):
        a1 = AssetFundNetwork.Asset(price=2, daily_volume=500, symbol='a1')
        a2 = AssetFundNetwork.Asset(price=2, daily_volume=1000, symbol='a2')
        f1 = AssetFundNetwork.Fund('f1', {'a1': 10, 'a2': 10}, 16, 1, 1)
        f2 = AssetFundNetwork.Fund('f2', {'a1': 5, 'a2': 4}, 5, 2, 0.25)
        network = AssetFundNetwork.AssetFundsNetwork(funds = {'f1':f1, 'f2':f2},
                                                     assets={'a1':a1, 'a2':a2},
                                                     mi_calc=MockMarketImpactTestCalculator(),
                                                     intraday_asset_gain_max_range=None,
                                                     limit_trade_step=False
                                                     )

        actions,cost = single_fund_attack_optimal_attack_generator(network, 'f1',
                                                                   MockMarketImpactTestCalculator(),0.25,3)
        self.assertEqual(cost, 500)
        self.assertEqual(actions, [Sell('a1', 125),Sell('a1', 125)])
Exemple #22
0
 def test_get_portfolio_dict(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=1, daily_volume=100, symbol='a3')
     a4 = AssetFundNetwork.Asset(price=1, daily_volume=100, symbol='a4')
     assets = {'a1': a1, 'a2': a2, 'a3': a3, 'a4': a4}
     actions_mgr = ActionsManager(assets, 0.1)
     attack1 = Attack([Sell('a1', 10)], 10)
     attack2 = Attack([Sell('a1', 10), Sell('a2', 10)], 40)
     attack3 = Attack([Sell('a3', 10)], 10)
     actions_mgr._ActionsManager__get_all_attacks = MagicMock(
         return_value=[attack1, attack2, attack3])
     expected_dict = {10: [attack1, attack3], 40: [attack2]}
     actual_dict = dict(
         actions_mgr._ActionsManager__get_portfolio_dict(assets))
     self.assertDictEqual(expected_dict, actual_dict)
     actions_mgr._ActionsManager__get_all_attacks.assert_called_once_with(
         assets, 4)
Exemple #23
0
 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_gen_weights(self):
        a1 = AssetFundNetwork.Asset(price=1, daily_volume=10, symbol='a1')
        a2 = AssetFundNetwork.Asset(price=2, daily_volume=4, symbol='a2')
        f1 = MockFund('f1', a1, 1)
        f2 = MockFund('f2', a2, 1)
        network = AssetFundNetwork.AssetFundsNetwork(
            funds={
                'f1': f1,
                'f2': f2
            },
            assets={
                'a1': a1,
                'a2': a2
            },
            mi_calc=MockMarketImpactTestCalculator())

        solver = SingleAgentDynamicProgrammingSolver(network, 4, 0.5, 1)
        self.assertEqual(solver.weights[1], 5)
        self.assertEqual(solver.weights[2], 4)
    def test_order_limited_by_parameter(self):
        a1 = AssetFundNetwork.Asset(price=1, daily_volume=1, symbol='a1')
        a2 = AssetFundNetwork.Asset(price=2, daily_volume=2, symbol='a2')
        f1 = MockFund(symbol='f1', my_asset=a1, margin_ratio=0.9)
        f2 = MockFund(symbol='f2', my_asset=a2, margin_ratio=0.9)
        network = AssetFundNetwork.AssetFundsNetwork(
            funds={
                'f1': f1,
                'f2': f2
            },
            assets={
                'a1': a1,
                'a2': a2
            },
            mi_calc=MockMarketImpactTestCalculator())

        solver = SingleAgentDynamicProgrammingSolver(network, 4, 0.5, 2)
        self.assertEqual(solver.results.value, 1)
        self.assertEqual(solver.results.actions[0], Sell('a2', 2))
    def test_2(self):
        a1 = AssetFundNetwork.Asset(price=1, daily_volume=1, symbol='a1')
        a2 = AssetFundNetwork.Asset(price=2, daily_volume=1, symbol='a2')
        f1 = MockFund('f1', a1, 1)
        f2 = MockFund('f2', a2, 1)
        network = AssetFundNetwork.AssetFundsNetwork(
            funds={
                'f1': f1,
                'f2': f2
            },
            assets={
                'a1': a1,
                'a2': a2
            },
            mi_calc=MockMarketImpactTestCalculator())

        solver = SingleAgentDynamicProgrammingSolver(network, 3, 1, 1)
        self.assertEqual(solver.results.value, 2)
        self.assertEqual(solver.results.actions[0], Sell('a1', 1))
        self.assertEqual(solver.results.actions[1], Sell('a2', 1))
    def run_one_time_attack_tree_test(self, pruning=False):
        a1 = AssetFundNetwork.Asset(price=100, daily_volume=390, symbol='a1')
        a2 = AssetFundNetwork.Asset(price=100, daily_volume=390, symbol='a2')
        f1 = MockFund('f1', a1, 0.82)
        f2 = MockFund('f2', a2, 0.82)
        f3 = MockFund('f3', a2, 0.82)
        SysConfig.set("STEP_ORDER_SIZE", 2 / 390)
        SysConfig.set("TIME_STEP_MINUTES", 1)
        SysConfig.set('DAILY_PORTION_PER_MIN', 1 / 390)
        SysConfig.set("MAX_NUM_ORDERS", 1)
        network = AssetFundNetwork.AssetFundsNetwork(
            funds={
                'f1': f1,
                'f2': f2,
                'f3': f3
            },
            assets={
                'a1': a1,
                'a2': a2
            },
            mi_calc=MockMarketImpactTestCalculator(),
            limit_trade_step=True)

        #   value, actions, actual_tree = minimax(ROOT_ATTACKER, network, 205,190)
        actions_mgr = ActionsManager(network.assets, 2 / 390, 1)
        if pruning:
            result = alphabeta(actions_mgr, ATTACKER, network, 405, 190,
                               (-inf, inf), (inf, inf), True)
        else:
            result = minimax2(actions_mgr, ATTACKER, network, 405, 190, True)
        expected_tree = self.gen_tree2()

        print(result.value)
        print(result.actions)
        self.compare_trees(result.tree, expected_tree)
        self.assertEqual(int(result.network.assets['a1'].price), 81)
        self.assertEqual(int(result.network.assets['a2'].price), 99)
        self.assertEqual(int(network.assets['a1'].price), 100)
        self.assertEqual(int(network.assets['a2'].price), 100)
Exemple #28
0
 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)
Exemple #29
0
 def test_gen_optimal_attacks(self):
     a1 = AssetFundNetwork.Asset(price=10, daily_volume=10, symbol='a1')
     a2 = AssetFundNetwork.Asset(price=20, daily_volume=10, symbol='a2')
     f1_margin_call = lambda assets: assets['a1'].price <= 8
     f2_margin_call = lambda assets: assets['a2'].price <= 19
     f3_margin_call = lambda assets: assets['a2'].price <= 19
     mi_calc = MockMarketImpactTestCalculator({
         'a1': {
             5: 9,
             10: 8
         },
         'a2': {
             5: 19,
             10: 18
         }
     })
     f1 = MockFund('f1', a1, f1_margin_call)
     f2 = MockFund('f2', a2, f2_margin_call)
     f3 = MockFund('f3', a2, f3_margin_call)
     network = AssetFundNetwork.AssetFundsNetwork(funds={
         'f1': f1,
         'f2': f2,
         'f3': f3
     },
                                                  assets={
                                                      'a1': a1,
                                                      'a2': a2
                                                  },
                                                  mi_calc=mi_calc,
                                                  limit_trade_step=False)
     solver = SingleAgentESSolver(network, 0.5, 2)
     actual_solutions = solver.gen_optimal_attacks()
     solution_2 = Solution(network, [Sell('a2', 5)], 2, ['f2', 'f3'], 100)
     solution_3 = Solution(network,
                           [Sell('a1', 10), Sell('a2', 5)], 3,
                           ['f1', 'f2', 'f3'], 200)
     expected_solutions = {1: solution_2, 2: solution_2, 3: solution_3}
     self.assertEqual(expected_solutions, actual_solutions)
Exemple #30
0
 def dont_test_get_possible_attacks_single_asset_per_attack(self):
     a1 = AssetFundNetwork.Asset(price=1, daily_volume=2, symbol='a1')
     f1 = Fund('f1', {'a1': 10}, 100, 1, 1)
     SysConfig.set("ORDER_SIZES", [0.5, 1])
     network = AssetFundNetwork.AssetFundsNetwork(
         funds={'f1': f1},
         assets={'a1': a1},
         mi_calc=MockMarketImpactTestCalculator())
     actual_sell_orders = get_possible_attacks(network, 10)
     expected_sell_orders = []
     expected_sell_orders.append(([Sell('a1', 2)], 2))
     expected_sell_orders.append(([Sell('a1', 1)], 1))
     expected_sell_orders.append(([], 0))
     self.assertEqual(actual_sell_orders, expected_sell_orders)