From efcec736b55205f732493c381a3d2671b9ae1fa4 Mon Sep 17 00:00:00 2001 From: Matthias Date: Thu, 29 Nov 2018 20:02:12 +0100 Subject: [PATCH] refactor startup_messages to rpc_manger this cleans up freqtradebot slightly --- freqtrade/freqtradebot.py | 34 +------------------------ freqtrade/rpc/rpc_manager.py | 34 ++++++++++++++++++++++++- freqtrade/tests/rpc/test_rpc_manager.py | 20 +++++++++++++++ 3 files changed, 54 insertions(+), 34 deletions(-) diff --git a/freqtrade/freqtradebot.py b/freqtrade/freqtradebot.py index 7a974d385..0c6369ef4 100644 --- a/freqtrade/freqtradebot.py +++ b/freqtrade/freqtradebot.py @@ -107,7 +107,7 @@ class FreqtradeBot(object): }) logger.info('Changing state to: %s', state.name) if state == State.RUNNING: - self._startup_messages() + self.rpc.startup_messages(self.config) if state == State.STOPPED: time.sleep(1) @@ -121,38 +121,6 @@ class FreqtradeBot(object): min_secs=min_secs) return state - def _startup_messages(self) -> None: - if self.config.get('dry_run', False): - self.rpc.send_msg({ - 'type': RPCMessageType.WARNING_NOTIFICATION, - 'status': 'Dry run is enabled. All trades are simulated.' - }) - stake_currency = self.config['stake_currency'] - stake_amount = self.config['stake_amount'] - minimal_roi = self.config['minimal_roi'] - ticker_interval = self.config['ticker_interval'] - exchange_name = self.config['exchange']['name'] - strategy_name = self.config.get('strategy', '') - self.rpc.send_msg({ - 'type': RPCMessageType.CUSTOM_NOTIFICATION, - 'status': f'*Exchange:* `{exchange_name}`\n' - f'*Stake per trade:* `{stake_amount} {stake_currency}`\n' - f'*Minimum ROI:* `{minimal_roi}`\n' - f'*Ticker Interval:* `{ticker_interval}`\n' - f'*Strategy:* `{strategy_name}`' - }) - if self.config.get('dynamic_whitelist', False): - top_pairs = 'top volume ' + str(self.config.get('dynamic_whitelist', 20)) - specific_pairs = '' - else: - top_pairs = 'whitelisted' - specific_pairs = '\n' + ', '.join(self.config['exchange'].get('pair_whitelist', '')) - self.rpc.send_msg({ - 'type': RPCMessageType.STATUS_NOTIFICATION, - 'status': f'Searching for {top_pairs} {stake_currency} pairs to buy and sell...' - f'{specific_pairs}' - }) - def _throttle(self, func: Callable[..., Any], min_secs: float, *args, **kwargs) -> Any: """ Throttles the given callable that it diff --git a/freqtrade/rpc/rpc_manager.py b/freqtrade/rpc/rpc_manager.py index 022578378..74a4e3bdc 100644 --- a/freqtrade/rpc/rpc_manager.py +++ b/freqtrade/rpc/rpc_manager.py @@ -4,7 +4,7 @@ This module contains class to manage RPC communications (Telegram, Slack, ...) import logging from typing import List, Dict, Any -from freqtrade.rpc import RPC +from freqtrade.rpc import RPC, RPCMessageType logger = logging.getLogger(__name__) @@ -51,3 +51,35 @@ class RPCManager(object): for mod in self.registered_modules: logger.debug('Forwarding message to rpc.%s', mod.name) mod.send_msg(msg) + + def startup_messages(self, config) -> None: + if config.get('dry_run', False): + self.send_msg({ + 'type': RPCMessageType.WARNING_NOTIFICATION, + 'status': 'Dry run is enabled. All trades are simulated.' + }) + stake_currency = config['stake_currency'] + stake_amount = config['stake_amount'] + minimal_roi = config['minimal_roi'] + ticker_interval = config['ticker_interval'] + exchange_name = config['exchange']['name'] + strategy_name = config.get('strategy', '') + self.send_msg({ + 'type': RPCMessageType.CUSTOM_NOTIFICATION, + 'status': f'*Exchange:* `{exchange_name}`\n' + f'*Stake per trade:* `{stake_amount} {stake_currency}`\n' + f'*Minimum ROI:* `{minimal_roi}`\n' + f'*Ticker Interval:* `{ticker_interval}`\n' + f'*Strategy:* `{strategy_name}`' + }) + if config.get('dynamic_whitelist', False): + top_pairs = 'top volume ' + str(config.get('dynamic_whitelist', 20)) + specific_pairs = '' + else: + top_pairs = 'whitelisted' + specific_pairs = '\n' + ', '.join(config['exchange'].get('pair_whitelist', '')) + self.send_msg({ + 'type': RPCMessageType.STATUS_NOTIFICATION, + 'status': f'Searching for {top_pairs} {stake_currency} pairs to buy and sell...' + f'{specific_pairs}' + }) diff --git a/freqtrade/tests/rpc/test_rpc_manager.py b/freqtrade/tests/rpc/test_rpc_manager.py index 90c693830..cbb858522 100644 --- a/freqtrade/tests/rpc/test_rpc_manager.py +++ b/freqtrade/tests/rpc/test_rpc_manager.py @@ -113,3 +113,23 @@ def test_init_webhook_enabled(mocker, default_conf, caplog) -> None: assert log_has('Enabling rpc.webhook ...', caplog.record_tuples) assert len(rpc_manager.registered_modules) == 1 assert 'webhook' in [mod.name for mod in rpc_manager.registered_modules] + + +def test_startupmessages_telegram_enabled(mocker, default_conf, caplog) -> None: + telegram_mock = mocker.patch('freqtrade.rpc.telegram.Telegram.send_msg', MagicMock()) + mocker.patch('freqtrade.rpc.telegram.Telegram._init', MagicMock()) + + freqtradebot = get_patched_freqtradebot(mocker, default_conf) + rpc_manager = RPCManager(freqtradebot) + rpc_manager.startup_messages(default_conf) + + assert telegram_mock.call_count == 3 + assert "*Exchange:* `bittrex`" in telegram_mock.call_args_list[1][0][0]['status'] + + telegram_mock.reset_mock() + default_conf['dry_run'] = True + default_conf['dynamic_whitelist'] = 20 + + rpc_manager.startup_messages(default_conf) + assert telegram_mock.call_count == 3 + assert "Dry run is enabled." in telegram_mock.call_args_list[0][0][0]['status']