From 186f7d816a6bfc4fbf64027c3d7ecea10b97db38 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Isma=C3=ABl=20Bouya?= Date: Mon, 12 Mar 2018 01:55:17 +0100 Subject: Fix fullfiled not having correct currencies --- portfolio.py | 17 +++++++------- test.py | 75 +++++++++++++++++++++++++++++++++++++++++++++--------------- 2 files changed, 65 insertions(+), 27 deletions(-) diff --git a/portfolio.py b/portfolio.py index 0f2c011..65fdc6c 100644 --- a/portfolio.py +++ b/portfolio.py @@ -291,6 +291,7 @@ class Trade: self.orders = [] self.market = market self.closed = False + self.inverted = None assert self.value_from.value * self.value_to.value >= 0 assert self.value_from.currency == self.value_to.currency if self.value_from != 0: @@ -315,8 +316,8 @@ class Trade: else: return "dispose" - def order_action(self, inverted): - if (self.value_from < self.value_to) != inverted: + def order_action(self): + if (self.value_from < self.value_to) != self.inverted: return "buy" else: return "sell" @@ -339,7 +340,7 @@ class Trade: @property def is_fullfiled(self): - return abs(self.filled_amount(in_base_currency=True)) >= abs(self.delta) + return abs(self.filled_amount(in_base_currency=(not self.inverted))) >= abs(self.delta) def filled_amount(self, in_base_currency=False): filled_amount = 0 @@ -385,17 +386,17 @@ class Trade: if self.action is None: return None ticker = self.market.get_ticker(self.currency, self.base_currency) - inverted = ticker["inverted"] - if inverted: + self.inverted = ticker["inverted"] + if self.inverted: ticker = ticker["original"] - rate = Computation.compute_value(ticker, self.order_action(inverted), compute_value=compute_value) + rate = Computation.compute_value(ticker, self.order_action(), compute_value=compute_value) # FIXME: Dust amount should be removed from there if they werent # honored in other sales delta_in_base = abs(self.delta) # 9 BTC's worth of move (10 - 1 or 1 - 10 depending on case) - if not inverted: + if not self.inverted: base_currency = self.base_currency # BTC if self.action == "dispose": @@ -453,7 +454,7 @@ class Trade: self.market.report.log_error("prepare_order", message="Less to do than already filled: {}".format(delta)) return None - order = Order(self.order_action(inverted), + order = Order(self.order_action(), delta, rate, base_currency, self.trade_type, self.market, self, close_if_possible=close_if_possible) self.orders.append(order) diff --git a/test.py b/test.py index a45010b..33a817d 100644 --- a/test.py +++ b/test.py @@ -1375,16 +1375,20 @@ class TradeTest(WebMockTestCase): value_to = portfolio.Amount("BTC", "1.0") trade = portfolio.Trade(value_from, value_to, "ETH", self.m) - self.assertEqual("buy", trade.order_action(False)) - self.assertEqual("sell", trade.order_action(True)) + trade.inverted = False + self.assertEqual("buy", trade.order_action()) + trade.inverted = True + self.assertEqual("sell", trade.order_action()) 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.m) - self.assertEqual("sell", trade.order_action(False)) - self.assertEqual("buy", trade.order_action(True)) + trade.inverted = False + self.assertEqual("sell", trade.order_action()) + trade.inverted = True + self.assertEqual("buy", trade.order_action()) def test_trade_type(self): value_from = portfolio.Amount("BTC", "0.5") @@ -1402,26 +1406,59 @@ class TradeTest(WebMockTestCase): self.assertEqual("short", trade.trade_type) def test_is_fullfiled(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.m) + with self.subTest(inverted=False): + 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.m) - order1 = mock.Mock() - order1.filled_amount.return_value = portfolio.Amount("BTC", "0.3") + order1 = mock.Mock() + order1.filled_amount.return_value = portfolio.Amount("BTC", "0.3") - order2 = mock.Mock() - order2.filled_amount.return_value = portfolio.Amount("BTC", "0.01") - trade.orders.append(order1) - trade.orders.append(order2) + order2 = mock.Mock() + order2.filled_amount.return_value = portfolio.Amount("BTC", "0.01") + trade.orders.append(order1) + trade.orders.append(order2) + + self.assertFalse(trade.is_fullfiled) + + order3 = mock.Mock() + order3.filled_amount.return_value = portfolio.Amount("BTC", "0.19") + trade.orders.append(order3) + + self.assertTrue(trade.is_fullfiled) + + order1.filled_amount.assert_called_with(in_base_currency=True) + order2.filled_amount.assert_called_with(in_base_currency=True) + order3.filled_amount.assert_called_with(in_base_currency=True) + + with self.subTest(inverted=True): + value_from = portfolio.Amount("BTC", "0.5") + value_from.linked_to = portfolio.Amount("USDT", "1000.0") + value_to = portfolio.Amount("BTC", "1.0") + trade = portfolio.Trade(value_from, value_to, "USDT", self.m) + trade.inverted = True + + order1 = mock.Mock() + order1.filled_amount.return_value = portfolio.Amount("BTC", "0.3") + + order2 = mock.Mock() + order2.filled_amount.return_value = portfolio.Amount("BTC", "0.01") + trade.orders.append(order1) + trade.orders.append(order2) + + self.assertFalse(trade.is_fullfiled) + + order3 = mock.Mock() + order3.filled_amount.return_value = portfolio.Amount("BTC", "0.19") + trade.orders.append(order3) - self.assertFalse(trade.is_fullfiled) + self.assertTrue(trade.is_fullfiled) - order3 = mock.Mock() - order3.filled_amount.return_value = portfolio.Amount("BTC", "0.19") - trade.orders.append(order3) + order1.filled_amount.assert_called_with(in_base_currency=False) + order2.filled_amount.assert_called_with(in_base_currency=False) + order3.filled_amount.assert_called_with(in_base_currency=False) - self.assertTrue(trade.is_fullfiled) def test_filled_amount(self): value_from = portfolio.Amount("BTC", "0.5") -- cgit v1.2.3