2022-06-01 15:44:48 +00:00
|
|
|
import logging
|
2022-06-11 10:02:41 +00:00
|
|
|
from typing import Any, Dict
|
2022-06-01 15:44:48 +00:00
|
|
|
|
2022-06-11 10:02:41 +00:00
|
|
|
from freqtrade.constants import DATETIME_PRINT_FORMAT
|
2022-06-01 15:44:48 +00:00
|
|
|
from freqtrade.enums import RPCMessageType
|
2022-06-11 10:02:41 +00:00
|
|
|
from freqtrade.rpc import RPC
|
|
|
|
from freqtrade.rpc.webhook import Webhook
|
|
|
|
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
2022-06-01 15:44:48 +00:00
|
|
|
|
|
|
|
|
2022-06-11 10:02:41 +00:00
|
|
|
class Discord(Webhook):
|
2022-06-01 15:44:48 +00:00
|
|
|
def __init__(self, rpc: 'RPC', config: Dict[str, Any]):
|
2022-06-11 10:02:41 +00:00
|
|
|
# super().__init__(rpc, config)
|
|
|
|
self.rpc = rpc
|
|
|
|
self.config = config
|
2022-06-01 15:44:48 +00:00
|
|
|
self.strategy = config.get('strategy', '')
|
|
|
|
self.timeframe = config.get('timeframe', '')
|
|
|
|
|
2022-06-11 10:02:41 +00:00
|
|
|
self._url = self.config['discord']['webhook_url']
|
|
|
|
self._format = 'json'
|
|
|
|
self._retries = 1
|
|
|
|
self._retry_delay = 0.1
|
2022-06-01 15:44:48 +00:00
|
|
|
|
2022-06-11 10:02:41 +00:00
|
|
|
def cleanup(self) -> None:
|
2022-06-01 15:44:48 +00:00
|
|
|
"""
|
2022-06-11 10:02:41 +00:00
|
|
|
Cleanup pending module resources.
|
|
|
|
This will do nothing for webhooks, they will simply not be called anymore
|
2022-06-01 15:44:48 +00:00
|
|
|
"""
|
2022-06-11 10:02:41 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
def send_msg(self, msg) -> None:
|
|
|
|
logger.info(f"Sending discord message: {msg}")
|
2022-06-01 15:44:48 +00:00
|
|
|
|
|
|
|
# TODO: handle other message types
|
|
|
|
if msg['type'] == RPCMessageType.EXIT_FILL:
|
|
|
|
profit_ratio = msg.get('profit_ratio')
|
2022-06-11 10:02:41 +00:00
|
|
|
open_date = msg.get('open_date').strftime(DATETIME_PRINT_FORMAT)
|
2022-06-02 05:49:29 +00:00
|
|
|
close_date = msg.get('close_date').strftime(
|
2022-06-11 10:02:41 +00:00
|
|
|
DATETIME_PRINT_FORMAT) if msg.get('close_date') else ''
|
2022-06-01 15:44:48 +00:00
|
|
|
|
|
|
|
embeds = [{
|
|
|
|
'title': '{} Trade: {}'.format(
|
|
|
|
'Profit' if profit_ratio > 0 else 'Loss',
|
|
|
|
msg.get('pair')),
|
|
|
|
'color': (0x00FF00 if profit_ratio > 0 else 0xFF0000),
|
|
|
|
'fields': [
|
2022-06-11 10:02:41 +00:00
|
|
|
{'name': 'Trade ID', 'value': msg.get('trade_id'), 'inline': True},
|
2022-06-01 15:44:48 +00:00
|
|
|
{'name': 'Exchange', 'value': msg.get('exchange').capitalize(), 'inline': True},
|
|
|
|
{'name': 'Pair', 'value': msg.get('pair'), 'inline': True},
|
2022-06-02 05:49:29 +00:00
|
|
|
{'name': 'Direction', 'value': 'Short' if msg.get(
|
|
|
|
'is_short') else 'Long', 'inline': True},
|
2022-06-01 15:44:48 +00:00
|
|
|
{'name': 'Open rate', 'value': msg.get('open_rate'), 'inline': True},
|
|
|
|
{'name': 'Close rate', 'value': msg.get('close_rate'), 'inline': True},
|
|
|
|
{'name': 'Amount', 'value': msg.get('amount'), 'inline': True},
|
|
|
|
{'name': 'Open order', 'value': msg.get('open_order_id'), 'inline': True},
|
|
|
|
{'name': 'Open date', 'value': open_date, 'inline': True},
|
|
|
|
{'name': 'Close date', 'value': close_date, 'inline': True},
|
|
|
|
{'name': 'Profit', 'value': msg.get('profit_amount'), 'inline': True},
|
2022-06-11 10:02:41 +00:00
|
|
|
{'name': 'Profitability', 'value': f'{profit_ratio:.2%}', 'inline': True},
|
2022-06-01 15:44:48 +00:00
|
|
|
{'name': 'Stake currency', 'value': msg.get('stake_currency'), 'inline': True},
|
2022-06-11 10:02:41 +00:00
|
|
|
{'name': 'Fiat currency', 'value': msg.get('fiat_display_currency'),
|
|
|
|
'inline': True},
|
2022-06-01 15:44:48 +00:00
|
|
|
{'name': 'Buy Tag', 'value': msg.get('enter_tag'), 'inline': True},
|
|
|
|
{'name': 'Sell Reason', 'value': msg.get('exit_reason'), 'inline': True},
|
|
|
|
{'name': 'Strategy', 'value': self.strategy, 'inline': True},
|
|
|
|
{'name': 'Timeframe', 'value': self.timeframe, 'inline': True},
|
|
|
|
],
|
|
|
|
}]
|
|
|
|
|
|
|
|
# convert all value in fields to string for discord
|
|
|
|
for embed in embeds:
|
2022-06-11 10:02:41 +00:00
|
|
|
for field in embed['fields']: # type: ignore
|
2022-06-01 15:44:48 +00:00
|
|
|
field['value'] = str(field['value'])
|
|
|
|
|
|
|
|
# Send the message to discord channel
|
2022-06-11 10:02:41 +00:00
|
|
|
payload = {'embeds': embeds}
|
|
|
|
self._send_msg(payload)
|