aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIsmaël Bouya <ismael.bouya@normalesup.org>2018-05-01 16:02:00 +0200
committerIsmaël Bouya <ismael.bouya@normalesup.org>2018-05-01 16:02:00 +0200
commit1902674cbeaa4dc0cdc31e6f0f7a548b3fa7f38e (patch)
tree8773f0fbba805f0e4f76fb742da766b4f1a56073
parent52ea19aa73348a523b3b884e2a7fb749b2bf4f19 (diff)
downloadTrader-1902674cbeaa4dc0cdc31e6f0f7a548b3fa7f38e.tar.gz
Trader-1902674cbeaa4dc0cdc31e6f0f7a548b3fa7f38e.tar.zst
Trader-1902674cbeaa4dc0cdc31e6f0f7a548b3fa7f38e.zip
Don’t close dust remaining orders before end of trade.
Fixes https://git.immae.eu/mantisbt/view.php?id=41
-rw-r--r--ccxt_wrapper.py7
-rw-r--r--portfolio.py8
-rw-r--r--tests/test_ccxt_wrapper.py4
-rw-r--r--tests/test_portfolio.py13
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):
66 def nanoseconds(): 66 def nanoseconds():
67 return int(time.time() * 1000000000) 67 return int(time.time() * 1000000000)
68 68
69 def is_dust_trade(self, amount, rate):
70 if abs(amount) < decimal.Decimal("0.000001"):
71 return True
72 if abs(amount * rate) < decimal.Decimal("0.0001"):
73 return True
74 return False
75
69 def fetch_margin_balance(self): 76 def fetch_margin_balance(self):
70 """ 77 """
71 portfolio.market.privatePostGetMarginPosition({"currencyPair": "BTC_DASH"}) 78 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:
578 self.fetch_mouvements() 578 self.fetch_mouvements()
579 579
580 self.mark_disappeared_order() 580 self.mark_disappeared_order()
581 581 self.mark_dust_amount_remaining_order()
582 self.mark_finished_order() 582 self.mark_finished_order()
583 # FIXME: consider open order with dust remaining as closed
584 583
585 def dust_amount_remaining(self): 584 def mark_dust_amount_remaining_order(self):
586 return self.remaining_amount() < Amount(self.amount.currency, D("0.001")) 585 if self.market.ccxt.is_dust_trade(self.remaining_amount().value, self.rate):
586 self.status = "closed_dust_remaining"
587 587
588 def remaining_amount(self): 588 def remaining_amount(self):
589 return self.amount - self.filled_amount() 589 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):
545 self.assertEqual("0.0000001", self.s.price_to_precision("BAR", D("0.0000001"))) 545 self.assertEqual("0.0000001", self.s.price_to_precision("BAR", D("0.0000001")))
546 self.assertEqual("0", self.s.price_to_precision("FOO", D("0.000001"))) 546 self.assertEqual("0", self.s.price_to_precision("FOO", D("0.000001")))
547 547
548 def test_is_dust_trade(self):
549 self.assertTrue(self.s.is_dust_trade(D("0.0000009"), D("1000")))
550 self.assertTrue(self.s.is_dust_trade(D("0.000001"), D("10")))
551 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):
823 order.cancel() 823 order.cancel()
824 self.m.ccxt.cancel_order.assert_not_called() 824 self.m.ccxt.cancel_order.assert_not_called()
825 825
826 def test_dust_amount_remaining(self): 826 def test_mark_dust_amount_remaining(self):
827 order = portfolio.Order("buy", portfolio.Amount("ETH", 10), 827 order = portfolio.Order("buy", portfolio.Amount("ETH", 10),
828 D("0.1"), "BTC", "long", self.m, "trade") 828 D("0.1"), "BTC", "long", self.m, "trade")
829 order.remaining_amount = mock.Mock(return_value=portfolio.Amount("ETH", 1)) 829 self.m.ccxt.is_dust_trade.return_value = False
830 self.assertFalse(order.dust_amount_remaining()) 830 order.mark_dust_amount_remaining_order()
831 self.assertEqual("pending", order.status)
831 832
832 order.remaining_amount = mock.Mock(return_value=portfolio.Amount("ETH", D("0.0001"))) 833 self.m.ccxt.is_dust_trade.return_value = True
833 self.assertTrue(order.dust_amount_remaining()) 834 order.mark_dust_amount_remaining_order()
835 self.assertEqual("closed_dust_remaining", order.status)
834 836
835 @mock.patch.object(portfolio.Order, "fetch") 837 @mock.patch.object(portfolio.Order, "fetch")
836 @mock.patch.object(portfolio.Order, "filled_amount", return_value=portfolio.Amount("ETH", 1)) 838 @mock.patch.object(portfolio.Order, "filled_amount", return_value=portfolio.Amount("ETH", 1))
@@ -965,6 +967,7 @@ class OrderTest(WebMockTestCase):
965 "status": "foo", 967 "status": "foo",
966 "datetime": "timestamp" 968 "datetime": "timestamp"
967 } 969 }
970 self.m.ccxt.is_dust_trade.return_value = False
968 order.fetch() 971 order.fetch()
969 972
970 self.m.ccxt.fetch_order.assert_called_once_with(45) 973 self.m.ccxt.fetch_order.assert_called_once_with(45)