]> git.immae.eu Git - perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Trader.git/commitdiff
Eat several positions in the order book after some time spent
authorIsmaël Bouya <ismael.bouya@normalesup.org>
Mon, 30 Jul 2018 22:36:05 +0000 (00:36 +0200)
committerIsmaël Bouya <ismael.bouya@normalesup.org>
Mon, 30 Jul 2018 22:36:05 +0000 (00:36 +0200)
ccxt_wrapper.py
portfolio.py
tests/test_ccxt_wrapper.py
tests/test_portfolio.py

index c4aa94d6cbe7ccfb5c9c38447bc9b59162a1bbe0..aaccc612ba036ff92123d377aed2ff208c02725d 100644 (file)
@@ -299,6 +299,10 @@ class poloniexE(poloniex):
                 "total": decimal.Decimal(summary["totalValue"]),
                 }
 
+    def fetch_nth_order_book(self, symbol, action, number):
+        book = self.fetch_order_book(symbol, limit=number)
+        return decimal.Decimal(book[action + "s"][-1][0])
+
     def nonce(self):
         """
         Wrapped to allow nonce with other libraries
index bed43261d70d127c1e4aca08b5b3baf9e9bbdfef..94472a3636de776d0724c6484fd7dbd85c406566 100644 (file)
@@ -4,6 +4,10 @@ from decimal import Decimal as D, ROUND_DOWN
 from ccxt import ExchangeError, InsufficientFunds, ExchangeNotAvailable, InvalidOrder, OrderNotCached, OrderNotFound, RequestTimeout, InvalidNonce
 
 class Computation:
+    @staticmethod
+    def eat_several(market):
+        return lambda x, y: market.ccxt.fetch_nth_order_book(x["symbol"], y, 15)
+
     computations = {
             "default": lambda x, y: x[y],
             "average": lambda x, y: x["average"],
@@ -288,8 +292,12 @@ class Trade:
         if tick in self.tick_actions:
             update, compute_value = self.tick_actions[tick]
         elif tick % 3 == 1:
-            update = "market_adjust"
-            compute_value = "default"
+            if tick < 20:
+                update = "market_adjust"
+                compute_value = "default"
+            else:
+                update = "market_adjust_eat"
+                compute_value = Computation.eat_several(self.market)
         else:
             update = "waiting"
             compute_value = None
index c326f0a202dae9d2755a4fb79eba2a87e4bdd067..44e660ef5633688cf2f71f1be86cb40c66c82881 100644 (file)
@@ -549,3 +549,35 @@ class poloniexETest(unittest.TestCase):
         self.assertTrue(self.s.is_dust_trade(D("0.0000009"), D("1000")))
         self.assertTrue(self.s.is_dust_trade(D("0.000001"), D("10")))
         self.assertFalse(self.s.is_dust_trade(D("0.000001"), D("100")))
+
+    def test_fetch_nth_order_book(self):
+        with mock.patch.object(self.s, "fetch_order_book") as t:
+            t.return_value = {
+                    "asks": [
+                        [1.269e-05, 781.23105917],
+                        [1.271e-05, 108.83577689],
+                        [1.276e-05, 19162.15732141],
+                        [1.277e-05, 34.13657561],
+                        [1.28e-05, 95.14285714],
+                        [1.281e-05, 11.13909862],
+                        [1.282e-05, 43.42379871],
+                        [1.284e-05, 493.67767887],
+                        [1.288e-05, 6179.57843281],
+                        [1.289e-05, 235.16250589]
+                        ],
+                    "bids": [
+                        [1.266e-05, 3496.42283539],
+                        [1.23e-05, 9.02439024],
+                        [1.229e-05, 3244.25987796],
+                        [1.228e-05, 6692.16061185],
+                        [1.207e-05, 9.19635459],
+                        [1.206e-05, 4711.05943978],
+                        [1.194e-05, 84.67400508],
+                        [1.168e-05, 61.75268779],
+                        [1.165e-05, 9.52789699],
+                        [1.157e-05, 16.4900605]
+                        ]
+                    }
+            self.assertAlmostEqual(D("0.00001289"), self.s.fetch_nth_order_book("BTC/HUC", "ask", 10), 8)
+            t.assert_called_once_with("BTC/HUC", limit=10)
+            self.assertAlmostEqual(D("0.00001157"), self.s.fetch_nth_order_book("BTC/HUC", "bid", 10), 8)
index bc69921fe01f58b649492ccbafdcb500eebcb0ce..d4e5ab93ea31b41a6c6c941122cffac374395e9d 100644 (file)
@@ -26,6 +26,12 @@ class ComputationTest(WebMockTestCase):
         portfolio.Computation.compute_value("foo", "bid", compute_value="test")
         compute.assert_called_with("foo", "bid")
 
+    def test_eat_several(self):
+        self.m.ccxt.fetch_nth_order_book.return_value = D("0.00001275")
+
+        self.assertEqual(D("0.00001275"), portfolio.Computation.eat_several(self.m)({ "symbol": "BTC/HUC" }, "ask"))
+
+
 @unittest.skipUnless("unit" in limits, "Unit skipped")
 class TradeTest(WebMockTestCase):
 
@@ -506,6 +512,25 @@ class TradeTest(WebMockTestCase):
             trade.orders = []
             self.m.report.log_order.reset_mock()
 
+        with self.subTest(tick=22):
+            trade.update_order(order_mock, 22)
+            order_mock.cancel.assert_called()
+            new_order_mock.run.assert_called()
+            prepare_order.assert_called_with(compute_value=mock.ANY)
+            self.m.report.log_order.assert_called()
+            self.assertEqual(2, self.m.report.log_order.call_count)
+            calls = [
+                    mock.call(order_mock, 22, update="market_adjust_eat",
+                        compute_value=mock.ANY,
+                        new_order=new_order_mock),
+                    mock.call(order_mock, 22, new_order=new_order_mock),
+                    ]
+            self.m.report.log_order.assert_has_calls(calls)
+
+        order_mock.reset_mock()
+        new_order_mock.reset_mock()
+        trade.orders = []
+        self.m.report.log_order.reset_mock()
 
     def test_print_with_order(self):
         value_from = portfolio.Amount("BTC", "0.5")