diff options
author | Ismaël Bouya <ismael.bouya@normalesup.org> | 2018-03-12 01:55:46 +0100 |
---|---|---|
committer | Ismaël Bouya <ismael.bouya@normalesup.org> | 2018-03-12 02:06:17 +0100 |
commit | d8deb0e9a6b7b2805e61dc19a287d5474c271cc5 (patch) | |
tree | f56ae35251cfeb60c563c023f7f48e59f8dbee7e | |
parent | 186f7d816a6bfc4fbf64027c3d7ecea10b97db38 (diff) | |
download | Trader-d8deb0e9a6b7b2805e61dc19a287d5474c271cc5.tar.gz Trader-d8deb0e9a6b7b2805e61dc19a287d5474c271cc5.tar.zst Trader-d8deb0e9a6b7b2805e61dc19a287d5474c271cc5.zip |
Fix mark finished order not alway called when necessary
-rw-r--r-- | portfolio.py | 9 | ||||
-rw-r--r-- | test.py | 13 |
2 files changed, 11 insertions, 11 deletions
diff --git a/portfolio.py b/portfolio.py index 65fdc6c..ed50b57 100644 --- a/portfolio.py +++ b/portfolio.py | |||
@@ -550,7 +550,7 @@ class Order: | |||
550 | 550 | ||
551 | @property | 551 | @property |
552 | def finished(self): | 552 | def finished(self): |
553 | return self.status == "closed" or self.status == "canceled" or self.status == "error" | 553 | return self.status.startswith("closed") or self.status == "canceled" or self.status == "error" |
554 | 554 | ||
555 | @retry(InsufficientFunds) | 555 | @retry(InsufficientFunds) |
556 | def run(self): | 556 | def run(self): |
@@ -594,15 +594,13 @@ class Order: | |||
594 | # other states are "closed" and "canceled" | 594 | # other states are "closed" and "canceled" |
595 | if not self.finished: | 595 | if not self.finished: |
596 | self.fetch() | 596 | self.fetch() |
597 | if self.finished: | ||
598 | self.mark_finished_order() | ||
599 | return self.status | 597 | return self.status |
600 | 598 | ||
601 | def mark_finished_order(self): | 599 | def mark_finished_order(self): |
602 | if self.market.debug: | 600 | if self.status.startswith("closed") and self.market.debug: |
603 | self.market.report.log_debug_action("Mark {} as finished".format(self)) | 601 | self.market.report.log_debug_action("Mark {} as finished".format(self)) |
604 | return | 602 | return |
605 | if self.status == "closed": | 603 | if self.status.startswith("closed"): |
606 | if self.trade_type == "short" and self.action == "buy" and self.close_if_possible: | 604 | if self.trade_type == "short" and self.action == "buy" and self.close_if_possible: |
607 | self.market.ccxt.close_margin_position(self.amount.currency, self.base_currency) | 605 | self.market.ccxt.close_margin_position(self.amount.currency, self.base_currency) |
608 | 606 | ||
@@ -621,6 +619,7 @@ class Order: | |||
621 | 619 | ||
622 | self.fetch_mouvements() | 620 | self.fetch_mouvements() |
623 | 621 | ||
622 | self.mark_finished_order() | ||
624 | # FIXME: consider open order with dust remaining as closed | 623 | # FIXME: consider open order with dust remaining as closed |
625 | 624 | ||
626 | def dust_amount_remaining(self): | 625 | def dust_amount_remaining(self): |
@@ -2118,7 +2118,8 @@ class OrderTest(WebMockTestCase): | |||
2118 | self.m.report.log_debug_action.assert_called_once() | 2118 | self.m.report.log_debug_action.assert_called_once() |
2119 | 2119 | ||
2120 | @mock.patch.object(portfolio.Order, "fetch_mouvements") | 2120 | @mock.patch.object(portfolio.Order, "fetch_mouvements") |
2121 | def test_fetch(self, fetch_mouvements): | 2121 | @mock.patch.object(portfolio.Order, "mark_finished_order") |
2122 | def test_fetch(self, mark_finished_order, fetch_mouvements): | ||
2122 | order = portfolio.Order("buy", portfolio.Amount("ETH", 10), | 2123 | order = portfolio.Order("buy", portfolio.Amount("ETH", 10), |
2123 | D("0.1"), "BTC", "long", self.m, "trade") | 2124 | D("0.1"), "BTC", "long", self.m, "trade") |
2124 | order.id = 45 | 2125 | order.id = 45 |
@@ -2128,6 +2129,7 @@ class OrderTest(WebMockTestCase): | |||
2128 | self.m.report.log_debug_action.assert_called_once() | 2129 | self.m.report.log_debug_action.assert_called_once() |
2129 | self.m.report.log_debug_action.reset_mock() | 2130 | self.m.report.log_debug_action.reset_mock() |
2130 | self.m.ccxt.fetch_order.assert_not_called() | 2131 | self.m.ccxt.fetch_order.assert_not_called() |
2132 | mark_finished_order.assert_not_called() | ||
2131 | fetch_mouvements.assert_not_called() | 2133 | fetch_mouvements.assert_not_called() |
2132 | 2134 | ||
2133 | with self.subTest(debug=False): | 2135 | with self.subTest(debug=False): |
@@ -2144,17 +2146,19 @@ class OrderTest(WebMockTestCase): | |||
2144 | self.assertEqual("timestamp", order.timestamp) | 2146 | self.assertEqual("timestamp", order.timestamp) |
2145 | self.assertEqual(1, len(order.results)) | 2147 | self.assertEqual(1, len(order.results)) |
2146 | self.m.report.log_debug_action.assert_not_called() | 2148 | self.m.report.log_debug_action.assert_not_called() |
2149 | mark_finished_order.assert_called_once() | ||
2147 | 2150 | ||
2151 | mark_finished_order.reset_mock() | ||
2148 | with self.subTest(missing_order=True): | 2152 | with self.subTest(missing_order=True): |
2149 | self.m.ccxt.fetch_order.side_effect = [ | 2153 | self.m.ccxt.fetch_order.side_effect = [ |
2150 | portfolio.OrderNotCached, | 2154 | portfolio.OrderNotCached, |
2151 | ] | 2155 | ] |
2152 | order.fetch() | 2156 | order.fetch() |
2153 | self.assertEqual("closed_unknown", order.status) | 2157 | self.assertEqual("closed_unknown", order.status) |
2158 | mark_finished_order.assert_called_once() | ||
2154 | 2159 | ||
2155 | @mock.patch.object(portfolio.Order, "fetch") | 2160 | @mock.patch.object(portfolio.Order, "fetch") |
2156 | @mock.patch.object(portfolio.Order, "mark_finished_order") | 2161 | def test_get_status(self, fetch): |
2157 | def test_get_status(self, mark_finished_order, fetch): | ||
2158 | with self.subTest(debug=True): | 2162 | with self.subTest(debug=True): |
2159 | self.m.debug = True | 2163 | self.m.debug = True |
2160 | order = portfolio.Order("buy", portfolio.Amount("ETH", 10), | 2164 | order = portfolio.Order("buy", portfolio.Amount("ETH", 10), |
@@ -2173,10 +2177,8 @@ class OrderTest(WebMockTestCase): | |||
2173 | return update_status | 2177 | return update_status |
2174 | fetch.side_effect = _fetch(order) | 2178 | fetch.side_effect = _fetch(order) |
2175 | self.assertEqual("open", order.get_status()) | 2179 | self.assertEqual("open", order.get_status()) |
2176 | mark_finished_order.assert_not_called() | ||
2177 | fetch.assert_called_once() | 2180 | fetch.assert_called_once() |
2178 | 2181 | ||
2179 | mark_finished_order.reset_mock() | ||
2180 | fetch.reset_mock() | 2182 | fetch.reset_mock() |
2181 | with self.subTest(debug=False, finished=True): | 2183 | with self.subTest(debug=False, finished=True): |
2182 | self.m.debug = False | 2184 | self.m.debug = False |
@@ -2188,7 +2190,6 @@ class OrderTest(WebMockTestCase): | |||
2188 | return update_status | 2190 | return update_status |
2189 | fetch.side_effect = _fetch(order) | 2191 | fetch.side_effect = _fetch(order) |
2190 | self.assertEqual("closed", order.get_status()) | 2192 | self.assertEqual("closed", order.get_status()) |
2191 | mark_finished_order.assert_called_once() | ||
2192 | fetch.assert_called_once() | 2193 | fetch.assert_called_once() |
2193 | 2194 | ||
2194 | def test_run(self): | 2195 | def test_run(self): |