From 4ae84fb7861938f7d98802a5621b1bbd6745c914 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Isma=C3=ABl=20Bouya?= Date: Sun, 6 May 2018 23:04:40 +0200 Subject: [PATCH] Fix infinite recursion during fetch --- market.py | 3 ++- portfolio.py | 14 +++++++------- tests/test_market.py | 2 +- tests/test_portfolio.py | 26 ++++++++++++++------------ 4 files changed, 24 insertions(+), 21 deletions(-) diff --git a/market.py b/market.py index 7996a58..3b6543a 100644 --- 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() diff --git a/portfolio.py b/portfolio.py index d8a5465..9b27e26 100644 --- a/portfolio.py +++ b/portfolio.py @@ -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: diff --git a/tests/test_market.py b/tests/test_market.py index 37c009b..0211638 100644 --- a/tests/test_market.py +++ b/tests/test_market.py @@ -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() diff --git a/tests/test_portfolio.py b/tests/test_portfolio.py index 969f5d4..6ca3327 100644 --- a/tests/test_portfolio.py +++ b/tests/test_portfolio.py @@ -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)) -- 2.41.0