]> git.immae.eu Git - perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Trader.git/blobdiff - tests/test_store.py
Merge branch 'dev'
[perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Trader.git] / tests / test_store.py
index c0b1fb947fd35fab9202778f4cd57a2d67113521..3097f6d5d49d30f97bea413c3138cac6c21683ce 100644 (file)
@@ -369,17 +369,239 @@ class BalanceStoreTest(WebMockTestCase):
 
         balance_store = market.BalanceStore(self.m)
 
-        balance_store.fetch_balances()
-        self.assertNotIn("ETC", balance_store.currencies())
-        self.assertListEqual(["USDT", "XVG", "XMR"], list(balance_store.currencies()))
+        with self.subTest(log_tickers=False):
+            balance_store.fetch_balances()
+            self.assertNotIn("ETC", balance_store.currencies())
+            self.assertListEqual(["USDT", "XVG", "XMR"], list(balance_store.currencies()))
+
+            balance_store.all["ETC"] = portfolio.Balance("ETC", {
+                "exchange_total": "1", "exchange_free": "0",
+                "exchange_used": "1" })
+            balance_store.fetch_balances(tag="foo")
+            self.assertEqual(0, balance_store.all["ETC"].total)
+            self.assertListEqual(["USDT", "XVG", "XMR", "ETC"], list(balance_store.currencies()))
+            self.m.report.log_balances.assert_called_with(tag="foo", checkpoint=None)
+
+        with self.subTest(log_tickers=True),\
+                mock.patch.object(balance_store, "in_currency") as in_currency:
+            in_currency.return_value = "tickers"
+            balance_store.fetch_balances(log_tickers=True, ticker_currency="FOO",
+                    ticker_compute_value="compute", ticker_type="type")
+            self.m.report.log_balances.assert_called_with(compute_value='compute',
+                    tag=None, checkpoint=None, ticker_currency='FOO', tickers='tickers',
+                    type='type')
+
+        balance_store = market.BalanceStore(self.m)
+        with self.subTest(add_portfolio=True),\
+                mock.patch.object(market.Portfolio, "repartition") as repartition:
+            repartition.return_value = {
+                    "DOGE": D("0.5"),
+                    "USDT": D("0.5"),
+                    }
+            balance_store.fetch_balances(add_portfolio=True)
+            self.assertListEqual(["USDT", "XVG", "XMR", "DOGE"], list(balance_store.currencies()))
+
+        self.m.ccxt.fetch_all_balances.return_value = {
+                "ETC": {
+                    "exchange_free": 0,
+                    "exchange_used": 0,
+                    "exchange_total": 0,
+                    "margin_total": 0,
+                    },
+                "XVG": {
+                    "exchange_free": 16,
+                    "exchange_used": 0,
+                    "exchange_total": 16,
+                    "margin_total": 0,
+                    },
+                "XMR": {
+                    "exchange_free": 0,
+                    "exchange_used": 0,
+                    "exchange_total": 0,
+                    "margin_total": D("-1.0"),
+                    "margin_free": 0,
+                    },
+                }
+
+        balance_store = market.BalanceStore(self.m)
+        with self.subTest(add_usdt=True),\
+                mock.patch.object(market.Portfolio, "repartition") as repartition:
+            repartition.return_value = {
+                    "DOGE": D("0.5"),
+                    "ETH": D("0.5"),
+                    }
+            balance_store.fetch_balances(add_usdt=True)
+            self.assertListEqual(["XVG", "XMR", "USDT"], list(balance_store.currencies()))
+
+    @mock.patch.object(market.Portfolio, "repartition")
+    def test_available_balances_for_repartition(self, repartition):
+        with self.subTest(available_balance_only=True):
+            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") }
+                if c1 == "FOO" and c2 == "BTC":
+                    return { "average": D("0.1") }
+                self.fail("Should not be called with {}, {}".format(c1, c2))
+            self.m.get_ticker.side_effect = _get_ticker
+
+            repartition.return_value = {
+                    "DOGE": (D("0.20"), "short"),
+                    "BTC": (D("0.20"), "long"),
+                    "ETH": (D("0.20"), "long"),
+                    "XMR": (D("0.20"), "long"),
+                    "FOO": (D("0.20"), "long"),
+                    }
+            self.m.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.065"),
+                        "exchange_used": D("0.02"),
+                        "exchange_total": D("0.085"),
+                        "margin_available": D("0.035"),
+                        "margin_in_position": D("0.01"),
+                        "margin_total": D("0.045"),
+                        "total": D("0.13")
+                        },
+                    "ETH": {
+                        "exchange_free": D("1.0"),
+                        "exchange_used": D("0.0"),
+                        "exchange_total": D("1.0"),
+                        "total": D("1.0")
+                        },
+                    "FOO": {
+                        "exchange_free": D("0.1"),
+                        "exchange_used": D("0.0"),
+                        "exchange_total": D("0.1"),
+                        "total": D("0.1"),
+                        },
+                    }
+
+            balance_store = market.BalanceStore(self.m)
+            balance_store.fetch_balances()
+            _repartition, total_base_value, amount_in_position = balance_store.available_balances_for_repartition()
+            repartition.assert_called_with(liquidity="medium")
+            self.assertEqual((D("0.20"), "short"), _repartition["DOGE"])
+            self.assertEqual((D("0.20"), "long"), _repartition["BTC"])
+            self.assertEqual((D("0.20"), "long"), _repartition["XMR"])
+            self.assertEqual((D("0.20"), "long"), _repartition["FOO"])
+            self.assertIsNone(_repartition.get("ETH"))
+            self.assertEqual(portfolio.Amount("BTC", "0.1"), total_base_value)
+            self.assertEqual(0, amount_in_position["DOGE"])
+            self.assertEqual(0, amount_in_position["BTC"])
+            self.assertEqual(0, amount_in_position["XMR"])
+            self.assertEqual(portfolio.Amount("BTC", "0.1"), amount_in_position["ETH"])
+            self.assertEqual(portfolio.Amount("BTC", "0.01"), amount_in_position["FOO"])
+
+        with self.subTest(available_balance_only=True, balance=0):
+            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))
+            self.m.get_ticker.side_effect = _get_ticker
+
+            repartition.return_value = {
+                    "BTC": (D("0.5"), "long"),
+                    "ETH": (D("0.5"), "long"),
+                    }
+            self.m.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")
+                        },
+                    }
+
+            balance_store = market.BalanceStore(self.m)
+            balance_store.fetch_balances()
+            _repartition, total_base_value, amount_in_position = balance_store.available_balances_for_repartition(liquidity="high")
+
+            repartition.assert_called_with(liquidity="high")
+            self.assertEqual((D("0.5"), "long"), _repartition["BTC"])
+            self.assertIsNone(_repartition.get("ETH"))
+            self.assertEqual(0, total_base_value)
+            self.assertEqual(0, amount_in_position["BTC"])
+            self.assertEqual(0, amount_in_position["BTC"])
+
+        repartition.reset_mock()
+        with self.subTest(available_balance_only=True, balance=0,
+                repartition="present"):
+            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))
+            self.m.get_ticker.side_effect = _get_ticker
+
+            _repartition = {
+                    "BTC": (D("0.5"), "long"),
+                    "ETH": (D("0.5"), "long"),
+                    }
+            self.m.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")
+                        },
+                    }
+
+            balance_store = market.BalanceStore(self.m)
+            balance_store.fetch_balances()
+            _repartition, total_base_value, amount_in_position = balance_store.available_balances_for_repartition(repartition=_repartition)
+            repartition.assert_not_called()
+
+            self.assertEqual((D("0.5"), "long"), _repartition["BTC"])
+            self.assertIsNone(_repartition.get("ETH"))
+            self.assertEqual(0, total_base_value)
+            self.assertEqual(0, amount_in_position["BTC"])
+            self.assertEqual(portfolio.Amount("BTC", "0.1"), amount_in_position["ETH"])
+
+        repartition.reset_mock()
+        with self.subTest(available_balance_only=True, balance=0,
+                repartition="present", base_currency="ETH"):
+            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))
+            self.m.get_ticker.side_effect = _get_ticker
+
+            _repartition = {
+                    "BTC": (D("0.5"), "long"),
+                    "ETH": (D("0.5"), "long"),
+                    }
+            self.m.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")
+                        },
+                    }
 
-        balance_store.all["ETC"] = portfolio.Balance("ETC", {
-            "exchange_total": "1", "exchange_free": "0",
-            "exchange_used": "1" })
-        balance_store.fetch_balances(tag="foo")
-        self.assertEqual(0, balance_store.all["ETC"].total)
-        self.assertListEqual(["USDT", "XVG", "XMR", "ETC"], list(balance_store.currencies()))
-        self.m.report.log_balances.assert_called_with(tag="foo")
+            balance_store = market.BalanceStore(self.m)
+            balance_store.fetch_balances()
+            _repartition, total_base_value, amount_in_position = balance_store.available_balances_for_repartition(repartition=_repartition, base_currency="ETH")
+
+            self.assertEqual((D("0.5"), "long"), _repartition["BTC"])
+            self.assertEqual((D("0.5"), "long"), _repartition["ETH"])
+            self.assertEqual(portfolio.Amount("ETH", 1), total_base_value)
+            self.assertEqual(0, amount_in_position["BTC"])
+            self.assertEqual(0, amount_in_position["ETH"])
 
     @mock.patch.object(market.Portfolio, "repartition")
     def test_dispatch_assets(self, repartition):
@@ -403,7 +625,7 @@ class BalanceStoreTest(WebMockTestCase):
         self.assertEqual(D("2.6"), amounts["BTC"].value)
         self.assertEqual(D("7.5"), amounts["XEM"].value)
         self.assertEqual(D("-1.0"), amounts["DASH"].value)
-        self.m.report.log_balances.assert_called_with(tag=None)
+        self.m.report.log_balances.assert_called_with(tag=None, checkpoint=None)
         self.m.report.log_dispatch.assert_called_once_with(portfolio.Amount("BTC",
             "11.1"), amounts, "medium", repartition_hash)
 
@@ -444,10 +666,27 @@ class BalanceStoreTest(WebMockTestCase):
 @unittest.skipUnless("unit" in limits, "Unit skipped")
 class ReportStoreTest(WebMockTestCase):
     def test_add_log(self):
+        with self.subTest(market=self.m):
+            self.m.user_id = 1
+            self.m.market_id = 3
+            report_store = market.ReportStore(self.m)
+            result = report_store.add_log({"foo": "bar"})
+
+            self.assertEqual({"foo": "bar", "date": mock.ANY, "user_id": 1, "market_id": 3}, result)
+            self.assertEqual(result, report_store.logs[0])
+
+        with self.subTest(market=None):
+            report_store = market.ReportStore(None)
+            result = report_store.add_log({"foo": "bar"})
+
+            self.assertEqual({"foo": "bar", "date": mock.ANY, "user_id": None, "market_id": None}, result)
+
+    def test_add_redis_status(self):
         report_store = market.ReportStore(self.m)
-        report_store.add_log({"foo": "bar"})
+        result = report_store.add_redis_status({"foo": "bar"})
 
-        self.assertEqual({"foo": "bar", "date": mock.ANY}, report_store.logs[0])
+        self.assertEqual({"foo": "bar"}, result)
+        self.assertEqual(result, report_store.redis_status[0])
 
     def test_set_verbose(self):
         report_store = market.ReportStore(self.m)
@@ -460,6 +699,8 @@ class ReportStoreTest(WebMockTestCase):
             self.assertFalse(report_store.verbose_print)
 
     def test_merge(self):
+        self.m.user_id = 1
+        self.m.market_id = 3
         report_store1 = market.ReportStore(self.m, verbose_print=False)
         report_store2 = market.ReportStore(None, verbose_print=False)
 
@@ -478,7 +719,7 @@ class ReportStoreTest(WebMockTestCase):
         with self.subTest(verbose=True),\
                 mock.patch.object(store, "datetime") as time_mock,\
                 mock.patch('sys.stdout', new_callable=StringIO) as stdout_mock:
-            time_mock.now.return_value = datetime.datetime(2018, 2, 25, 2, 20, 10)
+            time_mock.datetime.now.return_value = datetime.datetime(2018, 2, 25, 2, 20, 10)
             report_store.set_verbose(True)
             report_store.print_log("Coucou")
             report_store.print_log(portfolio.Amount("BTC", 1))
@@ -495,7 +736,7 @@ class ReportStoreTest(WebMockTestCase):
         report_store = market.ReportStore(self.m)
 
         self.assertEqual("2018-02-24T00:00:00",
-                report_store.default_json_serial(portfolio.datetime(2018, 2, 24)))
+                report_store.default_json_serial(portfolio.datetime.datetime(2018, 2, 24)))
         self.assertEqual("1.00000000 BTC",
                 report_store.default_json_serial(portfolio.Amount("BTC", 1)))
 
@@ -503,7 +744,7 @@ class ReportStoreTest(WebMockTestCase):
         report_store = market.ReportStore(self.m)
         report_store.logs.append({"foo": "bar"})
         self.assertEqual('[\n  {\n    "foo": "bar"\n  }\n]', report_store.to_json())
-        report_store.logs.append({"date": portfolio.datetime(2018, 2, 24)})
+        report_store.logs.append({"date": portfolio.datetime.datetime(2018, 2, 24)})
         self.assertEqual('[\n  {\n    "foo": "bar"\n  },\n  {\n    "date": "2018-02-24T00:00:00"\n  }\n]', report_store.to_json())
         report_store.logs.append({"amount": portfolio.Amount("BTC", 1)})
         self.assertEqual('[\n  {\n    "foo": "bar"\n  },\n  {\n    "date": "2018-02-24T00:00:00"\n  },\n  {\n    "amount": "1.00000000 BTC"\n  }\n]', report_store.to_json())
@@ -522,6 +763,20 @@ class ReportStoreTest(WebMockTestCase):
         self.assertEqual(("date1", "type1", '{\n  "foo": "bar",\n  "bla": "bla"\n}'), logs[0])
         self.assertEqual(("date2", "type2", '{\n  "foo": "bar",\n  "bla": "bla"\n}'), logs[1])
 
+    def test_to_json_redis(self):
+        report_store = market.ReportStore(self.m)
+        report_store.redis_status.append({
+            "type": "type1", "foo": "bar", "bla": "bla"
+            })
+        report_store.redis_status.append({
+            "type": "type2", "foo": "bar", "bla": "bla"
+            })
+        logs = list(report_store.to_json_redis())
+
+        self.assertEqual(2, len(logs))
+        self.assertEqual(("type1", '{"foo": "bar", "bla": "bla"}'), logs[0])
+        self.assertEqual(("type2", '{"foo": "bar", "bla": "bla"}'), logs[1])
+
     @mock.patch.object(market.ReportStore, "print_log")
     @mock.patch.object(market.ReportStore, "add_log")
     def test_log_stage(self, add_log, print_log):
@@ -547,22 +802,83 @@ class ReportStoreTest(WebMockTestCase):
 
     @mock.patch.object(market.ReportStore, "print_log")
     @mock.patch.object(market.ReportStore, "add_log")
-    def test_log_balances(self, add_log, print_log):
+    @mock.patch.object(market.ReportStore, "add_redis_status")
+    def test_log_balances(self, add_redis_status, add_log, print_log):
         report_store = market.ReportStore(self.m)
         self.m.balances.as_json.return_value = "json"
         self.m.balances.all = { "FOO": "bar", "BAR": "baz" }
 
-        report_store.log_balances(tag="tag")
-        print_log.assert_has_calls([
-            mock.call("[Balance]"),
-            mock.call("\tbar"),
-            mock.call("\tbaz"),
-            ])
-        add_log.assert_called_once_with({
-            'type': 'balance',
-            'balances': 'json',
-            'tag': 'tag'
-            })
+        with self.subTest(tickers=None):
+            report_store.log_balances(tag="tag")
+            print_log.assert_has_calls([
+                mock.call("[Balance]"),
+                mock.call("\tbar"),
+                mock.call("\tbaz"),
+                ])
+            add_log.assert_called_once_with({
+                'type': 'balance',
+                'checkpoint': None,
+                'balances': 'json',
+                'tag': 'tag'
+                })
+            add_redis_status.assert_called_once_with({
+                'type': 'balance',
+                'balances': 'json',
+                'checkpoint': None,
+                'tag': 'tag'
+                })
+        add_log.reset_mock()
+        add_redis_status.reset_mock()
+        with self.subTest(tickers="present"):
+            amounts = {
+                    "BTC": portfolio.Amount("BTC", 10),
+                    "ETH": portfolio.Amount("BTC", D("0.3"))
+                    }
+            amounts["ETH"].rate = D("0.1")
+
+            report_store.log_balances(tag="tag", tickers=amounts,
+                    ticker_currency="BTC", compute_value="default",
+                    type="total")
+            add_log.assert_called_once_with({
+                'type': 'balance',
+                'checkpoint': None,
+                'balances': 'json',
+                'tag': 'tag',
+                'tickers': {
+                    'compute_value': 'default',
+                    'balance_type': 'total',
+                    'currency': 'BTC',
+                    'balances': {
+                        'BTC': D('10'),
+                        'ETH': D('0.3')
+                        },
+                    'rates': {
+                        'BTC': None,
+                        'ETH': D('0.1')
+                        },
+                    'total': D('10.3')
+                    },
+                })
+            add_redis_status.assert_called_once_with({
+                'type': 'balance',
+                'checkpoint': None,
+                'balances': 'json',
+                'tag': 'tag',
+                'tickers': {
+                    'compute_value': 'default',
+                    'balance_type': 'total',
+                    'currency': 'BTC',
+                    'balances': {
+                        'BTC': D('10'),
+                        'ETH': D('0.3')
+                        },
+                    'rates': {
+                        'BTC': None,
+                        'ETH': D('0.1')
+                        },
+                    'total': D('10.3')
+                    },
+                })
 
     @mock.patch.object(market.ReportStore, "print_log")
     @mock.patch.object(market.ReportStore, "add_log")
@@ -817,53 +1133,101 @@ class ReportStoreTest(WebMockTestCase):
                 }
             })
 
-    @mock.patch.object(market.ReportStore, "print_log")
-    @mock.patch.object(market.ReportStore, "add_log")
-    def test_log_http_request(self, add_log, print_log):
-        report_store = market.ReportStore(self.m)
-        response = mock.Mock()
-        response.status_code = 200
-        response.text = "Hey"
+    def test_log_http_request(self):
+        with mock.patch.object(market.ReportStore, "add_log") as add_log:
+            report_store = market.ReportStore(self.m)
+            response = mock.Mock()
+            response.status_code = 200
+            response.text = "Hey"
+            response.elapsed.total_seconds.return_value = 120
 
-        report_store.log_http_request("method", "url", "body",
-                "headers", response)
-        print_log.assert_not_called()
-        add_log.assert_called_once_with({
-            'type': 'http_request',
-            'method': 'method',
-            'url': 'url',
-            'body': 'body',
-            'headers': 'headers',
-            'status': 200,
-            'response': 'Hey'
-            })
+            report_store.log_http_request("method", "url", "body",
+                    "headers", response)
+            add_log.assert_called_once_with({
+                'type': 'http_request',
+                'method': 'method',
+                'url': 'url',
+                'body': 'body',
+                'headers': 'headers',
+                'status': 200,
+                'duration': 120,
+                'response': 'Hey',
+                'response_same_as': None,
+                })
 
-        add_log.reset_mock()
-        report_store.log_http_request("method", "url", "body",
-                "headers", ValueError("Foo"))
-        add_log.assert_called_once_with({
-            'type': 'http_request',
-            'method': 'method',
-            'url': 'url',
-            'body': 'body',
-            'headers': 'headers',
-            'status': -1,
-            'response': None,
-            'error': 'ValueError',
-            'error_message': 'Foo',
-            })
+            add_log.reset_mock()
+            report_store.log_http_request("method", "url", "body",
+                    "headers", ValueError("Foo"))
+            add_log.assert_called_once_with({
+                'type': 'http_request',
+                'method': 'method',
+                'url': 'url',
+                'body': 'body',
+                'headers': 'headers',
+                'status': -1,
+                'response': None,
+                'error': 'ValueError',
+                'error_message': 'Foo',
+                })
+
+        with self.subTest(no_http_dup=True, duplicate=True):
+            self.m.user_id = 1
+            self.m.market_id = 3
+            report_store = market.ReportStore(self.m, no_http_dup=True)
+            original_add_log = report_store.add_log
+            with mock.patch.object(report_store, "add_log", side_effect=original_add_log) as add_log:
+                report_store.log_http_request("method", "url", "body",
+                        "headers", response)
+                report_store.log_http_request("method", "url", "body",
+                        "headers", response)
+                self.assertEqual(2, add_log.call_count)
+                self.assertIsNone(add_log.mock_calls[0][1][0]["response_same_as"])
+                self.assertIsNone(add_log.mock_calls[1][1][0]["response"])
+                self.assertEqual(add_log.mock_calls[0][1][0]["date"], add_log.mock_calls[1][1][0]["response_same_as"])
+        with self.subTest(no_http_dup=True, duplicate=False, case="Different call"):
+            self.m.user_id = 1
+            self.m.market_id = 3
+            report_store = market.ReportStore(self.m, no_http_dup=True)
+            original_add_log = report_store.add_log
+            with mock.patch.object(report_store, "add_log", side_effect=original_add_log) as add_log:
+                report_store.log_http_request("method", "url", "body",
+                        "headers", response)
+                report_store.log_http_request("method2", "url", "body",
+                        "headers", response)
+                self.assertEqual(2, add_log.call_count)
+                self.assertIsNone(add_log.mock_calls[0][1][0]["response_same_as"])
+                self.assertIsNone(add_log.mock_calls[1][1][0]["response_same_as"])
+        with self.subTest(no_http_dup=True, duplicate=False, case="Call inbetween"):
+            self.m.user_id = 1
+            self.m.market_id = 3
+            report_store = market.ReportStore(self.m, no_http_dup=True)
+            original_add_log = report_store.add_log
+
+            response2 = mock.Mock()
+            response2.status_code = 200
+            response2.text = "Hey there!"
+
+            with mock.patch.object(report_store, "add_log", side_effect=original_add_log) as add_log:
+                report_store.log_http_request("method", "url", "body",
+                        "headers", response)
+                report_store.log_http_request("method", "url", "body",
+                        "headers", response2)
+                report_store.log_http_request("method", "url", "body",
+                        "headers", response)
+                self.assertEqual(3, add_log.call_count)
+                self.assertIsNone(add_log.mock_calls[0][1][0]["response_same_as"])
+                self.assertIsNone(add_log.mock_calls[1][1][0]["response_same_as"])
+                self.assertIsNone(add_log.mock_calls[2][1][0]["response_same_as"])
 
     @mock.patch.object(market.ReportStore, "add_log")
     def test_log_market(self, add_log):
         report_store = market.ReportStore(self.m)
 
-        report_store.log_market(self.market_args(debug=True, quiet=False), 4, 1)
+        report_store.log_market(self.market_args(debug=True, quiet=False))
         add_log.assert_called_once_with({
             "type": "market",
             "commit": "$Format:%H$",
             "args": { "report_path": None, "debug": True, "quiet": False },
-            "user_id": 4,
-            "market_id": 1,
             })
 
     @mock.patch.object(market.ReportStore, "print_log")
@@ -953,7 +1317,8 @@ class PortfolioTest(WebMockTestCase):
         self.wm.get(market.Portfolio.URL, text=self.json_response)
 
     @mock.patch.object(market.Portfolio, "parse_cryptoportfolio")
-    def test_get_cryptoportfolio(self, parse_cryptoportfolio):
+    @mock.patch.object(market.Portfolio, "store_cryptoportfolio")
+    def test_get_cryptoportfolio(self, store_cryptoportfolio, parse_cryptoportfolio):
         with self.subTest(parallel=False):
             self.wm.get(market.Portfolio.URL, [
                 {"text":'{ "foo": "bar" }', "status_code": 200},
@@ -968,23 +1333,28 @@ class PortfolioTest(WebMockTestCase):
             market.Portfolio.report.log_error.assert_not_called()
             market.Portfolio.report.log_http_request.assert_called_once()
             parse_cryptoportfolio.assert_called_once_with()
+            store_cryptoportfolio.assert_called_once_with()
             market.Portfolio.report.log_http_request.reset_mock()
             parse_cryptoportfolio.reset_mock()
+            store_cryptoportfolio.reset_mock()
             market.Portfolio.data = store.LockedVar(None)
 
             market.Portfolio.get_cryptoportfolio()
             self.assertIsNone(market.Portfolio.data.get())
             self.assertEqual(2, self.wm.call_count)
             parse_cryptoportfolio.assert_not_called()
+            store_cryptoportfolio.assert_not_called()
             market.Portfolio.report.log_error.assert_not_called()
             market.Portfolio.report.log_http_request.assert_called_once()
             market.Portfolio.report.log_http_request.reset_mock()
             parse_cryptoportfolio.reset_mock()
+            store_cryptoportfolio.reset_mock()
 
             market.Portfolio.data = store.LockedVar("Foo")
             market.Portfolio.get_cryptoportfolio()
             self.assertEqual(2, self.wm.call_count)
             parse_cryptoportfolio.assert_not_called()
+            store_cryptoportfolio.assert_not_called()
 
             market.Portfolio.get_cryptoportfolio(refetch=True)
             self.assertEqual("Foo", market.Portfolio.data.get())
@@ -1005,6 +1375,7 @@ class PortfolioTest(WebMockTestCase):
                     market.Portfolio.get_cryptoportfolio()
                     self.assertIn("foo", market.Portfolio.data.get())
                 parse_cryptoportfolio.reset_mock()
+                store_cryptoportfolio.reset_mock()
                 with self.subTest(worker=False):
                     market.Portfolio.data = store.LockedVar(None)
                     market.Portfolio.worker = mock.Mock()
@@ -1012,6 +1383,7 @@ class PortfolioTest(WebMockTestCase):
                     market.Portfolio.get_cryptoportfolio()
                     notify.assert_called_once_with()
                     parse_cryptoportfolio.assert_not_called()
+                    store_cryptoportfolio.assert_not_called()
 
     def test_parse_cryptoportfolio(self):
         with self.subTest(description="Normal case"):
@@ -1034,7 +1406,7 @@ class PortfolioTest(WebMockTestCase):
                     'SC':   (D("0.0623"), "long"),
                     'ZEC':  (D("0.3701"), "long"),
                     }
-            date = portfolio.datetime(2018, 1, 8)
+            date = portfolio.datetime.datetime(2018, 1, 8)
             self.assertDictEqual(expected, liquidities["high"][date])
 
             expected = {
@@ -1051,7 +1423,7 @@ class PortfolioTest(WebMockTestCase):
                     'XCP':  (D("0.1"), "long"),
                     }
             self.assertDictEqual(expected, liquidities["medium"][date])
-            self.assertEqual(portfolio.datetime(2018, 1, 15), market.Portfolio.last_date.get())
+            self.assertEqual(portfolio.datetime.datetime(2018, 1, 15), market.Portfolio.last_date.get())
 
         with self.subTest(description="Missing weight"):
             data = store.json.loads(self.json_response, parse_int=D, parse_float=D)
@@ -1075,25 +1447,107 @@ class PortfolioTest(WebMockTestCase):
             self.assertEqual({}, market.Portfolio.liquidities.get("high"))
             self.assertEqual(datetime.datetime(1,1,1), market.Portfolio.last_date.get())
 
-
-    @mock.patch.object(market.Portfolio, "get_cryptoportfolio")
-    def test_repartition(self, get_cryptoportfolio):
-        market.Portfolio.liquidities = store.LockedVar({
+    @mock.patch.object(store.dbs, "redis_connected")
+    @mock.patch.object(store.dbs, "redis")
+    def test_store_cryptoportfolio(self, redis, redis_connected):
+        store.Portfolio.liquidities = store.LockedVar({
                 "medium": {
-                    "2018-03-01": "medium_2018-03-01",
-                    "2018-03-08": "medium_2018-03-08",
+                    datetime.datetime(2018,3,1): "medium_2018-03-01",
+                    datetime.datetime(2018,3,8): "medium_2018-03-08",
                     },
                 "high": {
-                    "2018-03-01": "high_2018-03-01",
-                    "2018-03-08": "high_2018-03-08",
+                    datetime.datetime(2018,3,1): "high_2018-03-01",
+                    datetime.datetime(2018,3,8): "high_2018-03-08",
                     }
                 })
-        market.Portfolio.last_date = store.LockedVar("2018-03-08")
+        store.Portfolio.last_date = store.LockedVar(datetime.datetime(2018,3,8))
+
+        with self.subTest(redis_connected=False):
+            redis_connected.return_value = False
+            store.Portfolio.store_cryptoportfolio()
+            redis.set.assert_not_called()
+
+        with self.subTest(redis_connected=True):
+            redis_connected.return_value = True
+            store.Portfolio.store_cryptoportfolio()
+            redis.set.assert_has_calls([
+                mock.call("/cryptoportfolio/repartition/latest", '{"medium": "medium_2018-03-08", "high": "high_2018-03-08"}'),
+                mock.call("/cryptoportfolio/repartition/date", "2018-03-08"),
+                ])
+
+    @mock.patch.object(store.dbs, "redis_connected")
+    @mock.patch.object(store.dbs, "redis")
+    def test_retrieve_cryptoportfolio(self, redis, redis_connected):
+        with self.subTest(redis_connected=False):
+            redis_connected.return_value = False
+            store.Portfolio.retrieve_cryptoportfolio()
+            redis.get.assert_not_called()
+            self.assertIsNone(store.Portfolio.data.get())
+
+        with self.subTest(redis_connected=True, value=None):
+            redis_connected.return_value = True
+            redis.get.return_value = None
+            store.Portfolio.retrieve_cryptoportfolio()
+            self.assertEqual(2, redis.get.call_count)
+
+        redis.reset_mock()
+        with self.subTest(redis_connected=True, value="present"):
+            redis_connected.return_value = True
+            redis.get.side_effect = [
+                    b'{ "medium": "medium_repartition", "high": "high_repartition" }',
+                    b"2018-03-08"
+                    ]
+            store.Portfolio.retrieve_cryptoportfolio()
+            self.assertEqual(2, redis.get.call_count)
+            self.assertEqual(datetime.datetime(2018,3,8), store.Portfolio.last_date.get())
+            self.assertEqual("", store.Portfolio.data.get())
+            expected_liquidities = {
+                    'high': { datetime.datetime(2018, 3, 8): 'high_repartition' },
+                    'medium': { datetime.datetime(2018, 3, 8): 'medium_repartition' },
+                    }
+            self.assertEqual(expected_liquidities, store.Portfolio.liquidities.get())
+
+    @mock.patch.object(market.Portfolio, "get_cryptoportfolio")
+    @mock.patch.object(market.Portfolio, "retrieve_cryptoportfolio")
+    def test_repartition(self, retrieve_cryptoportfolio, get_cryptoportfolio):
+        with self.subTest(from_cache=False):
+            market.Portfolio.liquidities = store.LockedVar({
+                    "medium": {
+                        "2018-03-01": ["medium_2018-03-01"],
+                        "2018-03-08": ["medium_2018-03-08"],
+                        },
+                    "high": {
+                        "2018-03-01": ["high_2018-03-01"],
+                        "2018-03-08": ["high_2018-03-08"],
+                        }
+                    })
+            market.Portfolio.last_date = store.LockedVar("2018-03-08")
+
+            self.assertEqual(["medium_2018-03-08"], market.Portfolio.repartition())
+            get_cryptoportfolio.assert_called_once_with()
+            retrieve_cryptoportfolio.assert_not_called()
+            self.assertEqual(["medium_2018-03-08"], market.Portfolio.repartition(liquidity="medium"))
+            self.assertEqual(["high_2018-03-08"], market.Portfolio.repartition(liquidity="high"))
+
+        retrieve_cryptoportfolio.reset_mock()
+        get_cryptoportfolio.reset_mock()
 
-        self.assertEqual("medium_2018-03-08", market.Portfolio.repartition())
-        get_cryptoportfolio.assert_called_once_with()
-        self.assertEqual("medium_2018-03-08", market.Portfolio.repartition(liquidity="medium"))
-        self.assertEqual("high_2018-03-08", market.Portfolio.repartition(liquidity="high"))
+        with self.subTest(from_cache=True):
+            self.assertEqual(["medium_2018-03-08"], market.Portfolio.repartition(from_cache=True))
+            get_cryptoportfolio.assert_called_once_with()
+            retrieve_cryptoportfolio.assert_called_once_with()
+
+        retrieve_cryptoportfolio.reset_mock()
+        get_cryptoportfolio.reset_mock()
+
+        with self.subTest("absent liquidities"):
+            market.Portfolio.last_date = store.LockedVar("2018-03-15")
+            self.assertIsNone(market.Portfolio.repartition())
+
+        with self.subTest("no liquidities"):
+            market.Portfolio.liquidities = store.LockedVar({})
+            market.Portfolio.last_date = store.LockedVar("2018-03-08")
+            self.assertIsNone(market.Portfolio.repartition())
 
     @mock.patch.object(market.time, "sleep")
     @mock.patch.object(market.Portfolio, "get_cryptoportfolio")
@@ -1105,9 +1559,9 @@ class PortfolioTest(WebMockTestCase):
             else:
                 self.assertFalse(refetch)
             self.call_count += 1
-            market.Portfolio.last_date = store.LockedVar(store.datetime.now()\
-                - store.timedelta(10)\
-                + store.timedelta(self.call_count))
+            market.Portfolio.last_date = store.LockedVar(store.datetime.datetime.now()\
+                - store.datetime.timedelta(10)\
+                + store.datetime.timedelta(self.call_count))
         get_cryptoportfolio.side_effect = _get
 
         market.Portfolio.wait_for_recent()
@@ -1166,6 +1620,19 @@ class PortfolioTest(WebMockTestCase):
             self.assertTrue(store.Portfolio.worker_started)
 
             self.assertFalse(store.Portfolio.worker.is_alive())
+            self.assertEqual(1, threading.active_count())
+
+    def test_stop_worker(self):
+        with mock.patch.object(store.Portfolio, "get_cryptoportfolio") as get,\
+                mock.patch.object(store.Portfolio, "report") as report,\
+                mock.patch.object(store.time, "sleep") as sleep:
+            store.Portfolio.start_worker(poll=3)
+            store.Portfolio.stop_worker()
+            store.Portfolio.worker.join()
+            get.assert_not_called()
+            report.assert_not_called()
+            sleep.assert_not_called()
+            self.assertFalse(store.Portfolio.worker.is_alive())
 
     def test_wait_for_notification(self):
         with self.assertRaises(RuntimeError):
@@ -1180,7 +1647,7 @@ class PortfolioTest(WebMockTestCase):
 
             store.Portfolio.callback.wait()
 
-            report.print_log.assert_called_once_with("Fetching cryptoportfolio")
+            report.print_log.assert_called_once_with("[Worker] Fetching cryptoportfolio")
             get.assert_called_once_with(refetch=True)
             sleep.assert_called_once_with(3)
             self.assertFalse(store.Portfolio.worker_notify.is_set())
@@ -1189,7 +1656,7 @@ class PortfolioTest(WebMockTestCase):
             store.Portfolio.callback.clear()
             store.Portfolio.worker_started = False
             store.Portfolio.worker_notify.set()
-            store.Portfolio.callback.wait()
+            store.Portfolio.worker.join()
             self.assertFalse(store.Portfolio.worker.is_alive())
 
     def test_notify_and_wait(self):