2018-02-13 03:45:59 +00:00
|
|
|
"""
|
|
|
|
This module contains class to manage RPC communications (Telegram, Slack, ...)
|
|
|
|
"""
|
2018-03-25 19:37:14 +00:00
|
|
|
import logging
|
2019-03-12 20:49:08 +00:00
|
|
|
from typing import Any, Dict, List
|
2018-02-13 03:45:59 +00:00
|
|
|
|
2018-11-29 19:02:12 +00:00
|
|
|
from freqtrade.rpc import RPC, RPCMessageType
|
2018-02-13 03:45:59 +00:00
|
|
|
|
2020-09-28 17:39:41 +00:00
|
|
|
|
2018-03-25 19:37:14 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2019-09-12 01:39:52 +00:00
|
|
|
class RPCManager:
|
2018-02-13 03:45:59 +00:00
|
|
|
"""
|
|
|
|
Class to manage RPC objects (Telegram, Slack, ...)
|
|
|
|
"""
|
|
|
|
def __init__(self, freqtrade) -> None:
|
2018-06-08 01:49:09 +00:00
|
|
|
""" Initializes all enabled rpc modules """
|
|
|
|
self.registered_modules: List[RPC] = []
|
2018-02-13 03:45:59 +00:00
|
|
|
|
2018-06-08 01:49:09 +00:00
|
|
|
# Enable telegram
|
2019-10-18 05:10:02 +00:00
|
|
|
if freqtrade.config.get('telegram', {}).get('enabled', False):
|
2018-03-25 19:37:14 +00:00
|
|
|
logger.info('Enabling rpc.telegram ...')
|
2018-06-08 21:50:38 +00:00
|
|
|
from freqtrade.rpc.telegram import Telegram
|
2018-06-08 01:49:09 +00:00
|
|
|
self.registered_modules.append(Telegram(freqtrade))
|
2018-02-13 03:45:59 +00:00
|
|
|
|
2018-07-12 17:59:17 +00:00
|
|
|
# Enable Webhook
|
|
|
|
if freqtrade.config.get('webhook', {}).get('enabled', False):
|
2018-07-12 18:17:45 +00:00
|
|
|
logger.info('Enabling rpc.webhook ...')
|
2018-07-12 17:59:17 +00:00
|
|
|
from freqtrade.rpc.webhook import Webhook
|
|
|
|
self.registered_modules.append(Webhook(freqtrade))
|
|
|
|
|
2019-04-04 05:13:40 +00:00
|
|
|
# Enable local rest api server for cmd line control
|
|
|
|
if freqtrade.config.get('api_server', {}).get('enabled', False):
|
|
|
|
logger.info('Enabling rpc.api_server')
|
|
|
|
from freqtrade.rpc.api_server import ApiServer
|
|
|
|
self.registered_modules.append(ApiServer(freqtrade))
|
|
|
|
|
2018-02-13 03:45:59 +00:00
|
|
|
def cleanup(self) -> None:
|
2018-06-08 01:49:09 +00:00
|
|
|
""" Stops all enabled rpc modules """
|
2018-06-08 22:58:24 +00:00
|
|
|
logger.info('Cleaning up rpc modules ...')
|
2018-06-09 11:30:48 +00:00
|
|
|
while self.registered_modules:
|
|
|
|
mod = self.registered_modules.pop()
|
2018-06-08 22:58:24 +00:00
|
|
|
logger.debug('Cleaning up rpc.%s ...', mod.name)
|
2018-06-08 01:49:09 +00:00
|
|
|
mod.cleanup()
|
2018-06-09 11:30:48 +00:00
|
|
|
del mod
|
2018-02-13 03:45:59 +00:00
|
|
|
|
2018-07-03 18:26:48 +00:00
|
|
|
def send_msg(self, msg: Dict[str, Any]) -> None:
|
2018-02-13 03:45:59 +00:00
|
|
|
"""
|
2018-06-24 22:04:27 +00:00
|
|
|
Send given message to all registered rpc modules.
|
|
|
|
A message consists of one or more key value pairs of strings.
|
|
|
|
e.g.:
|
|
|
|
{
|
|
|
|
'status': 'stopping bot'
|
|
|
|
}
|
2018-02-13 03:45:59 +00:00
|
|
|
"""
|
2018-06-08 01:49:09 +00:00
|
|
|
logger.info('Sending rpc message: %s', msg)
|
|
|
|
for mod in self.registered_modules:
|
|
|
|
logger.debug('Forwarding message to rpc.%s', mod.name)
|
2019-08-30 05:02:26 +00:00
|
|
|
try:
|
|
|
|
mod.send_msg(msg)
|
|
|
|
except NotImplementedError:
|
2020-09-19 18:04:12 +00:00
|
|
|
logger.error(f"Message type '{msg['type']}' not implemented by handler {mod.name}.")
|
2018-11-29 19:02:12 +00:00
|
|
|
|
2020-12-07 09:54:37 +00:00
|
|
|
def startup_messages(self, config: Dict[str, Any], pairlist, protections) -> None:
|
2020-01-20 19:24:40 +00:00
|
|
|
if config['dry_run']:
|
2018-11-29 19:02:12 +00:00
|
|
|
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']
|
2019-03-12 06:06:42 +00:00
|
|
|
stoploss = config['stoploss']
|
|
|
|
trailing_stop = config['trailing_stop']
|
2020-06-01 18:43:20 +00:00
|
|
|
timeframe = config['timeframe']
|
2018-11-29 19:02:12 +00:00
|
|
|
exchange_name = config['exchange']['name']
|
|
|
|
strategy_name = config.get('strategy', '')
|
|
|
|
self.send_msg({
|
2020-09-19 15:51:31 +00:00
|
|
|
'type': RPCMessageType.STARTUP_NOTIFICATION,
|
2018-11-29 19:02:12 +00:00
|
|
|
'status': f'*Exchange:* `{exchange_name}`\n'
|
|
|
|
f'*Stake per trade:* `{stake_amount} {stake_currency}`\n'
|
|
|
|
f'*Minimum ROI:* `{minimal_roi}`\n'
|
2019-03-12 06:06:42 +00:00
|
|
|
f'*{"Trailing " if trailing_stop else ""}Stoploss:* `{stoploss}`\n'
|
2020-06-01 18:43:20 +00:00
|
|
|
f'*Timeframe:* `{timeframe}`\n'
|
2018-11-29 19:02:12 +00:00
|
|
|
f'*Strategy:* `{strategy_name}`'
|
|
|
|
})
|
|
|
|
self.send_msg({
|
2020-09-19 15:51:31 +00:00
|
|
|
'type': RPCMessageType.STARTUP_NOTIFICATION,
|
2018-12-03 19:31:25 +00:00
|
|
|
'status': f'Searching for {stake_currency} pairs to buy and sell '
|
|
|
|
f'based on {pairlist.short_desc()}'
|
2018-11-29 19:02:12 +00:00
|
|
|
})
|
2020-12-07 09:54:37 +00:00
|
|
|
if len(protections.name_list) > 0:
|
2020-12-07 10:37:57 +00:00
|
|
|
prots = '\n'.join([p for prot in protections.short_desc() for k, p in prot.items()])
|
2020-12-07 09:54:37 +00:00
|
|
|
self.send_msg({
|
|
|
|
'type': RPCMessageType.STARTUP_NOTIFICATION,
|
2020-12-07 10:37:57 +00:00
|
|
|
'status': f'Using Protections: \n{prots}'
|
2020-12-07 09:54:37 +00:00
|
|
|
})
|