]> git.immae.eu Git - perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Trader.git/commitdiff
Fix infinite recursion during fetch
authorIsmaël Bouya <ismael.bouya@normalesup.org>
Sun, 6 May 2018 21:04:40 +0000 (23:04 +0200)
committerIsmaël Bouya <ismael.bouya@normalesup.org>
Sun, 6 May 2018 21:04:40 +0000 (23:04 +0200)
market.py
portfolio.py
tests/test_market.py
tests/test_portfolio.py

index 7996a58d5f2aa08f80a916f92d3e7f72926ef1d5..3b6543ad051411d6c97f879244127fd6c661198d 100644 (file)
--- a/market.py
+++ b/market.py
@@ -96,7 +96,8 @@ class Market:
                 elif after:
                     self.processor.process(action, steps="after")
         except Exception as e:
-            self.report.log_error("market_process", exception=e)
+            import traceback
+            self.report.log_error("market_process", exception=e, message=traceback.format_exc())
         finally:
             self.store_report()
 
index d8a546584afc4549be442e1593030ec82c5f0eef..9b27e261d0bfa93ab511e2c692cfc10f3ecd61f8 100644 (file)
@@ -261,12 +261,12 @@ class Trade:
 
     @property
     def is_fullfiled(self):
-        return abs(self.filled_amount(in_base_currency=(not self.inverted))) >= abs(self.delta)
+        return abs(self.filled_amount(in_base_currency=(not self.inverted), refetch=True)) >= abs(self.delta)
 
-    def filled_amount(self, in_base_currency=False):
+    def filled_amount(self, in_base_currency=False, refetch=False):
         filled_amount = 0
         for order in self.orders:
-            filled_amount += order.filled_amount(in_base_currency=in_base_currency)
+            filled_amount += order.filled_amount(in_base_currency=in_base_currency, refetch=refetch)
         return filled_amount
 
     tick_actions = {
@@ -585,11 +585,11 @@ class Order:
         if self.market.ccxt.is_dust_trade(self.remaining_amount().value, self.rate):
             self.status = "closed_dust_remaining"
 
-    def remaining_amount(self):
-        return self.amount - self.filled_amount()
+    def remaining_amount(self, refetch=False):
+        return self.amount - self.filled_amount(refetch=refetch)
 
-    def filled_amount(self, in_base_currency=False):
-        if self.status == "open":
+    def filled_amount(self, in_base_currency=False, refetch=False):
+        if refetch and self.status == "open":
             self.fetch()
         filled_amount = 0
         for mouvement in self.mouvements:
index 37c009b8ae846c51e03b3d219f875ab05f562b39..02116387a1f4298926c56c911a64b4816517b75e 100644 (file)
@@ -890,7 +890,7 @@ class MarketTest(WebMockTestCase):
             process.side_effect = Exception("bouh")
 
             m.process(["some_action"], before=True)
-            log_error.assert_called_with("market_process", exception=mock.ANY)
+            log_error.assert_called_with("market_process", exception=mock.ANY, message=mock.ANY)
             store_report.assert_called_once()
  
 
index 969f5d4caf2f4a6f0dc1b5b1705f8d879da4730c..6ca3327318b210cc6f624a3c5e2046313c7550ec 100644 (file)
@@ -142,9 +142,9 @@ class TradeTest(WebMockTestCase):
 
             self.assertTrue(trade.is_fullfiled)
 
-            order1.filled_amount.assert_called_with(in_base_currency=True)
-            order2.filled_amount.assert_called_with(in_base_currency=True)
-            order3.filled_amount.assert_called_with(in_base_currency=True)
+            order1.filled_amount.assert_called_with(in_base_currency=True, refetch=True)
+            order2.filled_amount.assert_called_with(in_base_currency=True, refetch=True)
+            order3.filled_amount.assert_called_with(in_base_currency=True, refetch=True)
 
         with self.subTest(inverted=True):
             value_from = portfolio.Amount("BTC", "0.5")
@@ -169,9 +169,9 @@ class TradeTest(WebMockTestCase):
 
             self.assertTrue(trade.is_fullfiled)
 
-            order1.filled_amount.assert_called_with(in_base_currency=False)
-            order2.filled_amount.assert_called_with(in_base_currency=False)
-            order3.filled_amount.assert_called_with(in_base_currency=False)
+            order1.filled_amount.assert_called_with(in_base_currency=False, refetch=True)
+            order2.filled_amount.assert_called_with(in_base_currency=False, refetch=True)
+            order3.filled_amount.assert_called_with(in_base_currency=False, refetch=True)
 
 
     def test_filled_amount(self):
@@ -189,16 +189,16 @@ class TradeTest(WebMockTestCase):
         trade.orders.append(order2)
 
         self.assertEqual(portfolio.Amount("ETH", "0.31"), trade.filled_amount())
-        order1.filled_amount.assert_called_with(in_base_currency=False)
-        order2.filled_amount.assert_called_with(in_base_currency=False)
+        order1.filled_amount.assert_called_with(in_base_currency=False, refetch=False)
+        order2.filled_amount.assert_called_with(in_base_currency=False, refetch=False)
 
         self.assertEqual(portfolio.Amount("ETH", "0.31"), trade.filled_amount(in_base_currency=False))
-        order1.filled_amount.assert_called_with(in_base_currency=False)
-        order2.filled_amount.assert_called_with(in_base_currency=False)
+        order1.filled_amount.assert_called_with(in_base_currency=False, refetch=False)
+        order2.filled_amount.assert_called_with(in_base_currency=False, refetch=False)
 
         self.assertEqual(portfolio.Amount("ETH", "0.31"), trade.filled_amount(in_base_currency=True))
-        order1.filled_amount.assert_called_with(in_base_currency=True)
-        order2.filled_amount.assert_called_with(in_base_currency=True)
+        order1.filled_amount.assert_called_with(in_base_currency=True, refetch=False)
+        order2.filled_amount.assert_called_with(in_base_currency=True, refetch=False)
 
     @mock.patch.object(portfolio.Computation, "compute_value")
     @mock.patch.object(portfolio.Trade, "filled_amount")
@@ -863,6 +863,8 @@ class OrderTest(WebMockTestCase):
         fetch.assert_not_called()
         order.status = "open"
         self.assertEqual(portfolio.Amount("ETH", 5), order.filled_amount(in_base_currency=False))
+        fetch.assert_not_called()
+        self.assertEqual(portfolio.Amount("ETH", 5), order.filled_amount(in_base_currency=False, refetch=True))
         fetch.assert_called_once()
         self.assertEqual(portfolio.Amount("BTC", "0.7"), order.filled_amount(in_base_currency=True))