From 1902674cbeaa4dc0cdc31e6f0f7a548b3fa7f38e Mon Sep 17 00:00:00 2001 From: =?utf8?q?Isma=C3=ABl=20Bouya?= Date: Tue, 1 May 2018 16:02:00 +0200 Subject: [PATCH] =?utf8?q?Don=E2=80=99t=20close=20dust=20remaining=20order?= =?utf8?q?s=20before=20end=20of=20trade.?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Fixes https://git.immae.eu/mantisbt/view.php?id=41 --- ccxt_wrapper.py | 7 +++++++ portfolio.py | 8 ++++---- tests/test_ccxt_wrapper.py | 4 ++++ tests/test_portfolio.py | 13 ++++++++----- 4 files changed, 23 insertions(+), 9 deletions(-) diff --git a/ccxt_wrapper.py b/ccxt_wrapper.py index f30c7d2..c4aa94d 100644 --- a/ccxt_wrapper.py +++ b/ccxt_wrapper.py @@ -66,6 +66,13 @@ class poloniexE(poloniex): def nanoseconds(): return int(time.time() * 1000000000) + def is_dust_trade(self, amount, rate): + if abs(amount) < decimal.Decimal("0.000001"): + return True + if abs(amount * rate) < decimal.Decimal("0.0001"): + return True + return False + def fetch_margin_balance(self): """ portfolio.market.privatePostGetMarginPosition({"currencyPair": "BTC_DASH"}) diff --git a/portfolio.py b/portfolio.py index c313fd9..c064249 100644 --- a/portfolio.py +++ b/portfolio.py @@ -578,12 +578,12 @@ class Order: self.fetch_mouvements() self.mark_disappeared_order() - + self.mark_dust_amount_remaining_order() self.mark_finished_order() - # FIXME: consider open order with dust remaining as closed - def dust_amount_remaining(self): - return self.remaining_amount() < Amount(self.amount.currency, D("0.001")) + def mark_dust_amount_remaining_order(self): + if self.market.ccxt.is_dust_trade(self.remaining_amount().value, self.rate): + self.status = "closed_dust_remaining" def remaining_amount(self): return self.amount - self.filled_amount() diff --git a/tests/test_ccxt_wrapper.py b/tests/test_ccxt_wrapper.py index 9ddfbc1..c326f0a 100644 --- a/tests/test_ccxt_wrapper.py +++ b/tests/test_ccxt_wrapper.py @@ -545,3 +545,7 @@ class poloniexETest(unittest.TestCase): self.assertEqual("0.0000001", self.s.price_to_precision("BAR", D("0.0000001"))) self.assertEqual("0", self.s.price_to_precision("FOO", D("0.000001"))) + def test_is_dust_trade(self): + self.assertTrue(self.s.is_dust_trade(D("0.0000009"), D("1000"))) + self.assertTrue(self.s.is_dust_trade(D("0.000001"), D("10"))) + self.assertFalse(self.s.is_dust_trade(D("0.000001"), D("100"))) diff --git a/tests/test_portfolio.py b/tests/test_portfolio.py index 4d78996..969f5d4 100644 --- a/tests/test_portfolio.py +++ b/tests/test_portfolio.py @@ -823,14 +823,16 @@ class OrderTest(WebMockTestCase): order.cancel() self.m.ccxt.cancel_order.assert_not_called() - def test_dust_amount_remaining(self): + def test_mark_dust_amount_remaining(self): order = portfolio.Order("buy", portfolio.Amount("ETH", 10), D("0.1"), "BTC", "long", self.m, "trade") - order.remaining_amount = mock.Mock(return_value=portfolio.Amount("ETH", 1)) - self.assertFalse(order.dust_amount_remaining()) + self.m.ccxt.is_dust_trade.return_value = False + order.mark_dust_amount_remaining_order() + self.assertEqual("pending", order.status) - order.remaining_amount = mock.Mock(return_value=portfolio.Amount("ETH", D("0.0001"))) - self.assertTrue(order.dust_amount_remaining()) + self.m.ccxt.is_dust_trade.return_value = True + order.mark_dust_amount_remaining_order() + self.assertEqual("closed_dust_remaining", order.status) @mock.patch.object(portfolio.Order, "fetch") @mock.patch.object(portfolio.Order, "filled_amount", return_value=portfolio.Amount("ETH", 1)) @@ -965,6 +967,7 @@ class OrderTest(WebMockTestCase): "status": "foo", "datetime": "timestamp" } + self.m.ccxt.is_dust_trade.return_value = False order.fetch() self.m.ccxt.fetch_order.assert_called_once_with(45) -- 2.41.0