From e813573f270eb8e5579b063d8426954cd87c1451 Mon Sep 17 00:00:00 2001 From: Theagainmen <24569139+Theagainmen@users.noreply.github.com> Date: Sat, 27 Jun 2020 18:35:46 +0200 Subject: [PATCH 1/7] Warning message for open trades when stopping bot --- freqtrade/freqtradebot.py | 13 +++++++++++++ freqtrade/worker.py | 3 +++ 2 files changed, 16 insertions(+) diff --git a/freqtrade/freqtradebot.py b/freqtrade/freqtradebot.py index 289850709..2a1a1492c 100644 --- a/freqtrade/freqtradebot.py +++ b/freqtrade/freqtradebot.py @@ -175,6 +175,19 @@ class FreqtradeBot: if self.config['cancel_open_orders_on_exit']: self.cancel_all_open_orders() + def check_for_open_trades(self): + open_trades = Trade.get_trades([Trade.is_open == True, + ]).all() + + if len(open_trades) is not 0: + msg = { + f'type': RPCMessageType.WARNING_NOTIFICATION, + f'status': f'{len(open_trades)} OPEN TRADES ACTIVE\n\n' + f'Handle these trades manually or \'/start\' the bot again ' + f'and use \'/stopbuy\' to handle open trades gracefully.' + } + self.rpc.send_msg(msg) + def _refresh_active_whitelist(self, trades: List[Trade] = []) -> List[str]: """ Refresh active whitelist from pairlist or edge and extend it with diff --git a/freqtrade/worker.py b/freqtrade/worker.py index 5bdb166c2..2fc206bd5 100755 --- a/freqtrade/worker.py +++ b/freqtrade/worker.py @@ -90,6 +90,9 @@ class Worker: if state == State.RUNNING: self.freqtrade.startup() + if state == State.STOPPED: + self.freqtrade.check_for_open_trades() + # Reset heartbeat timestamp to log the heartbeat message at # first throttling iteration when the state changes self._heartbeat_msg = 0 From 0642ab76bf0f69180a498a3b48e7b5ca25a4b26e Mon Sep 17 00:00:00 2001 From: Theagainmen <24569139+Theagainmen@users.noreply.github.com> Date: Sat, 27 Jun 2020 18:40:44 +0200 Subject: [PATCH 2/7] Added information to the new function --- freqtrade/freqtradebot.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/freqtrade/freqtradebot.py b/freqtrade/freqtradebot.py index 2a1a1492c..060250b49 100644 --- a/freqtrade/freqtradebot.py +++ b/freqtrade/freqtradebot.py @@ -176,6 +176,10 @@ class FreqtradeBot: self.cancel_all_open_orders() def check_for_open_trades(self): + """ + Notify the user when he stops the bot + and there are still open trades active. + """ open_trades = Trade.get_trades([Trade.is_open == True, ]).all() From 48289e8ca78236f6e2b9dc7eeb67409117f3d0eb Mon Sep 17 00:00:00 2001 From: Theagainmen <24569139+Theagainmen@users.noreply.github.com> Date: Sat, 27 Jun 2020 20:24:50 +0200 Subject: [PATCH 3/7] Added exchange name, removed capital letters --- freqtrade/freqtradebot.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/freqtrade/freqtradebot.py b/freqtrade/freqtradebot.py index 060250b49..e3bb5b70b 100644 --- a/freqtrade/freqtradebot.py +++ b/freqtrade/freqtradebot.py @@ -177,7 +177,7 @@ class FreqtradeBot: def check_for_open_trades(self): """ - Notify the user when he stops the bot + Notify the user when the bot is stopped and there are still open trades active. """ open_trades = Trade.get_trades([Trade.is_open == True, @@ -186,9 +186,10 @@ class FreqtradeBot: if len(open_trades) is not 0: msg = { f'type': RPCMessageType.WARNING_NOTIFICATION, - f'status': f'{len(open_trades)} OPEN TRADES ACTIVE\n\n' - f'Handle these trades manually or \'/start\' the bot again ' - f'and use \'/stopbuy\' to handle open trades gracefully.' + f'status': f'{len(open_trades)} open trades active.\n\n' + f'Handle these trades manually on {self.exchange.name}, ' + f'or \'/start\' the bot again and use \'/stopbuy\' ' + f'to handle open trades gracefully.' } self.rpc.send_msg(msg) From b938c536fa5cd61f86311e6e83d3901228cc91b9 Mon Sep 17 00:00:00 2001 From: Theagainmen <24569139+Theagainmen@users.noreply.github.com> Date: Sat, 27 Jun 2020 21:46:53 +0200 Subject: [PATCH 4/7] Trying to fix flake8 errors --- freqtrade/freqtradebot.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/freqtrade/freqtradebot.py b/freqtrade/freqtradebot.py index e3bb5b70b..6d9002c77 100644 --- a/freqtrade/freqtradebot.py +++ b/freqtrade/freqtradebot.py @@ -180,16 +180,16 @@ class FreqtradeBot: Notify the user when the bot is stopped and there are still open trades active. """ - open_trades = Trade.get_trades([Trade.is_open == True, - ]).all() + open_trades = Trade.get_trades([Trade.is_open == 1, + ]).all() - if len(open_trades) is not 0: + if len(open_trades) != 0: msg = { - f'type': RPCMessageType.WARNING_NOTIFICATION, - f'status': f'{len(open_trades)} open trades active.\n\n' + 'type': RPCMessageType.WARNING_NOTIFICATION, + 'status': f'{len(open_trades)} open trades active.\n\n' f'Handle these trades manually on {self.exchange.name}, ' f'or \'/start\' the bot again and use \'/stopbuy\' ' - f'to handle open trades gracefully.' + f'to handle open trades gracefully.', } self.rpc.send_msg(msg) From e5676867a871771dbc887f332464a1b0509e233f Mon Sep 17 00:00:00 2001 From: Theagainmen <24569139+Theagainmen@users.noreply.github.com> Date: Sat, 27 Jun 2020 21:53:12 +0200 Subject: [PATCH 5/7] Trying to fix flake8 errors --- freqtrade/freqtradebot.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/freqtrade/freqtradebot.py b/freqtrade/freqtradebot.py index 6d9002c77..a0bfe5bc4 100644 --- a/freqtrade/freqtradebot.py +++ b/freqtrade/freqtradebot.py @@ -180,8 +180,7 @@ class FreqtradeBot: Notify the user when the bot is stopped and there are still open trades active. """ - open_trades = Trade.get_trades([Trade.is_open == 1, - ]).all() + open_trades = Trade.get_trades([Trade.is_open == 1]).all() if len(open_trades) != 0: msg = { From 118f0511719a9479b83f5010367debc66c5b695d Mon Sep 17 00:00:00 2001 From: Theagainmen <24569139+Theagainmen@users.noreply.github.com> Date: Sun, 28 Jun 2020 11:02:50 +0200 Subject: [PATCH 6/7] Added message in cleanup and fixes --- freqtrade/freqtradebot.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/freqtrade/freqtradebot.py b/freqtrade/freqtradebot.py index a0bfe5bc4..e9e65ccac 100644 --- a/freqtrade/freqtradebot.py +++ b/freqtrade/freqtradebot.py @@ -119,6 +119,8 @@ class FreqtradeBot: if self.config['cancel_open_orders_on_exit']: self.cancel_all_open_orders() + self.check_for_open_trades() + self.rpc.cleanup() persistence.cleanup() @@ -185,10 +187,11 @@ class FreqtradeBot: if len(open_trades) != 0: msg = { 'type': RPCMessageType.WARNING_NOTIFICATION, - 'status': f'{len(open_trades)} open trades active.\n\n' - f'Handle these trades manually on {self.exchange.name}, ' - f'or \'/start\' the bot again and use \'/stopbuy\' ' - f'to handle open trades gracefully.', + 'status': f"{len(open_trades)} open trades active.\n\n" + f"Handle these trades manually on {self.exchange.name}, " + f"or '/start' the bot again and use '/stopbuy' " + f"to handle open trades gracefully. \n" + f"{'Trades are simulated.' if self.config['dry_run'] else ''}", } self.rpc.send_msg(msg) From efd6e4a87535adb95fa4eb7710259dd130a7395e Mon Sep 17 00:00:00 2001 From: Matthias Date: Tue, 30 Jun 2020 07:16:08 +0200 Subject: [PATCH 7/7] Add test for check_for_open_trades --- tests/test_freqtradebot.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/tests/test_freqtradebot.py b/tests/test_freqtradebot.py index 5d83c893e..d3014d7a8 100644 --- a/tests/test_freqtradebot.py +++ b/tests/test_freqtradebot.py @@ -4029,3 +4029,19 @@ def test_cancel_all_open_orders(mocker, default_conf, fee, limit_buy_order, limi freqtrade.cancel_all_open_orders() assert buy_mock.call_count == 1 assert sell_mock.call_count == 1 + + +@pytest.mark.usefixtures("init_persistence") +def test_check_for_open_trades(mocker, default_conf, fee, limit_buy_order, limit_sell_order): + freqtrade = get_patched_freqtradebot(mocker, default_conf) + + freqtrade.check_for_open_trades() + assert freqtrade.rpc.send_msg.call_count == 0 + + create_mock_trades(fee) + trade = Trade.query.first() + trade.is_open = True + + freqtrade.check_for_open_trades() + assert freqtrade.rpc.send_msg.call_count == 1 + assert 'Handle these trades manually' in freqtrade.rpc.send_msg.call_args[0][0]['status']