]> git.immae.eu Git - perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Trader.git/blobdiff - tests/test_ccxt_wrapper.py
Fix price imprecision due to floats
[perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Trader.git] / tests / test_ccxt_wrapper.py
index 10e334daee0d102aa3997d46201da8c6ee9cc40d..9ddfbc12963b80f386444432549d4ecb09ca7d43 100644 (file)
@@ -110,7 +110,23 @@ class poloniexETest(unittest.TestCase):
                     retry_call.assert_not_called()
 
     def test_order_precision(self):
-        self.assertEqual(8, self.s.order_precision("FOO"))
+        self.s.markets = {
+                "FOO": {
+                    "precision": {
+                        "price": 5,
+                        "amount": 6,
+                        }
+                    },
+                "BAR": {
+                    "precision": {
+                        "price": 7,
+                        "amount": 8,
+                        }
+                    }
+                }
+        with mock.patch.object(self.s, "load_markets") as load_markets:
+            self.assertEqual(5, self.s.order_precision("FOO"))
+            load_markets.assert_called_once()
 
     def test_transfer_balance(self):
         with self.subTest(success=True),\
@@ -167,33 +183,6 @@ class poloniexETest(unittest.TestCase):
                     }
             self.assertEqual(expected, self.s.margin_summary())
 
-    def test_create_order(self):
-        with mock.patch.object(self.s, "create_exchange_order") as exchange,\
-                mock.patch.object(self.s, "create_margin_order") as margin:
-            with self.subTest(account="unspecified"):
-                self.s.create_order("symbol", "type", "side", "amount", price="price", lending_rate="lending_rate", params="params")
-                exchange.assert_called_once_with("symbol", "type", "side", "amount", price="price", params="params")
-                margin.assert_not_called()
-                exchange.reset_mock()
-                margin.reset_mock()
-
-            with self.subTest(account="exchange"):
-                self.s.create_order("symbol", "type", "side", "amount", account="exchange", price="price", lending_rate="lending_rate", params="params")
-                exchange.assert_called_once_with("symbol", "type", "side", "amount", price="price", params="params")
-                margin.assert_not_called()
-                exchange.reset_mock()
-                margin.reset_mock()
-
-            with self.subTest(account="margin"):
-                self.s.create_order("symbol", "type", "side", "amount", account="margin", price="price", lending_rate="lending_rate", params="params")
-                margin.assert_called_once_with("symbol", "type", "side", "amount", lending_rate="lending_rate", price="price", params="params")
-                exchange.assert_not_called()
-                exchange.reset_mock()
-                margin.reset_mock()
-
-            with self.subTest(account="unknown"), self.assertRaises(NotImplementedError):
-                self.s.create_order("symbol", "type", "side", "amount", account="unknown")
-
     def test_parse_ticker(self):
         ticker = {
                 "high24hr": "12",
@@ -439,11 +428,13 @@ class poloniexETest(unittest.TestCase):
             self.assertEqual(expected_doge, result["DOGE"])
             self.assertEqual(expected_btc, result["BTC"])
 
-    def test_create_margin_order(self):
-        with self.assertRaises(market.ExchangeError):
-            self.s.create_margin_order("FOO", "market", "buy", "10")
+    def test_create_order(self):
+        with self.subTest(type="market"),\
+                self.assertRaises(market.ExchangeError):
+            self.s.create_order("FOO", "market", "buy", "10")
 
-        with mock.patch.object(self.s, "load_markets") as load_markets,\
+        with self.subTest(type="limit", account="margin"),\
+                mock.patch.object(self.s, "load_markets") as load_markets,\
                 mock.patch.object(self.s, "privatePostMarginBuy") as margin_buy,\
                 mock.patch.object(self.s, "privatePostMarginSell") as margin_sell,\
                 mock.patch.object(self.s, "market") as market_mock,\
@@ -460,26 +451,97 @@ class poloniexETest(unittest.TestCase):
             ptp.return_value = D("0.1")
             atp.return_value = D("12")
 
-            order = self.s.create_margin_order("BTC_ETC", "margin", "buy", "12", price="0.1")
+            order = self.s.create_order("BTC_ETC", "limit", "buy", "12",
+                    account="margin", price="0.1")
             self.assertEqual(123, order["id"])
             margin_buy.assert_called_once_with({"currencyPair": "BTC_ETC", "rate": D("0.1"), "amount": D("12")})
             margin_sell.assert_not_called()
             margin_buy.reset_mock()
             margin_sell.reset_mock()
 
-            order = self.s.create_margin_order("BTC_ETC", "margin", "sell", "12", lending_rate="0.01", price="0.1")
+            order = self.s.create_order("BTC_ETC", "limit", "sell",
+                    "12", account="margin", lending_rate="0.01", price="0.1")
             self.assertEqual(456, order["id"])
             margin_sell.assert_called_once_with({"currencyPair": "BTC_ETC", "rate": D("0.1"), "amount": D("12"), "lendingRate": "0.01"})
             margin_buy.assert_not_called()
 
-    def test_create_exchange_order(self):
-        with mock.patch.object(market.ccxt.poloniex, "create_order") as create_order:
-            self.s.create_order("symbol", "type", "side", "amount", price="price", params="params")
+        with self.subTest(type="limit", account="exchange"),\
+                mock.patch.object(self.s, "load_markets") as load_markets,\
+                mock.patch.object(self.s, "privatePostBuy") as exchange_buy,\
+                mock.patch.object(self.s, "privatePostSell") as exchange_sell,\
+                mock.patch.object(self.s, "market") as market_mock,\
+                mock.patch.object(self.s, "price_to_precision") as ptp,\
+                mock.patch.object(self.s, "amount_to_precision") as atp:
 
-            create_order.assert_called_once_with("symbol", "type", "side", "amount", price="price", params="params")
+            exchange_buy.return_value = {
+                    "orderNumber": 123
+                    }
+            exchange_sell.return_value = {
+                    "orderNumber": 456
+                    }
+            market_mock.return_value = { "id": "BTC_ETC", "symbol": "BTC_ETC" }
+            ptp.return_value = D("0.1")
+            atp.return_value = D("12")
+
+            order = self.s.create_order("BTC_ETC", "limit", "buy", "12",
+                    account="exchange", price="0.1")
+            self.assertEqual(123, order["id"])
+            exchange_buy.assert_called_once_with({"currencyPair": "BTC_ETC", "rate": D("0.1"), "amount": D("12")})
+            exchange_sell.assert_not_called()
+            exchange_buy.reset_mock()
+            exchange_sell.reset_mock()
+
+            order = self.s.create_order("BTC_ETC", "limit", "sell",
+                    "12", account="exchange", lending_rate="0.01", price="0.1")
+            self.assertEqual(456, order["id"])
+            exchange_sell.assert_called_once_with({"currencyPair": "BTC_ETC", "rate": D("0.1"), "amount": D("12")})
+            exchange_buy.assert_not_called()
+
+        with self.subTest(account="unknown"), self.assertRaises(NotImplementedError),\
+                mock.patch.object(self.s, "load_markets") as load_markets:
+            self.s.create_order("symbol", "type", "side", "amount", account="unknown")
 
     def test_common_currency_code(self):
         self.assertEqual("FOO", self.s.common_currency_code("FOO"))
 
     def test_currency_id(self):
         self.assertEqual("FOO", self.s.currency_id("FOO"))
+
+    def test_amount_to_precision(self):
+        self.s.markets = {
+                "FOO": {
+                    "precision": {
+                        "price": 5,
+                        "amount": 6,
+                        }
+                    },
+                "BAR": {
+                    "precision": {
+                        "price": 7,
+                        "amount": 8,
+                        }
+                    }
+                }
+        self.assertEqual("0.0001", self.s.amount_to_precision("FOO", D("0.0001")))
+        self.assertEqual("0.0000001", self.s.amount_to_precision("BAR", D("0.0000001")))
+        self.assertEqual("0.000001", self.s.amount_to_precision("FOO", D("0.000001")))
+
+    def test_price_to_precision(self):
+        self.s.markets = {
+                "FOO": {
+                    "precision": {
+                        "price": 5,
+                        "amount": 6,
+                        }
+                    },
+                "BAR": {
+                    "precision": {
+                        "price": 7,
+                        "amount": 8,
+                        }
+                    }
+                }
+        self.assertEqual("0.0001", self.s.price_to_precision("FOO", D("0.0001")))
+        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")))
+