+ value_from = portfolio.Amount("BTC", "0.5")
+ value_from.linked_to = portfolio.Amount("ETH", "10.0")
+ value_to = portfolio.Amount("BTC", "1.0")
+ trade = portfolio.Trade(value_from, value_to, "ETH")
+
+ self.assertEqual("buy", trade.order_action(False))
+ self.assertEqual("sell", trade.order_action(True))
+
+ value_from = portfolio.Amount("BTC", "0")
+ value_from.linked_to = portfolio.Amount("ETH", "0")
+ value_to = portfolio.Amount("BTC", "-1.0")
+ trade = portfolio.Trade(value_from, value_to, "ETH")
+
+ self.assertEqual("sell", trade.order_action(False))
+ self.assertEqual("buy", trade.order_action(True))
+
+ def test_trade_type(self):
+ value_from = portfolio.Amount("BTC", "0.5")
+ value_from.linked_to = portfolio.Amount("ETH", "10.0")
+ value_to = portfolio.Amount("BTC", "1.0")
+ trade = portfolio.Trade(value_from, value_to, "ETH")
+
+ self.assertEqual("long", trade.trade_type)
+
+ value_from = portfolio.Amount("BTC", "0")
+ value_from.linked_to = portfolio.Amount("ETH", "0")
+ value_to = portfolio.Amount("BTC", "-1.0")
+ trade = portfolio.Trade(value_from, value_to, "ETH")
+
+ self.assertEqual("short", trade.trade_type)
+
+ def test_filled_amount(self):
+ value_from = portfolio.Amount("BTC", "0.5")
+ value_from.linked_to = portfolio.Amount("ETH", "10.0")
+ value_to = portfolio.Amount("BTC", "1.0")
+ trade = portfolio.Trade(value_from, value_to, "ETH")
+
+ order1 = mock.Mock()
+ order1.filled_amount.return_value = portfolio.Amount("ETH", "0.3")
+
+ order2 = mock.Mock()
+ order2.filled_amount.return_value = portfolio.Amount("ETH", "0.01")
+ trade.orders.append(order1)
+ trade.orders.append(order2)
+
+ self.assertEqual(portfolio.Amount("ETH", "0.31"), trade.filled_amount())
+ order1.filled_amount.assert_called_with(in_base_currency=False)
+ order2.filled_amount.assert_called_with(in_base_currency=False)
+
+ self.assertEqual(portfolio.Amount("ETH", "0.31"), trade.filled_amount(in_base_currency=False))
+ order1.filled_amount.assert_called_with(in_base_currency=False)
+ order2.filled_amount.assert_called_with(in_base_currency=False)
+
+ self.assertEqual(portfolio.Amount("ETH", "0.31"), trade.filled_amount(in_base_currency=True))
+ order1.filled_amount.assert_called_with(in_base_currency=True)
+ order2.filled_amount.assert_called_with(in_base_currency=True)
+
+ @mock.patch.object(helper, "get_ticker")
+ @mock.patch.object(portfolio.Computation, "compute_value")
+ @mock.patch.object(portfolio.Trade, "filled_amount")
+ @mock.patch.object(portfolio, "Order")
+ def test_prepare_order(self, Order, filled_amount, compute_value, get_ticker):
+ Order.return_value = "Order"
+
+ with self.subTest(desc="Nothing to do"):
+ value_from = portfolio.Amount("BTC", "10")
+ value_from.rate = D("0.1")
+ value_from.linked_to = portfolio.Amount("FOO", "100")
+ value_to = portfolio.Amount("BTC", "10")
+ trade = portfolio.Trade(value_from, value_to, "FOO", market="market")
+
+ trade.prepare_order()
+
+ filled_amount.assert_not_called()
+ compute_value.assert_not_called()
+ self.assertEqual(0, len(trade.orders))
+ Order.assert_not_called()
+
+ get_ticker.return_value = { "inverted": False }
+ with self.subTest(desc="Already filled"),\
+ mock.patch("portfolio.ReportStore") as report_store:
+ filled_amount.return_value = portfolio.Amount("FOO", "100")
+ compute_value.return_value = D("0.125")
+
+ value_from = portfolio.Amount("BTC", "10")
+ value_from.rate = D("0.1")
+ value_from.linked_to = portfolio.Amount("FOO", "100")
+ value_to = portfolio.Amount("BTC", "0")
+ trade = portfolio.Trade(value_from, value_to, "FOO", market="market")
+
+ trade.prepare_order()
+
+ filled_amount.assert_called_with(in_base_currency=False)
+ compute_value.assert_called_with(get_ticker.return_value, "sell", compute_value="default")
+ self.assertEqual(0, len(trade.orders))
+ report_store.log_error.assert_called_with("prepare_order", message=mock.ANY)
+ Order.assert_not_called()
+
+ with self.subTest(action="dispose", inverted=False):
+ filled_amount.return_value = portfolio.Amount("FOO", "60")
+ compute_value.return_value = D("0.125")
+
+ value_from = portfolio.Amount("BTC", "10")
+ value_from.rate = D("0.1")
+ value_from.linked_to = portfolio.Amount("FOO", "100")
+ value_to = portfolio.Amount("BTC", "1")
+ trade = portfolio.Trade(value_from, value_to, "FOO", market="market")
+
+ trade.prepare_order()
+
+ filled_amount.assert_called_with(in_base_currency=False)
+ compute_value.assert_called_with(get_ticker.return_value, "sell", compute_value="default")
+ self.assertEqual(1, len(trade.orders))
+ Order.assert_called_with("sell", portfolio.Amount("FOO", 30),
+ D("0.125"), "BTC", "long", "market",
+ trade, close_if_possible=False)
+
+ with self.subTest(action="acquire", inverted=False):
+ filled_amount.return_value = portfolio.Amount("BTC", "3")
+ compute_value.return_value = D("0.125")
+
+ value_from = portfolio.Amount("BTC", "1")
+ value_from.rate = D("0.1")
+ value_from.linked_to = portfolio.Amount("FOO", "10")
+ value_to = portfolio.Amount("BTC", "10")
+ trade = portfolio.Trade(value_from, value_to, "FOO", market="market")
+
+ trade.prepare_order()
+
+ filled_amount.assert_called_with(in_base_currency=True)
+ compute_value.assert_called_with(get_ticker.return_value, "buy", compute_value="default")
+ self.assertEqual(1, len(trade.orders))
+
+ Order.assert_called_with("buy", portfolio.Amount("FOO", 48),
+ D("0.125"), "BTC", "long", "market",
+ trade, close_if_possible=False)
+
+ with self.subTest(close_if_possible=True):
+ filled_amount.return_value = portfolio.Amount("FOO", "0")
+ compute_value.return_value = D("0.125")
+
+ value_from = portfolio.Amount("BTC", "10")
+ value_from.rate = D("0.1")
+ value_from.linked_to = portfolio.Amount("FOO", "100")
+ value_to = portfolio.Amount("BTC", "0")
+ trade = portfolio.Trade(value_from, value_to, "FOO", market="market")
+
+ trade.prepare_order()
+
+ filled_amount.assert_called_with(in_base_currency=False)
+ compute_value.assert_called_with(get_ticker.return_value, "sell", compute_value="default")
+ self.assertEqual(1, len(trade.orders))
+ Order.assert_called_with("sell", portfolio.Amount("FOO", 100),
+ D("0.125"), "BTC", "long", "market",
+ trade, close_if_possible=True)
+
+ get_ticker.return_value = { "inverted": True, "original": {} }
+ with self.subTest(action="dispose", inverted=True):
+ filled_amount.return_value = portfolio.Amount("FOO", "300")
+ compute_value.return_value = D("125")
+
+ value_from = portfolio.Amount("BTC", "10")
+ value_from.rate = D("0.01")
+ value_from.linked_to = portfolio.Amount("FOO", "1000")
+ value_to = portfolio.Amount("BTC", "1")
+ trade = portfolio.Trade(value_from, value_to, "FOO", market="market")
+
+ trade.prepare_order(compute_value="foo")
+
+ filled_amount.assert_called_with(in_base_currency=True)
+ compute_value.assert_called_with(get_ticker.return_value["original"], "buy", compute_value="foo")
+ self.assertEqual(1, len(trade.orders))
+ Order.assert_called_with("buy", portfolio.Amount("BTC", D("4.8")),
+ D("125"), "FOO", "long", "market",
+ trade, close_if_possible=False)
+
+ with self.subTest(action="acquire", inverted=True):
+ filled_amount.return_value = portfolio.Amount("BTC", "4")
+ compute_value.return_value = D("125")
+
+ value_from = portfolio.Amount("BTC", "1")
+ value_from.rate = D("0.01")
+ value_from.linked_to = portfolio.Amount("FOO", "100")
+ value_to = portfolio.Amount("BTC", "10")
+ trade = portfolio.Trade(value_from, value_to, "FOO", market="market")
+
+ trade.prepare_order(compute_value="foo")
+
+ filled_amount.assert_called_with(in_base_currency=False)
+ compute_value.assert_called_with(get_ticker.return_value["original"], "sell", compute_value="foo")
+ self.assertEqual(1, len(trade.orders))
+ Order.assert_called_with("sell", portfolio.Amount("BTC", D("5")),
+ D("125"), "FOO", "long", "market",
+ trade, close_if_possible=False)
+
+
+ @mock.patch.object(portfolio.Trade, "prepare_order")
+ def test_update_order(self, prepare_order):
+ order_mock = mock.Mock()
+ new_order_mock = mock.Mock()
+
+ value_from = portfolio.Amount("BTC", "0.5")
+ value_from.linked_to = portfolio.Amount("ETH", "10.0")
+ value_to = portfolio.Amount("BTC", "1.0")
+ trade = portfolio.Trade(value_from, value_to, "ETH")
+ prepare_order.return_value = new_order_mock
+
+ for i in [0, 1, 3, 4, 6]:
+ with self.subTest(tick=i),\
+ mock.patch.object(portfolio.ReportStore, "log_order") as log_order:
+ trade.update_order(order_mock, i)
+ order_mock.cancel.assert_not_called()
+ new_order_mock.run.assert_not_called()
+ log_order.assert_called_once_with(order_mock, i,
+ update="waiting", compute_value=None, new_order=None)
+
+ order_mock.reset_mock()
+ new_order_mock.reset_mock()
+ trade.orders = []
+
+ with mock.patch.object(portfolio.ReportStore, "log_order") as log_order:
+ trade.update_order(order_mock, 2)
+ order_mock.cancel.assert_called()
+ new_order_mock.run.assert_called()
+ prepare_order.assert_called()
+ log_order.assert_called()
+ self.assertEqual(2, log_order.call_count)
+ calls = [
+ mock.call(order_mock, 2, update="adjusting",
+ compute_value='lambda x, y: (x[y] + x["average"]) / 2',
+ new_order=new_order_mock),
+ mock.call(order_mock, 2, new_order=new_order_mock),
+ ]
+ log_order.assert_has_calls(calls)
+
+ order_mock.reset_mock()
+ new_order_mock.reset_mock()
+ trade.orders = []
+
+ with mock.patch.object(portfolio.ReportStore, "log_order") as log_order:
+ trade.update_order(order_mock, 5)
+ order_mock.cancel.assert_called()
+ new_order_mock.run.assert_called()
+ prepare_order.assert_called()
+ self.assertEqual(2, log_order.call_count)
+ log_order.assert_called()
+ calls = [
+ mock.call(order_mock, 5, update="adjusting",
+ compute_value='lambda x, y: (x[y]*2 + x["average"]) / 3',
+ new_order=new_order_mock),
+ mock.call(order_mock, 5, new_order=new_order_mock),
+ ]
+ log_order.assert_has_calls(calls)
+
+ order_mock.reset_mock()
+ new_order_mock.reset_mock()
+ trade.orders = []
+
+ with mock.patch.object(portfolio.ReportStore, "log_order") as log_order:
+ trade.update_order(order_mock, 7)
+ order_mock.cancel.assert_called()
+ new_order_mock.run.assert_called()
+ prepare_order.assert_called_with(compute_value="default")
+ log_order.assert_called()
+ self.assertEqual(2, log_order.call_count)
+ calls = [
+ mock.call(order_mock, 7, update="market_fallback",
+ compute_value='default',
+ new_order=new_order_mock),
+ mock.call(order_mock, 7, new_order=new_order_mock),
+ ]
+ log_order.assert_has_calls(calls)
+
+ order_mock.reset_mock()
+ new_order_mock.reset_mock()
+ trade.orders = []
+
+ for i in [10, 13, 16]:
+ with self.subTest(tick=i), mock.patch.object(portfolio.ReportStore, "log_order") as log_order:
+ trade.update_order(order_mock, i)
+ order_mock.cancel.assert_called()
+ new_order_mock.run.assert_called()
+ prepare_order.assert_called_with(compute_value="default")
+ log_order.assert_called()
+ self.assertEqual(2, log_order.call_count)
+ calls = [
+ mock.call(order_mock, i, update="market_adjust",
+ compute_value='default',
+ new_order=new_order_mock),
+ mock.call(order_mock, i, new_order=new_order_mock),
+ ]
+ log_order.assert_has_calls(calls)
+
+ order_mock.reset_mock()
+ new_order_mock.reset_mock()
+ trade.orders = []
+
+ for i in [8, 9, 11, 12]:
+ with self.subTest(tick=i), mock.patch.object(portfolio.ReportStore, "log_order") as log_order:
+ trade.update_order(order_mock, i)
+ order_mock.cancel.assert_not_called()
+ new_order_mock.run.assert_not_called()
+ log_order.assert_called_once_with(order_mock, i, update="waiting",
+ compute_value=None, new_order=None)
+
+ order_mock.reset_mock()
+ new_order_mock.reset_mock()
+ trade.orders = []
+
+
+ @mock.patch.object(portfolio.ReportStore, "print_log")
+ def test_print_with_order(self, print_log):
+ value_from = portfolio.Amount("BTC", "0.5")
+ value_from.linked_to = portfolio.Amount("ETH", "10.0")
+ value_to = portfolio.Amount("BTC", "1.0")
+ trade = portfolio.Trade(value_from, value_to, "ETH")
+
+ order_mock1 = mock.Mock()
+ order_mock1.__repr__ = mock.Mock()
+ order_mock1.__repr__.return_value = "Mock 1"
+ order_mock2 = mock.Mock()
+ order_mock2.__repr__ = mock.Mock()
+ order_mock2.__repr__.return_value = "Mock 2"
+ order_mock1.mouvements = []
+ mouvement_mock1 = mock.Mock()
+ mouvement_mock1.__repr__ = mock.Mock()
+ mouvement_mock1.__repr__.return_value = "Mouvement 1"
+ mouvement_mock2 = mock.Mock()
+ mouvement_mock2.__repr__ = mock.Mock()
+ mouvement_mock2.__repr__.return_value = "Mouvement 2"
+ order_mock2.mouvements = [
+ mouvement_mock1, mouvement_mock2
+ ]
+ trade.orders.append(order_mock1)
+ trade.orders.append(order_mock2)