]> git.immae.eu Git - perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Trader.git/commitdiff
Don’t close dust remaining orders before end of trade.
authorIsmaël Bouya <ismael.bouya@normalesup.org>
Tue, 1 May 2018 14:02:00 +0000 (16:02 +0200)
committerIsmaël Bouya <ismael.bouya@normalesup.org>
Tue, 1 May 2018 14:02:00 +0000 (16:02 +0200)
Fixes https://git.immae.eu/mantisbt/view.php?id=41

ccxt_wrapper.py
portfolio.py
tests/test_ccxt_wrapper.py
tests/test_portfolio.py

index f30c7d20fd6434e2ae36a22e11676ba27da5235b..c4aa94d6cbe7ccfb5c9c38447bc9b59162a1bbe0 100644 (file)
@@ -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"})
index c313fd9067cae8e5624620b7a237001ce48082a4..c064249af992bcd46a9f5e41f47cd3c1c5fff724 100644 (file)
@@ -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()
index 9ddfbc12963b80f386444432549d4ecb09ca7d43..c326f0a202dae9d2755a4fb79eba2a87e4bdd067 100644 (file)
@@ -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")))
index 4d789965673b9090c0df2ac7d74c0cd81e46d4ce..969f5d4caf2f4a6f0dc1b5b1705f8d879da4730c 100644 (file)
@@ -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)