Skip to content

Commit

Permalink
Merge pull request freqtrade#3527 from Theagainmen/Warning_message2
Browse files Browse the repository at this point in the history
Warning message bot is stopped and left open trades
  • Loading branch information
xmatthias authored Jun 30, 2020
2 parents 8a2f631 + efd6e4a commit cf26ab1
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
20 changes: 20 additions & 0 deletions freqtrade/freqtradebot.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ def cleanup(self) -> None:
if self.config['cancel_open_orders_on_exit']:
self.cancel_all_open_orders()

self.check_for_open_trades()

self.rpc.cleanup()
persistence.cleanup()

Expand Down Expand Up @@ -175,6 +177,24 @@ def process_stopped(self) -> None:
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
Expand Down
3 changes: 3 additions & 0 deletions freqtrade/worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ def _worker(self, old_state: Optional[State]) -> State:
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
Expand Down
16 changes: 16 additions & 0 deletions tests/test_freqtradebot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']

0 comments on commit cf26ab1

Please sign in to comment.