Merge pull request #3527 from Theagainmen/Warning_message2

Warning message bot is stopped and left open trades
This commit is contained in:
Matthias 2020-06-30 07:48:02 +02:00 committed by GitHub
commit cf26ab1dd8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 39 additions and 0 deletions

View File

@ -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()
@ -175,6 +177,24 @@ class FreqtradeBot:
if self.config['cancel_open_orders_on_exit']:
self.cancel_all_open_orders()
def check_for_open_trades(self):
"""
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()
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. \n"
f"{'Trades are simulated.' if self.config['dry_run'] else ''}",
}
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

View File

@ -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

View File

@ -4028,3 +4028,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']