]> git.immae.eu Git - perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Trader.git/blobdiff - tests/test_market.py
Fixes https://git.immae.eu/mantisbt/view.php?id=44
[perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Trader.git] / tests / test_market.py
index e0cf70aa4beda24db29454ad861bb29726c8b245..53630b70f11edeb97d37d0271c73c146acd77345 100644 (file)
@@ -130,19 +130,20 @@ class MarketTest(WebMockTestCase):
     @mock.patch.object(market.Market, "get_ticker")
     @mock.patch.object(market.TradeStore, "compute_trades")
     def test_prepare_trades(self, compute_trades, get_ticker, repartition):
-        repartition.return_value = {
-                "XEM": (D("0.75"), "long"),
-                "BTC": (D("0.25"), "long"),
-                }
-        def _get_ticker(c1, c2):
-            if c1 == "USDT" and c2 == "BTC":
-                return { "average": D("0.0001") }
-            if c1 == "XVG" and c2 == "BTC":
-                return { "average": D("0.000001") }
-            self.fail("Should be called with {}, {}".format(c1, c2))
-        get_ticker.side_effect = _get_ticker
-
-        with mock.patch("market.ReportStore"):
+        with self.subTest(available_balance_only=False),\
+                mock.patch("market.ReportStore"):
+            def _get_ticker(c1, c2):
+                if c1 == "USDT" and c2 == "BTC":
+                    return { "average": D("0.0001") }
+                if c1 == "XVG" and c2 == "BTC":
+                    return { "average": D("0.000001") }
+                self.fail("Should not be called with {}, {}".format(c1, c2))
+            get_ticker.side_effect = _get_ticker
+
+            repartition.return_value = {
+                    "XEM": (D("0.75"), "long"),
+                    "BTC": (D("0.25"), "long"),
+                    }
             m = market.Market(self.ccxt, self.market_args())
             self.ccxt.fetch_all_balances.return_value = {
                     "USDT": {
@@ -171,9 +172,109 @@ class MarketTest(WebMockTestCase):
             self.assertEqual(D("0.7575"), call[0][1]["XEM"].value)
             m.report.log_stage.assert_called_once_with("prepare_trades",
                     base_currency='BTC', compute_value='average',
-                    liquidity='medium', only=None, repartition=None)
+                    available_balance_only=False, liquidity='medium',
+                    only=None, repartition=None)
             m.report.log_balances.assert_called_once_with(tag="tag")
 
+        compute_trades.reset_mock()
+        with self.subTest(available_balance_only=True),\
+                mock.patch("market.ReportStore"):
+            def _get_ticker(c1, c2):
+                if c1 == "ZRC" and c2 == "BTC":
+                    return { "average": D("0.0001") }
+                if c1 == "DOGE" and c2 == "BTC":
+                    return { "average": D("0.000001") }
+                if c1 == "ETH" and c2 == "BTC":
+                    return { "average": D("0.1") }
+                self.fail("Should not be called with {}, {}".format(c1, c2))
+            get_ticker.side_effect = _get_ticker
+
+            repartition.return_value = {
+                    "DOGE": (D("0.25"), "short"),
+                    "BTC": (D("0.25"), "long"),
+                    "ETH": (D("0.25"), "long"),
+                    "XMR": (D("0.25"), "long"),
+                    }
+            m = market.Market(self.ccxt, self.market_args())
+            self.ccxt.fetch_all_balances.return_value = {
+                    "ZRC": {
+                        "exchange_free": D("2.0"),
+                        "exchange_used": D("0.0"),
+                        "exchange_total": D("2.0"),
+                        "total": D("2.0")
+                        },
+                    "DOGE": {
+                        "exchange_free": D("5.0"),
+                        "exchange_used": D("0.0"),
+                        "exchange_total": D("5.0"),
+                        "total": D("5.0")
+                        },
+                    "BTC": {
+                        "exchange_free": D("0.075"),
+                        "exchange_used": D("0.02"),
+                        "exchange_total": D("0.095"),
+                        "margin_available": D("0.025"),
+                        "margin_in_position": D("0.01"),
+                        "margin_total": D("0.035"),
+                        "total": D("0.13")
+                        },
+                    "ETH": {
+                        "exchange_free": D("1.0"),
+                        "exchange_used": D("0.0"),
+                        "exchange_total": D("1.0"),
+                        "total": D("1.0")
+                        },
+                    }
+
+            m.balances.fetch_balances(tag="tag")
+            m.prepare_trades(available_balance_only=True)
+            compute_trades.assert_called_once()
+
+            call = compute_trades.call_args[0]
+            values_in_base = call[0]
+            new_repartition = call[1]
+
+            self.assertEqual(portfolio.Amount("BTC", "-0.025"),
+                    new_repartition["DOGE"] - values_in_base["DOGE"])
+            self.assertEqual(portfolio.Amount("BTC", "0.025"),
+                    new_repartition["ETH"] - values_in_base["ETH"])
+            self.assertEqual(0,
+                    new_repartition["ZRC"] - values_in_base["ZRC"])
+            self.assertEqual(portfolio.Amount("BTC", "0.025"),
+                    new_repartition["XMR"])
+
+        compute_trades.reset_mock()
+        with self.subTest(available_balance_only=True, balance=0),\
+                mock.patch("market.ReportStore"):
+            def _get_ticker(c1, c2):
+                if c1 == "ETH" and c2 == "BTC":
+                    return { "average": D("0.1") }
+                self.fail("Should not be called with {}, {}".format(c1, c2))
+            get_ticker.side_effect = _get_ticker
+
+            repartition.return_value = {
+                    "BTC": (D("0.5"), "long"),
+                    "ETH": (D("0.5"), "long"),
+                    }
+            m = market.Market(self.ccxt, self.market_args())
+            self.ccxt.fetch_all_balances.return_value = {
+                    "ETH": {
+                        "exchange_free": D("1.0"),
+                        "exchange_used": D("0.0"),
+                        "exchange_total": D("1.0"),
+                        "total": D("1.0")
+                        },
+                    }
+
+            m.balances.fetch_balances(tag="tag")
+            m.prepare_trades(available_balance_only=True)
+            compute_trades.assert_called_once()
+
+            call = compute_trades.call_args[0]
+            values_in_base = call[0]
+            new_repartition = call[1]
+
+            self.assertEqual(new_repartition["ETH"], values_in_base["ETH"])
 
     @mock.patch.object(market.time, "sleep")
     @mock.patch.object(market.TradeStore, "all_orders")
@@ -859,7 +960,7 @@ class ProcessorTest(WebMockTestCase):
 
         method, arguments = processor.method_arguments("prepare_trades")
         self.assertEqual(m.prepare_trades, method)
-        self.assertEqual(['base_currency', 'liquidity', 'compute_value', 'repartition', 'only'], arguments)
+        self.assertEqual(['base_currency', 'liquidity', 'compute_value', 'repartition', 'only', 'available_balance_only'], arguments)
 
         method, arguments = processor.method_arguments("prepare_orders")
         self.assertEqual(m.trades.prepare_orders, method)