]> git.immae.eu Git - perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Trader.git/blobdiff - test.py
Add dummy README and gitignore
[perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Trader.git] / test.py
diff --git a/test.py b/test.py
index fde3a0684808c9afc2bf2dd05fa2ddb1b8a9aa96..1b8a109ce64d6286541c88a86b86ccc0d288638d 100644 (file)
--- a/test.py
+++ b/test.py
@@ -22,6 +22,8 @@ class AmountTest(unittest.TestCase):
 
         with mock.patch.object(portfolio.Trade, 'get_ticker', new=ticker_mock):
             ticker_mock.return_value = {
+                    "bid": D("0.2"),
+                    "ask": D("0.4"),
                     "average": D("0.3"),
                     "foo": "bar",
                     }
@@ -32,6 +34,15 @@ class AmountTest(unittest.TestCase):
             self.assertEqual(amount, converted_amount.linked_to)
             self.assertEqual("bar", converted_amount.ticker["foo"])
 
+            converted_amount = amount.in_currency("ETH", None, action="bid", compute_value="default")
+            self.assertEqual(D("2"), converted_amount.value)
+
+            converted_amount = amount.in_currency("ETH", None, compute_value="ask")
+            self.assertEqual(D("4"), converted_amount.value)
+
+        converted_amount = amount.in_currency("ETH", None, rate=D("0.02"))
+        self.assertEqual(D("0.2"), converted_amount.value)
+
     def test__abs(self):
         amount = portfolio.Amount("SC", -120)
         self.assertEqual(120, abs(amount).value)
@@ -245,6 +256,11 @@ class BalanceTest(unittest.TestCase):
                 "info": "bar",
                 "used": "baz",
                 "total": "bazz",
+                "ETC": {
+                    "free": 0.0,
+                    "used": 0.0,
+                    "total": 0.0
+                    },
                 "USDT": {
                     "free": 6.0,
                     "used": 1.2,
@@ -295,11 +311,11 @@ class BalanceTest(unittest.TestCase):
         self.assertEqual(D("0.65"), amounts["BTC"].value)
         self.assertEqual(D("0.30"), amounts["ETH"].value)
 
-        amounts = portfolio.Balance.in_currency("BTC", market, action="bid")
+        amounts = portfolio.Balance.in_currency("BTC", market, compute_value="bid")
         self.assertEqual(D("0.65"), amounts["BTC"].value)
         self.assertEqual(D("0.27"), amounts["ETH"].value)
 
-        amounts = portfolio.Balance.in_currency("BTC", market, action="bid", type="used")
+        amounts = portfolio.Balance.in_currency("BTC", market, compute_value="bid", type="used")
         self.assertEqual(D("0.30"), amounts["BTC"].value)
         self.assertEqual(0, amounts["ETH"].value)
 
@@ -316,7 +332,12 @@ class BalanceTest(unittest.TestCase):
 
         portfolio.Balance.fetch_balances(portfolio.market)
         self.assertNotIn("XMR", portfolio.Balance.currencies())
-        self.assertEqual(["USDT", "XVG"], list(portfolio.Balance.currencies()))
+        self.assertListEqual(["USDT", "XVG"], list(portfolio.Balance.currencies()))
+
+        portfolio.Balance.known_balances["ETC"] = portfolio.Balance("ETC", "1", "0", "1")
+        portfolio.Balance.fetch_balances(portfolio.market)
+        self.assertEqual(0, portfolio.Balance.known_balances["ETC"].total)
+        self.assertListEqual(["USDT", "XVG", "ETC"], list(portfolio.Balance.currencies()))
 
     @mock.patch.object(portfolio.Portfolio, "repartition_pertenthousand")
     @mock.patch.object(portfolio.market, "fetch_balance")
@@ -344,10 +365,16 @@ class BalanceTest(unittest.TestCase):
                 "XEM": 7500,
                 "BTC": 2500,
                 }
-        get_ticker.side_effect = [
-                { "average": D("0.0001") },
-                { "average": D("0.000001") }
-                ]
+        def _get_ticker(c1, c2, market):
+            if c1 == "USDT" and c2 == "BTC":
+                return { "average": D("0.0001") }
+            if c1 == "XVG" and c2 == "BTC":
+                return { "average": D("0.000001") }
+            if c1 == "XEM" and c2 == "BTC":
+                return { "average": D("0.001") }
+            self.fail("Should be called with {}, {}".format(c1, c2))
+        get_ticker.side_effect = _get_ticker
+
         market = mock.Mock()
         market.fetch_balance.return_value = {
                 "USDT": {
@@ -371,6 +398,10 @@ class BalanceTest(unittest.TestCase):
         self.assertEqual(D("0.2525"), call[0][1]["BTC"].value)
         self.assertEqual(D("0.7575"), call[0][1]["XEM"].value)
 
+    @unittest.skip("TODO")
+    def test_update_trades(self):
+        pass
+
     def test__repr(self):
         balance = portfolio.Balance("BTX", 3, 1, 2)
         self.assertEqual("Balance(BTX [1.00000000 BTX/2.00000000 BTX/3.00000000 BTX])", repr(balance))
@@ -492,6 +523,10 @@ class TradeTest(unittest.TestCase):
     def test_follow_orders(self):
         pass
 
+    @unittest.skip("TODO")
+    def test_compute_value(self):
+        pass
+
     @unittest.skip("TODO")
     def test__repr(self):
         pass
@@ -499,5 +534,253 @@ class TradeTest(unittest.TestCase):
     def tearDown(self):
         self.patcher.stop()
 
+class AcceptanceTest(unittest.TestCase):
+    import time
+
+    def setUp(self):
+        super(AcceptanceTest, self).setUp()
+
+        self.patchers = [
+                mock.patch.multiple(portfolio.Balance, known_balances={}),
+                mock.patch.multiple(portfolio.Portfolio, data=None, liquidities={}),
+                mock.patch.multiple(portfolio.Trade,
+                    ticker_cache={},
+                    ticker_cache_timestamp=self.time.time(),
+                    fees_cache={},
+                    trades={}),
+                mock.patch.multiple(portfolio.Computation,
+                    computations=portfolio.Computation.computations)
+                ]
+        for patcher in self.patchers:
+            patcher.start()
+
+    def test_success_sell_only_necessary(self):
+        fetch_balance = {
+                "ETH": {
+                    "free": D("1.0"),
+                    "used": D("0.0"),
+                    "total": D("1.0"),
+                    },
+                "ETC": {
+                    "free": D("4.0"),
+                    "used": D("0.0"),
+                    "total": D("4.0"),
+                    },
+                "XVG": {
+                    "free": D("1000.0"),
+                    "used": D("0.0"),
+                    "total": D("1000.0"),
+                    },
+                }
+        repartition = {
+                "ETH": 2500,
+                "ETC": 2500,
+                "BTC": 4000,
+                "BTD": 500,
+                "USDT": 500,
+                }
+
+        def fetch_ticker(symbol):
+            if symbol == "ETH/BTC":
+                return {
+                        "symbol": "ETH/BTC",
+                        "bid": D("0.14"),
+                        "ask": D("0.16")
+                        }
+            if symbol == "ETC/BTC":
+                return {
+                        "symbol": "ETC/BTC",
+                        "bid": D("0.002"),
+                        "ask": D("0.003")
+                        }
+            if symbol == "XVG/BTC":
+                return {
+                        "symbol": "XVG/BTC",
+                        "bid": D("0.00003"),
+                        "ask": D("0.00005")
+                        }
+            if symbol == "BTD/BTC":
+                return {
+                        "symbol": "BTD/BTC",
+                        "bid": D("0.0008"),
+                        "ask": D("0.0012")
+                        }
+            if symbol == "USDT/BTC":
+                raise portfolio.ccxt.ExchangeError
+            if symbol == "BTC/USDT":
+                return {
+                        "symbol": "BTC/USDT",
+                        "bid": D("14000"),
+                        "ask": D("16000")
+                        }
+            self.fail("Shouldn't have been called with {}".format(symbol))
+
+        market = mock.Mock()
+        market.fetch_balance.return_value = fetch_balance
+        market.fetch_ticker.side_effect = fetch_ticker
+        with mock.patch.object(portfolio.Portfolio, "repartition_pertenthousand", return_value=repartition):
+            # Action 1
+            portfolio.Balance.prepare_trades(market)
+
+        balances = portfolio.Balance.known_balances
+        self.assertEqual(portfolio.Amount("ETH", 1), balances["ETH"].total)
+        self.assertEqual(portfolio.Amount("ETC", 4), balances["ETC"].total)
+        self.assertEqual(portfolio.Amount("XVG", 1000), balances["XVG"].total)
+
+
+        trades = portfolio.Trade.trades
+        self.assertEqual(portfolio.Amount("BTC", D("0.15")), trades["ETH"].value_from)
+        self.assertEqual(portfolio.Amount("BTC", D("0.05")), trades["ETH"].value_to)
+        self.assertEqual("sell", trades["ETH"].action)
+
+        self.assertEqual(portfolio.Amount("BTC", D("0.01")), trades["ETC"].value_from)
+        self.assertEqual(portfolio.Amount("BTC", D("0.05")), trades["ETC"].value_to)
+        self.assertEqual("buy", trades["ETC"].action)
+
+        self.assertNotIn("BTC", trades)
+
+        self.assertEqual(portfolio.Amount("BTC", D("0.00")), trades["BTD"].value_from)
+        self.assertEqual(portfolio.Amount("BTC", D("0.01")), trades["BTD"].value_to)
+        self.assertEqual("buy", trades["BTD"].action)
+
+        self.assertEqual(portfolio.Amount("BTC", D("0.00")), trades["USDT"].value_from)
+        self.assertEqual(portfolio.Amount("BTC", D("0.01")), trades["USDT"].value_to)
+        self.assertEqual("buy", trades["USDT"].action)
+
+        self.assertEqual(portfolio.Amount("BTC", D("0.04")), trades["XVG"].value_from)
+        self.assertEqual(portfolio.Amount("BTC", D("0.00")), trades["XVG"].value_to)
+        self.assertEqual("sell", trades["XVG"].action)
+
+        # Action 2
+        portfolio.Trade.prepare_orders(only="sell", compute_value=lambda x, y: x["bid"] * D("1.001"))
+
+        all_orders = portfolio.Trade.all_orders()
+        self.assertEqual(2, len(all_orders))
+        self.assertEqual(2, 3*all_orders[0].amount.value)
+        self.assertEqual(D("0.14014"), all_orders[0].rate)
+        self.assertEqual(1000, all_orders[1].amount.value)
+        self.assertEqual(D("0.00003003"), all_orders[1].rate)
+
+
+        def create_order(symbol, type, action, amount, price=None):
+            self.assertEqual("limit", type)
+            if symbol == "ETH/BTC":
+                self.assertEqual("sell", action)
+                self.assertEqual(2, 3*amount)
+                self.assertEqual(D("0.14014"), price)
+            elif symbol == "XVG/BTC":
+                self.assertEqual("sell", action)
+                self.assertEqual(1000, amount)
+                self.assertEqual(D("0.00003003"), price)
+            else:
+                self.fail("I shouldn't have been called")
+
+            return {
+                    "id": symbol,
+                    }
+        market.create_order.side_effect = create_order
+
+        # Action 3
+        portfolio.Trade.run_orders()
+
+        self.assertEqual("open", all_orders[0].status)
+        self.assertEqual("open", all_orders[1].status)
+
+        market.fetch_order.return_value = { "status": "closed" }
+        with mock.patch.object(portfolio.time, "sleep") as sleep:
+            # Action 4
+            portfolio.Trade.follow_orders(verbose=False)
+
+            sleep.assert_called_with(30)
+
+        for order in all_orders:
+            self.assertEqual("closed", order.status)
+
+        fetch_balance = {
+                "ETH": {
+                    "free": D("1.0") / 3,
+                    "used": D("0.0"),
+                    "total": D("1.0") / 3,
+                    },
+                "BTC": {
+                    "free": D("0.134"),
+                    "used": D("0.0"),
+                    "total": D("0.134"),
+                    },
+                "ETC": {
+                    "free": D("4.0"),
+                    "used": D("0.0"),
+                    "total": D("4.0"),
+                    },
+                "XVG": {
+                    "free": D("0.0"),
+                    "used": D("0.0"),
+                    "total": D("0.0"),
+                    },
+                }
+        market.fetch_balance.return_value = fetch_balance
+
+        with mock.patch.object(portfolio.Portfolio, "repartition_pertenthousand", return_value=repartition):
+            # Action 5
+            portfolio.Balance.update_trades(market, only="buy", compute_value="average")
+
+        balances = portfolio.Balance.known_balances
+        self.assertEqual(portfolio.Amount("ETH", 1 / D("3")), balances["ETH"].total)
+        self.assertEqual(portfolio.Amount("ETC", 4), balances["ETC"].total)
+        self.assertEqual(portfolio.Amount("BTC", D("0.134")), balances["BTC"].total)
+        self.assertEqual(portfolio.Amount("XVG", 0), balances["XVG"].total)
+
+
+        trades = portfolio.Trade.trades
+        self.assertEqual(portfolio.Amount("BTC", D("0.15")), trades["ETH"].value_from)
+        self.assertEqual(portfolio.Amount("BTC", D("0.05")), trades["ETH"].value_to)
+        self.assertEqual("sell", trades["ETH"].action)
+
+        self.assertEqual(portfolio.Amount("BTC", D("0.01")), trades["ETC"].value_from)
+        self.assertEqual(portfolio.Amount("BTC", D("0.0485")), trades["ETC"].value_to)
+        self.assertEqual("buy", trades["ETC"].action)
+
+        self.assertNotIn("BTC", trades)
+
+        self.assertEqual(portfolio.Amount("BTC", D("0.00")), trades["BTD"].value_from)
+        self.assertEqual(portfolio.Amount("BTC", D("0.0097")), trades["BTD"].value_to)
+        self.assertEqual("buy", trades["BTD"].action)
+
+        self.assertEqual(portfolio.Amount("BTC", D("0.00")), trades["USDT"].value_from)
+        self.assertEqual(portfolio.Amount("BTC", D("0.0097")), trades["USDT"].value_to)
+        self.assertEqual("buy", trades["USDT"].action)
+
+        self.assertEqual(portfolio.Amount("BTC", D("0.04")), trades["XVG"].value_from)
+        self.assertEqual(portfolio.Amount("BTC", D("0.00")), trades["XVG"].value_to)
+        self.assertEqual("sell", trades["XVG"].action)
+
+        # Action 6
+        portfolio.Trade.prepare_orders(only="buy", compute_value=lambda x, y: x["ask"])
+
+
+        all_orders = portfolio.Trade.all_orders(state="pending")
+        self.assertEqual(3, len(all_orders))
+        self.assertEqual(portfolio.Amount("ETC", D("38.5")/3), all_orders[0].amount)
+        self.assertEqual(D("0.003"), all_orders[0].rate)
+        self.assertEqual("buy", all_orders[0].action)
+
+        self.assertEqual(portfolio.Amount("BTD", D("24.25")/3), all_orders[1].amount)
+        self.assertEqual(D("0.0012"), all_orders[1].rate)
+        self.assertEqual("buy", all_orders[1].action)
+
+        self.assertEqual(portfolio.Amount("BTC", D("0.0097")), all_orders[2].amount)
+        self.assertEqual(D("16000"), all_orders[2].rate)
+        self.assertEqual("sell", all_orders[2].action)
+
+        with mock.patch.object(portfolio.time, "sleep") as sleep:
+            # Action 7
+            portfolio.Trade.follow_orders(verbose=False)
+
+            sleep.assert_called_with(30)
+
+    def tearDown(self):
+        for patcher in self.patchers:
+            patcher.stop()
+
 if __name__ == '__main__':
     unittest.main()