2018-02-13 03:45:59 +00:00
|
|
|
"""
|
2021-07-11 09:13:27 +00:00
|
|
|
This module contains class to manage RPC communications (Telegram, API, ...)
|
2018-02-13 03:45:59 +00:00
|
|
|
"""
|
2024-05-12 14:51:11 +00:00
|
|
|
|
2018-03-25 19:37:14 +00:00
|
|
|
import logging
|
2022-07-26 18:15:49 +00:00
|
|
|
from collections import deque
|
2022-09-20 17:51:42 +00:00
|
|
|
from typing import List
|
2018-02-13 03:45:59 +00:00
|
|
|
|
2022-09-18 11:31:52 +00:00
|
|
|
from freqtrade.constants import Config
|
2022-12-05 18:23:03 +00:00
|
|
|
from freqtrade.enums import NO_ECHO_MESSAGES, RPCMessageType
|
2021-06-09 17:51:44 +00:00
|
|
|
from freqtrade.rpc import RPC, RPCHandler
|
2022-09-20 17:51:42 +00:00
|
|
|
from freqtrade.rpc.rpc_types import RPCSendMsg
|
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
|
|
|
"""
|
2021-07-11 09:13:27 +00:00
|
|
|
Class to manage RPC objects (Telegram, API, ...)
|
2018-02-13 03:45:59 +00:00
|
|
|
"""
|
2021-08-06 22:19:36 +00:00
|
|
|
|
2018-02-13 03:45:59 +00:00
|
|
|
def __init__(self, freqtrade) -> None:
|
2024-05-12 14:51:11 +00:00
|
|
|
"""Initializes all enabled rpc modules"""
|
2020-12-24 08:01:53 +00:00
|
|
|
self.registered_modules: List[RPCHandler] = []
|
|
|
|
self._rpc = RPC(freqtrade)
|
|
|
|
config = freqtrade.config
|
2018-06-08 01:49:09 +00:00
|
|
|
# Enable telegram
|
2024-05-12 14:51:11 +00:00
|
|
|
if config.get("telegram", {}).get("enabled", False):
|
|
|
|
logger.info("Enabling rpc.telegram ...")
|
2018-06-08 21:50:38 +00:00
|
|
|
from freqtrade.rpc.telegram import Telegram
|
2024-05-12 14:51:11 +00:00
|
|
|
|
2020-12-24 08:01:53 +00:00
|
|
|
self.registered_modules.append(Telegram(self._rpc, config))
|
2018-02-13 03:45:59 +00:00
|
|
|
|
2022-06-01 15:44:48 +00:00
|
|
|
# Enable discord
|
2024-05-12 14:51:11 +00:00
|
|
|
if config.get("discord", {}).get("enabled", False):
|
|
|
|
logger.info("Enabling rpc.discord ...")
|
2022-06-01 15:44:48 +00:00
|
|
|
from freqtrade.rpc.discord import Discord
|
2024-05-12 14:51:11 +00:00
|
|
|
|
2022-06-01 15:44:48 +00:00
|
|
|
self.registered_modules.append(Discord(self._rpc, config))
|
|
|
|
|
2018-07-12 17:59:17 +00:00
|
|
|
# Enable Webhook
|
2024-05-12 14:51:11 +00:00
|
|
|
if config.get("webhook", {}).get("enabled", False):
|
|
|
|
logger.info("Enabling rpc.webhook ...")
|
2018-07-12 17:59:17 +00:00
|
|
|
from freqtrade.rpc.webhook import Webhook
|
2024-05-12 14:51:11 +00:00
|
|
|
|
2020-12-24 08:01:53 +00:00
|
|
|
self.registered_modules.append(Webhook(self._rpc, config))
|
2018-07-12 17:59:17 +00:00
|
|
|
|
2019-04-04 05:13:40 +00:00
|
|
|
# Enable local rest api server for cmd line control
|
2024-05-12 14:51:11 +00:00
|
|
|
if config.get("api_server", {}).get("enabled", False):
|
|
|
|
logger.info("Enabling rpc.api_server")
|
2020-12-31 10:01:50 +00:00
|
|
|
from freqtrade.rpc.api_server import ApiServer
|
2024-05-12 14:51:11 +00:00
|
|
|
|
2020-12-31 19:02:27 +00:00
|
|
|
apiserver = ApiServer(config)
|
|
|
|
apiserver.add_rpc_handler(self._rpc)
|
|
|
|
self.registered_modules.append(apiserver)
|
2019-04-04 05:13:40 +00:00
|
|
|
|
2018-02-13 03:45:59 +00:00
|
|
|
def cleanup(self) -> None:
|
2024-05-12 14:51:11 +00:00
|
|
|
"""Stops all enabled rpc modules"""
|
|
|
|
logger.info("Cleaning up rpc modules ...")
|
2018-06-09 11:30:48 +00:00
|
|
|
while self.registered_modules:
|
|
|
|
mod = self.registered_modules.pop()
|
2024-05-12 14:51:11 +00:00
|
|
|
logger.info("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
|
|
|
|
2022-09-20 17:51:42 +00:00
|
|
|
def send_msg(self, msg: RPCSendMsg) -> 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
|
|
|
"""
|
2024-05-12 14:51:11 +00:00
|
|
|
if msg.get("type") not in NO_ECHO_MESSAGES:
|
|
|
|
logger.info("Sending rpc message: %s", msg)
|
2018-06-08 01:49:09 +00:00
|
|
|
for mod in self.registered_modules:
|
2024-05-12 14:51:11 +00:00
|
|
|
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}.")
|
2022-09-20 16:09:14 +00:00
|
|
|
except Exception:
|
2024-05-12 14:51:11 +00:00
|
|
|
logger.exception("Exception occurred within RPC module %s", mod.name)
|
2018-11-29 19:02:12 +00:00
|
|
|
|
2022-07-26 18:15:49 +00:00
|
|
|
def process_msg_queue(self, queue: deque) -> None:
|
|
|
|
"""
|
|
|
|
Process all messages in the queue.
|
|
|
|
"""
|
|
|
|
while queue:
|
|
|
|
msg = queue.popleft()
|
2024-05-12 14:51:11 +00:00
|
|
|
logger.info("Sending rpc strategy_msg: %s", msg)
|
2022-10-07 15:06:30 +00:00
|
|
|
for mod in self.registered_modules:
|
2024-05-12 14:51:11 +00:00
|
|
|
if mod._config.get(mod.name, {}).get("allow_custom_messages", False):
|
|
|
|
mod.send_msg(
|
|
|
|
{
|
|
|
|
"type": RPCMessageType.STRATEGY_MSG,
|
|
|
|
"msg": msg,
|
|
|
|
}
|
|
|
|
)
|
2022-07-26 18:15:49 +00:00
|
|
|
|
2022-09-18 11:31:52 +00:00
|
|
|
def startup_messages(self, config: Config, pairlist, protections) -> None:
|
2024-05-12 14:51:11 +00:00
|
|
|
if config["dry_run"]:
|
|
|
|
self.send_msg(
|
|
|
|
{
|
|
|
|
"type": RPCMessageType.WARNING,
|
|
|
|
"status": "Dry run is enabled. All trades are simulated.",
|
|
|
|
}
|
|
|
|
)
|
|
|
|
stake_currency = config["stake_currency"]
|
|
|
|
stake_amount = config["stake_amount"]
|
|
|
|
minimal_roi = config["minimal_roi"]
|
|
|
|
stoploss = config["stoploss"]
|
|
|
|
trailing_stop = config["trailing_stop"]
|
|
|
|
timeframe = config["timeframe"]
|
|
|
|
exchange_name = config["exchange"]["name"]
|
|
|
|
strategy_name = config.get("strategy", "")
|
|
|
|
pos_adjust_enabled = "On" if config["position_adjustment_enable"] else "Off"
|
|
|
|
self.send_msg(
|
|
|
|
{
|
|
|
|
"type": RPCMessageType.STARTUP,
|
2024-05-13 17:49:15 +00:00
|
|
|
"status": f"*Exchange:* `{exchange_name}`\n"
|
|
|
|
f"*Stake per trade:* `{stake_amount} {stake_currency}`\n"
|
|
|
|
f"*Minimum ROI:* `{minimal_roi}`\n"
|
|
|
|
f"*{'Trailing ' if trailing_stop else ''}Stoploss:* `{stoploss}`\n"
|
|
|
|
f"*Position adjustment:* `{pos_adjust_enabled}`\n"
|
|
|
|
f"*Timeframe:* `{timeframe}`\n"
|
|
|
|
f"*Strategy:* `{strategy_name}`",
|
2024-05-12 14:51:11 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
self.send_msg(
|
|
|
|
{
|
|
|
|
"type": RPCMessageType.STARTUP,
|
|
|
|
"status": f"Searching for {stake_currency} pairs to buy and sell "
|
|
|
|
f"based on {pairlist.short_desc()}",
|
|
|
|
}
|
|
|
|
)
|
2020-12-07 09:54:37 +00:00
|
|
|
if len(protections.name_list) > 0:
|
2024-05-12 14:51:11 +00:00
|
|
|
prots = "\n".join([p for prot in protections.short_desc() for k, p in prot.items()])
|
|
|
|
self.send_msg(
|
|
|
|
{"type": RPCMessageType.STARTUP, "status": f"Using Protections: \n{prots}"}
|
|
|
|
)
|