def test_compute_portfolio_value(self): assets = { 'XXX': Asset(1, 20, 1.5, 'XXX'), 'YYY': Asset(4, 20, 1.5, 'yyy') } fund = Fund('F1', {'XXX': 10, 'YYY': 10}, 5, 2, 3) self.assertEqual(50, fund.compute_portfolio_value(assets))
def test_get_orders(self): assets = { 'XXX': Asset(1, 20, 1.5, 'XXX'), 'YYY': Asset(1, 20, 1.5, 'yyy') } fund = Fund('F1', {'XXX': 5, 'YYY': 4}, 5, 2, 0.25) self.assertEqual(fund.get_orders(assets), [])
def test_run_intraday_simulation_raises_exception_for_price_reduction( self): a0 = Asset(price=1, daily_volume=40, volatility=1.5, symbol='a0') a1 = Asset(price=2, daily_volume=40, volatility=1.5, symbol='a1') f0 = Fund('f0', {'a0': 10}, initial_capital=2, initial_leverage=8, tolerance=2) f1 = Fund('f1', { 'a0': 10, 'a1': 1 }, initial_capital=1, initial_leverage=1, tolerance=3) network = AssetFundsNetwork({ 'f0': f0, 'f1': f1 }, { 'a0': a0, 'a1': a1 }, MockMarketImpactTestCalculator()) with self.assertRaises(ValueError): network.run_intraday_simulation(0.8, 0.7)
def test_get_canonical_form(self): a0 = Asset(price=1, daily_volume=40, volatility=1.5, symbol='a0') a1 = Asset(price=2, daily_volume=40, volatility=1.5, symbol='a1') f0 = Fund('f0', {'a0': 10}, initial_capital=2, initial_leverage=8, tolerance=2) f1 = Fund('f1', { 'a0': 10, 'a1': 10 }, initial_capital=1, initial_leverage=1, tolerance=3) network = AssetFundsNetwork({ 'f0': f0, 'f1': f1 }, { 'a0': a0, 'a1': a1 }, MockMarketImpactTestCalculator()) expected_canonical_form = np.array([[10., 0.], [10., 20.]]) actual_canonical_form = network.get_canonical_form() self.assertTrue( np.array_equal(expected_canonical_form, actual_canonical_form))
def test_update_funds(self): assets = { 'XXX': Asset(1, 20, 1.5, 'XXX'), 'YYY': Asset(1, 20, 1.5, 'yyy') } f1 = Fund('F1', {'XXX': 10, 'YYY': 10}, 5, 2, 0.25) f2 = Fund('F1', {'XXX': 5, 'YYY': 4}, 5, 2, 0.25) f3 = Fund('F1', {'XXX': 20, 'YYY': 4}, 5, 2, 4) network = AssetFundsNetwork({ 'f1': f1, 'f2': f2, 'f3': f3 }, assets, MockMarketImpactTestCalculator()) network.update_funds() self.assertTrue(f1.is_in_margin_call()) self.assertFalse(f1.default()) self.assertTrue(f2.is_in_margin_call()) self.assertTrue(f2.default()) self.assertFalse(f3.is_in_margin_call()) self.assertFalse(f3.default()) """a0 = Asset(price=1, daily_volume=40, volatility=1.5, symbol='a0')
def test_marginal_call_true(self): assets = { 'XXX': Asset(1, 20, 1.5, 'XXX'), 'YYY': Asset(1, 20, 1.5, 'yyy') } fund = Fund('F1', {'XXX': 10, 'YYY': 10}, 5, 2, 3) self.assertTrue(True, fund.marginal_call(assets))
def test_compute_compute_curr_leverage(self): assets = { 'XXX': Asset(1, 20, 1.5, 'XXX'), 'YYY': Asset(4, 20, 1.5, 'yyy') } fund = Fund('F1', {'XXX': 10, 'YYY': 10}, 5, 2, 3) self.assertEqual(0.25, fund.compute_curr_leverage(assets))
def test_read_two_assets_from_file(self): assets = AssetFundNetwork.read_assets_file( '../../resources/assets.csv', 2) expected_assets = { 'A1': Asset(price=145.6, daily_volume=605.3, symbol='A1'), 'A2': Asset(price=100, daily_volume=10, symbol='A2') } self.assertEqual(len(assets), 2) self.assertEqual(assets, expected_assets)
def test_update_state_margin_only(self): assets = { 'XXX': Asset(1, 20, 1.5, 'XXX'), 'YYY': Asset(1, 20, 1.5, 'yyy') } fund = Fund('F1', {'XXX': 10, 'YYY': 10}, 5, 2, 0.25) fund.update_state(assets) self.assertTrue(fund.is_in_margin_call()) self.assertFalse(fund.default())
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_gen_liquidation_orders(self): SysConfig.set("MINUTE_VOLUME_LIMIT", 0.1) assets = { 'XXX': Asset(2, 3900, 1.5, 'XXX'), 'YYY': Asset(1, 7900, 1.5, 'yyy'), 'ZZZ': Asset(1, 7900, 1.5, 'yyy') } fund = Fund('F1', {'XXX': 10, 'YYY': 11, 'ZZZ': 2}, 5, 2, 0.25) expected_orders = [Sell('XXX', 1), Sell('YYY', 2), Sell('ZZZ', 2)] expected_portfolio = {'XXX': 9, 'YYY': 9} orders = fund.gen_liquidation_orders(assets) self.assertEqual(orders, expected_orders) self.assertEqual(fund.portfolio, expected_portfolio)
def test_gen_network_from_graph_with_assets(self): num_funds = 2 assets = { 'A1': Asset(price=1, daily_volume=3, symbol='A1', volatility=1.5), 'A2': Asset(price=2, daily_volume=4, symbol='A2', volatility=1) } initial_capitals = [100, 200] initial_leverages = [1, 2] tolerances = [4, 5] investment_proportions = {'f0': [0.6, 0.4], 'f1': [1.0]} g = nx.DiGraph() g.add_nodes_from([0, 1, 2, 3]) g.add_edges_from([(0, 2), (0, 3), (1, 3)]) network = AssetFundsNetwork.gen_network_from_graph_with_assets( g=g, investment_proportions=investment_proportions, initial_capitals=initial_capitals, initial_leverages=initial_leverages, tolerances=tolerances, assets=assets, mi_calc=ExponentialMarketImpactCalculator(1)) assets = network.assets funds = network.funds self.assertEqual(len(assets), 2) self.assertEqual(len(funds), num_funds) self.assertTrue((network.assets, assets)) for i in range(len(funds)): fund = funds['f' + str(i)] self.assertEqual(initial_capitals[i], fund.initial_capital) self.assertEqual(initial_leverages[i], fund.initial_leverage) self.assertEqual(tolerances[i], fund.tolerance) prot0 = funds['f0'].portfolio self.assertEqual(len(prot0.items()), 2) self.assertEqual(prot0['A1'], 120) self.assertEqual(prot0['A2'], 40) prot1 = funds['f1'].portfolio self.assertEqual(len(prot1.items()), 1) self.assertEqual(prot1['A2'], 300) self.assertTrue('A1' not in prot1)
def test_run_intraday_simulation_goal_leverage_reached(self): a0 = Asset(price=1, daily_volume=40, volatility=1.5, symbol='a0') a1 = Asset(price=2, daily_volume=40, volatility=1.5, symbol='a1') f0 = Fund('f0', {'a0': 10}, initial_capital=2, initial_leverage=8, tolerance=2) f1 = Fund('f1', { 'a0': 10, 'a1': 1 }, initial_capital=1, initial_leverage=1, tolerance=3) assets = {'a0': a0, 'a1': a1} network = AssetFundsNetwork({ 'f0': f0, 'f1': f1 }, assets, MockMarketImpactTestCalculator()) network.run_intraday_simulation(2, 0.8) self.assertTrue(f0.compute_curr_leverage(assets) <= 0.8) self.assertTrue(f1.compute_curr_leverage(assets) <= 0.8)
def test_run_intraday_simulation_price_rises(self): a0 = Asset(price=1, daily_volume=40, volatility=1.5, symbol='a0') a1 = Asset(price=2, daily_volume=40, volatility=1.5, symbol='a1') f0 = Fund('f0', {'a0': 10}, initial_capital=2, initial_leverage=8, tolerance=2) f1 = Fund('f1', { 'a0': 10, 'a1': 1 }, initial_capital=1, initial_leverage=1, tolerance=3) network = AssetFundsNetwork({ 'f0': f0, 'f1': f1 }, { 'a0': a0, 'a1': a1 }, MockMarketImpactTestCalculator()) network.run_intraday_simulation(1.5, 1) self.assertTrue(a0.price >= 1) self.assertTrue(a1.price >= 2)
def test_updated_price_buy_exp(self): calc = ExponentialMarketImpactCalculator(2) a = Asset(price=2, daily_volume=100, volatility=1.5, symbol='a1') updated_price = calc.get_updated_price(10, a, 1) npt.assert_almost_equal(2.4428, updated_price, decimal=4)
def test_market_impact_sell_order_exp(self): calc = ExponentialMarketImpactCalculator(2) a = Asset(price=2, daily_volume=100, volatility=1.5, symbol='a1') order = Sell('a1', 10) mi = calc.get_market_impact(order, a) npt.assert_almost_equal(0.8187, mi, decimal=4)
def test_updated_price_sell_sqrt(self): calc = SqrtMarketImpactCalculator(0.5) a = Asset(price=2, daily_volume=1000, volatility=1.5, symbol='a1') order = Sell('a1', 10) mi = calc.get_updated_price(order.num_shares, a, -1) npt.assert_almost_equal(1.925, mi, decimal=4)
def test_market_impact_sell_sqrt(self): calc = SqrtMarketImpactCalculator(0.5) a = Asset(price=2, daily_volume=1000, volatility=1.5, symbol='a1') order = Sell('a1', 10) mi = calc.get_market_impact(order, a) npt.assert_almost_equal(-0.075, mi, decimal=4)
def test_updated_price_sell_exp(self): calc = ExponentialMarketImpactCalculator(2) a = Asset(price=2, daily_volume=100, volatility=1.5, symbol='a1') mi = calc.get_updated_price(10, a, -1) npt.assert_almost_equal(1.6374, mi, decimal=4)
def test_set_price(self): asset = Asset(2, 100, 1.5, 'a1') asset.set_price(3) self.assertEqual(3, asset.price)